|
| 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 | +#include <lagrange/bvh/internal/resolve_tjunctions.h> |
| 13 | + |
| 14 | +#include <lagrange/SurfaceMeshTypes.h> |
| 15 | +#include <lagrange/bvh/AABB.h> |
| 16 | +#include <lagrange/bvh/api.h> |
| 17 | +#include <lagrange/internal/split_edges.h> |
| 18 | +#include <lagrange/triangulate_polygonal_facets.h> |
| 19 | +#include <lagrange/utils/assert.h> |
| 20 | +#include <lagrange/utils/function_ref.h> |
| 21 | +#include <lagrange/utils/span.h> |
| 22 | +#include <lagrange/views.h> |
| 23 | + |
| 24 | +// clang-format off |
| 25 | +#include <lagrange/utils/warnoff.h> |
| 26 | +#include <tbb/parallel_for.h> |
| 27 | +#include <lagrange/utils/warnon.h> |
| 28 | +// clang-format on |
| 29 | + |
| 30 | +#include <Eigen/Geometry> |
| 31 | + |
| 32 | +#include <algorithm> |
| 33 | +#include <utility> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +namespace lagrange::bvh::internal { |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +template <typename Scalar, typename Index, int Dim> |
| 41 | +void resolve_tjunctions_impl( |
| 42 | + SurfaceMesh<Scalar, Index>& mesh, |
| 43 | + const ResolveTJunctionsOptions& options) |
| 44 | +{ |
| 45 | + using Tree = AABB<Scalar, Dim>; |
| 46 | + using Box = typename Tree::Box; |
| 47 | + using Point = typename Tree::Point; |
| 48 | + |
| 49 | + mesh.initialize_edges(); |
| 50 | + const Index num_vertices = mesh.get_num_vertices(); |
| 51 | + const Index num_edges = mesh.get_num_edges(); |
| 52 | + if (num_vertices == 0 || num_edges == 0) return; |
| 53 | + |
| 54 | + auto vertices = vertex_view(mesh); |
| 55 | + auto to_point = [&](Index v) { |
| 56 | + Point p; |
| 57 | + for (int d = 0; d < Dim; ++d) p[d] = vertices(v, d); |
| 58 | + return p; |
| 59 | + }; |
| 60 | + |
| 61 | + // Resolve the detection tolerance (default is relative to the bounding box diagonal). |
| 62 | + Scalar tol = static_cast<Scalar>(options.tolerance); |
| 63 | + if (options.tolerance < 0) { |
| 64 | + Scalar diag = (vertices.colwise().maxCoeff() - vertices.colwise().minCoeff()).norm(); |
| 65 | + tol = static_cast<Scalar>(1e-6) * diag; |
| 66 | + } |
| 67 | + if (tol < 0) tol = 0; |
| 68 | + const Scalar tol_sq = tol * tol; |
| 69 | + |
| 70 | + // Build an AABB tree over the vertices (each stored as a degenerate point box). |
| 71 | + std::vector<Box> boxes(static_cast<size_t>(num_vertices)); |
| 72 | + for (Index v = 0; v < num_vertices; ++v) { |
| 73 | + const Point p = to_point(v); |
| 74 | + boxes[v] = Box(p, p); |
| 75 | + } |
| 76 | + Tree tree; |
| 77 | + tree.build({boxes.data(), boxes.size()}); |
| 78 | + |
| 79 | + // For each edge, query the tree with the edge's tolerance-expanded box for candidate vertices. |
| 80 | + std::vector<std::vector<std::pair<Scalar, Index>>> edge_splits(num_edges); |
| 81 | + tbb::parallel_for(Index(0), num_edges, [&](Index e) { |
| 82 | + if (options.boundary_only && !mesh.is_boundary_edge(e)) return; |
| 83 | + auto ev = mesh.get_edge_vertices(e); |
| 84 | + const Index v0 = ev[0]; |
| 85 | + const Index v1 = ev[1]; |
| 86 | + const Point p0 = to_point(v0); |
| 87 | + const Point p1 = to_point(v1); |
| 88 | + const Point edge_dir = p1 - p0; |
| 89 | + const Scalar len_sq = edge_dir.squaredNorm(); |
| 90 | + if (len_sq <= 0) return; // degenerate edge |
| 91 | + |
| 92 | + Box query(p0, p0); |
| 93 | + query.extend(p1); |
| 94 | + query = |
| 95 | + Box((query.min() - Point::Constant(tol)).eval(), |
| 96 | + (query.max() + Point::Constant(tol)).eval()); |
| 97 | + |
| 98 | + tree.intersect( |
| 99 | + query, |
| 100 | + function_ref<bool(typename Tree::Index)>([&](typename Tree::Index candidate) { |
| 101 | + const Index v = static_cast<Index>(candidate); |
| 102 | + if (v == v0 || v == v1) return true; |
| 103 | + const Point pv = to_point(v); |
| 104 | + const Scalar t = (pv - p0).dot(edge_dir) / len_sq; |
| 105 | + if (t <= 0 || t >= 1) return true; // must lie strictly between endpoints |
| 106 | + const Scalar dist_sq = (pv - (p0 + t * edge_dir)).squaredNorm(); |
| 107 | + if (dist_sq > tol_sq) return true; |
| 108 | + edge_splits[e].emplace_back(t, v); |
| 109 | + return true; |
| 110 | + })); |
| 111 | + std::sort(edge_splits[e].begin(), edge_splits[e].end(), [](const auto& a, const auto& b) { |
| 112 | + return a.first < b.first; |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + // Build CSR split lists, ordered from get_edge_vertices(e)[0] to [1] (ascending t). |
| 117 | + std::vector<Index> edge_split_offsets(num_edges + 1, 0); |
| 118 | + std::vector<Index> split_pts; |
| 119 | + for (Index e = 0; e < num_edges; e++) { |
| 120 | + for (const auto& entry : edge_splits[e]) split_pts.push_back(entry.second); |
| 121 | + edge_split_offsets[e + 1] = static_cast<Index>(split_pts.size()); |
| 122 | + } |
| 123 | + |
| 124 | + if (split_pts.empty()) return; |
| 125 | + |
| 126 | + // Split edges without retriangulating: each affected facet gets a polygonal copy appended at |
| 127 | + // id >= old_num_facets, leaving the originals (to be removed) in place. |
| 128 | + const Index old_num_facets = mesh.get_num_facets(); |
| 129 | + auto facets_to_remove = lagrange::internal::split_edges_only( |
| 130 | + mesh, |
| 131 | + function_ref<span<Index>(Index)>([&](Index e) -> span<Index> { |
| 132 | + const Index n = edge_split_offsets[e + 1] - edge_split_offsets[e]; |
| 133 | + return span<Index>(split_pts.data() + edge_split_offsets[e], n); |
| 134 | + }), |
| 135 | + function_ref<bool(Index)>([](Index) { return true; })); |
| 136 | + |
| 137 | + // Optionally triangulate only the new facets, then drop the original split facets. |
| 138 | + if (options.triangulate_affected) { |
| 139 | + auto is_new_facet = [old_num_facets](Index f) { return f >= old_num_facets; }; |
| 140 | + triangulate_polygonal_facets(mesh, function_ref<bool(Index)>(is_new_facet)); |
| 141 | + } |
| 142 | + mesh.remove_facets(facets_to_remove); |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace |
| 146 | + |
| 147 | +template <typename Scalar, typename Index> |
| 148 | +void resolve_tjunctions(SurfaceMesh<Scalar, Index>& mesh, ResolveTJunctionsOptions options) |
| 149 | +{ |
| 150 | + const Index dim = mesh.get_dimension(); |
| 151 | + if (dim == 2) { |
| 152 | + resolve_tjunctions_impl<Scalar, Index, 2>(mesh, options); |
| 153 | + } else if (dim == 3) { |
| 154 | + resolve_tjunctions_impl<Scalar, Index, 3>(mesh, options); |
| 155 | + } else { |
| 156 | + la_runtime_assert(false, "resolve_tjunctions: only 2D and 3D meshes are supported."); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +#define LA_X_resolve_tjunctions(_, Scalar, Index) \ |
| 161 | + template LA_BVH_API void resolve_tjunctions<Scalar, Index>( \ |
| 162 | + SurfaceMesh<Scalar, Index>&, \ |
| 163 | + ResolveTJunctionsOptions); |
| 164 | +LA_SURFACE_MESH_X(resolve_tjunctions, 0) |
| 165 | + |
| 166 | +} // namespace lagrange::bvh::internal |
0 commit comments