Skip to content

Commit 5bb2b3d

Browse files
panquezclaude
andcommitted
fix(IntersectionDetection): handle triangles collapsed to a point
In coplanar segment/triangle detection, best_projection_axis indexed the triangle vertices with longest_edge_index(), which is NO_LID when all the triangle vertices are colocated. This out-of-bounds read made BRepInspector segfault on models with such triangles. The projection axis now only depends on the segment in that case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 47844a8 commit 5bb2b3d

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎src/geode/geometry/intersection_detection.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,19 @@ namespace
106106
return max_abs_axis( normal.value() );
107107
}
108108
const auto& vertices = triangle.vertices();
109+
const auto& segment_vertices = segment.vertices();
110+
const geode::Vector3D segment_direction{ segment_vertices[0],
111+
segment_vertices[1] };
109112
const auto longest_edge = triangle.longest_edge_index();
113+
if( longest_edge == geode::NO_LID )
114+
{
115+
return coplanar_projection_axis(
116+
segment_vertices[0], segment_direction, { vertices[0] } );
117+
}
110118
const geode::Point3D& origin = vertices[longest_edge];
111119
const geode::Point3D& next =
112120
vertices[longest_edge == 2 ? 0 : longest_edge + 1];
113121
const geode::Vector3D direction{ origin, next };
114-
const auto& segment_vertices = segment.vertices();
115-
const geode::Vector3D segment_direction{ segment_vertices[0],
116-
segment_vertices[1] };
117122
if( segment_direction.length2() > direction.length2() )
118123
{
119124
return coplanar_projection_axis( segment_vertices[0],

‎tests/geometry/test-intersection-detection.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ void test_segment_degenerated_triangle_intersection_detection()
224224
geode::POSITION::parallel, geode::POSITION::parallel ),
225225
"Wrong result for segment_triangle_intersection_detection with "
226226
"segment crossing a cap triangle in its plane" );
227+
228+
const geode::Triangle3D point_triangle{ c, c, c };
229+
const auto far_from_point = geode::segment_triangle_intersection_detection(
230+
geode::Segment3D{ d, e }, point_triangle );
231+
geode::OpenGeodeGeometryException::test(
232+
far_from_point
233+
== std::make_pair(
234+
geode::POSITION::outside, geode::POSITION::outside ),
235+
"Wrong result for segment_triangle_intersection_detection with "
236+
"triangle collapsed to a point far from the segment" );
227237
}
228238

229239
void test_segment_line_intersection_detection()

0 commit comments

Comments
 (0)