@@ -47,10 +47,11 @@ namespace
4747{
4848    struct  ComponentOverlap 
4949    {
50-         void  operator ()(
50+         bool  operator ()(
5151            geode::index_t  first_component, geode::index_t  second_component )
5252        {
5353            component_pairs.emplace_back ( first_component, second_component );
54+             return  false ;
5455        }
5556
5657        std::vector< std::pair< geode::index_t , geode::index_t  > >
@@ -75,10 +76,10 @@ namespace
7576    }
7677
7778    template  < geode::index_t  dimension, typename  Model >
78-     class  ModelSurfacesIntersection 
79+     class  ModelSurfacesIntersectionBase 
7980    {
8081    public: 
81-         ModelSurfacesIntersection ( const  Model& model,
82+         ModelSurfacesIntersectionBase ( const  Model& model,
8283            const  geode::uuid& surface_id1,
8384            const  geode::uuid& surface_id2 )
8485            : model_( model ),
@@ -94,31 +95,13 @@ namespace
9495        {
9596        }
9697
97-         void  operator ()( geode::index_t  t1_id, geode::index_t  t2_id )
98-         {
99-             if ( same_surface_ && t1_id == t2_id )
100-             {
101-                 return ;
102-             }
103-             const  auto  t1_vertices = mesh1_.polygon_vertices ( t1_id );
104-             const  auto  t2_vertices = mesh2_.polygon_vertices ( t2_id );
105-             const  auto  common_vertices =
106-                 triangles_common_vertices ( t1_vertices, t2_vertices );
107-             if ( common_vertices.size () == 3 
108-                 || triangles_intersect (
109-                     t1_id, t2_id, t1_vertices, t2_vertices, common_vertices ) )
110-             {
111-                 intersecting_triangles_.emplace_back ( t1_id, t2_id );
112-             }
113-         }
114- 
11598        std::vector< std::pair< geode::index_t , geode::index_t  > >
11699            intersecting_triangles ()
117100        {
118101            return  std::move ( intersecting_triangles_ );
119102        }
120103
121-     private :
104+     protected :
122105        absl::InlinedVector< std::array< geode::index_t , 2  >, 3  >
123106            triangles_common_vertices (
124107                const  geode::PolygonVertices& t1_vertices,
@@ -151,6 +134,21 @@ namespace
151134            absl::Span< const  std::array< geode::index_t , 2  > >
152135                common_vertices ) const ;
153136
137+         void  emplace ( geode::index_t  t1_id, geode::index_t  t2_id )
138+         {
139+             intersecting_triangles_.emplace_back ( t1_id, t2_id );
140+         }
141+ 
142+         const  geode::TriangulatedSurface< dimension >& mesh1 () const 
143+         {
144+             return  mesh1_;
145+         }
146+ 
147+         const  geode::TriangulatedSurface< dimension >& mesh2 () const 
148+         {
149+             return  mesh2_;
150+         }
151+ 
154152    private: 
155153        const  Model& model_;
156154        DEBUG_CONST bool  same_surface_;
@@ -162,6 +160,81 @@ namespace
162160            intersecting_triangles_;
163161    };
164162
163+     template  < geode::index_t  dimension, typename  Model >
164+     class  OneModelSurfacesIntersection 
165+         : public ModelSurfacesIntersectionBase< dimension, Model >
166+     {
167+     public: 
168+         OneModelSurfacesIntersection ( const  Model& model,
169+             const  geode::uuid& surface_id1,
170+             const  geode::uuid& surface_id2 )
171+             : ModelSurfacesIntersectionBase< dimension, Model >(
172+                 model, surface_id1, surface_id2 ),
173+               same_surface_{ surface_id1 == surface_id2 }
174+         {
175+         }
176+ 
177+         bool  operator ()( geode::index_t  t1_id, geode::index_t  t2_id )
178+         {
179+             if ( same_surface_ && t1_id == t2_id )
180+             {
181+                 return  false ;
182+             }
183+             const  auto  t1_vertices = this ->mesh1 ().polygon_vertices ( t1_id );
184+             const  auto  t2_vertices = this ->mesh2 ().polygon_vertices ( t2_id );
185+             const  auto  common_vertices =
186+                 this ->triangles_common_vertices ( t1_vertices, t2_vertices );
187+             if ( common_vertices.size () == 3 
188+                 || this ->triangles_intersect (
189+                     t1_id, t2_id, t1_vertices, t2_vertices, common_vertices ) )
190+             {
191+                 this ->emplace ( t1_id, t2_id );
192+                 return  true ;
193+             }
194+             return  false ;
195+         }
196+ 
197+     private: 
198+         DEBUG_CONST bool  same_surface_;
199+     };
200+ 
201+     template  < geode::index_t  dimension, typename  Model >
202+     class  AllModelSurfacesIntersection 
203+         : public ModelSurfacesIntersectionBase< dimension, Model >
204+     {
205+     public: 
206+         AllModelSurfacesIntersection ( const  Model& model,
207+             const  geode::uuid& surface_id1,
208+             const  geode::uuid& surface_id2 )
209+             : ModelSurfacesIntersectionBase< dimension, Model >(
210+                 model, surface_id1, surface_id2 ),
211+               same_surface_{ surface_id1 == surface_id2 }
212+         {
213+         }
214+ 
215+         bool  operator ()( geode::index_t  t1_id, geode::index_t  t2_id )
216+         {
217+             if ( same_surface_ && t1_id == t2_id )
218+             {
219+                 return  false ;
220+             }
221+             const  auto  t1_vertices = this ->mesh1 ().polygon_vertices ( t1_id );
222+             const  auto  t2_vertices = this ->mesh2 ().polygon_vertices ( t2_id );
223+             const  auto  common_vertices =
224+                 this ->triangles_common_vertices ( t1_vertices, t2_vertices );
225+             if ( common_vertices.size () == 3 
226+                 || this ->triangles_intersect (
227+                     t1_id, t2_id, t1_vertices, t2_vertices, common_vertices ) )
228+             {
229+                 this ->emplace ( t1_id, t2_id );
230+             }
231+             return  false ;
232+         }
233+ 
234+     private: 
235+         DEBUG_CONST bool  same_surface_;
236+     };
237+ 
165238    geode::index_t  third_point_index ( const  geode::PolygonVertices& vertices,
166239        absl::Span< const  std::array< geode::index_t , 2  > > common_vertices,
167240        geode::local_index_t  vertex_position )
@@ -181,13 +254,14 @@ namespace
181254    }
182255
183256    template  <>
184-     bool  ModelSurfacesIntersection< 2 , geode::Section >::triangles_intersect(
185-         geode::index_t  t1_id,
186-         geode::index_t  t2_id,
187-         const  geode::PolygonVertices& t1_vertices,
188-         const  geode::PolygonVertices& t2_vertices,
189-         absl::Span< const  std::array< geode::index_t , 2  > > common_vertices )
190-         const 
257+     bool 
258+         ModelSurfacesIntersectionBase< 2 , geode::Section >::triangles_intersect(
259+             geode::index_t  t1_id,
260+             geode::index_t  t2_id,
261+             const  geode::PolygonVertices& t1_vertices,
262+             const  geode::PolygonVertices& t2_vertices,
263+             absl::Span< const  std::array< geode::index_t , 2  > >
264+                 common_vertices ) const 
191265    {
192266        if ( common_vertices.size () == 2  )
193267        {
@@ -307,7 +381,7 @@ namespace
307381    }
308382
309383    template  <>
310-     bool  ModelSurfacesIntersection < 3 , geode::BRep >::triangles_intersect(
384+     bool  ModelSurfacesIntersectionBase < 3 , geode::BRep >::triangles_intersect(
311385        geode::index_t  t1_id,
312386        geode::index_t  t2_id,
313387        const  geode::PolygonVertices& t1_vertices,
@@ -359,7 +433,8 @@ namespace geode
359433
360434        bool  model_has_intersecting_surfaces () const 
361435        {
362-             const  auto  intersections = intersecting_triangles ();
436+             const  auto  intersections = intersecting_triangles<
437+                 OneModelSurfacesIntersection< dimension, Model > >();
363438            if ( intersections.empty () )
364439            {
365440                return  false ;
@@ -369,7 +444,8 @@ namespace geode
369444
370445        index_t  nb_intersecting_surfaces_elements_pair () const 
371446        {
372-             const  auto  intersections = intersecting_triangles ();
447+             const  auto  intersections = intersecting_triangles<
448+                 AllModelSurfacesIntersection< dimension, Model > >();
373449            if ( verbose_ )
374450            {
375451                for ( const  auto & triangle_pair : intersections )
@@ -388,7 +464,8 @@ namespace geode
388464        std::vector< std::pair< ComponentMeshElement, ComponentMeshElement > >
389465            intersecting_surfaces_elements () const 
390466        {
391-             const  auto  intersections = intersecting_triangles ();
467+             const  auto  intersections = intersecting_triangles<
468+                 AllModelSurfacesIntersection< dimension, Model > >();
392469            if ( verbose_ )
393470            {
394471                for ( const  auto & triangle_pair : intersections )
@@ -405,6 +482,7 @@ namespace geode
405482        }
406483
407484    private: 
485+         template  < typename  Action >
408486        std::vector< std::pair< ComponentMeshElement, ComponentMeshElement > >
409487            intersecting_triangles () const 
410488        {
@@ -430,9 +508,8 @@ namespace geode
430508                {
431509                    continue ;
432510                }
433-                 ModelSurfacesIntersection< dimension, Model >
434-                     surfaces_intersection_action{ model_, surface.id (),
435-                         surface.id () };
511+                 Action surfaces_intersection_action{ model_, surface.id (),
512+                     surface.id () };
436513                model_tree
437514                    .mesh_trees_ [model_tree.mesh_tree_ids_ .at ( surface.id () )]
438515                    .compute_self_element_bbox_intersections (
@@ -462,9 +539,8 @@ namespace geode
462539                {
463540                    continue ;
464541                }
465-                 ModelSurfacesIntersection< dimension, Model >
466-                     surfaces_intersection_action{ model_, surface_uuid1,
467-                         surface_uuid2 };
542+                 Action surfaces_intersection_action{ model_, surface_uuid1,
543+                     surface_uuid2 };
468544                model_tree.mesh_trees_ [components.first ]
469545                    .compute_other_element_bbox_intersections (
470546                        model_tree.mesh_trees_ [components.second ],
0 commit comments