14#include <lagrange/SurfaceMesh.h>
15#include <lagrange/internal/split_edges.h>
16#include <lagrange/triangulate_polygonal_facets.h>
17#include <lagrange/utils/assert.h>
18#include <lagrange/utils/function_ref.h>
19#include <lagrange/utils/invalid.h>
20#include <lagrange/utils/span.h>
27namespace lagrange::bvh::internal {
29enum : std::uint8_t { AffectedTriangle = 1, AffectedPolygon = 2 };
31template <
typename Scalar,
typename Index>
32void classify_affected_facets(
33 SurfaceMesh<Scalar, Index>& mesh,
35 std::atomic<std::uint8_t>& affected_types)
37 const auto known = affected_types.load(std::memory_order_relaxed);
38 if (known == (AffectedTriangle | AffectedPolygon))
return;
39 std::uint8_t found = 0;
40 for (Index c = mesh.get_first_corner_around_edge(edge); c !=
invalid<Index>();
41 c = mesh.get_next_corner_around_edge(c)) {
43 mesh.get_facet_size(mesh.get_corner_facet(c)) == 3 ? AffectedTriangle : AffectedPolygon;
44 if ((known | found) == (AffectedTriangle | AffectedPolygon))
break;
46 if (found & ~known) affected_types.fetch_or(found, std::memory_order_relaxed);
51template <
typename Scalar,
typename Index>
52void split_tjunction_edges(
53 SurfaceMesh<Scalar, Index>& mesh,
55 bool triangulate_affected,
56 std::uint8_t affected_types)
58 auto all_active = [](Index) {
return true; };
59 std::vector<Index> facets_to_remove;
61 if (!triangulate_affected || !(affected_types & AffectedTriangle)) {
62 const Index old_num_facets = mesh.get_num_facets();
67 if (triangulate_affected) {
68 auto is_new_facet = [old_num_facets](Index f) {
return f >= old_num_facets; };
69 TriangulationOptions options;
76 }
else if (!(affected_types & AffectedPolygon)) {
82 const Index original_num_facets = mesh.get_num_facets();
88 struct SplitPolygonEdge
94 std::vector<SplitPolygonEdge> polygon_edges;
95 for (Index e = 0; e < mesh.get_num_edges(); ++e) {
96 if (get_edge_split_pts(e).empty())
continue;
97 for (Index c = mesh.get_first_corner_around_edge(e); c !=
invalid<Index>();
98 c = mesh.get_next_corner_around_edge(c)) {
99 if (mesh.get_facet_size(mesh.get_corner_facet(c)) == 3)
continue;
100 const auto ev = mesh.get_edge_vertices(e);
101 polygon_edges.push_back({e, ev[0], ev[1]});
106 auto is_original_triangle = [&](Index f) {
107 return f < original_num_facets && mesh.get_facet_size(f) == 3;
116 mesh.initialize_edges();
117 std::vector<Index> old_edge_for_new_edge(mesh.get_num_edges(),
invalid<Index>());
118 for (
const auto& old : polygon_edges) {
119 const Index e = mesh.find_edge_from_vertices(old.v0, old.v1);
121 old_edge_for_new_edge[e] = old.edge;
122 if (mesh.get_edge_vertices(e)[0] != old.v0) {
123 auto pts = get_edge_split_pts(old.edge);
124 std::reverse(pts.begin(), pts.end());
127 auto get_remapped_split_pts = [&](Index e) ->
span<Index> {
128 const Index old = old_edge_for_new_edge[e];
131 auto is_original_polygon = [&](Index f) {
132 return f < original_num_facets && mesh.get_facet_size(f) != 3;
134 const Index before_polygons = mesh.get_num_facets();
139 auto is_new_polygon = [before_polygons](Index f) {
return f >= before_polygons; };
140 TriangulationOptions options;
146 facets_to_remove.insert(
147 facets_to_remove.end(),
148 polygons_to_remove.begin(),
149 polygons_to_remove.end());
150 std::sort(facets_to_remove.begin(), facets_to_remove.end());
152 mesh.remove_facets(facets_to_remove);
void triangulate_polygonal_facets(SurfaceMesh< Scalar, Index > &mesh, const TriangulationOptions &options={})
Triangulate polygonal facets of a mesh using a prescribed set of rules.
Definition triangulate_polygonal_facets.cpp:550
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:197
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
constexpr T invalid()
You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices ...
Definition invalid.h:40
function_ref(R(*)(Args...)) -> function_ref< R(Args...)>
Deduce function_ref type from a function pointer.
std::vector< Index > split_edges_only(SurfaceMesh< Scalar, Index > &mesh, function_ref< span< Index >(Index)> get_edge_split_pts, function_ref< bool(Index)> active_facet)
Split edges based on the input split points without retriangulating the affected facets (in contrast ...
Definition split_edges.cpp:360
std::vector< Index > split_edges(SurfaceMesh< Scalar, Index > &mesh, function_ref< span< Index >(Index)> get_edge_split_pts, function_ref< bool(Index)> active_facet)
Split edges based on the input split points, retriangulating the affected facets accordingly.
Definition split_edges.cpp:104
@ Delaunay
Earcut triangulation refined toward a Delaunay triangulation via edge flips.
Definition triangulate_polygonal_facets.h:34