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_INTERSECTION_ELEMENT_H
24#define VCL_ALGORITHMS_CORE_INTERSECTION_ELEMENT_H
25
26#include "misc.h"
27
28#include <vclib/algorithms/core/polygon.h>
29#include <vclib/concepts/mesh/elements/face.h>
30#include <vclib/space/core/box.h>
31#include <vclib/space/core/sphere.h>
32#include <vclib/space/core/triangle_wrapper.h>
33
34namespace vcl {
35
56template<FaceConcept FaceType, PointConcept PointType>
57bool intersect(const FaceType& f, const Box<PointType>& box)
58{
59 if constexpr (TriangleFaceConcept<FaceType>) {
60 return intersect(
62 f.vertex(0)->coord(),
63 f.vertex(1)->coord(),
64 f.vertex(2)->coord()),
65 box);
66 }
67 else {
68 bool b = false;
69
70 std::vector<uint> tris = earCut(f);
71 for (uint i = 0; i < tris.size() && !b; i += 3) {
72 b |= intersect(
74 f.vertex(tris[i])->coord(),
75 f.vertex(tris[i + 1])->coord(),
76 f.vertex(tris[i + 2])->coord()),
77 box);
78 }
79 return b;
80 }
81}
82
88template<PointConcept PointType, FaceConcept FaceType>
89bool intersect(const Box<PointType>& box, const FaceType& f)
90{
91 return intersect(f, box);
92}
93
114template<FaceConcept FaceType, PointConcept PointType, typename SScalar>
116 const FaceType& f,
117 const Sphere<SScalar>& sphere,
118 PointType& witness,
119 std::pair<SScalar, SScalar>& res)
120{
121 if constexpr (TriangleFaceConcept<FaceType>) {
122 return intersect(
124 f.vertex(0)->coord(),
125 f.vertex(1)->coord(),
126 f.vertex(2)->coord()),
127 sphere,
128 witness,
129 res);
130 }
131 else {
132 if (f.vertexNumber() == 3) {
133 return intersect(
135 f.vertex(0)->coord(),
136 f.vertex(1)->coord(),
137 f.vertex(2)->coord()),
138 sphere,
139 witness,
140 res);
141 }
142 else {
143 res.first = std::numeric_limits<SScalar>::max();
144 std::pair<SScalar, SScalar> r;
145
146 bool b = false;
147 PointType w;
148
149 std::vector<uint> tris = earCut(f);
150 for (uint i = 0; i < tris.size() && !b; i += 3) {
151 b |= intersect(
153 f.vertex(tris[i])->coord(),
154 f.vertex(tris[i + 1])->coord(),
155 f.vertex(tris[i + 2])->coord()),
156 sphere,
157 w,
158 r);
159
160 if (r.first < res.first) {
161 res = r;
162 witness = w;
163 }
164 }
165 return b;
166 }
167 }
168}
169
185template<FaceConcept FaceType, typename SScalar>
186bool intersect(const FaceType& f, const Sphere<SScalar>& sphere)
187{
189 std::pair<SScalar, SScalar> res;
190 return intersect(f, sphere, witness, res);
191}
192
198template<typename SScalar, FaceConcept FaceType>
199bool intersect(const Sphere<SScalar>& sphere, const FaceType& f)
200{
201 return intersect(f, sphere);
202}
203
204} // namespace vcl
205
206#endif // VCL_ALGORITHMS_CORE_INTERSECTION_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
bool intersect(const FaceType &f, const Box< PointType > &box)
Checks if a face intersects a box.
Definition element.h:57
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