GCC Code Coverage Report


Directory: ./
File: mesh/mesh_surface_intersection.h
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 182 209 87.1%
Functions: 51 58 87.9%
Branches: 61 184 33.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef GEOGRAM_MESH_MESH_SURFACE_INTERSECTION
41 #define GEOGRAM_MESH_MESH_SURFACE_INTERSECTION
42
43 #include <geogram/basic/common.h>
44 #include <geogram/mesh/mesh.h>
45 #include <geogram/mesh/mesh_io.h>
46 #include <geogram/numerics/exact_geometry.h>
47 #include <geogram/numerics/predicates.h>
48 #include <geogram/basic/process.h>
49 #include <geogram/basic/attributes.h>
50 #include <geogram/basic/debug_stream.h>
51 #include <functional>
52 #include <tuple>
53
54 /**
55 * \file geogram/mesh/mesh_surface_intersection.h
56 * \brief Functions for computing intersections between surfacic meshes and
57 * for boolean operations.
58 */
59
60 namespace GEO {
61
62 struct IsectInfo;
63
64 /********************************************************************/
65
66 /**
67 * \brief Computes surface intersections
68 * \details New vertices are stored with exact coordinates
69 */
70 class GEOGRAM_API MeshSurfaceIntersection {
71 public:
72
73 typedef exact::vec3h ExactPoint;
74
75 MeshSurfaceIntersection(Mesh& M);
76 ~MeshSurfaceIntersection();
77
78 /**
79 * \details A facet attribute of type index_t named "operand_bit" can
80 * indicate for each facet to which operand of a n-ary boolean
81 * operation it corresponds to (the same facet might belong to
82 * several operands). It is taken into account by the two variants of
83 * mesh_classify_intersections()
84 */
85 void intersect();
86
87 /**
88 * \brief Removes all the facets that are on the outer boundary
89 * \pre set_radial_sort(true) was set before calling intersect()
90 */
91 void remove_external_shell();
92
93 /**
94 * \brief Removes all the facets that are not on the outer boundary
95 * \pre set_radial_sort(true) was set before calling intersect()
96 */
97 void remove_internal_shells();
98
99 /**
100 * \brief Not implemented yet
101 * \details The goal here is to remove degenerate facet pairs that are
102 * attached to the border, and that may appear due to snap rounding issues.
103 */
104 void remove_fins();
105
106 /**
107 * \brief Classifies the facets and keep only
108 * the ones on the boundary of a combination of regions defined
109 * by a boolean expression.
110 * \details A facet attribute of type index_t named "operand_bit"
111 * indicates for each facet to which operand of a n-ary boolean
112 * operation it corresponds to (the same facet might belong to
113 * several operands).
114 * \pre set_radial_sort(true) was set before calling intersect()
115 * \param[in] expr the boolean function in ASCII.
116 * One can use the following elements, and parentheses:
117 * - Variables: A..Z or x0..x31, correspond to the bits of the
118 * "operand_bit" attribute
119 * - the special variable '*' corresponds to the union of everything
120 * - and: '&' or '*'
121 * - or: '|' or '+'
122 * - xor: '^'
123 * - difference: '-'
124 * - not: '!' or '~'
125 * Special values for expr:
126 * - "union" (union of everything), synonym of '*'
127 * - "intersection" (intersection of everything).
128 */
129 void classify(const std::string& expr);
130
131 /**
132 * \brief Finds the operands in which a component is included
133 * \param[in] component a connected component
134 * \param[in] v a vertex of the connected component
135 * \return the inclusion bits of the connected component relative
136 * to the operands
137 */
138 index_t compute_component_inclusion_bits(index_t component, index_t v);
139
140 /**
141 * \brief Like compute_component_inclusion_bits(), but when v is not an
142 * original vertex.
143 * \param[in] component a connected component
144 * \param[in] p a point on the connected component
145 * \return the inclusion bits of the connected component relative
146 * to the operands
147 * \details Called by compute_component_inclusion_bits()
148 * \see compute_component_inclusion_bits()
149 */
150 index_t compute_component_inclusion_bits_exact(
151 index_t component, const ExactPoint& p
152 );
153
154 /**
155 * \brief Merge coplanar facets and retriangulate them using a
156 * Constrained Delaunay triangulation
157 * \param[in] angle_tolerance angle tolerance for detecting coplanar
158 * facets and colinear edges (in degrees)
159 */
160 void simplify_coplanar_facets(double angle_tolerance = 0.0);
161
162 /**
163 * \brief Display information while computing the intersection.
164 * Default is unset.
165 */
166 578 void set_verbose(bool x) {
167 578 verbose_ = x;
168
3/4
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 559 times.
578 if(!verbose_ && fine_verbose_) {
169 ✗ fine_verbose_ = false;
170 }
171 578 }
172
173 /**
174 * \brief Display detailed information while computing the intersection.
175 * Default is unset.
176 */
177 57 void set_fine_verbose(bool x) {
178 57 fine_verbose_ = x;
179
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
57 if(fine_verbose_ && !verbose_) {
180 ✗ verbose_ = true;
181 }
182 57 }
183
184 /**
185 * \brief Sets the threshold from which triangle is considered
186 * to be a monster.
187 * \details Monster triangles are saved to a file for the zoo.
188 * \param[in] nb if a triangle has more than \p nb intersections
189 * in it, then it is considered to be a monster.
190 */
191 13 void set_monster_threshold(index_t nb) {
192 13 monster_threshold_ = nb;
193 13 }
194
195 /**
196 * \brief In dry run mode, the computed local triangulations
197 * are not inserted in the global mesh. This is for benchmarking.
198 * Default is off.
199 */
200 13 void set_dry_run(bool x) {
201 13 dry_run_ = x;
202 13 }
203
204 /**
205 * \brief If set, compute constrained Delaunay triangulation
206 * in the intersected triangles. If there are intersections
207 * in coplanar facets, it guarantees uniqueness of their
208 * triangulation. Default is set.
209 */
210 57 void set_delaunay(bool x) {
211 57 delaunay_ = x;
212 57 }
213
214 /**
215 * \brief detect and compute intersections between facets that share
216 * a facet or an edge. Set to false if input is a set of conformal
217 * meshes. Default is set.
218 */
219 57 void set_detect_intersecting_neighbors(bool x) {
220 57 detect_intersecting_neighbors_ = x;
221 57 }
222
223 /**
224 * \brief Specifies whether surfaces should be duplicated and
225 * radial edges sorted in order to create the volumetric
226 * partition yielded by the intersection
227 * \param[in] x true if radial edges should be sorted. Default is
228 * set
229 */
230 542 void set_radial_sort(bool x) {
231 542 use_radial_sort_ = x;
232 542 }
233
234 /**
235 * \brief Optionally save the skeleton (that is, the collection of
236 * non-manifold edges) to a given mesh. This option is not compatible
237 * with dry_run (throws an assertion fail if set).
238 * \param[in] skeleton a pointer to the mesh that will receive the
239 * skeleton.
240 * \param[in] trim_fins if set, do not keep bundles that have
241 * less than three halfedges.
242 */
243 ✗ void set_build_skeleton(Mesh* skeleton, bool trim_fins=false) {
244 ✗ skeleton_ = skeleton;
245 ✗ skeleton_trim_fins_ = trim_fins;
246 ✗ }
247
248 /**
249 * \brief Specifies that attributes should be interpolated
250 * \param[in] x true if attributes should be interpolated,
251 * false otherwise. Default is false.
252 */
253 ✗ void set_interpolate_attributes(bool x) {
254 ✗ interpolate_attributes_ = x;
255 ✗ }
256
257 protected:
258 /**
259 * \brief substep of intersect(), prepares the mesh
260 * \details Tesselates the facets if they are not triangulated,
261 * creates the operand bit for boolean op classification, removes
262 * the exactly degenerate triangles, colocate the points,
263 * optionally scales the coordinates and sets symbolic perturbation
264 * mode to lexicographic.
265 */
266 void intersect_prologue();
267
268 /**
269 * \brief substep of intersect(), finds all the intersection points
270 * and segments.
271 * \param[out] intersections the vector of IsectInfo. Each IsectInfo
272 * is either an intersection vertex or a pair of intersection
273 * vertices. Intersection vertices are represented in symbolic
274 * form, as a couple of triangle indices plus a couple of triangle
275 * subregion id (TriangleRegion).
276 * \details First uses a MeshFacetsAABB to detect candidate pairs
277 * of intersecting triangles, then calls triangles_intersection()
278 * in parallel. Finally, mesh facets are shuffled randomly, to
279 * ensure balanced multithreading for the subsequent steps.
280 */
281 void intersect_get_intersections(vector<IsectInfo>& intersections);
282
283 /**
284 * \brief substep of intersect(), inserts the intersection points
285 * and segments into the triangles.
286 * \param[in,out] intersections the vector of IsectInfo. Each IsectInfo
287 * is either an intersection vertex or a pair of intersection
288 * vertices. Intersection vertices are represented in symbolic
289 * form, as a couple of triangle indices plus a couple of triangle
290 * subregion id (TriangleRegion).
291 * \details Uses MeshInTriangle, a class derived from CDTBase2d,
292 * that computes a constrained Delaunay triangulation with
293 * intersection points represented with exact coordinates.
294 * Operates in parallel. Each thread computes constrained
295 * Delaunay triangulations independently, and commits them in the
296 * resulting mesh (with a lock to protect concurrent accesses).
297 * The initial mesh is copied (and kept in the mesh_copy_ member),
298 * so that concurrent read access do not need a lock.
299 */
300 void intersect_remesh_intersections(vector<IsectInfo>& intersections);
301
302 /**
303 * \brief subset of intersect(), cleans the resulting mesh and
304 * undoes optional geometric normalization.
305 * \param[in] intersections the vector of IsectInfo. Each IsectInfo
306 * is either an intersection vertex or a pair of intersection
307 * vertices. Intersection vertices are represented in symbolic
308 * form, as a couple of triangle indices plus a couple of triangle
309 * subregion id (TriangleRegion).
310 * \details find the intersection that landed exactly onto an
311 * existing mesh vertex and merges them. Removes the initial
312 * triangles that had intersections (they are replaced with new
313 * triangles). Merges duplicated triangles that come from
314 * coplanar regions. Undoes geometric normalizations. Restores
315 * initial symbolic perturbation mode.
316 */
317 void intersect_epilogue(const vector<IsectInfo>& intersections);
318
319
320 /**
321 * \brief Acquires a lock on this mesh
322 * \details A single thread can have the lock. When multiple threads
323 * want the lock, the ones that do not have it keep waiting until
324 * the one that owns the lock calls unlock(). All threads that modify
325 * the target mesh should call this function
326 * \see unlock()
327 */
328 15506 void lock() {
329 15506 Process::acquire_spinlock(lock_);
330 15506 }
331
332 /**
333 * \brief Releases the lock associated with this mesh
334 */
335 15506 void unlock() {
336 15506 Process::release_spinlock(lock_);
337 15506 }
338
339 /**
340 * \brief Gets the exact point associated with a vertex
341 * \details If the vertex has explicit exact coordinates associated
342 * with it, they are returned, else an exact ExactPoint is constructed
343 * from the double-precision coordinates stored in the mesh
344 * \param[in] v a vertex of the mesh
345 * \return the exact coordinates of this vertex, as a vector in
346 * homogeneous coordinates stored as expansions
347 */
348 ExactPoint exact_vertex(index_t v) const;
349
350 /**
351 * \brief Tests whether a given vertex is an original mesh vertex or an
352 * intersection
353 * \param[in] v a vertex of the mesh
354 * \retval true if v is an original vertex of the mesh
355 * \retval false if v is an intersection vertex, with exact coordinates
356 */
357 107256 bool is_original_vertex(index_t v) const {
358 107256 return (vertex_to_exact_point_[v] == nullptr);
359 }
360
361 /**
362 * \brief Finds or creates a vertex in the mesh, by exact coordinates
363 * \details If there is already a vertex with coordinates \p p, then
364 * the existing vertex is returned, else a new vertex is constructed.
365 * Note that only the vertices created by find_or_create_vertex() can
366 * be returned as existing vertices. Mesh vertices stored as double-
367 * precision coordinates are not retreived by this function.
368 * \param[in] p the exact coordinates of a point
369 * \return the index of a mesh vertex with \p p as coordinates
370 */
371 index_t find_or_create_exact_vertex(const ExactPoint& p);
372
373 /**
374 * \brief Gets the target mesh
375 * \return a modifiable reference to the mesh that was passed to
376 * the constructor
377 */
378 273909 Mesh& target_mesh() {
379 273909 return mesh_;
380 }
381
382 /**
383 * \brief Gets the target mesh
384 * \return a const reference to the mesh that was passed to
385 * the constructor
386 */
387 715 const Mesh& target_mesh() const {
388 715 return mesh_;
389 }
390
391 /**
392 * \brief Gets a copy of the initial mesh passed to the constructor
393 * \details It is used by the multithreaded mesh intersection algorithm.
394 * Each thread needs to both access the initial geometry and create
395 * new vertices and triangles in the target mesh. Creating new mesh
396 * elements can reallocate the internal vectors of the mesh, and
397 * change the address of the elements. This should not occur while
398 * another thread is reading the mesh. Copying the initial geometry
399 * in another mesh prevents this type of problems.
400 * \return a const reference to the mesh that was copied from the one
401 * passed to the constructor
402 */
403 2979 const Mesh& readonly_mesh() const {
404 2979 return mesh_copy_;
405 }
406
407 class RadialSort;
408
409 /**
410 * \brief Builds the Weiler model
411 * \details The Weiler model is a volumetric representation, where each
412 * facet is on the boundary of a closed region. Facets are duplicated,
413 * so that when two regions touch each other, each region has its own
414 * facet on the boundary. Two facets that touch in this way are
415 * connected by alpha3 links. Facets on the boundary of the same
416 * region are connected by alpha2 links.
417 */
418 void build_Weiler_model();
419
420 /**
421 * \brief Marks all the facets that are on the external shell
422 */
423 void mark_external_shell(vector<index_t>& on_external_shell);
424
425 /**
426 * \brief Gets the vertices of the initial facet (in mesh_copy_) that
427 * supports a facet in the intersection mesh (in mesh_)
428 * \param[in] f the facet in mesh_
429 * \return the index of the original facet in mesh_copy_
430 */
431 397446 index_t get_initial_facet(index_t f) const {
432 397446 return original_facet_id_[f];
433 }
434
435 /**
436 * \brief Indicates whether the initial facet (in mesh_copy_) that
437 * supports a facet in the intersection mesh (in mesh_) has same
438 * orientation or not.
439 * \param[in] f the facet in mesh_
440 * \retval false if \p f and the initial facet in mesh_copy_ have the
441 * same orientation
442 * \retval true otherwise
443 */
444 554503 bool initial_facet_is_flipped(index_t f) const {
445
4/6
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 554503 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 391760 times.
✓ Branch 8 taken 162743 times.
554503 return !f_is_flipped_.is_bound() || f_is_flipped_[f];
446 }
447
448 /**
449 * \brief Gets the vertices of the initial facet (in mesh_copy_) that
450 * supports a facet in the intersection mesh (in mesh_)
451 * \details Orientation is preserved. It is important, since it makes
452 * it possible to call predicates with points that have simpler
453 * coordinates
454 * \param[in] f the facet in mesh_
455 * \return the three vertices of the initial facet in mesh_copy_
456 * as a tuple of vec3
457 */
458 554503 std::tuple<vec3, vec3, vec3> get_initial_facet_vertices(index_t f) const {
459 // All facets are duplicated before radial sort. If f is one of
460 // the duplicated facets then its orientation is flipped as
461 // compared to initial facet.
462
1/2
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
554503 index_t orig_f = original_facet_id_[f];
463
1/2
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
554503 vec3 p1 = mesh_copy_.facets.point(orig_f,0);
464
1/2
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
554503 vec3 p2 = mesh_copy_.facets.point(orig_f,1);
465
1/2
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
554503 vec3 p3 = mesh_copy_.facets.point(orig_f,2);
466
3/4
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 391760 times.
✓ Branch 4 taken 162743 times.
554503 if(initial_facet_is_flipped(f)) {
467 391760 std::swap(p1,p3);
468 }
469
1/2
✓ Branch 1 taken 554503 times.
✗ Branch 2 not taken.
1109006 return std::make_tuple(p1,p2,p3);
470 }
471
472 protected:
473
474 /**
475 * A class for sorting triangles around their common radial edge.
476 */
477 class GEOGRAM_API RadialSort {
478 public:
479 /**
480 * \brief RadialSort constructor
481 * \param[in] I a reference to the MeshSurfaceIntersection
482 */
483 715 RadialSort(const MeshSurfaceIntersection& I) :
484 715 I_(I),
485 715 mesh_(I_.target_mesh()),
486 715 h_ref_(NO_INDEX),
487 715 degenerate_(false)
488 {
489 715 }
490
491 /**
492 * \brief Initializes radial sorting around a given halfedge
493 * \param[in] h_ref the reference halfedge
494 */
495 void init(index_t h_ref);
496
497 /**
498 * \brief Compares two halfedges
499 * \param[in] h1 , h2 the two halfedges
500 * \retval true if \p h1 should be before \p h2 in radial order
501 * \retval false otherwise
502 */
503 bool operator()(index_t h1, index_t h2) const;
504
505 /**
506 * \brief Tests if a degeneracy was encountered
507 * \retval true if there were two coplanar triangles on the same
508 * side relative to h_ref
509 * \retval false otherwise
510 */
511 3820 bool degenerate() const {
512 3820 return degenerate_;
513 }
514
515 protected:
516 /**
517 * \brief Computes the relative orientations of two halfedges
518 * \param[in] h1 , h2 the two halfedges
519 * \retval POSITIVE if going from \p h1's triangle to
520 * \p h2's triangle is a left turn (with h_ref facing to you)
521 * \retval ZERO if \p h1 and \p h2 have co-linear normals
522 * \retval NEGATIVE otherwise
523 */
524 Sign h_orient(index_t h1, index_t h2) const;
525
526 /**
527 * \brief Computes the normal orientation of a halfedge
528 * relative to h_ref
529 * \return the sign of the dot product between h_ref's triangle
530 * normal and \p h2's triangle normal.
531 */
532 Sign h_refNorient(index_t h2) const;
533
534 /**
535 * \brief Computes the normal to a facet with exact coordinates
536 * \param[in] h an halfedge incident to the facet
537 * \return the normal to the facet with exact coordinates
538 */
539 exact::vec3 normal(index_t h) const;
540
541 /**
542 * \brief This function is called whenever radial sort encounters
543 * a configuration not supposed to happen. It positions the generate_
544 * flag for this RadialSort. It can happen when using the
545 * expansion-based open-source kernel, that can encounter
546 * overflows or underflows. In this case, one may need the geogram+
547 * arithmetic kernel (marketed by the TESSAEL company).
548 */
549 void report_problem(const char* message) const;
550
551 private:
552 const MeshSurfaceIntersection& I_;
553 const Mesh& mesh_;
554 index_t h_ref_; // reference halfedge
555 exact::vec3 N_ref_; // normal to reference triangle (exact)
556 mutable bool degenerate_;
557 };
558
559 protected:
560 Process::spinlock lock_;
561 Mesh& mesh_;
562 Mesh mesh_copy_;
563 Attribute<const ExactPoint*> vertex_to_exact_point_;
564 Attribute<index_t> original_facet_id_; // mesh_ facet to mesh_copy_ facet
565 Attribute<bool> f_is_flipped_; // mesh_ facet is flipped wrt mesh_copy_
566
567 #if defined(GEOGRAM_USE_EXACT_NT) && defined(GEOGRAM_EXACT_NT_IS_MPF_NT)
568 // Exact points are canonicalized
569 // (by Numeric::optimize_number_representation(vec3HEx)) so
570 // we can use this comparator that makes the global vertex map
571 // much much faster.
572 typedef vec3HExLexicoCompareCanonical ExactPointCompare;
573 #else
574 // Generic comparator for global vertex map.
575 typedef vec3HgLexicoCompare<exact::scalar> ExactPointCompare;
576 #endif
577 std::map<ExactPoint,index_t,ExactPointCompare> exact_point_to_vertex_;
578
579 bool verbose_;
580 bool fine_verbose_;
581 bool delaunay_;
582 bool detect_intersecting_neighbors_;
583 bool use_radial_sort_;
584
585 PCK::SOSMode SOS_bkp_;
586
587 index_t monster_threshold_;
588 bool dry_run_;
589 friend class MeshInTriangle;
590 friend class CoplanarFacets;
591
592 Mesh* skeleton_;
593 bool skeleton_trim_fins_;
594 bool interpolate_attributes_;
595
596 bool has_operand_bits_;
597 /***************************************************/
598
599 /**
600 * \brief Halfedfge-like API wrappers on top of a triangulated mesh
601 * \details These are volumetric halfedges, also called
602 * combinatorial 3-map, with both volumetric links (alpha3)
603 * and surfacic link (alpha2).
604 * One may refer to this webpage for the definition of a 3-map:
605 * https://doc.cgal.org/latest/Combinatorial_map/
606 */
607 class Halfedges {
608 public:
609
610 /**
611 * \brief Halfedges constructor
612 * \param[in] I a reference to the MeshSurfaceIntersection
613 */
614 586 Halfedges(MeshSurfaceIntersection& I) : mesh_(I.mesh_) {
615 586 }
616
617 /**
618 * \brief Halfedges destructor
619 */
620 586 ~Halfedges() {
621 // TODO: destroy alpha3 attribute (kept now for debugging
622 586 }
623
624 /**
625 * \brief Initializes the structure
626 * \details Needs to be called before any other function
627 */
628 583 void initialize() {
629
1/2
✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
1166 facet_corner_alpha3_.bind(
630
1/2
✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
1166 mesh_.facet_corners.attributes(), "alpha3"
631 );
632 583 }
633
634 /**
635 * \brief Gets the number of halfedegs in the map
636 * \return the number of halfedges, that is, three times
637 * the number of triangles (halfedges are not stored explicitly).
638 */
639 index_t nb() const {
640 return mesh_.facet_corners.nb();
641 }
642
643 /**
644 * \brief used by range-based for
645 * \return a non-iterator corresponding to the first index.
646 */
647 index_as_iterator begin() const {
648 return index_as_iterator(0);
649 }
650
651 /**
652 * \brief used by range-based for
653 * \return a non-iterator to one position past the last index.
654 */
655 index_as_iterator end() const {
656 return index_as_iterator(nb());
657 }
658
659 /**
660 * \brief Gets the facet associated to a halfedge
661 * \param[in] h a halfedge index
662 * \return the facet index, that is, h/3
663 */
664 179041 index_t facet(index_t h) const {
665 179041 return h/3;
666 }
667
668 /**
669 * \brief gets the surfacic neighbor of a halfedge
670 * \details see definition of a combinatorial 3-map
671 * here: https://doc.cgal.org/latest/Combinatorial_map/
672 * \param[in] h a halfedge index
673 * \return another halfedge in the same surface, connecting the same
674 * vertices as \p h, but in opposite order
675 * \see sew2()
676 */
677 index_t alpha2(index_t h) const {
678 index_t t1 = facet(h);
679 index_t t2 = mesh_.facet_corners.adjacent_facet(h);
680 if(t2 == NO_INDEX) {
681 return NO_INDEX;
682 }
683 for(index_t h2: mesh_.facets.corners(t2)) {
684 if(mesh_.facet_corners.adjacent_facet(h2) == t1) {
685 return h2;
686 }
687 }
688 geo_assert_not_reached;
689 }
690
691 /**
692 * \brief gets the volumetric neighbor of a halfedge
693 * \details see definition of a combinatorial 3-map
694 * here: https://doc.cgal.org/latest/Combinatorial_map/
695 * \param[in] h a halfedge index
696 * \return another halfedge in a different volume, connecting
697 * the same vertices as \p h, but in opposite order
698 * \see sew3()
699 */
700 2145029 index_t alpha3(index_t h) const {
701 2145029 return facet_corner_alpha3_[h];
702 }
703
704 /**
705 * \brief gets the volumetric neighbor of a facet
706 * \param[in] f a facet
707 * \return a facet with the same vertices as \p f but in
708 * opposite index
709 */
710 773850 index_t facet_alpha3(index_t f) const {
711 773850 return alpha3(3*f)/3;
712 }
713
714 /**
715 * \brief gets a vertex of an halfedge
716 * \param[in] h the halfedge
717 * \param[in] dlv the local index of the vertex, in {0,1,2}
718 * \return
719 * - if \p dlv = 0 returns the origin vertex of \p h
720 * - if \p dlv = 1 returns the destination vertex of \p h
721 * - if \p dlv = 2 returns the vertex of the facet adjacent to \p h
722 * that is neither the origin nor the destination of \p h
723 */
724 46157684 index_t vertex(index_t h, index_t dlv) const {
725 46157684 index_t f = h/3;
726 46157684 index_t lv = (h+dlv)%3;
727 46157684 return mesh_.facets.vertex(f,lv);
728 }
729
730
731 /**
732 * \brief Creates a surfacic link between two halfedges
733 * \param[in] h1 , h2 the two halfedges to be connected
734 * \pre \p h1 and \p h2 should have the same origins and
735 * destinations but in reverse order (\p h1 's origin should
736 * be \p h2 's destination and vice-versa).
737 * \see alpha2()
738 */
739 726536 void sew2(index_t h1, index_t h2) {
740
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 726536 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
726536 geo_debug_assert(vertex(h1,0) == vertex(h2,1));
741
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 726536 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
726536 geo_debug_assert(vertex(h2,0) == vertex(h1,1));
742 726536 index_t t1 = h1/3;
743 726536 index_t t2 = h2/3;
744 726536 mesh_.facet_corners.set_adjacent_facet(h1,t2);
745 726536 mesh_.facet_corners.set_adjacent_facet(h2,t1);
746 726536 }
747
748 /**
749 * \brief Creates a volumetric link between two halfedges
750 * \param[in] h1 , h2 the two halfedges to be connected
751 * \pre \p h1 and \p h2 should have the same origins and
752 * destinations but in reverse order (\p h1 's origin should
753 * be \p h2 's destination and vice-versa).
754 * \see alpha3()
755 */
756 644643 void sew3(index_t h1, index_t h2) {
757
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 644643 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
644643 geo_debug_assert(vertex(h1,0) == vertex(h2,1));
758
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 644643 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
644643 geo_debug_assert(vertex(h2,0) == vertex(h1,1));
759 644643 facet_corner_alpha3_[h1] = h2;
760 644643 facet_corner_alpha3_[h2] = h1;
761 644643 }
762
763 private:
764 Mesh& mesh_;
765 Attribute<index_t> facet_corner_alpha3_;
766 } halfedges_;
767
768 /***************************************************/
769
770 /**
771 * \brief Represents the set of radial halfedge bundles
772 * \details A Radial bundle corresponds to the set of halfedges
773 * connecting the same pair of vertices (and in the same order).
774 */
775 class RadialBundles {
776 public:
777
778 /**
779 * \brief RadialBundles constructor
780 * \param[in] I a reference to the MeshSurfaceIntersectionx
781 */
782 586 RadialBundles(MeshSurfaceIntersection& I) : I_(I), mesh_(I.mesh_) {
783 586 }
784
785 /**
786 * \brief Initializes the structure
787 * \details Needs to be called before any other function
788 */
789 void initialize();
790
791 /**
792 * \brief Gets the number of bundles
793 */
794 6302657 index_t nb() const {
795 6302657 return bndl_start_.size() - 1;
796 }
797
798 /**
799 * \brief used by range-based for
800 * \return a non-iterator corresponding to the first bundle
801 */
802 1166 index_as_iterator begin() const {
803 1166 return index_as_iterator(0);
804 }
805
806 /**
807 * \brief used by range-based for
808 * \return a non-iterator to one position past the last bundle
809 */
810 1166 index_as_iterator end() const {
811
1/2
✓ Branch 1 taken 1166 times.
✗ Branch 2 not taken.
1166 return index_as_iterator(nb());
812 }
813
814 /**
815 * \brief Gets the number of halfedges in a bundle
816 * \param[in] bndl the bundle
817 * \return the number of halfedges in \p bndl
818 */
819 3976517 index_t nb_halfedges(index_t bndl) const {
820
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 3976517 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3976517 geo_debug_assert(bndl < nb());
821 3976517 return bndl_start_[bndl+1] - bndl_start_[bndl];
822 }
823
824 /**
825 * \brief Gets a halfedge in a bundle from local index
826 * \param[in] bndl the bundle
827 * \param[in] li the local index of the halfedge in the bundle,
828 * in [0 .. nb_halfedges(bndl)-1]
829 * \return the halfedge
830 */
831 1439163 index_t halfedge(index_t bndl, index_t li) const {
832
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1439163 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1439163 geo_debug_assert(bndl < nb());
833
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1439163 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1439163 geo_debug_assert(li < nb_halfedges(bndl));
834 1439163 return H_[bndl_start_[bndl] + li];
835 }
836
837 /**
838 * \brief Sets a halfedge in a bundle
839 * \param[in] bndl the bundle
840 * \param[in] li the local index of the halfedge in the bunble,
841 * in [0 .. nb_halfedges(bndl)-1]
842 * \param[in] h the new halfedge
843 */
844 67984 void set_halfedge(index_t bndl, index_t li, index_t h) {
845
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 67984 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
67984 geo_debug_assert(bndl < nb());
846
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 67984 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
67984 geo_debug_assert(li < nb_halfedges(bndl));
847 67984 H_[bndl_start_[bndl] + li] = h;
848 67984 }
849
850 /**
851 * \brief gets the halfedges in a bundle
852 * \param[in] bndl bundle index
853 * \return a modifiable sequence of halfedge indices
854 */
855 21837 index_ptr_range halfedges(index_t bndl) {
856 return index_ptr_range(
857 21837 H_, bndl_start_[bndl], bndl_start_[bndl+1]
858 21837 );
859 }
860
861 /**
862 * \brief gets the halfedges in a bundle
863 * \param[in] bndl bundle index
864 * \return a non-modifiable sequence of halfedge indices
865 */
866 const_index_ptr_range halfedges(index_t bndl) const {
867 return const_index_ptr_range(
868 H_, bndl_start_[bndl], bndl_start_[bndl+1]
869 );
870 }
871
872 /**
873 * \brief gets one of the vertices at the two extremities of a bundle
874 * \param[in] bndl bundle index
875 * \param[in] lv local vertex index, in {0,1}
876 * \return if \p lv = 0 the source vertex, if \p lv = 1 the
877 * destination vertex
878 */
879 80132 index_t vertex(index_t bndl, index_t lv) const {
880
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 80132 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
80132 geo_debug_assert(bndl_start_[bndl+1] - bndl_start_[bndl] > 0);
881 80132 index_t h = H_[bndl_start_[bndl]];
882 80132 return I_.halfedges_.vertex(h,lv);
883 }
884
885 /**
886 * \brief gets the first bundle starting from a vertex
887 * \param[in] v the vertex
888 * \details bundles starting from the same vertex are chained
889 * \return the index of the first bundle starting from \p v, or
890 * NO_INDEX if there is no such bundle
891 */
892 65546 index_t vertex_first_bundle(index_t v) const {
893 65546 return v_first_bndl_[v];
894 }
895
896 /**
897 * \brief gets the next bundle around a vertex
898 * \param[in] bndl the bundle
899 * \details bundles starting from the same vertex are chained
900 * \return the index of the next bundle that has the same origin
901 * vertex as \p bndl, or NO_INDEX if there is no such bundle
902 */
903 109846 index_t next_around_vertex(index_t bndl) const {
904 109846 return bndl_next_around_v_[bndl];
905 }
906
907 /**
908 * \brief gets the bumber of bundles around a vertex
909 * \param[in] v the vertex
910 * \return the number of bundles starting from \p v
911 */
912 36458 index_t nb_bundles_around_vertex(index_t v) const {
913 36458 index_t result = 0;
914 36458 for(
915 36458 index_t bndl = vertex_first_bundle(v);
916
2/2
✓ Branch 0 taken 94950 times.
✓ Branch 1 taken 36458 times.
131408 bndl != NO_INDEX;
917 94950 bndl = next_around_vertex(bndl)
918 ) {
919 94950 ++result;
920 }
921 36458 return result;
922 }
923
924 /**
925 * \brief gets the opposite bundle
926 * \param[in] bndl a bundle index
927 * \return the bundle connecting the same vertices as a given
928 * bundle but in the reverse order
929 */
930 61326 index_t opposite(index_t bndl) {
931
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 61326 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
61326 geo_debug_assert(bndl < nb());
932
2/2
✓ Branch 1 taken 30127 times.
✓ Branch 2 taken 31199 times.
61326 return (bndl >= nb()/2) ? (bndl-nb()/2) : (bndl+nb()/2);
933 }
934
935 /**
936 * \brief gets the predecessor of a bundle along its polyline
937 * \return the bundle arriving at the source vertex if it exists and
938 * is unique, NO_INDEX otherwise
939 */
940 14621 index_t prev_along_polyline(index_t bndl) {
941 14621 index_t v = vertex(bndl,0);
942
2/2
✓ Branch 1 taken 3685 times.
✓ Branch 2 taken 10936 times.
14621 if(nb_bundles_around_vertex(v) != 2) {
943 3685 return NO_INDEX;
944 }
945 10936 for(
946 10936 index_t bndl2 = vertex_first_bundle(v);
947
1/2
✓ Branch 1 taken 15431 times.
✗ Branch 2 not taken.
15431 bndl2 != NO_INDEX; bndl2 = next_around_vertex(bndl2)
948 ) {
949
2/2
✓ Branch 0 taken 10936 times.
✓ Branch 1 taken 4495 times.
15431 if(bndl2 != bndl) {
950 10936 return opposite(bndl2);
951 }
952 }
953 ✗ geo_assert_not_reached;
954 }
955
956 /**
957 * \brief gets the successor of a bundle along its polyline
958 * \return the bundle originated at the destination vertex
959 * if it exists and is unique, NO_INDEX otherwise
960 */
961 21837 index_t next_along_polyline(index_t bndl) {
962 21837 index_t v = vertex(bndl,1);
963
2/2
✓ Branch 1 taken 3685 times.
✓ Branch 2 taken 18152 times.
21837 if(nb_bundles_around_vertex(v) != 2) {
964 3685 return NO_INDEX;
965 }
966 18152 for(
967 18152 index_t bndl2 = vertex_first_bundle(v);
968
1/2
✓ Branch 1 taken 28553 times.
✗ Branch 2 not taken.
28553 bndl2 != NO_INDEX; bndl2 = next_around_vertex(bndl2)
969 ) {
970
2/2
✓ Branch 1 taken 18152 times.
✓ Branch 2 taken 10401 times.
28553 if(opposite(bndl2) != bndl) {
971 18152 return bndl2;
972 }
973 }
974 ✗ geo_assert_not_reached;
975 }
976
977 /**
978 * \brief Sorts the halfedges of the bundle in-place
979 * \param[in] bndl the bundle
980 * \param[in] RS a RadialSort structure (that caches
981 * some information)
982 * \retval true if radial sort was successful
983 * \retval false otherwise (may happen with expansion_nt)
984 */
985 3820 bool radial_sort(index_t bndl, RadialSort& RS) {
986
2/8
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3820 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
3820 geo_debug_assert(bndl < nb());
987
2/4
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3820 times.
3820 if(nb_halfedges(bndl) <= 2) {
988 ✗ return true;
989 }
990
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 auto b = H_.begin() + std::ptrdiff_t(bndl_start_[bndl]);
991
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
7640 auto e = H_.begin() + std::ptrdiff_t(bndl_start_[bndl+1]);
992
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 RS.init(*b);
993
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 std::sort(
994 24586 b, e, [&RS](index_t h1, index_t h2) {
995 24586 return RS(h1,h2);
996 }
997 );
998 3820 bool OK = !RS.degenerate();
999
1/2
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
3820 bndl_is_sorted_[bndl] = OK;
1000 3820 return OK;
1001 }
1002
1003 /**
1004 * \brief Sets the halfedges of a bundle
1005 * \details Used when radial sorting can be replaced with
1006 * combinatorial propagation.
1007 * \param[in] bndl a bundle
1008 * \param[in] halfedges the sorted list of the halfedges
1009 * in the bundle
1010 */
1011 18017 void set_sorted_halfedges(
1012 index_t bndl, const vector<index_t>& halfedges
1013 ) {
1014
1/6
✗ Branch 2 not taken.
✓ Branch 3 taken 18017 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
18017 geo_debug_assert(halfedges.size() == nb_halfedges(bndl));
1015
2/2
✓ Branch 1 taken 67984 times.
✓ Branch 2 taken 18017 times.
86001 for(index_t i=0; i<halfedges.size(); ++i) {
1016 67984 set_halfedge(bndl, i, halfedges[i]);
1017 }
1018 18017 bndl_is_sorted_[bndl] = true;
1019 18017 }
1020
1021 /**
1022 * \brief Indicates where to find a chart in a bundle
1023 * \details the first index is a chart index, and the second index
1024 * indicates which halfedge in a bundle is incident to that chart.
1025 */
1026 typedef std::pair<index_t, index_t> ChartPos;
1027
1028 /**
1029 * \brief Gets the sorted list of charts around bundle
1030 * \param[in] bndl a bundle
1031 * \param[out] chart_pos a list of (chart id, halfedge index)
1032 * couples, sorted by chart id, and where the halfedge index
1033 * is the original index in the bundle before sorting
1034 */
1035 void get_sorted_incident_charts(
1036 index_t bndl, vector<ChartPos>& chart_pos
1037 );
1038
1039 21837 bool is_sorted(index_t bndl) const {
1040
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 21837 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
21837 geo_debug_assert(bndl < nb());
1041 21837 return bndl_is_sorted_[bndl];
1042 }
1043
1044 // private:
1045 MeshSurfaceIntersection& I_;
1046 Mesh& mesh_;
1047 Attribute<index_t> facet_chart_;
1048 vector<index_t> H_;
1049 vector<index_t> bndl_start_;
1050 vector<index_t> v_first_bndl_;
1051 vector<index_t> bndl_next_around_v_;
1052 vector<char> bndl_is_sorted_; // not vector<bool>, multithread! (#308)
1053 } radial_bundles_;
1054
1055 /***************************************************/
1056
1057 class RadialPolylines {
1058 public:
1059 /**
1060 * \brief RadialPolylines constructor
1061 * \param[in] I a reference to the MeshSurfaceIntersection
1062 */
1063 586 RadialPolylines(MeshSurfaceIntersection& I) : I_(I), mesh_(I.mesh_) {
1064 586 }
1065
1066 /**
1067 * \brief Initializes the structure
1068 * \details Needs to be called before any other function
1069 */
1070 void initialize();
1071
1072 /**
1073 * \brief Sorts all the bundles of all polylines
1074 * \details The "chart" facet attribute needs to be initialized with
1075 * all surface connected components before calling this function.
1076 */
1077 void radial_sort();
1078
1079 /**
1080 * \brief Gets the number of polylines
1081 */
1082 13247 index_t nb() const {
1083 13247 return polyline_start_.size() - 1;
1084 }
1085
1086 /**
1087 * \brief used by range-based for
1088 * \return a non-iterator corresponding to the first polyline
1089 */
1090 583 index_as_iterator begin() const {
1091 583 return index_as_iterator(0);
1092 }
1093
1094 /**
1095 * \brief used by range-based for
1096 * \return a non-iterator to one position past the last polyline
1097 */
1098 583 index_as_iterator end() const {
1099
1/2
✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
583 return index_as_iterator(nb());
1100 }
1101
1102 /**
1103 * \brief gets the bundles in a polyline
1104 * \param[in] polyline index of the polyline
1105 * \return a non-modifiable sequence of bundle indices
1106 */
1107 11460 const_index_ptr_range bundles(index_t polyline) const {
1108
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 11460 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
11460 geo_debug_assert(polyline < nb());
1109 return const_index_ptr_range(
1110 11460 B_, polyline_start_[polyline], polyline_start_[polyline+1]
1111 11460 );
1112 }
1113
1114 ✗ index_t nb_bundles(index_t polyline) const {
1115 ✗ geo_debug_assert(polyline < nb());
1116 ✗ return polyline_start_[polyline+1] - polyline_start_[polyline];
1117 }
1118
1119 index_t bundle(index_t polyline, index_t li) const {
1120 geo_debug_assert(polyline < nb());
1121 geo_debug_assert(li < nb_bundles(polyline));
1122 return B_[polyline_start_[polyline] + li];
1123 }
1124
1125 /**
1126 * \brief Copies the set of polylines to a mesh
1127 * \details Used for visualization purposes
1128 * \param[out] to a mesh that will contain all the polygonal lines
1129 * \param[in] trim_fins if set, do not keep bundles that have
1130 * less than three halfedges.
1131 */
1132 void get_skeleton(Mesh& to, bool trim_fins=false);
1133
1134 private:
1135 MeshSurfaceIntersection& I_;
1136 Mesh& mesh_;
1137 vector<index_t> B_;
1138 vector<index_t> polyline_start_;
1139 } radial_polylines_;
1140 };
1141
1142 /********************************************************************/
1143
1144 enum MeshBooleanOperationFlags {
1145 MESH_BOOL_OPS_DEFAULT = 0,
1146 MESH_BOOL_OPS_VERBOSE = 1,
1147 MESH_BOOL_OPS_ATTRIBS = 2,
1148 MESH_BOOL_OPS_NO_SIMPLIFY = 4,
1149 MESH_BOOL_OPS_NO_CHECK_NEIGHBORS = 8
1150 };
1151
1152 /**
1153 * \brief Computes a boolean operation with two surface meshes.
1154 * \details A and B need to be two closed surface
1155 * mesh without intersections.
1156 * \param[in] A , B the two operands.
1157 * \param[out] result the computed mesh.
1158 * \param[in] operation one of "A+B", "A*B", "A-B", "B-A"
1159 * \param[in] flags MESH_BOOL_OPS_DEFAULT or an '|'-combination of:
1160 * - MESH_BOOL_OPS_VERBOSE: displays additional information
1161 * - MESH_BOOL_OPS_ATTRIBS: interpolates attributes
1162 * (implies MESH_BOOL_OPS_NO_SIMPLIFY)
1163 * - MESH_BOOL_OPS_NO_SIMPLIFY: do not simplify coplanar facets
1164 * - MESH_BOOL_OPS_NO_CHECK_NEIGHBORS: do not check intersections between
1165 * triangles that share an edge or a vertex
1166 */
1167 void GEOGRAM_API mesh_boolean_operation(
1168 Mesh& result, const Mesh& A, const Mesh& B, const std::string& operation,
1169 MeshBooleanOperationFlags flags = MESH_BOOL_OPS_DEFAULT
1170 );
1171
1172 /**
1173 * \brief Computes a boolean operation with two surface meshes.
1174 * \details A and B need to be two closed surface
1175 * mesh without intersections.
1176 * \param[in] A , B the two operands.
1177 * \param[out] result the computed mesh.
1178 * \param[in] operation one of "A+B", "A*B", "A-B", "B-A"
1179 * \param[in] verbose if set, display additional information
1180 * during computation
1181 */
1182 ✗ inline void mesh_boolean_operation(
1183 Mesh& result, const Mesh& A, const Mesh& B, const std::string& operation,
1184 bool verbose
1185 ) {
1186 ✗ mesh_boolean_operation(
1187 result, A, B, operation,
1188 verbose ? MESH_BOOL_OPS_VERBOSE : MESH_BOOL_OPS_DEFAULT
1189 );
1190 ✗ }
1191
1192 /**
1193 * \brief Computes the union of two surface meshes.
1194 * \details A and B need to be two closed surface
1195 * mesh without intersections.
1196 * \param[in] A , B the two operands.
1197 * \param[out] result the computed mesh.
1198 * \param[in] flags MESH_BOOL_OPS_DEFAULT or an '|'-combination of:
1199 * - MESH_BOOL_OPS_VERBOSE: displays additional information
1200 * - MESH_BOOL_OPS_ATTRIBS: interpolates attributes
1201 * (implies MESH_BOOL_OPS_NO_SIMPLIFY)
1202 * - MESH_BOOL_OPS_NO_SIMPLIFY: do not simplify coplanar facets
1203 * - MESH_BOOL_OPS_NO_CHECK_NEIGHBORS: do not check intersections between
1204 * triangles that share an edge or a vertex
1205 */
1206 ✗ inline void mesh_union(
1207 Mesh& result, const Mesh& A, const Mesh& B,
1208 MeshBooleanOperationFlags flags=MESH_BOOL_OPS_DEFAULT
1209 ) {
1210 ✗ mesh_boolean_operation(result, A, B, "A+B", flags);
1211 ✗ }
1212
1213 /**
1214 * \brief Computes the union of two surface meshes.
1215 * \details A and B need to be two closed surface
1216 * mesh without intersections.
1217 * \param[in] A , B the two operands.
1218 * \param[out] result the computed mesh.
1219 * \param[in] verbose if set, display additional
1220 * information during computation
1221 */
1222 inline void mesh_union(
1223 Mesh& result, const Mesh& A, const Mesh& B, bool verbose
1224 ) {
1225 mesh_boolean_operation(result, A, B, "A+B", verbose);
1226 }
1227
1228
1229 /**
1230 * \brief Computes the intersection of two surface meshes.
1231 * \details A and B need to be two closed surface
1232 * mesh without intersections.
1233 * \param[in] A , B the two operands.
1234 * \param[out] result the computed mesh.
1235 * \param[in] flags MESH_BOOL_OPS_DEFAULT or an '|'-combination of:
1236 * - MESH_BOOL_OPS_VERBOSE: displays additional information
1237 * - MESH_BOOL_OPS_ATTRIBS: interpolates attributes
1238 * (implies MESH_BOOL_OPS_NO_SIMPLIFY)
1239 * - MESH_BOOL_OPS_NO_SIMPLIFY: do not simplify coplanar facets
1240 * - MESH_BOOL_OPS_NO_CHECK_NEIGHBORS: do not check intersections between
1241 * triangles that share an edge or a vertex
1242 */
1243 ✗ inline void mesh_intersection(
1244 Mesh& result, const Mesh& A, const Mesh& B,
1245 MeshBooleanOperationFlags flags=MESH_BOOL_OPS_DEFAULT
1246 ) {
1247 ✗ mesh_boolean_operation(result, A, B, "A*B", flags);
1248 ✗ }
1249
1250 /**
1251 * \brief Computes the intersection of two surface meshes.
1252 * \details A and B need to be two closed surface
1253 * mesh without intersections.
1254 * \param[in] A , B the two operands.
1255 * \param[out] result the computed mesh.
1256 * \param[in] verbose if set, display additional information
1257 * during computation
1258 */
1259 inline void mesh_intersection(
1260 Mesh& result, const Mesh& A, const Mesh& B, bool verbose
1261 ) {
1262 mesh_boolean_operation(result, A, B, "A*B", verbose);
1263 }
1264
1265 /**
1266 * \brief Computes the difference of two surface meshes.
1267 * \details A and B need to be two closed surface
1268 * mesh without intersections.
1269 * \param[in] A , B the two operands.
1270 * \param[out] result the computed mesh.
1271 * \param[in] flags MESH_BOOL_OPS_DEFAULT or an '|'-combination of:
1272 * - MESH_BOOL_OPS_VERBOSE: displays additional information
1273 * - MESH_BOOL_OPS_ATTRIBS: interpolates attributes
1274 * (implies MESH_BOOL_OPS_NO_SIMPLIFY)
1275 * - MESH_BOOL_OPS_NO_SIMPLIFY: do not simplify coplanar facets
1276 * - MESH_BOOL_OPS_NO_CHECK_NEIGHBORS: do not check intersections between
1277 * triangles that share an edge or a vertex
1278 */
1279 ✗ inline void mesh_difference(
1280 Mesh& result, const Mesh& A, const Mesh& B,
1281 MeshBooleanOperationFlags flags=MESH_BOOL_OPS_DEFAULT
1282 ) {
1283 ✗ mesh_boolean_operation(result, A, B, "A-B", flags);
1284 ✗ }
1285
1286 /**
1287 * \brief Computes the difference of two surface meshes.
1288 * \details A and B need to be two closed surface
1289 * mesh without intersections.
1290 * \param[in] A , B the two operands.
1291 * \param[out] result the computed mesh.
1292 * \param[in] verbose if set, display additional information
1293 * during computation
1294 */
1295 inline void mesh_difference(
1296 Mesh& result, const Mesh& A, const Mesh& B, bool verbose
1297 ) {
1298 mesh_boolean_operation(result, A, B, "A-B", verbose);
1299 }
1300
1301 /**
1302 * \brief Attempts to make a surface mesh conformal by
1303 * removing intersecting facets and re-triangulating the holes.
1304 * \param[in] verbose if set, display additional information
1305 * during computation
1306 */
1307 void GEOGRAM_API mesh_remove_intersections(
1308 Mesh& M, index_t max_iter = 3, bool verbose=false
1309 );
1310
1311 /**
1312 * \brief Tests whether two mesh facets have a non-degenerate intersection.
1313 * \details If the facets are polygonal, they are triangulated from the
1314 * first vertex, and intersections between each pair of triangles is
1315 * tested.
1316 * \retval true if the two facets have an intersection. If they share a
1317 * vertex, it does not count as an intersection.
1318 * \retval false otherwise.
1319 */
1320 bool GEOGRAM_API mesh_facets_have_intersection(
1321 Mesh& M, index_t f1, index_t f2
1322 );
1323
1324 /**************************************************************************/
1325 }
1326
1327 #endif
1328