Skip to content

Commit 3ad39c0

Browse files
qnzhouQingnan Zhou
andauthored
Sync with f7df06a (#91)
Co-authored-by: Qingnan Zhou <qzhou@Qingnans-MacBook-Pro.local>
1 parent cd1321c commit 3ad39c0

47 files changed

Lines changed: 3294 additions & 369 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,13 @@ endif()
234234
# When using vcpkg, use find_package() to find external libraries
235235
if(DEFINED VCPKG_INSTALLED_DIR)
236236
set(LAGRANGE_USE_FIND_PACKAGE_DEFAULT ON)
237-
option(LAGRANGE_WITH_EMBREE_3 "Use Embree3 with Lagrange" ON)
238237
else()
239238
set(LAGRANGE_USE_FIND_PACKAGE_DEFAULT OFF)
240-
option(LAGRANGE_WITH_EMBREE_3 "Use Embree3 with Lagrange" OFF)
241239
endif()
242240

241+
# Default to Embree 4
242+
option(LAGRANGE_WITH_EMBREE_3 "Use Embree3 with Lagrange" OFF)
243+
243244
if(EMSCRIPTEN)
244245
set(LAGRANGE_DISABLE_FPE_DEFAULT ON)
245246
else()

cmake/recipes/external/nanobind.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ include(CPM)
1919
CPMAddPackage(
2020
NAME nanobind
2121
GITHUB_REPOSITORY wjakob/nanobind
22-
GIT_TAG v2.9.2
22+
GIT_TAG v2.13.0
2323
DOWNLOAD_ONLY ON
2424
)
2525

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/bvh/api.h>
16+
#include <lagrange/bvh/resolve_tjunctions.h>
17+
18+
namespace lagrange::bvh::internal {
19+
20+
///
21+
/// Resolve T-junctions formed by collinear, overlapping edges (BVH-accelerated variant).
22+
///
23+
/// This behaves identically to @ref lagrange::bvh::resolve_tjunctions, but locates the candidate
24+
/// vertices near each edge using an AABB tree built over the mesh vertices, rather than an
25+
/// axis-aligned sort-and-sweep. It shares @ref lagrange::bvh::ResolveTJunctionsOptions and produces
26+
/// the same output. Kept as an internal reference/benchmark alternative to the public variant.
27+
///
28+
/// @param[in,out] mesh Input mesh (triangle or polygonal). Modified in place.
29+
/// @param[in] options Optional settings.
30+
///
31+
/// @tparam Scalar Mesh scalar type.
32+
/// @tparam Index Mesh index type.
33+
///
34+
/// @see lagrange::bvh::resolve_tjunctions
35+
///
36+
template <typename Scalar, typename Index>
37+
void resolve_tjunctions(SurfaceMesh<Scalar, Index>& mesh, ResolveTJunctionsOptions options = {});
38+
39+
} // namespace lagrange::bvh::internal
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/bvh/api.h>
16+
17+
namespace lagrange::bvh {
18+
19+
/// @addtogroup module-bvh
20+
/// @{
21+
22+
///
23+
/// Option settings for @ref resolve_tjunctions.
24+
///
25+
struct ResolveTJunctionsOptions
26+
{
27+
/// Absolute distance tolerance used to detect T-junctions. A vertex whose distance to a
28+
/// non-incident edge is within this tolerance, and whose projection lies strictly between the
29+
/// edge endpoints, is treated as lying on that edge. If negative, the tolerance defaults to
30+
/// `1e-6` times the bounding box diagonal of the mesh.
31+
double tolerance = -1;
32+
33+
/// If true, only boundary edges (edges adjacent to a single facet) are checked for
34+
/// T-junctions. Set to false to also resolve T-junctions on interior edges.
35+
bool boundary_only = true;
36+
37+
/// If true (default), triangulate the facets affected by edge splitting, so a triangle-mesh
38+
/// input yields a triangle-mesh output. If false, leave those facets as polygons.
39+
bool triangulate_affected = true;
40+
};
41+
42+
///
43+
/// Resolve T-junctions formed by collinear, overlapping edges.
44+
///
45+
/// A T-junction occurs when a vertex lies on an edge that it is not topologically connected to,
46+
/// causing the edge to overlap with the (unconnected) sub-edges incident to that vertex. This
47+
/// function splits every such edge at the vertices lying on it, and splits the adjacent facets
48+
/// accordingly, so the output mesh contains no overlapping collinear edges.
49+
///
50+
/// Vertices are not moved: only edges and facets are subdivided so the topology conforms to the
51+
/// existing vertex positions. The `tolerance` only controls detection, not geometric snapping.
52+
///
53+
/// Both triangle and polygonal meshes are supported. By default (`triangulate_affected == true`)
54+
/// the facets touched by a split are triangulated, so a triangle-mesh input yields a triangle-mesh
55+
/// output. Set `triangulate_affected` to false to instead keep those facets as polygons, with the
56+
/// split points inserted as additional (collinear) boundary vertices.
57+
///
58+
/// @param[in,out] mesh Input mesh (triangle or polygonal). Modified in place.
59+
/// @param[in] options Optional settings.
60+
///
61+
/// @tparam Scalar Mesh scalar type.
62+
/// @tparam Index Mesh index type.
63+
///
64+
/// @note Consider running @ref remove_duplicate_vertices and @ref remove_degenerate_facets
65+
/// beforehand to avoid propagating pre-existing degeneracies.
66+
///
67+
template <typename Scalar, typename Index>
68+
void resolve_tjunctions(SurfaceMesh<Scalar, Index>& mesh, ResolveTJunctionsOptions options = {});
69+
70+
/// @}
71+
72+
} // namespace lagrange::bvh

modules/bvh/python/src/bvh.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <lagrange/bvh/compute_mesh_distances.h>
1717
#include <lagrange/bvh/compute_uv_overlap.h>
1818
#include <lagrange/bvh/remove_interior_shells.h>
19+
#include <lagrange/bvh/resolve_tjunctions.h>
1920
#include <lagrange/bvh/weld_vertices.h>
2021
#include <lagrange/python/binding.h>
2122
#include <lagrange/python/bvh.h>
@@ -600,6 +601,36 @@ that do not share vertices are tested (vertex-adjacent facets are skipped).
600601
Vertex-adjacent facets are filtered before testing. The geometric test uses
601602
include_boundary=false (interior intersection only).
602603
)");
604+
605+
m.def(
606+
"resolve_tjunctions",
607+
[](MeshType& mesh, double tolerance, bool boundary_only, bool triangulate_affected) {
608+
bvh::ResolveTJunctionsOptions opts;
609+
opts.tolerance = tolerance;
610+
opts.boundary_only = boundary_only;
611+
opts.triangulate_affected = triangulate_affected;
612+
bvh::resolve_tjunctions(mesh, std::move(opts));
613+
},
614+
"mesh"_a,
615+
nb::kw_only(),
616+
"tolerance"_a = bvh::ResolveTJunctionsOptions().tolerance,
617+
"boundary_only"_a = bvh::ResolveTJunctionsOptions().boundary_only,
618+
"triangulate_affected"_a = bvh::ResolveTJunctionsOptions().triangulate_affected,
619+
R"(Resolve T-junctions formed by collinear, overlapping edges.
620+
621+
A T-junction occurs when a vertex lies on an edge it is not topologically connected to. This
622+
splits every such edge at the vertices lying on it, and splits the adjacent facets accordingly,
623+
so the output mesh has no overlapping collinear edges. Vertices are not moved; only edges and
624+
facets are subdivided. Both triangle and polygonal meshes are supported.
625+
626+
:param mesh: Input mesh, triangle or polygonal (modified in place).
627+
:param tolerance: Absolute distance tolerance for detecting vertices on an edge. If negative,
628+
defaults to 1e-6 times the bounding box diagonal.
629+
:param boundary_only: If True, only boundary edges are checked for T-junctions. Set to False to
630+
also resolve T-junctions on interior edges.
631+
:param triangulate_affected: If True (default), triangulate the facets affected by a split, so a
632+
triangle mesh stays triangular. If False, keep those facets as polygons.
633+
)");
603634
}
604635

605636
} // namespace lagrange::python
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)