libigl v2.5.0
Loading...
Searching...
No Matches
SelfIntersectMesh.h
Go to the documentation of this file.
1// This file is part of libigl, a simple c++ geometry processing library.
2//
3// Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
4//
5// This Source Code Form is subject to the terms of the Mozilla Public License
6// v. 2.0. If a copy of the MPL was not distributed with this file, You can
7// obtain one at http://mozilla.org/MPL/2.0/.
8#ifndef IGL_COPYLEFT_CGAL_SELFINTERSECTMESH_H
9#define IGL_COPYLEFT_CGAL_SELFINTERSECTMESH_H
10
11#include "CGAL_includes.hpp"
13#include "../../unique.h"
15
16#include <Eigen/Dense>
17#include <list>
18#include <map>
19#include <vector>
20#include <thread>
21#include <mutex>
22#include <cstdio>
23
24//#define IGL_SELFINTERSECTMESH_TIMING
25#ifndef IGL_FIRST_HIT_EXCEPTION
26#define IGL_FIRST_HIT_EXCEPTION 10
27#endif
28
29// The easiest way to keep track of everything is to use a class
30
31namespace igl
32{
33 namespace copyleft
34 {
35 namespace cgal
36 {
43 template <
44 typename Kernel,
45 typename DerivedV,
46 typename DerivedF,
47 typename DerivedVV,
48 typename DerivedFF,
49 typename DerivedIF,
50 typename DerivedJ,
51 typename DerivedIM>
53 {
54 typedef
56 Kernel,
57 DerivedV,
58 DerivedF,
59 DerivedVV,
60 DerivedFF,
61 DerivedIF,
62 DerivedJ,
63 DerivedIM> Self;
64 public:
65 // 3D Primitives
66 typedef CGAL::Point_3<Kernel> Point_3;
67 typedef CGAL::Segment_3<Kernel> Segment_3;
68 typedef CGAL::Triangle_3<Kernel> Triangle_3;
69 typedef CGAL::Plane_3<Kernel> Plane_3;
70 typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron_3;
71 // 2D Primitives
72 typedef CGAL::Point_2<Kernel> Point_2;
73 typedef CGAL::Segment_2<Kernel> Segment_2;
74 typedef CGAL::Triangle_2<Kernel> Triangle_2;
75 // 2D Constrained Delaunay Triangulation types
76 typedef CGAL::Exact_intersections_tag Itag;
77 // Axis-align boxes for all-pairs self-intersection detection
78 typedef std::vector<Triangle_3> Triangles;
79 typedef typename Triangles::iterator TrianglesIterator;
80 typedef typename Triangles::const_iterator TrianglesConstIterator;
81 typedef
82 CGAL::Box_intersection_d::Box_with_handle_d<double,3,TrianglesIterator>
84
85 // Input mesh
86 const Eigen::MatrixBase<DerivedV> & V;
87 const Eigen::MatrixBase<DerivedF> & F;
88 // Number of self-intersecting triangle pairs
89 typedef typename DerivedF::Index Index;
91 typedef std::vector<std::pair<Index, CGAL::Object>> ObjectList;
92 // Using a vector here makes this **not** output sensitive
94 typedef std::vector<Index> IndexList;
96 // #F-long list of faces with intersections mapping to the order in
97 // which they were first found
98 std::map<Index,ObjectList> offending;
99 // Make a short name for the edge map's key
100 typedef std::pair<Index,Index> EMK;
101 // Make a short name for the type stored at each edge, the edge map's
102 // value
103 typedef std::vector<Index> EMV;
104 // Make a short name for the edge map
105 typedef std::map<EMK,EMV> EdgeMap;
106 // Maps edges of offending faces to all incident offending faces
107 std::vector<std::pair<TrianglesIterator, TrianglesIterator> >
109
110 public:
112 public:
127 inline SelfIntersectMesh(
128 const Eigen::MatrixBase<DerivedV> & V,
129 const Eigen::MatrixBase<DerivedF> & F,
131 Eigen::PlainObjectBase<DerivedVV> & VV,
132 Eigen::PlainObjectBase<DerivedFF> & FF,
133 Eigen::PlainObjectBase<DerivedIF> & IF,
134 Eigen::PlainObjectBase<DerivedJ> & J,
135 Eigen::PlainObjectBase<DerivedIM> & IM);
136 private:
140 inline void mark_offensive(const Index f);
145 inline void count_intersection( const Index fa, const Index fb);
156 inline bool intersect(
157 const Triangle_3 & A,
158 const Triangle_3 & B,
159 const Index fa,
160 const Index fb);
174 inline bool single_shared_vertex(
175 const Triangle_3 & A,
176 const Triangle_3 & B,
177 const Index fa,
178 const Index fb,
179 const Index va,
180 const Index vb);
189 inline bool single_shared_vertex(
190 const Triangle_3 & A,
191 const Triangle_3 & B,
192 const Index fa,
193 const Index fb,
194 const Index va);
206 inline bool double_shared_vertex(
207 const Triangle_3 & A,
208 const Triangle_3 & B,
209 const Index fa,
210 const Index fb,
211 const std::vector<std::pair<Index,Index> > shared);
212
213 public:
220 inline void box_intersect(const Box& a, const Box& b);
222 inline void process_intersecting_boxes();
223 public:
224 // Getters:
225 //const IndexList& get_lIF() const{ return lIF;}
231 static inline void box_intersect_static(
232 SelfIntersectMesh * SIM,
233 const Box &a,
234 const Box &b);
235 private:
236 std::mutex m_offending_lock;
237 };
238 }
239 }
240}
241
242// Implementation
243
245#include "remesh_intersections.h"
246
247#include "../../get_seconds.h"
248#include "../../C_STR.h"
249
250
251#include <functional>
252#include <algorithm>
253#include <exception>
254#include <cassert>
255
256// References:
257// http://minregret.googlecode.com/svn/trunk/skyline/src/extern/CGAL-3.3.1/examples/Polyhedron/polyhedron_self_intersection.cpp
258// http://www.cgal.org/Manual/3.9/examples/Boolean_set_operations_2/do_intersect.cpp
259
260// Q: Should we be using CGAL::Polyhedron_3?
261// A: No! Input is just a list of unoriented triangles. Polyhedron_3 requires
262// a 2-manifold.
263// A: But! It seems we could use CGAL::Triangulation_3. Though it won't be easy
264// to take advantage of functions like insert_in_facet because we want to
265// constrain segments. Hmmm. Actually Triangulation_3 doesn't look right...
266
267// CGAL's box_self_intersection_d uses C-style function callbacks without
268// userdata. This is a leapfrog method for calling a member function. It should
269// be bound as if the prototype was:
270// static void box_intersect(const Box &a, const Box &b)
271// using boost:
272// boost::function<void(const Box &a,const Box &b)> cb
273// = boost::bind(&::box_intersect, this, _1,_2);
274//
275template <
276 typename Kernel,
277 typename DerivedV,
278 typename DerivedF,
279 typename DerivedVV,
280 typename DerivedFF,
281 typename DerivedIF,
282 typename DerivedJ,
283 typename DerivedIM>
285 Kernel,
286 DerivedV,
287 DerivedF,
288 DerivedVV,
289 DerivedFF,
290 DerivedIF,
291 DerivedJ,
293 Self * SIM,
294 const typename Self::Box &a,
295 const typename Self::Box &b)
296{
297 SIM->box_intersect(a,b);
298}
299
300template <
301 typename Kernel,
302 typename DerivedV,
303 typename DerivedF,
304 typename DerivedVV,
305 typename DerivedFF,
306 typename DerivedIF,
307 typename DerivedJ,
308 typename DerivedIM>
310 Kernel,
311 DerivedV,
312 DerivedF,
313 DerivedVV,
314 DerivedFF,
315 DerivedIF,
316 DerivedJ,
318 const Eigen::MatrixBase<DerivedV> & V,
319 const Eigen::MatrixBase<DerivedF> & F,
321 Eigen::PlainObjectBase<DerivedVV> & VV,
322 Eigen::PlainObjectBase<DerivedFF> & FF,
323 Eigen::PlainObjectBase<DerivedIF> & IF,
324 Eigen::PlainObjectBase<DerivedJ> & J,
325 Eigen::PlainObjectBase<DerivedIM> & IM):
326 V(V),
327 F(F),
328 count(0),
329 T(),
330 lIF(),
331 offending(),
333{
334#ifdef IGL_SELFINTERSECTMESH_TIMING
335 const auto & tictoc = []() -> double
336 {
337 static double t_start = igl::get_seconds();
338 double diff = igl::get_seconds()-t_start;
339 t_start += diff;
340 return diff;
341 };
342 const auto log_time = [&](const std::string& label) -> void{
343 std::printf("%50s: %0.5lf\n",
344 C_STR("SelfIntersectMesh." << label),tictoc());
345 };
346 tictoc();
347#endif
348
349 // Compute and process self intersections
351#ifdef IGL_SELFINTERSECTMESH_TIMING
352 log_time("convert_to_triangle_list");
353#endif
354 // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Box_intersection_d/Chapter_main.html#Section_63.5
355 // Create the corresponding vector of bounding boxes
356 std::vector<Box> boxes;
357 boxes.reserve(T.size());
358 for (
359 TrianglesIterator tit = T.begin();
360 tit != T.end();
361 ++tit)
362 {
363 if (!tit->is_degenerate())
364 {
365 boxes.push_back(Box(tit->bbox(), tit));
366 }
367 }
368 // Leapfrog callback
369 std::function<void(const Box &a,const Box &b)> cb =
370 std::bind(&box_intersect_static, this,
371 // Explicitly use std namespace to avoid confusion with boost (who puts
372 // _1 etc. in global namespace)
373 std::placeholders::_1,
374 std::placeholders::_2);
375#ifdef IGL_SELFINTERSECTMESH_TIMING
376 log_time("box_and_bind");
377#endif
378 // Run the self intersection algorithm with given cutoff size
379 CGAL::box_self_intersection_d(boxes.begin(), boxes.end(),cb,std::ptrdiff_t(params.cutoff));
380#ifdef IGL_SELFINTERSECTMESH_TIMING
381 log_time("box_intersection_d");
382#endif
383 try{
385 }catch(int e)
386 {
387 // Rethrow if not IGL_FIRST_HIT_EXCEPTION
389 {
390 throw e;
391 }
392 // Otherwise just fall through
393 }
394#ifdef IGL_SELFINTERSECTMESH_TIMING
395 log_time("resolve_intersection");
396#endif
397
398 // Convert lIF to Eigen matrix
399 assert(lIF.size()%2 == 0);
400 IF.resize(lIF.size()/2,2);
401 {
402 Index i=0;
403 for(
404 typename IndexList::const_iterator ifit = lIF.begin();
405 ifit!=lIF.end();
406 )
407 {
408 IF(i,0) = (*ifit);
409 ifit++;
410 IF(i,1) = (*ifit);
411 ifit++;
412 i++;
413 }
414 }
415#ifdef IGL_SELFINTERSECTMESH_TIMING
416 log_time("store_intersecting_face_pairs");
417#endif
418
419 if(params.detect_only)
420 {
421 return;
422 }
423
425 V,F,T,offending,
426 params.stitch_all,params.slow_and_more_precise_rounding,VV,FF,J,IM);
427
428#ifdef IGL_SELFINTERSECTMESH_TIMING
429 log_time("remesh_intersection");
430#endif
431}
432
433
434template <
435 typename Kernel,
436 typename DerivedV,
437 typename DerivedF,
438 typename DerivedVV,
439 typename DerivedFF,
440 typename DerivedIF,
441 typename DerivedJ,
442 typename DerivedIM>
444 Kernel,
445 DerivedV,
446 DerivedF,
447 DerivedVV,
448 DerivedFF,
449 DerivedIF,
450 DerivedJ,
451 DerivedIM>::mark_offensive(const Index f)
452{
453 lIF.push_back(f);
454 if(offending.count(f) == 0)
455 {
456 // first time marking, initialize with new id and empty list
457 offending[f] = {};
458 }
459}
460
461template <
462 typename Kernel,
463 typename DerivedV,
464 typename DerivedF,
465 typename DerivedVV,
466 typename DerivedFF,
467 typename DerivedIF,
468 typename DerivedJ,
469 typename DerivedIM>
471 Kernel,
472 DerivedV,
473 DerivedF,
474 DerivedVV,
475 DerivedFF,
476 DerivedIF,
477 DerivedJ,
478 DerivedIM>::count_intersection(
479 const Index fa,
480 const Index fb)
481{
482 std::lock_guard<std::mutex> guard(m_offending_lock);
483 mark_offensive(fa);
484 mark_offensive(fb);
485 this->count++;
486 // We found the first intersection
487 if(params.first_only && this->count >= 1)
488 {
490 }
491
492}
493
494template <
495 typename Kernel,
496 typename DerivedV,
497 typename DerivedF,
498 typename DerivedVV,
499 typename DerivedFF,
500 typename DerivedIF,
501 typename DerivedJ,
502 typename DerivedIM>
503inline bool igl::copyleft::cgal::SelfIntersectMesh<
504 Kernel,
505 DerivedV,
506 DerivedF,
507 DerivedVV,
508 DerivedFF,
509 DerivedIF,
510 DerivedJ,
511 DerivedIM>::intersect(
512 const Triangle_3 & A,
513 const Triangle_3 & B,
514 const Index fa,
515 const Index fb)
516{
517 // Determine whether there is an intersection
518 if(!CGAL::do_intersect(A,B))
519 {
520 return false;
521 }
522 count_intersection(fa,fb);
523 if(!params.detect_only)
524 {
525 // Construct intersection
526 CGAL::Object result = CGAL::intersection(A,B);
527 // Could avoid this mutex if `offending` was per-thread and passed as input
528 // reference.
529 std::lock_guard<std::mutex> guard(m_offending_lock);
530 offending[fa].push_back({fb, result});
531 offending[fb].push_back({fa, result});
532 }
533 return true;
534}
535
536template <
537 typename Kernel,
538 typename DerivedV,
539 typename DerivedF,
540 typename DerivedVV,
541 typename DerivedFF,
542 typename DerivedIF,
543 typename DerivedJ,
544 typename DerivedIM>
545inline bool igl::copyleft::cgal::SelfIntersectMesh<
546 Kernel,
547 DerivedV,
548 DerivedF,
549 DerivedVV,
550 DerivedFF,
551 DerivedIF,
552 DerivedJ,
553 DerivedIM>::single_shared_vertex(
554 const Triangle_3 & A,
555 const Triangle_3 & B,
556 const Index fa,
557 const Index fb,
558 const Index va,
559 const Index vb)
560{
561 if(single_shared_vertex(A,B,fa,fb,va))
562 {
563 return true;
564 }
565 return single_shared_vertex(B,A,fb,fa,vb);
566}
567
568template <
569 typename Kernel,
570 typename DerivedV,
571 typename DerivedF,
572 typename DerivedVV,
573 typename DerivedFF,
574 typename DerivedIF,
575 typename DerivedJ,
576 typename DerivedIM>
577inline bool igl::copyleft::cgal::SelfIntersectMesh<
578 Kernel,
579 DerivedV,
580 DerivedF,
581 DerivedVV,
582 DerivedFF,
583 DerivedIF,
584 DerivedJ,
585 DerivedIM>::single_shared_vertex(
586 const Triangle_3 & A,
587 const Triangle_3 & B,
588 const Index fa,
589 const Index fb,
590 const Index va)
591{
592 // This was not a good idea. It will not handle coplanar triangles well.
593 Segment_3 sa(
594 A.vertex((va+1)%3),
595 A.vertex((va+2)%3));
596
597 if(CGAL::do_intersect(sa,B))
598 {
599 // can't put count_intersection(fa,fb) here since we use intersect below
600 // and then it will be counted twice.
601 if(params.detect_only)
602 {
603 count_intersection(fa,fb);
604 return true;
605 }
606 CGAL::Object result = CGAL::intersection(sa,B);
607 if(const Point_3 * p = CGAL::object_cast<Point_3 >(&result))
608 {
609 // Single intersection --> segment from shared point to intersection
610 CGAL::Object seg = CGAL::make_object(Segment_3(
611 A.vertex(va),
612 *p));
613 count_intersection(fa,fb);
614 std::lock_guard<std::mutex> guard(m_offending_lock);
615 offending[fa].push_back({fb, seg});
616 offending[fb].push_back({fa, seg});
617 return true;
618 }else if(CGAL::object_cast<Segment_3 >(&result))
619 {
620 // Need to do full test. Intersection could be a general poly.
621 bool test = intersect(A,B,fa,fb);
622 ((void)test);
623 assert(test && "intersect should agree with do_intersect");
624 return true;
625 }else
626 {
627 // Should never happen.
628 assert(false && "Segment ∩ triangle neither point nor segment?");
629 }
630 }
631
632 return false;
633}
634
635
636template <
637 typename Kernel,
638 typename DerivedV,
639 typename DerivedF,
640 typename DerivedVV,
641 typename DerivedFF,
642 typename DerivedIF,
643 typename DerivedJ,
644 typename DerivedIM>
645inline bool igl::copyleft::cgal::SelfIntersectMesh<
646 Kernel,
647 DerivedV,
648 DerivedF,
649 DerivedVV,
650 DerivedFF,
651 DerivedIF,
652 DerivedJ,
653 DerivedIM>::double_shared_vertex(
654 const Triangle_3 & A,
655 const Triangle_3 & B,
656 const Index fa,
657 const Index fb,
658 const std::vector<std::pair<Index,Index> > shared)
659{
660 auto opposite_vertex = [](const Index a0, const Index a1) {
661 // get opposite index of A
662 int a2=-1;
663 for(int c=0;c<3;++c)
664 if(c!=a0 && c!=a1) {
665 a2 = c;
666 break;
667 }
668 assert(a2 != -1);
669 return a2;
670 };
671
672 // must be co-planar
673 Index a2 = opposite_vertex(shared[0].first, shared[1].first);
674 if (! B.supporting_plane().has_on(A.vertex(a2)))
675 return false;
676
677 Index b2 = opposite_vertex(shared[0].second, shared[1].second);
678
679 if (int(CGAL::coplanar_orientation(A.vertex(shared[0].first), A.vertex(shared[1].first), A.vertex(a2))) *
680 int(CGAL::coplanar_orientation(B.vertex(shared[0].second), B.vertex(shared[1].second), B.vertex(b2))) < 0)
681 // There is certainly no self intersection as the non-shared triangle vertices lie on opposite sides of the shared edge.
682 return false;
683
684 // Since A and B are non-degenerate the intersection must be a polygon
685 // (triangle). Either
686 // - the vertex of A (B) opposite the shared edge of lies on B (A), or
687 // - an edge of A intersects and edge of B without sharing a vertex
688 //
689 // Determine if the vertex opposite edge (a0,a1) in triangle A lies in
690 // (intersects) triangle B
691 const auto & opposite_point_inside = [](
692 const Triangle_3 & A, const Index a2, const Triangle_3 & B)
693 -> bool
694 {
695 return CGAL::do_intersect(A.vertex(a2),B);
696 };
697
698 // Determine if edge opposite vertex va in triangle A intersects edge
699 // opposite vertex vb in triangle B.
700 const auto & opposite_edges_intersect = [](
701 const Triangle_3 & A, const Index va,
702 const Triangle_3 & B, const Index vb) -> bool
703 {
704 Segment_3 sa( A.vertex((va+1)%3), A.vertex((va+2)%3));
705 Segment_3 sb( B.vertex((vb+1)%3), B.vertex((vb+2)%3));
706 bool ret = CGAL::do_intersect(sa,sb);
707 return ret;
708 };
709
710 if(
711 !opposite_point_inside(A,a2,B) &&
712 !opposite_point_inside(B,b2,A) &&
713 !opposite_edges_intersect(A,shared[0].first,B,shared[1].second) &&
714 !opposite_edges_intersect(A,shared[1].first,B,shared[0].second))
715 {
716 return false;
717 }
718
719 // there is an intersection indeed
720 count_intersection(fa,fb);
721 if(params.detect_only)
722 {
723 return true;
724 }
725 // Construct intersection
726 try
727 {
728 // This can fail for Epick but not Epeck
729 CGAL::Object result = CGAL::intersection(A,B);
730 if(!result.empty())
731 {
732 if(CGAL::object_cast<Segment_3 >(&result))
733 {
734 // not coplanar
735 assert(false &&
736 "Co-planar non-degenerate triangles should intersect over triangle");
737 return false;
738 } else if(CGAL::object_cast<Point_3 >(&result))
739 {
740 // this "shouldn't" happen but does for inexact
741 assert(false &&
742 "Co-planar non-degenerate triangles should intersect over triangle");
743 return false;
744 } else
745 {
746 // Triangle object
747 std::lock_guard<std::mutex> guard(m_offending_lock);
748 offending[fa].push_back({fb, result});
749 offending[fb].push_back({fa, result});
750 return true;
751 }
752 }else
753 {
754 // CGAL::intersection is disagreeing with do_intersect
755 assert(false && "CGAL::intersection should agree with predicate tests");
756 return false;
757 }
758 }catch(...)
759 {
760 // This catches some cgal assertion:
761 // CGAL error: assertion violation!
762 // Expression : is_finite(d)
763 // File : /opt/local/include/CGAL/GMP/Gmpq_type.h
764 // Line : 132
765 // Explanation:
766 // But only if NDEBUG is not defined, otherwise there's an uncaught
767 // "Floating point exception: 8" SIGFPE
768 return false;
769 }
770 // No intersection.
771 return false;
772}
773
774template <
775 typename Kernel,
776 typename DerivedV,
777 typename DerivedF,
778 typename DerivedVV,
779 typename DerivedFF,
780 typename DerivedIF,
781 typename DerivedJ,
782 typename DerivedIM>
783inline void igl::copyleft::cgal::SelfIntersectMesh<
784 Kernel,
785 DerivedV,
786 DerivedF,
787 DerivedVV,
788 DerivedFF,
789 DerivedIF,
790 DerivedJ,
792 const Box& a,
793 const Box& b)
794{
795 candidate_triangle_pairs.push_back({a.handle(), b.handle()});
796}
797
798template <
799 typename Kernel,
800 typename DerivedV,
801 typename DerivedF,
802 typename DerivedVV,
803 typename DerivedFF,
804 typename DerivedIF,
805 typename DerivedJ,
806 typename DerivedIM>
808 Kernel,
809 DerivedV,
810 DerivedF,
811 DerivedVV,
812 DerivedFF,
813 DerivedIF,
814 DerivedJ,
816{
817 std::mutex exception_mutex;
818 bool exception_fired = false;
819 int exception = -1;
820 // Eventually switching to igl::parallel_for would be good, but currently
821 // igl::parallel_for does not provide a way to catch exceptions fired on a
822 // spawned thread _outside_ of its loop-chunk which is the mechanism used here
823 // to bail out early when `first_only=true` to avoid
824 // O(#candidate_triangle_pairs) behavior.
825 auto process_chunk = [&]( const size_t first, const size_t last) -> void
826 {
827 try
828 {
829 assert(last >= first);
830
831 for (size_t i=first; i<last; i++)
832 {
833 if(exception_fired) return;
834 Index fa=T.size(), fb=T.size();
835 {
836 const auto& tri_pair = candidate_triangle_pairs[i];
837 fa = tri_pair.first - T.begin();
838 fb = tri_pair.second - T.begin();
839 }
840 assert(fa < T.size());
841 assert(fb < T.size());
842
843 if(exception_fired) return;
844
845 const Triangle_3& A = T[fa];
846 const Triangle_3& B = T[fb];
847
848 // Number of combinatorially shared vertices
849 Index comb_shared_vertices = 0;
850 // Number of geometrically shared vertices (*not* including
851 // combinatorially shared)
852 Index geo_shared_vertices = 0;
853 // Keep track of shared vertex indices
854 std::vector<std::pair<Index,Index> > shared;
855 Index ea,eb;
856 for(ea=0;ea<3;ea++)
857 {
858 for(eb=0;eb<3;eb++)
859 {
860 if(F(fa,ea) == F(fb,eb))
861 {
862 comb_shared_vertices++;
863 shared.emplace_back(ea,eb);
864 }else if(A.vertex(ea) == B.vertex(eb))
865 {
866 geo_shared_vertices++;
867 shared.emplace_back(ea,eb);
868 }
869 }
870 }
871 const Index total_shared_vertices =
872 comb_shared_vertices + geo_shared_vertices;
873 if(exception_fired) return;
874
875 if(comb_shared_vertices== 3)
876 {
877 assert(shared.size() == 3);
878 // Combinatorially duplicate face, these should be removed by
879 // preprocessing
880 continue;
881 }
882 if(total_shared_vertices== 3)
883 {
884 assert(shared.size() == 3);
885 // Geometrically duplicate face, these should be removed by
886 // preprocessing
887 continue;
888 }
889 if(total_shared_vertices == 2)
890 {
891 assert(shared.size() == 2);
892 // Q: What about coplanar?
893 //
894 // o o
895 // |\ /|
896 // | \/ |
897 // | /\ |
898 // |/ \|
899 // o----o
900 double_shared_vertex(A,B,fa,fb,shared);
901 continue;
902 }
903 assert(total_shared_vertices<=1);
904 if(total_shared_vertices==1)
905 {
906 single_shared_vertex(A,B,fa,fb,shared[0].first,shared[0].second);
907 }else
908 {
909 intersect(A,B,fa,fb);
910 }
911 }
912 }catch(int e)
913 {
914 std::lock_guard<std::mutex> exception_lock(exception_mutex);
915 exception_fired = true;
916 exception = e;
917 }
918 };
919 const size_t num_threads = default_num_threads();
920 assert(num_threads > 0);
921 const size_t num_pairs = candidate_triangle_pairs.size();
922 const size_t chunk_size = num_pairs / num_threads;
923 std::vector<std::thread> threads;
924 for (size_t i=0; i<num_threads-1; i++)
925 {
926 threads.emplace_back(process_chunk, i*chunk_size, (i+1)*chunk_size);
927 }
928 // Do some work in the master thread.
929 process_chunk((num_threads-1)*chunk_size, num_pairs);
930 for (auto& t : threads)
931 {
932 if (t.joinable()) t.join();
933 }
934 if(exception_fired) throw exception;
935 //process_chunk(0, candidate_triangle_pairs.size());
936}
937
938#endif
#define C_STR(X)
Convert a stream of things to a const char *.
Definition C_STR.h:28
#define IGL_FIRST_HIT_EXCEPTION
Definition SelfIntersectMesh.h:26
Class for computing the self-intersections of a mesh.
Definition SelfIntersectMesh.h:53
CGAL::Triangle_3< Kernel > Triangle_3
Definition SelfIntersectMesh.h:68
CGAL::Segment_3< Kernel > Segment_3
Definition SelfIntersectMesh.h:67
CGAL::Tetrahedron_3< Kernel > Tetrahedron_3
Definition SelfIntersectMesh.h:70
IndexList lIF
Definition SelfIntersectMesh.h:95
CGAL::Point_2< Kernel > Point_2
Definition SelfIntersectMesh.h:72
Index count
Definition SelfIntersectMesh.h:90
void process_intersecting_boxes()
Process all of the intersecting boxes.
Definition SelfIntersectMesh.h:815
std::pair< Index, Index > EMK
Definition SelfIntersectMesh.h:100
std::vector< std::pair< TrianglesIterator, TrianglesIterator > > candidate_triangle_pairs
Definition SelfIntersectMesh.h:108
DerivedF::Index Index
Definition SelfIntersectMesh.h:89
CGAL::Box_intersection_d::Box_with_handle_d< double, 3, TrianglesIterator > Box
Definition SelfIntersectMesh.h:83
const Eigen::MatrixBase< DerivedF > & F
Definition SelfIntersectMesh.h:87
RemeshSelfIntersectionsParam params
Definition SelfIntersectMesh.h:111
Triangles T
Definition SelfIntersectMesh.h:93
CGAL::Plane_3< Kernel > Plane_3
Definition SelfIntersectMesh.h:69
const Eigen::MatrixBase< DerivedV > & V
Definition SelfIntersectMesh.h:86
std::map< EMK, EMV > EdgeMap
Definition SelfIntersectMesh.h:105
CGAL::Segment_2< Kernel > Segment_2
Definition SelfIntersectMesh.h:73
SelfIntersectMesh(const Eigen::MatrixBase< DerivedV > &V, const Eigen::MatrixBase< DerivedF > &F, const RemeshSelfIntersectionsParam &params, Eigen::PlainObjectBase< DerivedVV > &VV, Eigen::PlainObjectBase< DerivedFF > &FF, Eigen::PlainObjectBase< DerivedIF > &IF, Eigen::PlainObjectBase< DerivedJ > &J, Eigen::PlainObjectBase< DerivedIM > &IM)
Constructs (VV,FF) a new mesh with self-intersections of (V,F) subdivided.
Definition SelfIntersectMesh.h:317
std::vector< std::pair< Index, CGAL::Object > > ObjectList
Definition SelfIntersectMesh.h:91
CGAL::Point_3< Kernel > Point_3
Definition SelfIntersectMesh.h:66
static void box_intersect_static(SelfIntersectMesh *SIM, const Box &a, const Box &b)
Static function that captures a SelfIntersectMesh instance to pass to cgal.
Definition SelfIntersectMesh.h:292
void box_intersect(const Box &a, const Box &b)
Callback function called during box self intersections test.
Definition SelfIntersectMesh.h:791
std::vector< Index > IndexList
Definition SelfIntersectMesh.h:94
std::vector< Triangle_3 > Triangles
Definition SelfIntersectMesh.h:78
Triangles::iterator TrianglesIterator
Definition SelfIntersectMesh.h:79
std::vector< Index > EMV
Definition SelfIntersectMesh.h:103
std::map< Index, ObjectList > offending
Definition SelfIntersectMesh.h:98
CGAL::Exact_intersections_tag Itag
Definition SelfIntersectMesh.h:76
CGAL::Triangle_2< Kernel > Triangle_2
Definition SelfIntersectMesh.h:74
Triangles::const_iterator TrianglesConstIterator
Definition SelfIntersectMesh.h:80
void remesh_intersections(const Eigen::MatrixBase< DerivedV > &V, const Eigen::MatrixBase< DerivedF > &F, const std::vector< CGAL::Triangle_3< Kernel > > &T, const std::map< typename DerivedF::Index, std::vector< std::pair< typename DerivedF::Index, CGAL::Object > > > &offending, bool stitch_all, bool slow_and_more_precise_rounding, Eigen::PlainObjectBase< DerivedVV > &VV, Eigen::PlainObjectBase< DerivedFF > &FF, Eigen::PlainObjectBase< DerivedJ > &J, Eigen::PlainObjectBase< DerivedIM > &IM)
Remesh faces according to results of intersection detection and construction (e.g.
void mesh_to_cgal_triangle_list(const Eigen::MatrixBase< DerivedV > &V, const Eigen::MatrixBase< DerivedF > &F, std::vector< CGAL::Triangle_3< Kernel > > &T)
Convert a mesh (V,F) to a list of CGAL triangles.
Definition AABB.h:18
double get_seconds()
Current time in seconds.
void intersect(const M &A, const M &B, M &C)
Determine the intersect between two sets of coefficients using ==.
void count(const Eigen::SparseMatrix< XType > &X, const int dim, Eigen::SparseVector< SType > &S)
Count the number of non-zeros in the columns or rows of a sparse matrix.
unsigned int default_num_threads(unsigned int force_num_threads=0)
Returns the default number of threads used in libigl.
Parameters for SelfIntersectMesh, remesh_self_intersections and remesh_intersections,...
Definition RemeshSelfIntersectionsParam.h:21