Lagrange
Loading...
Searching...
No Matches
split_tjunction_edges.h
1/*
2 * Copyright 2026 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
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>
21
22#include <algorithm>
23#include <atomic>
24#include <cstdint>
25#include <vector>
26
27namespace lagrange::bvh::internal {
28
29enum : std::uint8_t { AffectedTriangle = 1, AffectedPolygon = 2 };
30
31template <typename Scalar, typename Index>
32void classify_affected_facets(
33 SurfaceMesh<Scalar, Index>& mesh,
34 Index edge,
35 std::atomic<std::uint8_t>& affected_types)
36{
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)) {
42 found |=
43 mesh.get_facet_size(mesh.get_corner_facet(c)) == 3 ? AffectedTriangle : AffectedPolygon;
44 if ((known | found) == (AffectedTriangle | AffectedPolygon)) break;
45 }
46 if (found & ~known) affected_types.fetch_or(found, std::memory_order_relaxed);
47}
48
49// Both splitters retain the original facets, so their IDs remain stable until the final removal.
50// Only the mixed case needs a second pass and an edge-ID remap after split_edges clears the edges.
51template <typename Scalar, typename Index>
52void split_tjunction_edges(
53 SurfaceMesh<Scalar, Index>& mesh,
54 function_ref<span<Index>(Index)> get_edge_split_pts,
55 bool triangulate_affected,
56 std::uint8_t affected_types)
57{
58 auto all_active = [](Index) { return true; };
59 std::vector<Index> facets_to_remove;
60
61 if (!triangulate_affected || !(affected_types & AffectedTriangle)) {
62 const Index old_num_facets = mesh.get_num_facets();
63 facets_to_remove = lagrange::internal::split_edges_only(
64 mesh,
65 get_edge_split_pts,
66 function_ref<bool(Index)>(all_active));
67 if (triangulate_affected) {
68 auto is_new_facet = [old_num_facets](Index f) { return f >= old_num_facets; };
69 TriangulationOptions options;
72 mesh,
73 function_ref<bool(Index)>(is_new_facet),
74 std::move(options));
75 }
76 } else if (!(affected_types & AffectedPolygon)) {
77 facets_to_remove = lagrange::internal::split_edges(
78 mesh,
79 get_edge_split_pts,
80 function_ref<bool(Index)>(all_active));
81 } else {
82 const Index original_num_facets = mesh.get_num_facets();
83
84 // In the very rare case that the region affected by T-junctions contains both triangles
85 // and polygons, we need to split the edges in two passes. Unfortunately, extra book
86 // keeping is needed to remap the split points of the original polygon edges after
87 // split_edges clears the edge table.
88 struct SplitPolygonEdge
89 {
90 Index edge;
91 Index v0;
92 Index v1;
93 };
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]});
102 break;
103 }
104 }
105
106 auto is_original_triangle = [&](Index f) {
107 return f < original_num_facets && mesh.get_facet_size(f) == 3;
108 };
109 facets_to_remove = lagrange::internal::split_edges(
110 mesh,
111 get_edge_split_pts,
112 function_ref<bool(Index)>(is_original_triangle));
113
114 // split_edges clears the edge table. The original polygon facets still carry their old
115 // edges; rebuild it and map only their split edges to the original CSR ranges.
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());
125 }
126 }
127 auto get_remapped_split_pts = [&](Index e) -> span<Index> {
128 const Index old = old_edge_for_new_edge[e];
129 return old == invalid<Index>() ? span<Index>() : get_edge_split_pts(old);
130 };
131 auto is_original_polygon = [&](Index f) {
132 return f < original_num_facets && mesh.get_facet_size(f) != 3;
133 };
134 const Index before_polygons = mesh.get_num_facets();
135 auto polygons_to_remove = lagrange::internal::split_edges_only(
136 mesh,
137 function_ref<span<Index>(Index)>(get_remapped_split_pts),
138 function_ref<bool(Index)>(is_original_polygon));
139 auto is_new_polygon = [before_polygons](Index f) { return f >= before_polygons; };
140 TriangulationOptions options;
143 mesh,
144 function_ref<bool(Index)>(is_new_polygon),
145 std::move(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());
151 }
152 mesh.remove_facets(facets_to_remove);
153}
154
155} // namespace lagrange::bvh::internal
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