Visual Computing Library
Loading...
Searching...
No Matches
element.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_CORE_DISTANCE_ELEMENT_H
24#define VCL_ALGORITHMS_CORE_DISTANCE_ELEMENT_H
25
26#include "misc.h"
27
28#include <vclib/algorithms/core/bounding_box.h>
29#include <vclib/algorithms/core/polygon.h>
30#include <vclib/concepts/mesh.h>
31#include <vclib/math/min_max.h>
32#include <vclib/space/core/triangle_wrapper.h>
33
34namespace vcl {
35
49template<VertexConcept VertexType, Point3Concept PointType>
50auto distance(const VertexType& v, const PointType& p)
51{
52 return v.coord().dist(p);
53}
54
60template<Point3Concept PointType, VertexConcept VertexType>
61auto distance(const PointType& p, const VertexType& v)
62{
63 return v.coord().dist(p);
64}
65
80template<VertexConcept VertexType1, VertexConcept VertexType2>
81auto distance(const VertexType1& v1, const VertexType2& v2)
82{
83 return v1.coord().dist(v2.coord());
84}
85
109template<Point3Concept PointType, FaceConcept FaceType, typename ScalarType>
111 const PointType& p,
112 const FaceType& f,
113 ScalarType maxDist,
114 PointType& closest,
115 bool signedDist = false)
116{
117 if constexpr (TriangleFaceConcept<FaceType>) {
119 f.vertex(0)->coord(), f.vertex(1)->coord(), f.vertex(2)->coord());
120
121 return boundedDistance(p, tw, maxDist, closest, signedDist);
122 }
123 else {
124 if (f.vertexNumber() == 3) {
126 f.vertex(0)->coord(),
127 f.vertex(1)->coord(),
128 f.vertex(2)->coord());
129
130 return boundedDistance(p, tw, maxDist, closest, signedDist);
131 }
132
133 ScalarType minDist = maxDist;
134
135 std::vector<uint> tris = earCut(f);
136 for (uint i = 0; i < tris.size(); i += 3) {
137 PointType w;
138 ScalarType d = boundedDistance(
139 p,
141 f.vertex(tris[i])->coord(),
142 f.vertex(tris[i + 1])->coord(),
143 f.vertex(tris[i + 2])->coord()),
144 minDist,
145 w,
146 signedDist);
147
148 if (std::abs(d) < minDist) {
149 minDist = std::abs(d);
150 closest = w;
151 }
152 }
153
154 return minDist;
155 }
156}
157
180template<Point3Concept PointType, FaceConcept FaceType, typename ScalarType>
182 const PointType& p,
183 const FaceType& f,
184 ScalarType maxDist,
185 bool signedDist = false)
186{
187 PointType closest;
188 return boundedDistance(p, f, maxDist, closest, signedDist);
189}
190
197template<FaceConcept FaceType, Point3Concept PointType, typename ScalarType>
199 const FaceType& f,
200 const PointType& p,
201 ScalarType maxDist,
202 bool signedDist = false)
203{
204 PointType closest;
205 return boundedDistance(p, f, maxDist, closest, signedDist);
206}
207
228template<Point3Concept PointType, FaceConcept FaceType>
230 const PointType& p,
231 const FaceType& f,
232 PointType& closest,
233 bool signedDist = false)
234{
235 using ScalarType = PointType::ScalarType;
236
237 ScalarType maxDist = std::numeric_limits<ScalarType>::max();
238 return boundedDistance(p, f, maxDist, closest, signedDist);
239}
240
260template<Point3Concept PointType, FaceConcept FaceType>
261auto distance(const PointType& p, const FaceType& f, bool signedDist = false)
262{
263 using ScalarType = PointType::ScalarType;
264
265 PointType closest;
266
267 ScalarType maxDist = std::numeric_limits<ScalarType>::max();
268 return boundedDistance(p, f, maxDist, closest, signedDist);
269}
270
276template<FaceConcept FaceType, Point3Concept PointType>
277auto distance(const FaceType& f, const PointType& p, bool signedDist = false)
278{
279 return distance(p, f, signedDist);
280}
281
301template<VertexConcept VertexType, FaceConcept FaceType>
302auto distance(const VertexType& v, const FaceType& f, bool signedDist = false)
303{
304 return distance(v.coord(), f, signedDist);
305}
306
312template<FaceConcept FaceType, VertexConcept VertexType>
313auto distance(const FaceType& f, const VertexType& v, bool signedDist = false)
314{
315 return distance(v.coord(), f, signedDist);
316}
317
318} // namespace vcl
319
320#endif // VCL_ALGORITHMS_CORE_DISTANCE_ELEMENT_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The TriangleWrapper class is a wrapper around a N-Dimensional triangle.
Definition triangle_wrapper.h:54
Definition face.h:133
auto boundedDistance(const PointType &p, const FaceType &f, ScalarType maxDist, PointType &closest, bool signedDist=false)
Compute the distance between a 3D point and a face.
Definition element.h:110
auto distance(const VertexType &v, const PointType &p)
Computes the distance between a Vertex and a 3D point.
Definition element.h:50
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:92