Add Triangulate_Contours for planar polygon triangulation#8
Add Triangulate_Contours for planar polygon triangulation#8
Conversation
…nd add unit tests Co-authored-by: hdclark <934858+hdclark@users.noreply.github.com>
Co-authored-by: hdclark <934858+hdclark@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a Triangulate_Contours<T,I> utility that projects a contour_collection onto a best-fit plane and triangulates it into an fv_surface_mesh, along with a focused unit test suite validating the new behavior.
Changes:
- Declare and implement
Triangulate_Contours<T,I>(const contour_collection<T>&)inYgorMath.h/.cc, including explicit template instantiations for{float,double} × {uint32_t,uint64_t}. - Implement an ear-clipping based triangulation on a best-fit plane with support for multiple contours via a bridging heuristic.
- Add
tests2/YgorMath/triangulate_contours.cccovering basic polygons, concave shapes, tilted planes, multiple contours, degeneracies, and different scalar/index types.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/YgorMath.h | Declares the Triangulate_Contours template and documents its intended behavior and assumptions. |
| src/YgorMath.cc | Implements Triangulate_Contours, including plane fitting, 2D projection, multi-contour bridging, ear-clipping, and explicit template instantiations. |
| tests2/YgorMath/triangulate_contours.cc | Adds doctest-based unit tests for Triangulate_Contours across a variety of geometric configurations and type combinations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| struct ContourInfo { | ||
| size_t start_idx; | ||
| size_t end_idx; // One past the last index. | ||
| bool is_ccw; // Counter-clockwise orientation (outer contour) or clockwise (hole). | ||
| }; |
There was a problem hiding this comment.
ContourInfo::is_ccw is computed but never used in the rest of the function, which adds noise and suggests orientation‑dependent logic that is not actually implemented. Either remove this field and the associated area computation, or use it to distinguish outer contours from holes in the bridging logic so the stored flag has a purpose.
There was a problem hiding this comment.
Use it to distinguish outer contours from holes in the bridging logic so the stored flag has a purpose.
There was a problem hiding this comment.
Updated the bridging logic to use is_ccw to distinguish outer contours from holes. The sorting now prioritizes outer contours (CCW) first before processing holes (CW), ensuring outer boundaries are established before holes are merged in. See commit d25f924.
| try { | ||
| best_fit_plane = Plane_Orthogonal_Regression(all_points); | ||
| } catch(const std::exception &) { | ||
| // Points are likely collinear; cannot triangulate. | ||
| return mesh; |
There was a problem hiding this comment.
This catch block returns an empty mesh when Plane_Orthogonal_Regression throws (e.g., for collinear input), but there is no unit test exercising this failure path. Given the rest of the function is well covered by tests, consider adding a test case with collinear contour points to confirm the intended "empty mesh on regression failure" behaviour and guard against regressions.
There was a problem hiding this comment.
Added a test case "collinear points cause plane regression failure and return empty mesh" that verifies the empty mesh behavior when Plane_Orthogonal_Regression throws due to collinear input. See commit d25f924.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
…ollinear points test Co-authored-by: hdclark <934858+hdclark@users.noreply.github.com>
Applied all changes from the review thread in commit d25f924:
|
Implements polygon triangulation for
contour_collectionvertices projected onto a best-fit plane, returning anfv_surface_meshwith triangles as faces.Changes
Triangulate_Contours<T,I>(const contour_collection<T>&)— new free function inYgorMath.h/.ccPlane_Orthogonal_Regressionis_ccwflag to distinguish outer contours (CCW) from holes (CW) in bridging logicfloat/double×uint32_t/uint64_tUnit tests in
tests2/YgorMath/triangulate_contours.ccUsage
Resulting mesh is a non-watertight surface patch as specified.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.