Visual Computing Library  devel
Loading...
Searching...
No Matches
distance.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_MESH_ELEM_ALGORITHMS_DISTANCE_H
24#define VCL_MESH_ELEM_ALGORITHMS_DISTANCE_H
25
26#include <vclib/mesh/elements.h>
27
28#include <vclib/algorithms/core.h>
29#include <vclib/space/core.h>
30
31namespace vcl {
32
46template<VertexConcept VertexType, Point3Concept PointType>
47auto distance(const VertexType& v, const PointType& p)
48{
49 return v.position().dist(p);
50}
51
57template<Point3Concept PointType, VertexConcept VertexType>
58auto distance(const PointType& p, const VertexType& v)
59{
60 return v.position().dist(p);
61}
62
77template<VertexConcept VertexType1, VertexConcept VertexType2>
78auto distance(const VertexType1& v1, const VertexType2& v2)
79{
80 return v1.position().dist(v2.position());
81}
82
106template<Point3Concept PointType, FaceConcept FaceType, typename ScalarType>
108 const PointType& p,
109 const FaceType& f,
110 ScalarType maxDist,
111 PointType& closest,
112 bool signedDist = false)
113{
114 if constexpr (TriangleFaceConcept<FaceType>) {
116 f.vertex(0)->position(),
117 f.vertex(1)->position(),
118 f.vertex(2)->position());
119
120 return boundedDistance(p, tw, maxDist, closest, signedDist);
121 }
122 else {
123 if (f.vertexNumber() == 3) {
125 f.vertex(0)->position(),
126 f.vertex(1)->position(),
127 f.vertex(2)->position());
128
129 return boundedDistance(p, tw, maxDist, closest, signedDist);
130 }
131
132 ScalarType minDist = maxDist;
133
134 std::vector<uint> tris = earCut(f);
135 for (uint i = 0; i < tris.size(); i += 3) {
136 PointType w;
137 ScalarType d = boundedDistance(
138 p,
140 f.vertex(tris[i])->position(),
141 f.vertex(tris[i + 1])->position(),
142 f.vertex(tris[i + 2])->position()),
143 minDist,
144 w,
145 signedDist);
146
147 if (std::abs(d) < minDist) {
148 minDist = std::abs(d);
149 closest = w;
150 }
151 }
152
153 return minDist;
154 }
155}
156
179template<Point3Concept PointType, FaceConcept FaceType, typename ScalarType>
181 const PointType& p,
182 const FaceType& f,
183 ScalarType maxDist,
184 bool signedDist = false)
185{
186 PointType closest;
187 return boundedDistance(p, f, maxDist, closest, signedDist);
188}
189
196template<FaceConcept FaceType, Point3Concept PointType, typename ScalarType>
198 const FaceType& f,
199 const PointType& p,
200 ScalarType maxDist,
201 bool signedDist = false)
202{
203 PointType closest;
204 return boundedDistance(p, f, maxDist, closest, signedDist);
205}
206
227template<Point3Concept PointType, FaceConcept FaceType>
229 const PointType& p,
230 const FaceType& f,
231 PointType& closest,
232 bool signedDist = false)
233{
234 using ScalarType = PointType::ScalarType;
235
236 ScalarType maxDist = std::numeric_limits<ScalarType>::max();
237 return boundedDistance(p, f, maxDist, closest, signedDist);
238}
239
259template<Point3Concept PointType, FaceConcept FaceType>
260auto distance(const PointType& p, const FaceType& f, bool signedDist = false)
261{
262 using ScalarType = PointType::ScalarType;
263
264 PointType closest;
265
266 ScalarType maxDist = std::numeric_limits<ScalarType>::max();
267 return boundedDistance(p, f, maxDist, closest, signedDist);
268}
269
275template<FaceConcept FaceType, Point3Concept PointType>
276auto distance(const FaceType& f, const PointType& p, bool signedDist = false)
277{
278 return distance(p, f, signedDist);
279}
280
300template<VertexConcept VertexType, FaceConcept FaceType>
301auto distance(const VertexType& v, const FaceType& f, bool signedDist = false)
302{
303 return distance(v.position(), f, signedDist);
304}
305
311template<FaceConcept FaceType, VertexConcept VertexType>
312auto distance(const FaceType& f, const VertexType& v, bool signedDist = false)
313{
314 return distance(v.position(), f, signedDist);
315}
316
317} // namespace vcl
318
319#endif // VCL_MESH_ELEM_ALGORITHMS_DISTANCE_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
The TriangleWrapper class is a wrapper around a N-Dimensional triangle.
Definition triangle_wrapper.h:54
Definition face.h:305
auto boundedDistance(const PointType &p, const TriangleType &triangle, ScalarType maxDist, PointType &closest, bool signedDist=false)
Compute the bounded distance between a 3D point and a 3D triangle.
Definition distance.h:203
auto distance(const PointType &point0, const PointType &point1)
Compute the distance between two Points of any dimension.
Definition distance.h:45
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:90