Lagrange
Loading...
Searching...
No Matches
bind_utilities.h
1/*
2 * Copyright 2022 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/AttributeTypes.h>
15#include <lagrange/NormalWeightingType.h>
16#include <lagrange/cast_attribute.h>
17#include <lagrange/combine_meshes.h>
18#include <lagrange/compute_area.h>
19#include <lagrange/compute_centroid.h>
20#include <lagrange/compute_components.h>
21#include <lagrange/compute_dihedral_angles.h>
22#include <lagrange/compute_dijkstra_distance.h>
23#include <lagrange/compute_edge_lengths.h>
24#include <lagrange/compute_facet_circumcenter.h>
25#include <lagrange/compute_facet_normal.h>
26#include <lagrange/compute_greedy_coloring.h>
27#include <lagrange/compute_mesh_covariance.h>
28#include <lagrange/compute_normal.h>
29#include <lagrange/compute_pointcloud_pca.h>
30#include <lagrange/compute_seam_edges.h>
31#include <lagrange/compute_tangent_bitangent.h>
32#include <lagrange/compute_uv_charts.h>
33#include <lagrange/compute_uv_distortion.h>
34#include <lagrange/compute_vertex_normal.h>
35#include <lagrange/compute_vertex_valence.h>
36#include <lagrange/extract_submesh.h>
37#include <lagrange/filter_attributes.h>
38#include <lagrange/internal/constants.h>
39#include <lagrange/isoline.h>
40#include <lagrange/map_attribute.h>
41#include <lagrange/normalize_meshes.h>
42#include <lagrange/orient_outward.h>
43#include <lagrange/orientation.h>
44#include <lagrange/permute_facets.h>
45#include <lagrange/permute_vertices.h>
46#include <lagrange/python/binding.h>
47#include <lagrange/python/tensor_utils.h>
48#include <lagrange/python/utils/StackVector.h>
49#include <lagrange/remap_vertices.h>
50#include <lagrange/reorder_mesh.h>
51#include <lagrange/select_facets_by_normal_similarity.h>
52#include <lagrange/select_facets_in_frustum.h>
53#include <lagrange/separate_by_components.h>
54#include <lagrange/separate_by_facet_groups.h>
55#include <lagrange/split_facets_by_material.h>
56#include <lagrange/thicken_and_close_mesh.h>
57#include <lagrange/topology.h>
58#include <lagrange/transform_mesh.h>
59#include <lagrange/triangulate_polygonal_facets.h>
60#include <lagrange/unify_index_buffer.h>
61#include <lagrange/utils/invalid.h>
62#include <lagrange/uv_mesh.h>
63#include <lagrange/weld_indexed_attribute.h>
64
65#include <optional>
66#include <string_view>
67#include <vector>
68
69namespace lagrange::python {
70
71template <typename Scalar, typename Index>
72void bind_utilities(nanobind::module_& m)
73{
74 namespace nb = nanobind;
75 using namespace nb::literals;
76 using MeshType = SurfaceMesh<Scalar, Index>;
77
78 nb::enum_<NormalWeightingType>(m, "NormalWeightingType", "Normal weighting type.")
79 .value("Uniform", NormalWeightingType::Uniform, "Uniform weighting")
80 .value(
81 "CornerTriangleArea",
83 "Weight by corner triangle area")
84 .value("Angle", NormalWeightingType::Angle, "Weight by corner angle");
85
86 nb::class_<VertexNormalOptions>(
87 m,
88 "VertexNormalOptions",
89 "Options for computing vertex normals")
90 .def(nb::init<>())
91 .def_rw(
92 "output_attribute_name",
94 "Output attribute name. Default is `@vertex_normal`.")
95 .def_rw(
96 "weight_type",
98 "Weighting type for normal computation. Default is Angle.")
99 .def_rw(
100 "weighted_corner_normal_attribute_name",
102 R"(Precomputed weighted corner normals attribute name (default: @weighted_corner_normal).
103
104If attribute exists, the precomputed weighted corner normal will be used.)")
105 .def_rw(
106 "recompute_weighted_corner_normals",
108 "Whether to recompute weighted corner normals (default: false).")
109 .def_rw(
110 "keep_weighted_corner_normals",
112 "Whether to keep the weighted corner normal attribute (default: false).")
113 .def_rw(
114 "distance_tolerance",
116 "Distance tolerance for degenerate edge check in polygon facets.");
117
118 m.def(
119 "compute_vertex_normal",
121 "mesh"_a,
122 "options"_a = VertexNormalOptions(),
123 R"(Compute vertex normal.
124
125:param mesh: Input mesh.
126:param options: Options for computing vertex normals.
127
128:returns: Vertex normal attribute id.)");
129
130 m.def(
131 "compute_vertex_normal",
132 [](MeshType& mesh,
133 std::optional<std::string_view> output_attribute_name,
134 std::optional<NormalWeightingType> weight_type,
135 std::optional<std::string_view> weighted_corner_normal_attribute_name,
136 std::optional<bool> recompute_weighted_corner_normals,
137 std::optional<bool> keep_weighted_corner_normals,
138 std::optional<float> distance_tolerance) {
139 VertexNormalOptions options;
140 if (output_attribute_name) options.output_attribute_name = *output_attribute_name;
141 if (weight_type) options.weight_type = *weight_type;
142 if (weighted_corner_normal_attribute_name)
143 options.weighted_corner_normal_attribute_name =
144 *weighted_corner_normal_attribute_name;
145 if (recompute_weighted_corner_normals)
146 options.recompute_weighted_corner_normals = *recompute_weighted_corner_normals;
147 if (keep_weighted_corner_normals)
148 options.keep_weighted_corner_normals = *keep_weighted_corner_normals;
149 if (distance_tolerance) options.distance_tolerance = *distance_tolerance;
150
151 return compute_vertex_normal<Scalar, Index>(mesh, options);
152 },
153 "mesh"_a,
154 "output_attribute_name"_a = nb::none(),
155 "weight_type"_a = nb::none(),
156 "weighted_corner_normal_attribute_name"_a = nb::none(),
157 "recompute_weighted_corner_normals"_a = nb::none(),
158 "keep_weighted_corner_normals"_a = nb::none(),
159 "distance_tolerance"_a = nb::none(),
160 R"(Compute vertex normal (Pythonic API).
161
162:param mesh: Input mesh.
163:param output_attribute_name: Output attribute name.
164:param weight_type: Weighting type for normal computation.
165:param weighted_corner_normal_attribute_name: Precomputed weighted corner normals attribute name.
166:param recompute_weighted_corner_normals: Whether to recompute weighted corner normals.
167:param keep_weighted_corner_normals: Whether to keep the weighted corner normal attribute.
168:param distance_tolerance: Distance tolerance for degenerate edge check.
169 (Only used to bypass degenerate edge in polygon facets.)
170
171:returns: Vertex normal attribute id.)");
172
173 nb::class_<FacetNormalOptions>(m, "FacetNormalOptions", "Facet normal computation options.")
174 .def(nb::init<>())
175 .def_rw(
176 "output_attribute_name",
178 "Output attribute name. Default: `@facet_normal`");
179
180 m.def(
181 "compute_facet_normal",
183 "mesh"_a,
184 "options"_a = FacetNormalOptions(),
185 R"(Compute facet normal.
186
187:param mesh: Input mesh.
188:param options: Options for computing facet normals.
189
190:returns: Facet normal attribute id.)");
191
192 m.def(
193 "compute_facet_normal",
194 [](MeshType& mesh, std::optional<std::string_view> output_attribute_name) {
195 FacetNormalOptions options;
196 if (output_attribute_name) options.output_attribute_name = *output_attribute_name;
197 return compute_facet_normal<Scalar, Index>(mesh, options);
198 },
199 "mesh"_a,
200 "output_attribute_name"_a = nb::none(),
201 R"(Compute facet normal (Pythonic API).
202
203:param mesh: Input mesh.
204:param output_attribute_name: Output attribute name.
205
206:returns: Facet normal attribute id.)");
207
208 nb::class_<NormalOptions>(m, "NormalOptions", "Normal computation options.")
209 .def(nb::init<>())
210 .def_rw(
211 "output_attribute_name",
213 "Output attribute name. Default: `@normal`")
214 .def_rw(
215 "weight_type",
217 "Weighting type for normal computation. Default is Angle.")
218 .def_rw(
219 "facet_normal_attribute_name",
221 "Facet normal attribute name to use. Default is `@facet_normal`.")
222 .def_rw(
223 "recompute_facet_normals",
225 "Whether to recompute facet normals. Default is false.")
226 .def_rw(
227 "keep_facet_normals",
229 "Whether to keep the computed facet normal attribute. Default is false.")
230 .def_rw(
231 "distance_tolerance",
233 "Distance tolerance for degenerate edge check. (Only used to bypass degenerate edge in "
234 "polygon facets.)");
235
236 m.def(
237 "compute_normal",
238 [](MeshType& mesh,
239 Scalar feature_angle_threshold,
240 nb::object cone_vertices,
241 std::optional<NormalOptions> normal_options) {
242 NormalOptions options;
243 if (normal_options.has_value()) {
244 options = std::move(normal_options.value());
245 }
246
247 if (cone_vertices.is_none()) {
248 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, {}, options);
249 } else if (nb::isinstance<nb::list>(cone_vertices)) {
250 auto cone_vertices_list = nb::cast<std::vector<Index>>(cone_vertices);
251 span<const Index> data{cone_vertices_list.data(), cone_vertices_list.size()};
252 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, data, options);
253 } else if (nb::isinstance<Tensor<Index>>(cone_vertices)) {
254 auto cone_vertices_tensor = nb::cast<Tensor<Index>>(cone_vertices);
255 auto [data, shape, stride] = tensor_to_span(cone_vertices_tensor);
256 la_runtime_assert(is_dense(shape, stride));
257 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, data, options);
258 } else {
259 throw std::runtime_error("Invalid cone_vertices type");
260 }
261 },
262 "mesh"_a,
263 "feature_angle_threshold"_a = lagrange::internal::pi / 4,
264 "cone_vertices"_a = nb::none(),
265 "options"_a = nb::none(),
266 R"(Compute indexed normal attribute.
267
268Edge with dihedral angles larger than `feature_angle_threshold` are considered as sharp edges.
269Vertices listed in `cone_vertices` are considered as cone vertices, which is always sharp.
270
271:param mesh: input mesh
272:param feature_angle_threshold: feature angle threshold
273:param cone_vertices: cone vertices
274:param options: normal options
275
276:returns: the id of the indexed normal attribute.
277)");
278
279 m.def(
280 "compute_normal",
281 [](MeshType& mesh,
282 Scalar feature_angle_threshold,
283 nb::object cone_vertices,
284 std::optional<std::string_view> output_attribute_name,
285 std::optional<NormalWeightingType> weight_type,
286 std::optional<std::string_view> facet_normal_attribute_name,
287 std::optional<bool> recompute_facet_normals,
288 std::optional<bool> keep_facet_normals,
289 std::optional<float> distance_tolerance) {
290 NormalOptions options;
291 if (output_attribute_name) options.output_attribute_name = *output_attribute_name;
292 if (weight_type) options.weight_type = *weight_type;
293 if (facet_normal_attribute_name)
294 options.facet_normal_attribute_name = *facet_normal_attribute_name;
295 if (recompute_facet_normals) options.recompute_facet_normals = *recompute_facet_normals;
296 if (keep_facet_normals) options.keep_facet_normals = *keep_facet_normals;
297 if (distance_tolerance) options.distance_tolerance = *distance_tolerance;
298
299 if (cone_vertices.is_none()) {
300 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, {}, options);
301 } else if (nb::isinstance<nb::list>(cone_vertices)) {
302 auto cone_vertices_list = nb::cast<std::vector<Index>>(cone_vertices);
303 span<const Index> data{cone_vertices_list.data(), cone_vertices_list.size()};
304 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, data, options);
305 } else if (nb::isinstance<Tensor<Index>>(cone_vertices)) {
306 auto cone_vertices_tensor = nb::cast<Tensor<Index>>(cone_vertices);
307 auto [data, shape, stride] = tensor_to_span(cone_vertices_tensor);
308 la_runtime_assert(is_dense(shape, stride));
309 return compute_normal<Scalar, Index>(mesh, feature_angle_threshold, data, options);
310 } else {
311 throw std::runtime_error("Invalid cone_vertices type");
312 }
313 },
314 "mesh"_a,
315 "feature_angle_threshold"_a = lagrange::internal::pi / 4,
316 "cone_vertices"_a = nb::none(),
317 "output_attribute_name"_a = nb::none(),
318 "weight_type"_a = nb::none(),
319 "facet_normal_attribute_name"_a = nb::none(),
320 "recompute_facet_normals"_a = nb::none(),
321 "keep_facet_normals"_a = nb::none(),
322 "distance_tolerance"_a = nb::none(),
323 R"(Compute indexed normal attribute (Pythonic API).
324
325:param mesh: input mesh
326:param feature_angle_threshold: feature angle threshold
327:param cone_vertices: cone vertices
328:param output_attribute_name: output normal attribute name
329:param weight_type: normal weighting type
330:param facet_normal_attribute_name: facet normal attribute name
331:param recompute_facet_normals: whether to recompute facet normals
332:param keep_facet_normals: whether to keep the computed facet normal attribute
333:param distance_tolerance: distance tolerance for degenerate edge check
334 (only used to bypass degenerate edges in polygon facets)
335
336:returns: the id of the indexed normal attribute.)");
337
338 using ConstArray3d = nb::ndarray<const double, nb::shape<-1, 3>, nb::c_contig, nb::device::cpu>;
339 m.def(
340 "compute_pointcloud_pca",
341 [](ConstArray3d points, bool shift_centroid, bool normalize) {
342 ComputePointcloudPCAOptions options;
343 options.shift_centroid = shift_centroid;
344 options.normalize = normalize;
345 PointcloudPCAOutput<Scalar> output =
346 compute_pointcloud_pca<Scalar>({points.data(), points.size()}, options);
347 return std::make_tuple(output.center, output.eigenvectors, output.eigenvalues);
348 },
349 "points"_a,
350 "shift_centroid"_a = ComputePointcloudPCAOptions().shift_centroid,
351 "normalize"_a = ComputePointcloudPCAOptions().normalize,
352 R"(Compute principal components of a point cloud.
353
354:param points: Input points.
355:param shift_centroid: When true: covariance = (P-centroid)^T (P-centroid), when false: covariance = (P)^T (P).
356:param normalize: Should we divide the result by number of points?
357
358:returns: tuple of (center, eigenvectors, eigenvalues).)");
359
360 m.def(
361 "compute_greedy_coloring",
362 [](MeshType& mesh,
363 AttributeElement element_type,
364 size_t num_color_used,
365 std::optional<std::string_view> output_attribute_name) {
366 GreedyColoringOptions options;
367 options.element_type = element_type;
368 options.num_color_used = num_color_used;
369 if (output_attribute_name) options.output_attribute_name = *output_attribute_name;
370 return compute_greedy_coloring<Scalar, Index>(mesh, options);
371 },
372 "mesh"_a,
373 "element_type"_a = AttributeElement::Facet,
374 "num_color_used"_a = 8,
375 "output_attribute_name"_a = nb::none(),
376 R"(Compute greedy coloring of mesh elements.
377
378:param mesh: Input mesh.
379:param element_type: Element type to be colored. Can be either Vertex or Facet.
380:param num_color_used: Minimum number of colors to use. The algorithm will cycle through them but may use more.
381:param output_attribute_name: Output attribute name.
382
383:returns: Color attribute id.)");
384
385 m.def(
386 "normalize_mesh_with_transform",
387 [](MeshType& mesh,
388 bool normalize_normals,
389 bool normalize_tangents_bitangents) -> Eigen::Matrix<Scalar, 4, 4> {
390 TransformOptions options;
391 options.normalize_normals = normalize_normals;
392 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
393 return normalize_mesh_with_transform(mesh, options).matrix();
394 },
395 "mesh"_a,
396 "normalize_normals"_a = TransformOptions().normalize_normals,
397 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
398 R"(Normalize a mesh to fit into a unit box centered at the origin.
399
400:param mesh: Input mesh.
401:param normalize_normals: Whether to normalize normals.
402:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.
403
404:return Inverse transform, can be used to undo the normalization process.)");
405
406
407 m.def(
408 "normalize_mesh_with_transform_2d",
409 [](MeshType& mesh,
410 bool normalize_normals,
411 bool normalize_tangents_bitangents) -> Eigen::Matrix<Scalar, 3, 3> {
412 TransformOptions options;
413 options.normalize_normals = normalize_normals;
414 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
415 return normalize_mesh_with_transform<2>(mesh, options).matrix();
416 },
417 "mesh"_a,
418 "normalize_normals"_a = TransformOptions().normalize_normals,
419 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
420 R"(Normalize a mesh to fit into a unit box centered at the origin.
421
422:param mesh: Input mesh.
423:param normalize_normals: Whether to normalize normals.
424:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.
425
426:return Inverse transform, can be used to undo the normalization process.)");
427
428 m.def(
429 "normalize_mesh",
430 [](MeshType& mesh, bool normalize_normals, bool normalize_tangents_bitangents) -> void {
431 TransformOptions options;
432 options.normalize_normals = normalize_normals;
433 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
434 normalize_mesh(mesh, options);
435 },
436 "mesh"_a,
437 "normalize_normals"_a = TransformOptions().normalize_normals,
438 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
439 R"(Normalize a mesh to fit into a unit box centered at the origin.
440
441:param mesh: Input mesh.
442:param normalize_normals: Whether to normalize normals.
443:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.)");
444
445 m.def(
446 "normalize_meshes_with_transform",
447 [](std::vector<MeshType*> meshes,
448 bool normalize_normals,
449 bool normalize_tangents_bitangents) -> Eigen::Matrix<Scalar, 4, 4> {
450 TransformOptions options;
451 options.normalize_normals = normalize_normals;
452 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
453 span<MeshType*> meshes_span(meshes.data(), meshes.size());
454 return normalize_meshes_with_transform(meshes_span, options).matrix();
455 },
456 "meshes"_a,
457 "normalize_normals"_a = TransformOptions().normalize_normals,
458 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
459 R"(Normalize a mesh to fit into a unit box centered at the origin.
460
461:param meshes: Input meshes.
462:param normalize_normals: Whether to normalize normals.
463:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.
464
465:return Inverse transform, can be used to undo the normalization process.)");
466
467 m.def(
468 "normalize_meshes_with_transform_2d",
469 [](std::vector<MeshType*> meshes,
470 bool normalize_normals,
471 bool normalize_tangents_bitangents) -> Eigen::Matrix<Scalar, 3, 3> {
472 TransformOptions options;
473 options.normalize_normals = normalize_normals;
474 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
475 span<MeshType*> meshes_span(meshes.data(), meshes.size());
476 return normalize_meshes_with_transform<2>(meshes_span, options).matrix();
477 },
478 "meshes"_a,
479 "normalize_normals"_a = TransformOptions().normalize_normals,
480 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
481 R"(Normalize a mesh to fit into a unit box centered at the origin.
482
483:param meshes: Input meshes.
484:param normalize_normals: Whether to normalize normals.
485:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.
486
487:return Inverse transform, can be used to undo the normalization process.)");
488
489
490 m.def(
491 "normalize_meshes",
492 [](std::vector<MeshType*> meshes,
493 bool normalize_normals,
494 bool normalize_tangents_bitangents) {
495 TransformOptions options;
496 options.normalize_normals = normalize_normals;
497 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
498 span<MeshType*> meshes_span(meshes.data(), meshes.size());
499 normalize_meshes(meshes_span, options);
500 },
501 "meshes"_a,
502 "normalize_normals"_a = TransformOptions().normalize_normals,
503 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
504 R"(Normalize a list of meshes to fit into a unit box centered at the origin.
505
506:param meshes: Input meshes.
507:param normalize_normals: Whether to normalize normals.
508:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.)");
509
510 m.def(
511 "combine_meshes",
512 [](std::vector<MeshType*> meshes, bool preserve_vertices) {
514 meshes.size(),
515 [&](size_t i) -> const MeshType& { return *meshes[i]; },
516 preserve_vertices);
517 },
518 "meshes"_a,
519 "preserve_attributes"_a = true,
520 R"(Combine a list of meshes into a single mesh.
521
522:param meshes: Input meshes.
523:param preserve_attributes: Whether to preserve attributes.
524
525:returns: The combined mesh.)");
526
527 m.def(
528 "compute_seam_edges",
529 [](MeshType& mesh,
530 AttributeId indexed_attribute_id,
531 std::optional<std::string_view> output_attribute_name) {
532 SeamEdgesOptions options;
533 if (output_attribute_name) options.output_attribute_name = *output_attribute_name;
534 return compute_seam_edges<Scalar, Index>(mesh, indexed_attribute_id, options);
535 },
536 "mesh"_a,
537 "indexed_attribute_id"_a,
538 "output_attribute_name"_a = nb::none(),
539 R"(Compute seam edges for a given indexed attribute.
540
541:param mesh: Input mesh.
542:param indexed_attribute_id: Input indexed attribute id.
543:param output_attribute_name: Output attribute name.
544
545:returns: Attribute id for the output per-edge seam attribute (1 is a seam, 0 is not).)");
546
547 m.def(
548 "orient_outward",
549 [](MeshType& mesh, bool positive) {
550 OrientOptions options;
551 options.positive = positive;
552 orient_outward<Scalar, Index>(mesh, options);
553 },
554 "mesh"_a,
555 "positive"_a = OrientOptions().positive,
556 R"(Orient mesh facets to ensure positive or negative signed volume.
557
558:param mesh: Input mesh.
559:param positive: Whether to orient volumes positively or negatively.)");
560
561 m.def(
562 "unify_index_buffer",
563 [](MeshType& mesh) { return unify_index_buffer(mesh); },
564 "mesh"_a,
565 R"(Unify the index buffer for all indexed attributes.
566
567:param mesh: Input mesh.
568
569:returns: Unified mesh.)");
570
571 m.def(
572 "unify_index_buffer",
574 "mesh"_a,
575 "attribute_ids"_a,
576 R"(Unify the index buffer for selected attributes.
577
578:param mesh: Input mesh.
579:param attribute_ids: Attribute IDs to unify.
580
581:returns: Unified mesh.)");
582
583 m.def(
584 "unify_index_buffer",
586 "mesh"_a,
587 "attribute_names"_a,
588 R"(Unify the index buffer for selected attributes.
589
590:param mesh: Input mesh.
591:param attribute_names: Attribute names to unify.
592
593:returns: Unified mesh.)");
594
595 m.def(
596 "triangulate_polygonal_facets",
597 [](MeshType& mesh, std::string_view scheme) {
598 lagrange::TriangulationOptions opt;
599 if (scheme == "earcut") {
601 } else if (scheme == "centroid_fan") {
603 } else {
604 throw Error(fmt::format("Unsupported triangulation scheme {}", scheme));
605 }
607 },
608 "mesh"_a,
609 "scheme"_a = "earcut",
610 R"(Triangulate polygonal facets of the mesh.
611
612:param mesh: The input mesh to be triangulated in place.
613:param scheme: The triangulation scheme (options are 'earcut' and 'centroid_fan'))");
614
615 nb::enum_<ComponentOptions::ConnectivityType>(m, "ConnectivityType", "Mesh connectivity type")
616 .value(
617 "Vertex",
618 ComponentOptions::ConnectivityType::Vertex,
619 "Two facets are connected if they share a vertex")
620 .value(
621 "Edge",
622 ComponentOptions::ConnectivityType::Edge,
623 "Two facets are connected if they share an edge");
624
625 m.def(
626 "compute_components",
627 [](MeshType& mesh,
628 std::optional<std::string_view> output_attribute_name,
629 std::optional<lagrange::ConnectivityType> connectivity_type,
630 std::optional<nb::list>& blocker_elements) {
631 lagrange::ComponentOptions opt;
632 if (output_attribute_name.has_value()) {
633 opt.output_attribute_name = output_attribute_name.value();
634 }
635 if (connectivity_type.has_value()) {
636 opt.connectivity_type = connectivity_type.value();
637 }
638 std::vector<Index> blocker_elements_vec;
639 if (blocker_elements.has_value()) {
640 for (auto val : blocker_elements.value()) {
641 blocker_elements_vec.push_back(nb::cast<Index>(val));
642 }
643 }
644 return lagrange::compute_components<Scalar, Index>(mesh, blocker_elements_vec, opt);
645 },
646 "mesh"_a,
647 "output_attribute_name"_a = nb::none(),
648 "connectivity_type"_a = nb::none(),
649 "blocker_elements"_a = nb::none(),
650 R"(Compute connected components.
651
652This method will create a per-facet component id attribute named by the `output_attribute_name`
653argument. Each component id is in [0, num_components-1] range.
654
655:param mesh: The input mesh.
656:param output_attribute_name: The name of the output attribute.
657:param connectivity_type: The connectivity type. Either "Vertex" or "Edge".
658:param blocker_elements: The list of blocker element indices. If `connectivity_type` is `Edge`, facets adjacent to a blocker edge are not considered as connected through this edge. If `connectivity_type` is `Vertex`, facets sharing a blocker vertex are not considered as connected through this vertex.
659
660:returns: The total number of components.)");
661
662 nb::class_<VertexValenceOptions>(m, "VertexValenceOptions", "Vertex valence options")
663 .def(nb::init<>())
664 .def_rw(
665 "output_attribute_name",
667 "The name of the output attribute")
668 .def_rw(
669 "induced_by_attribute",
671 "Optional per-edge attribute used as indicator function to restrict the graph used for "
672 "vertex valence computation");
673
674 m.def(
675 "compute_vertex_valence",
677 "mesh"_a,
678 "options"_a = VertexValenceOptions(),
679 R"(Compute vertex valence
680
681:param mesh: The input mesh.
682:param options: The vertex valence options.
683
684:returns: The vertex valence attribute id.)");
685
686 m.def(
687 "compute_vertex_valence",
688 [](MeshType& mesh,
689 std::optional<std::string_view> output_attribute_name,
690 std::optional<std::string_view> induced_by_attribute) {
691 VertexValenceOptions opt;
692 if (output_attribute_name.has_value()) {
693 opt.output_attribute_name = output_attribute_name.value();
694 }
695 if (induced_by_attribute.has_value()) {
696 opt.induced_by_attribute = induced_by_attribute.value();
697 }
699 },
700 "mesh"_a,
701 "output_attribute_name"_a = nb::none(),
702 "induced_by_attribute"_a = nb::none(),
703 R"(Compute vertex valence);
704
705:param mesh: The input mesh.
706:param output_attribute_name: The name of the output attribute.
707:param induced_by_attribute: Optional per-edge attribute used as indicator function to restrict the graph used for vertex valence computation.
708
709:returns: The vertex valence attribute id)");
710
711 nb::class_<TangentBitangentOptions>(m, "TangentBitangentOptions", "Tangent bitangent options")
712 .def(nb::init<>())
713 .def_rw(
714 "tangent_attribute_name",
716 "The name of the output tangent attribute, default is `@tangent`")
717 .def_rw(
718 "bitangent_attribute_name",
720 "The name of the output bitangent attribute, default is `@bitangent`")
721 .def_rw(
722 "uv_attribute_name",
724 "The name of the uv attribute")
725 .def_rw(
726 "normal_attribute_name",
728 "The name of the normal attribute")
729 .def_rw(
730 "output_element_type",
732 "The output element type")
733 .def_rw(
734 "pad_with_sign",
736 "Whether to pad the output tangent/bitangent with sign")
737 .def_rw(
738 "orthogonalize_bitangent",
740 "Whether to compute the bitangent as cross(normal, tangent). If false, the bitangent "
741 "is computed as the derivative of v-coordinate")
742 .def_rw(
743 "keep_existing_tangent",
745 "Whether to recompute tangent if the tangent attribute (specified by "
746 "tangent_attribute_name) already exists. If true, bitangent is computed by normalizing "
747 "cross(normal, tangent) and param orthogonalize_bitangent must be true.");
748 nb::class_<TangentBitangentResult>(m, "TangentBitangentResult", "Tangent bitangent result")
749 .def(nb::init<>())
750 .def_rw(
751 "tangent_id",
753 "The output tangent attribute id")
754 .def_rw(
755 "bitangent_id",
757 "The output bitangent attribute id");
758
759 m.def(
760 "compute_tangent_bitangent",
762 "mesh"_a,
763 "options"_a = TangentBitangentOptions(),
764 R"(Compute tangent and bitangent vector attributes.
765
766:param mesh: The input mesh.
767:param options: The tangent bitangent options.
768
769:returns: The tangent and bitangent attribute ids)");
770
771 m.def(
772 "compute_tangent_bitangent",
773 [](MeshType& mesh,
774 std::optional<std::string_view>(tangent_attribute_name),
775 std::optional<std::string_view>(bitangent_attribute_name),
776 std::optional<std::string_view>(uv_attribute_name),
777 std::optional<std::string_view>(normal_attribute_name),
778 std::optional<AttributeElement>(output_attribute_type),
779 std::optional<bool>(pad_with_sign),
780 std::optional<bool>(orthogonalize_bitangent),
781 std::optional<bool>(keep_existing_tangent)) {
782 TangentBitangentOptions opt;
783 if (tangent_attribute_name.has_value()) {
784 opt.tangent_attribute_name = tangent_attribute_name.value();
785 }
786 if (bitangent_attribute_name.has_value()) {
787 opt.bitangent_attribute_name = bitangent_attribute_name.value();
788 }
789 if (uv_attribute_name.has_value()) {
790 opt.uv_attribute_name = uv_attribute_name.value();
791 }
792 if (normal_attribute_name.has_value()) {
793 opt.normal_attribute_name = normal_attribute_name.value();
794 }
795 if (output_attribute_type.has_value()) {
796 opt.output_element_type = output_attribute_type.value();
797 }
798 if (pad_with_sign.has_value()) {
799 opt.pad_with_sign = pad_with_sign.value();
800 }
801 if (orthogonalize_bitangent.has_value()) {
802 opt.orthogonalize_bitangent = orthogonalize_bitangent.value();
803 }
804 if (keep_existing_tangent.has_value()) {
805 opt.keep_existing_tangent = keep_existing_tangent.value();
806 }
807
809 return std::make_tuple(r.tangent_id, r.bitangent_id);
810 },
811 "mesh"_a,
812 "tangent_attribute_name"_a = nb::none(),
813 "bitangent_attribute_name"_a = nb::none(),
814 "uv_attribute_name"_a = nb::none(),
815 "normal_attribute_name"_a = nb::none(),
816 "output_attribute_type"_a = nb::none(),
817 "pad_with_sign"_a = nb::none(),
818 "orthogonalize_bitangent"_a = nb::none(),
819 "keep_existing_tangent"_a = nb::none(),
820 R"(Compute tangent and bitangent vector attributes (Pythonic API).
821
822:param mesh: The input mesh.
823:param tangent_attribute_name: The name of the output tangent attribute.
824:param bitangent_attribute_name: The name of the output bitangent attribute.
825:param uv_attribute_name: The name of the uv attribute.
826:param normal_attribute_name: The name of the normal attribute.
827:param output_attribute_type: The output element type.
828:param pad_with_sign: Whether to pad the output tangent/bitangent with sign.
829:param orthogonalize_bitangent: Whether to compute the bitangent as sign * cross(normal, tangent).
830:param keep_existing_tangent: Whether to recompute tangent if the tangent attribute (specified by tangent_attribute_name) already exists. If true, bitangent is computed by normalizing cross(normal, tangent) and param orthogonalize_bitangent must be true.
831
832:returns: The tangent and bitangent attribute ids)");
833
834 m.def(
835 "map_attribute",
836 static_cast<AttributeId (*)(MeshType&, AttributeId, std::string_view, AttributeElement)>(
838 "mesh"_a,
839 "old_attribute_id"_a,
840 "new_attribute_name"_a,
841 "new_element"_a,
842 R"(Map an attribute to a new element type.
843
844:param mesh: The input mesh.
845:param old_attribute_id: The id of the input attribute.
846:param new_attribute_name: The name of the new attribute.
847:param new_element: The new element type.
848
849:returns: The id of the new attribute.)");
850
851 m.def(
852 "map_attribute",
853 static_cast<
854 AttributeId (*)(MeshType&, std::string_view, std::string_view, AttributeElement)>(
856 "mesh"_a,
857 "old_attribute_name"_a,
858 "new_attribute_name"_a,
859 "new_element"_a,
860 R"(Map an attribute to a new element type.
861
862:param mesh: The input mesh.
863:param old_attribute_name: The name of the input attribute.
864:param new_attribute_name: The name of the new attribute.
865:param new_element: The new element type.
866
867:returns: The id of the new attribute.)");
868
869 m.def(
870 "map_attribute_in_place",
871 static_cast<AttributeId (*)(MeshType&, AttributeId, AttributeElement)>(
873 "mesh"_a,
874 "id"_a,
875 "new_element"_a,
876 R"(Map an attribute to a new element type in place.
877
878:param mesh: The input mesh.
879:param id: The id of the input attribute.
880:param new_element: The new element type.
881
882:returns: The id of the new attribute.)");
883
884 m.def(
885 "map_attribute_in_place",
886 static_cast<AttributeId (*)(MeshType&, std::string_view, AttributeElement)>(
888 "mesh"_a,
889 "name"_a,
890 "new_element"_a,
891 R"(Map an attribute to a new element type in place.
892
893:param mesh: The input mesh.
894:param name: The name of the input attribute.
895:param new_element: The new element type.
896
897:returns: The id of the new attribute.)");
898
899 nb::class_<FacetAreaOptions>(m, "FacetAreaOptions", "Options for computing facet area.")
900 .def(nb::init<>())
901 .def_rw(
902 "output_attribute_name",
904 "The name of the output attribute.");
905
906 m.def(
907 "compute_facet_area",
909 "mesh"_a,
910 "options"_a = FacetAreaOptions(),
911 R"(Compute facet area.
912
913:param mesh: The input mesh.
914:param options: The options for computing facet area.
915
916:returns: The id of the new attribute.)");
917
918 m.def(
919 "compute_facet_area",
920 [](MeshType& mesh, std::optional<std::string_view> name) {
921 FacetAreaOptions opt;
922 if (name.has_value()) {
923 opt.output_attribute_name = name.value();
924 }
926 },
927 "mesh"_a,
928 "output_attribute_name"_a = nb::none(),
929 R"(Compute facet area (Pythonic API).
930
931:param mesh: The input mesh.
932:param output_attribute_name: The name of the output attribute.
933
934:returns: The id of the new attribute.)");
935
936 m.def(
937 "compute_facet_vector_area",
938 [](MeshType& mesh, std::optional<std::string_view> name) {
939 FacetVectorAreaOptions opt;
940 if (name.has_value()) {
941 opt.output_attribute_name = name.value();
942 }
944 },
945 "mesh"_a,
946 "output_attribute_name"_a = nb::none(),
947 R"(Compute facet vector area (Pythonic API).
948
949Vector area is defined as the area multiplied by the facet normal.
950For triangular facets, it is equivalent to half of the cross product of two edges.
951For non-planar polygonal facets, the vector area offers a robust way to compute the area and normal.
952The magnitude of the vector area is the largest area of any orthogonal projection of the facet.
953The direction of the vector area is the normal direction that maximizes the projected area [1, 2].
954
955[1] Sullivan, John M. "Curvatures of smooth and discrete surfaces." Discrete differential geometry.
956Basel: Birkhäuser Basel, 2008. 175-188.
957
958[2] Alexa, Marc, and Max Wardetzky. "Discrete Laplacians on general polygonal meshes." ACM SIGGRAPH
9592011 papers. 2011. 1-10.
960
961:param mesh: The input mesh.
962:param output_attribute_name: The name of the output attribute.
963
964:returns: The id of the new attribute.)");
965
966 nb::class_<MeshAreaOptions>(m, "MeshAreaOptions", "Options for computing mesh area.")
967 .def(nb::init<>())
968 .def_rw(
969 "input_attribute_name",
971 "The name of the pre-computed facet area attribute, default is `@facet_area`.")
972 .def_rw(
973 "use_signed_area",
975 "Whether to use signed area.");
976
977 m.def(
978 "compute_mesh_area",
980 "mesh"_a,
981 "options"_a = MeshAreaOptions(),
982 R"(Compute mesh area.
983
984:param mesh: The input mesh.
985:param options: The options for computing mesh area.
986
987:returns: The mesh area.)");
988
989 m.def(
990 "compute_mesh_area",
991 [](MeshType& mesh,
992 std::optional<std::string_view> input_attribute_name,
993 std::optional<bool> use_signed_area) {
994 MeshAreaOptions opt;
995 if (input_attribute_name.has_value()) {
996 opt.input_attribute_name = input_attribute_name.value();
997 }
998 if (use_signed_area.has_value()) {
999 opt.use_signed_area = use_signed_area.value();
1000 }
1001 return compute_mesh_area(mesh, opt);
1002 },
1003 "mesh"_a,
1004 "input_attribute_name"_a = nb::none(),
1005 "use_signed_area"_a = nb::none(),
1006 R"(Compute mesh area (Pythonic API).
1007
1008:param mesh: The input mesh.
1009:param input_attribute_name: The name of the pre-computed facet area attribute.
1010:param use_signed_area: Whether to use signed area.
1011
1012:returns: The mesh area.)");
1013
1014 nb::class_<FacetCentroidOptions>(m, "FacetCentroidOptions", "Facet centroid options.")
1015 .def(nb::init<>())
1016 .def_rw(
1017 "output_attribute_name",
1019 "The name of the output attribute.");
1020 m.def(
1021 "compute_facet_centroid",
1023 "mesh"_a,
1024 "options"_a = FacetCentroidOptions(),
1025 R"(Compute facet centroid.
1026
1027:param mesh: The input mesh.
1028:param options: The options for computing facet centroid.
1029
1030:returns: The id of the new attribute.)");
1031
1032 m.def(
1033 "compute_facet_centroid",
1034 [](MeshType& mesh, std::optional<std::string_view> output_attribute_name) {
1035 FacetCentroidOptions opt;
1036 if (output_attribute_name.has_value()) {
1037 opt.output_attribute_name = output_attribute_name.value();
1038 }
1040 },
1041 "mesh"_a,
1042 "output_attribute_name"_a = nb::none(),
1043 R"(Compute facet centroid (Pythonic API).
1044
1045:param mesh: Input mesh.
1046:param output_attribute_name: Output attribute name.
1047
1048:returns: Attribute ID.)");
1049
1050 m.def(
1051 "compute_facet_circumcenter",
1052 [](MeshType& mesh, std::optional<std::string_view> output_attribute_name) {
1053 FacetCircumcenterOptions opt;
1054 if (output_attribute_name.has_value()) {
1055 opt.output_attribute_name = output_attribute_name.value();
1056 }
1058 },
1059 "mesh"_a,
1060 "output_attribute_name"_a = nb::none(),
1061 R"(Compute facet circumcenter (Pythonic API).
1062
1063:param mesh: The input mesh.
1064:param output_attribute_name: The name of the output attribute.
1065
1066:returns: The id of the new attribute.)");
1067
1068 nb::enum_<MeshCentroidOptions::WeightingType>(
1069 m,
1070 "CentroidWeightingType",
1071 "Centroid weighting type.")
1072 .value("Uniform", MeshCentroidOptions::Uniform, "Uniform weighting.")
1073 .value("Area", MeshCentroidOptions::Area, "Area weighting.");
1074
1075 nb::class_<MeshCentroidOptions>(m, "MeshCentroidOptions", "Mesh centroid options.")
1076 .def(nb::init<>())
1077 .def_rw("weighting_type", &MeshCentroidOptions::weighting_type, "The weighting type.")
1078 .def_rw(
1079 "facet_centroid_attribute_name",
1081 "The name of the pre-computed facet centroid attribute if available.")
1082 .def_rw(
1083 "facet_area_attribute_name",
1085 "The name of the pre-computed facet area attribute if available.");
1086
1087 m.def(
1088 "compute_mesh_centroid",
1089 [](const MeshType& mesh, MeshCentroidOptions opt) {
1090 const Index dim = mesh.get_dimension();
1091 std::vector<Scalar> centroid(dim, invalid<Scalar>());
1092 compute_mesh_centroid<Scalar, Index>(mesh, centroid, opt);
1093 return centroid;
1094 },
1095 "mesh"_a,
1096 "options"_a = MeshCentroidOptions(),
1097 R"(Compute mesh centroid.
1098
1099:param mesh: Input mesh.
1100:param options: Centroid computation options.
1101
1102:returns: Mesh centroid coordinates.)");
1103
1104 m.def(
1105 "compute_mesh_centroid",
1106 [](MeshType& mesh,
1107 std::optional<MeshCentroidOptions::WeightingType> weighting_type,
1108 std::optional<std::string_view> facet_centroid_attribute_name,
1109 std::optional<std::string_view> facet_area_attribute_name) {
1110 MeshCentroidOptions opt;
1111 if (weighting_type.has_value()) {
1112 opt.weighting_type = weighting_type.value();
1113 }
1114 if (facet_centroid_attribute_name.has_value()) {
1115 opt.facet_centroid_attribute_name = facet_centroid_attribute_name.value();
1116 }
1117 if (facet_area_attribute_name.has_value()) {
1118 opt.facet_area_attribute_name = facet_area_attribute_name.value();
1119 }
1120 const Index dim = mesh.get_dimension();
1121 std::vector<Scalar> centroid(dim, invalid<Scalar>());
1122 compute_mesh_centroid<Scalar, Index>(mesh, centroid, opt);
1123 return centroid;
1124 },
1125 "mesh"_a,
1126 "weighting_type"_a = nb::none(),
1127 "facet_centroid_attribute_name"_a = nb::none(),
1128 "facet_area_attribute_name"_a = nb::none(),
1129 R"(Compute mesh centroid (Pythonic API).
1130
1131:param mesh: Input mesh.
1132:param weighting_type: Weighting type (default: Area).
1133:param facet_centroid_attribute_name: Pre-computed facet centroid attribute name.
1134:param facet_area_attribute_name: Pre-computed facet area attribute name.
1135
1136:returns: Mesh centroid coordinates.)");
1137
1138 m.def(
1139 "permute_vertices",
1140 [](MeshType& mesh, Tensor<Index> new_to_old) {
1141 auto [data, shape, stride] = tensor_to_span(new_to_old);
1142 la_runtime_assert(is_dense(shape, stride));
1144 },
1145 "mesh"_a,
1146 "new_to_old"_a,
1147 R"(Reorder vertices of a mesh in place based on a permutation.
1148
1149:param mesh: input mesh
1150:param new_to_old: permutation vector for vertices)");
1151
1152 m.def(
1153 "permute_facets",
1154 [](MeshType& mesh, Tensor<Index> new_to_old) {
1155 auto [data, shape, stride] = tensor_to_span(new_to_old);
1156 la_runtime_assert(is_dense(shape, stride));
1158 },
1159 "mesh"_a,
1160 "new_to_old"_a,
1161 R"(Reorder facets of a mesh in place based on a permutation.
1162
1163:param mesh: input mesh
1164:param new_to_old: permutation vector for facets)");
1165
1166 nb::enum_<MappingPolicy>(m, "MappingPolicy", "Mapping policy for handling collisions.")
1167 .value("Average", MappingPolicy::Average, "Compute the average of the collided values.")
1168 .value("KeepFirst", MappingPolicy::KeepFirst, "Keep the first collided value.")
1169 .value("Error", MappingPolicy::Error, "Throw an error when collision happens.");
1170
1171 nb::class_<RemapVerticesOptions>(m, "RemapVerticesOptions", "Options for remapping vertices.")
1172 .def(nb::init<>())
1173 .def_rw(
1174 "collision_policy_float",
1176 "The collision policy for float attributes.")
1177 .def_rw(
1178 "collision_policy_integral",
1180 "The collision policy for integral attributes.");
1181
1182 m.def(
1183 "remap_vertices",
1184 [](MeshType& mesh, Tensor<Index> old_to_new, RemapVerticesOptions opt) {
1185 auto [data, shape, stride] = tensor_to_span(old_to_new);
1186 la_runtime_assert(is_dense(shape, stride));
1187 remap_vertices<Scalar, Index>(mesh, data, opt);
1188 },
1189 "mesh"_a,
1190 "old_to_new"_a,
1191 "options"_a = RemapVerticesOptions(),
1192 R"(Remap vertices of a mesh in place based on a permutation.
1193
1194:param mesh: input mesh
1195:param old_to_new: permutation vector for vertices
1196:param options: options for remapping vertices)");
1197
1198 m.def(
1199 "remap_vertices",
1200 [](MeshType& mesh,
1201 Tensor<Index> old_to_new,
1202 std::optional<MappingPolicy> collision_policy_float,
1203 std::optional<MappingPolicy> collision_policy_integral) {
1204 RemapVerticesOptions opt;
1205 if (collision_policy_float.has_value()) {
1206 opt.collision_policy_float = collision_policy_float.value();
1207 }
1208 if (collision_policy_integral.has_value()) {
1209 opt.collision_policy_integral = collision_policy_integral.value();
1210 }
1211 auto [data, shape, stride] = tensor_to_span(old_to_new);
1212 la_runtime_assert(is_dense(shape, stride));
1213 remap_vertices<Scalar, Index>(mesh, data, opt);
1214 },
1215 "mesh"_a,
1216 "old_to_new"_a,
1217 "collision_policy_float"_a = nb::none(),
1218 "collision_policy_integral"_a = nb::none(),
1219 R"(Remap vertices of a mesh in place based on a permutation (Pythonic API).
1220
1221:param mesh: input mesh
1222:param old_to_new: permutation vector for vertices
1223:param collision_policy_float: The collision policy for float attributes.
1224:param collision_policy_integral: The collision policy for integral attributes.)");
1225
1226 m.def(
1227 "reorder_mesh",
1228 [](MeshType& mesh, std::string_view method) {
1229 lagrange::ReorderingMethod reorder_method;
1230 if (method == "Lexicographic" || method == "lexicographic") {
1231 reorder_method = ReorderingMethod::Lexicographic;
1232 } else if (method == "Morton" || method == "morton") {
1233 reorder_method = ReorderingMethod::Morton;
1234 } else if (method == "Hilbert" || method == "hilbert") {
1235 reorder_method = ReorderingMethod::Hilbert;
1236 } else if (method == "None" || method == "none") {
1237 reorder_method = ReorderingMethod::None;
1238 } else {
1239 throw std::runtime_error(fmt::format("Invalid reordering method: {}", method));
1240 }
1241
1242 lagrange::reorder_mesh(mesh, reorder_method);
1243 },
1244 "mesh"_a,
1245 "method"_a = "Morton",
1246 R"(Reorder a mesh in place.
1247
1248:param mesh: input mesh
1249:param method: reordering method, options are 'Lexicographic', 'Morton', 'Hilbert', 'None' (default is 'Morton').)",
1250 nb::sig(
1251 "def reorder_mesh(mesh: SurfaceMesh, "
1252 "method: typing.Literal['Lexicographic', 'Morton', 'Hilbert', 'None']) -> None"));
1253
1254 m.def(
1255 "separate_by_facet_groups",
1256 [](MeshType& mesh,
1257 Tensor<Index> facet_group_indices,
1258 std::string_view source_vertex_attr_name,
1259 std::string_view source_facet_attr_name,
1260 bool map_attributes) {
1261 SeparateByFacetGroupsOptions options;
1262 options.source_vertex_attr_name = source_vertex_attr_name;
1263 options.source_facet_attr_name = source_facet_attr_name;
1264 options.map_attributes = map_attributes;
1265 auto [data, shape, stride] = tensor_to_span(facet_group_indices);
1266 la_runtime_assert(is_dense(shape, stride));
1267 return separate_by_facet_groups<Scalar, Index>(mesh, data, options);
1268 },
1269 "mesh"_a,
1270 "facet_group_indices"_a,
1271 "source_vertex_attr_name"_a = "",
1272 "source_facet_attr_name"_a = "",
1273 "map_attributes"_a = false,
1274 R"(Extract a set of submeshes based on facet groups.
1275
1276:param mesh: The source mesh.
1277:param facet_group_indices: The group index for each facet. Each group index must be in the range of [0, max(facet_group_indices)]
1278:param source_vertex_attr_name: The optional attribute name to track source vertices.
1279:param source_facet_attr_name: The optional attribute name to track source facets.
1280
1281:returns: A list of meshes, one for each facet group.
1282)");
1283
1284 m.def(
1285 "separate_by_components",
1286 [](MeshType& mesh,
1287 std::string_view source_vertex_attr_name,
1288 std::string_view source_facet_attr_name,
1289 bool map_attributes,
1290 ConnectivityType connectivity_type) {
1291 SeparateByComponentsOptions options;
1292 options.source_vertex_attr_name = source_vertex_attr_name;
1293 options.source_facet_attr_name = source_facet_attr_name;
1294 options.map_attributes = map_attributes;
1295 options.connectivity_type = connectivity_type;
1296 return separate_by_components(mesh, options);
1297 },
1298 "mesh"_a,
1299 "source_vertex_attr_name"_a = "",
1300 "source_facet_attr_name"_a = "",
1301 "map_attributes"_a = false,
1302 "connectivity_type"_a = ConnectivityType::Edge,
1303 R"(Extract a set of submeshes based on connected components.
1304
1305:param mesh: The source mesh.
1306:param source_vertex_attr_name: The optional attribute name to track source vertices.
1307:param source_facet_attr_name: The optional attribute name to track source facets.
1308:param map_attributes: Map attributes from the source to target meshes.
1309:param connectivity_type: The connectivity used for component computation.
1310
1311:returns: A list of meshes, one for each connected component.
1312)");
1313
1314 m.def(
1315 "extract_submesh",
1316 [](MeshType& mesh,
1317 Tensor<Index> selected_facets,
1318 std::string_view source_vertex_attr_name,
1319 std::string_view source_facet_attr_name,
1320 bool map_attributes) {
1321 SubmeshOptions options;
1322 options.source_vertex_attr_name = source_vertex_attr_name;
1323 options.source_facet_attr_name = source_facet_attr_name;
1324 options.map_attributes = map_attributes;
1325 auto [data, shape, stride] = tensor_to_span(selected_facets);
1326 la_runtime_assert(is_dense(shape, stride));
1327 return extract_submesh<Scalar, Index>(mesh, data, options);
1328 },
1329 "mesh"_a,
1330 "selected_facets"_a,
1331 "source_vertex_attr_name"_a = "",
1332 "source_facet_attr_name"_a = "",
1333 "map_attributes"_a = false,
1334 R"(Extract a submesh based on the selected facets.
1335
1336:param mesh: The source mesh.
1337:param selected_facets: A listed of facet ids to extract.
1338:param source_vertex_attr_name: The optional attribute name to track source vertices.
1339:param source_facet_attr_name: The optional attribute name to track source facets.
1340:param map_attributes: Map attributes from the source to target meshes.
1341
1342:returns: A mesh that contains only the selected facets.
1343)");
1344
1345 m.def(
1346 "compute_dihedral_angles",
1347 [](MeshType& mesh,
1348 std::optional<std::string_view> output_attribute_name,
1349 std::optional<std::string_view> facet_normal_attribute_name,
1350 std::optional<bool> recompute_facet_normals,
1351 std::optional<bool> keep_facet_normals) {
1352 DihedralAngleOptions options;
1353 if (output_attribute_name.has_value()) {
1354 options.output_attribute_name = output_attribute_name.value();
1355 }
1356 if (facet_normal_attribute_name.has_value()) {
1357 options.facet_normal_attribute_name = facet_normal_attribute_name.value();
1358 }
1359 if (recompute_facet_normals.has_value()) {
1360 options.recompute_facet_normals = recompute_facet_normals.value();
1361 }
1362 if (keep_facet_normals.has_value()) {
1363 options.keep_facet_normals = keep_facet_normals.value();
1364 }
1365 return compute_dihedral_angles(mesh, options);
1366 },
1367 "mesh"_a,
1368 "output_attribute_name"_a = nb::none(),
1369 "facet_normal_attribute_name"_a = nb::none(),
1370 "recompute_facet_normals"_a = nb::none(),
1371 "keep_facet_normals"_a = nb::none(),
1372 R"(Compute dihedral angles for each edge.
1373
1374The dihedral angle of an edge is defined as the angle between the __normals__ of two facets adjacent
1375to the edge. The dihedral angle is always in the range [0, pi] for manifold edges. For boundary
1376edges, the dihedral angle defaults to 0. For non-manifold edges, the dihedral angle is not
1377well-defined and will be set to the special value 2 * π.
1378
1379:param mesh: The source mesh.
1380:param output_attribute_name: The optional edge attribute name to store the dihedral angles.
1381:param facet_normal_attribute_name: The optional attribute name to store the facet normals.
1382:param recompute_facet_normals: Whether to recompute facet normals.
1383:param keep_facet_normals: Whether to keep newly computed facet normals. It has no effect on pre-existing facet normals.
1384
1385:return: The edge attribute id of dihedral angles.)");
1386
1387 m.def(
1388 "compute_edge_lengths",
1389 [](MeshType& mesh, std::optional<std::string_view> output_attribute_name) {
1390 EdgeLengthOptions options;
1391 if (output_attribute_name.has_value())
1392 options.output_attribute_name = output_attribute_name.value();
1393 return compute_edge_lengths(mesh, options);
1394 },
1395 "mesh"_a,
1396 "output_attribute_name"_a = nb::none(),
1397 R"(Compute edge lengths.
1398
1399:param mesh: The source mesh.
1400:param output_attribute_name: The optional edge attribute name to store the edge lengths.
1401
1402:return: The edge attribute id of edge lengths.)");
1403
1404 m.def(
1405 "compute_dijkstra_distance",
1406 [](MeshType& mesh,
1407 Index seed_facet,
1408 const nb::list& barycentric_coords,
1409 std::optional<Scalar> radius,
1410 std::string_view output_attribute_name,
1411 bool output_involved_vertices) {
1412 DijkstraDistanceOptions<Scalar, Index> options;
1413 options.seed_facet = seed_facet;
1414 for (auto val : barycentric_coords) {
1415 options.barycentric_coords.push_back(nb::cast<Scalar>(val));
1416 }
1417 if (radius.has_value()) {
1418 options.radius = radius.value();
1419 }
1420 options.output_attribute_name = output_attribute_name;
1421 options.output_involved_vertices = output_involved_vertices;
1422 return compute_dijkstra_distance(mesh, options);
1423 },
1424 "mesh"_a,
1425 "seed_facet"_a,
1426 "barycentric_coords"_a,
1427 "radius"_a = nb::none(),
1428 "output_attribute_name"_a = DijkstraDistanceOptions<Scalar, Index>{}.output_attribute_name,
1429 "output_involved_vertices"_a =
1430 DijkstraDistanceOptions<Scalar, Index>{}.output_involved_vertices,
1431 R"(Compute Dijkstra distance from a seed facet.
1432
1433:param mesh: The source mesh.
1434:param seed_facet: The seed facet index.
1435:param barycentric_coords: The barycentric coordinates of the seed facet.
1436:param radius: The maximum radius of the dijkstra distance.
1437:param output_attribute_name: The output attribute name to store the dijkstra distance.
1438:param output_involved_vertices: Whether to output the list of involved vertices.)");
1439
1440 m.def(
1441 "weld_indexed_attribute",
1442 [](MeshType& mesh,
1443 AttributeId attribute_id,
1444 std::optional<double> epsilon_rel,
1445 std::optional<double> epsilon_abs,
1446 std::optional<double> angle_abs,
1447 std::optional<std::vector<size_t>> exclude_vertices) {
1448 WeldOptions options;
1449 options.epsilon_rel = epsilon_rel;
1450 options.epsilon_abs = epsilon_abs;
1451 options.angle_abs = angle_abs;
1452 if (exclude_vertices.has_value()) {
1453 const auto& exclude_vertices_vec = exclude_vertices.value();
1454 options.exclude_vertices = {
1455 exclude_vertices_vec.data(),
1456 exclude_vertices_vec.size()};
1457 }
1458 return weld_indexed_attribute(mesh, attribute_id, options);
1459 },
1460 "mesh"_a,
1461 "attribute_id"_a,
1462 "epsilon_rel"_a = nb::none(),
1463 "epsilon_abs"_a = nb::none(),
1464 "angle_abs"_a = nb::none(),
1465 "exclude_vertices"_a = nb::none(),
1466 R"(Weld indexed attribute.
1467
1468:param mesh: The source mesh to be updated in place.
1469:param attribute_id: The indexed attribute id to weld.
1470:param epsilon_rel: The relative tolerance for welding.
1471:param epsilon_abs: The absolute tolerance for welding.
1472:param angle_abs: The absolute angle tolerance for welding.
1473:param exclude_vertices: Optional list of vertex indices to exclude from welding.)");
1474
1475 m.def(
1476 "compute_euler",
1478 "mesh"_a,
1479 R"(Compute the Euler characteristic.
1480
1481:param mesh: The source mesh.
1482
1483:return: The Euler characteristic.)");
1484
1485 m.def(
1486 "is_closed",
1488 "mesh"_a,
1489 R"(Check if the mesh is closed.
1490
1491A mesh is considered closed if it has no boundary edges.
1492
1493:param mesh: The source mesh.
1494
1495:return: Whether the mesh is closed.)");
1496
1497 m.def(
1498 "is_vertex_manifold",
1500 "mesh"_a,
1501 R"(Check if the mesh is vertex manifold.
1502
1503:param mesh: The source mesh.
1504
1505:return: Whether the mesh is vertex manifold.)");
1506
1507 m.def(
1508 "is_edge_manifold",
1510 "mesh"_a,
1511 R"(Check if the mesh is edge manifold.
1512
1513:param mesh: The source mesh.
1514
1515:return: Whether the mesh is edge manifold.)");
1516
1517 m.def("is_manifold", &is_manifold<Scalar, Index>, "mesh"_a, R"(Check if the mesh is manifold.
1518
1519A mesh considered as manifold if it is both vertex and edge manifold.
1520
1521:param mesh: The source mesh.
1522
1523:return: Whether the mesh is manifold.)");
1524
1525 m.def(
1526 "is_oriented",
1528 "mesh"_a,
1529 R"(Check if the mesh is oriented.
1530
1531:param mesh: The source mesh.
1532
1533:return: Whether the mesh is oriented.)");
1534
1535 m.def(
1536 "transform_mesh",
1537 [](MeshType& mesh,
1538 Eigen::Matrix<Scalar, 4, 4> affine_transform,
1539 bool normalize_normals,
1540 bool normalize_tangents_bitangents,
1541 bool in_place) -> std::optional<MeshType> {
1542 Eigen::Transform<Scalar, 3, Eigen::Affine> M(affine_transform);
1543 TransformOptions options;
1544 options.normalize_normals = normalize_normals;
1545 options.normalize_tangents_bitangents = normalize_tangents_bitangents;
1546
1547 std::optional<MeshType> result;
1548 if (in_place) {
1549 transform_mesh(mesh, M, options);
1550 } else {
1551 result = transformed_mesh(mesh, M, options);
1552 }
1553 return result;
1554 },
1555 "mesh"_a,
1556 "affine_transform"_a,
1557 "normalize_normals"_a = TransformOptions().normalize_normals,
1558 "normalize_tangents_bitangents"_a = TransformOptions().normalize_tangents_bitangents,
1559 "in_place"_a = true,
1560 R"(Apply affine transformation to a mesh.
1561
1562:param mesh: Input mesh.
1563:param affine_transform: Affine transformation matrix.
1564:param normalize_normals: Whether to normalize normals.
1565:param normalize_tangents_bitangents: Whether to normalize tangents and bitangents.
1566:param in_place: Whether to apply transformation in place.
1567
1568:returns: Transformed mesh if in_place is False.)");
1569
1570 nb::enum_<DistortionMetric>(m, "DistortionMetric", "Distortion metric.")
1571 .value("Dirichlet", DistortionMetric::Dirichlet, "Dirichlet energy")
1572 .value("InverseDirichlet", DistortionMetric::InverseDirichlet, "Inverse Dirichlet energy")
1573 .value(
1574 "SymmetricDirichlet",
1576 "Symmetric Dirichlet energy")
1577 .value("AreaRatio", DistortionMetric::AreaRatio, "Area ratio")
1578 .value("MIPS", DistortionMetric::MIPS, "Most isotropic parameterization energy");
1579
1580 m.def(
1581 "compute_uv_distortion",
1582 [](MeshType& mesh,
1583 std::string_view uv_attribute_name,
1584 std::string_view output_attribute_name,
1585 DistortionMetric metric) {
1586 UVDistortionOptions opt;
1587 opt.uv_attribute_name = uv_attribute_name;
1588 opt.output_attribute_name = output_attribute_name;
1589 opt.metric = metric;
1590 return compute_uv_distortion(mesh, opt);
1591 },
1592 "mesh"_a,
1593 "uv_attribute_name"_a = "@uv",
1594 "output_attribute_name"_a = "@uv_measure",
1596 R"(Compute UV distortion.
1597
1598:param mesh: Input mesh.
1599:param uv_attribute_name: UV attribute name (default: "@uv").
1600:param output_attribute_name: Output attribute name (default: "@uv_measure").
1601:param metric: Distortion metric (default: MIPS).
1602
1603:returns: Facet attribute ID for distortion.)");
1604
1605 m.def(
1606 "trim_by_isoline",
1607 [](const MeshType& mesh,
1608 std::variant<AttributeId, std::string_view> attribute,
1609 double isovalue,
1610 bool keep_below) {
1611 IsolineOptions opt;
1612 if (std::holds_alternative<AttributeId>(attribute)) {
1613 opt.attribute_id = std::get<AttributeId>(attribute);
1614 } else {
1615 opt.attribute_id = mesh.get_attribute_id(std::get<std::string_view>(attribute));
1616 }
1617 opt.isovalue = isovalue;
1618 opt.keep_below = keep_below;
1619 return trim_by_isoline(mesh, opt);
1620 },
1621 "mesh"_a,
1622 "attribute"_a,
1623 "isovalue"_a = IsolineOptions().isovalue,
1624 "keep_below"_a = IsolineOptions().keep_below,
1625 R"(Trim a triangle mesh by an isoline.
1626
1627:param mesh: Input triangle mesh.
1628:param attribute: Attribute ID or name of scalar field (vertex or indexed).
1629:param isovalue: Isovalue to trim with.
1630:param keep_below: Whether to keep the part below the isoline.
1631
1632:returns: Trimmed mesh.)");
1633
1634 m.def(
1635 "extract_isoline",
1636 [](const MeshType& mesh,
1637 std::variant<AttributeId, std::string_view> attribute,
1638 double isovalue) {
1639 IsolineOptions opt;
1640 if (std::holds_alternative<AttributeId>(attribute)) {
1641 opt.attribute_id = std::get<AttributeId>(attribute);
1642 } else {
1643 opt.attribute_id = mesh.get_attribute_id(std::get<std::string_view>(attribute));
1644 }
1645 opt.isovalue = isovalue;
1646 return extract_isoline(mesh, opt);
1647 },
1648 "mesh"_a,
1649 "attribute"_a,
1650 "isovalue"_a = IsolineOptions().isovalue,
1651 R"(Extract the isoline of an implicit function defined on the mesh vertices/corners.
1652
1653The input mesh must be a triangle mesh.
1654
1655:param mesh: Input triangle mesh to extract the isoline from.
1656:param attribute: Attribute id or name of the scalar field to use. Can be a vertex or indexed attribute.
1657:param isovalue: Isovalue to extract.
1658
1659:return: A mesh whose facets is a collection of size 2 elements representing the extracted isoline.)");
1660
1661 using AttributeNameOrId = AttributeFilter::AttributeNameOrId;
1662 m.def(
1663 "filter_attributes",
1664 [](MeshType& mesh,
1665 std::optional<std::vector<AttributeNameOrId>> included_attributes,
1666 std::optional<std::vector<AttributeNameOrId>> excluded_attributes,
1667 std::optional<std::unordered_set<AttributeUsage>> included_usages,
1668 std::optional<std::unordered_set<AttributeElement>> included_element_types) {
1669 AttributeFilter filter;
1670 if (included_attributes.has_value()) {
1671 filter.included_attributes = included_attributes.value();
1672 }
1673 if (excluded_attributes.has_value()) {
1674 filter.excluded_attributes = excluded_attributes.value();
1675 }
1676 if (included_usages.has_value()) {
1677 filter.included_usages.clear_all();
1678 for (auto usage : included_usages.value()) {
1679 filter.included_usages.set(usage);
1680 }
1681 }
1682 if (included_element_types.has_value()) {
1683 filter.included_element_types.clear_all();
1684 for (auto element_type : included_element_types.value()) {
1685 filter.included_element_types.set(element_type);
1686 }
1687 }
1688 return filter_attributes(mesh, filter);
1689 },
1690 "mesh"_a,
1691 "included_attributes"_a = nb::none(),
1692 "excluded_attributes"_a = nb::none(),
1693 "included_usages"_a = nb::none(),
1694 "included_element_types"_a = nb::none(),
1695 R"(Filters the attributes of mesh according to user specifications.
1696
1697:param mesh: Input mesh.
1698:param included_attributes: List of attribute names or ids to include. By default, all attributes are included.
1699:param excluded_attributes: List of attribute names or ids to exclude. By default, no attribute is excluded.
1700:param included_usages: List of attribute usages to include. By default, all usages are included.
1701:param included_element_types: List of attribute element types to include. By default, all element types are included.)");
1702
1703 m.def(
1704 "cast_attribute",
1705 [](MeshType& mesh,
1706 std::variant<AttributeId, std::string_view> input_attribute,
1707 nb::type_object dtype,
1708 std::optional<std::string_view> output_attribute_name) {
1710 auto cast = [&](AttributeId attr_id) {
1711 auto np = nb::module_::import_("numpy");
1712 if (output_attribute_name.has_value()) {
1713 auto name = output_attribute_name.value();
1714 if (dtype.is(&PyFloat_Type)) {
1715 // Native python float is a C double.
1716 return cast_attribute<double>(mesh, attr_id, name);
1717 } else if (dtype.is(&PyLong_Type)) {
1718 // Native python int maps to int64.
1719 return cast_attribute<int64_t>(mesh, attr_id, name);
1720 } else if (dtype.is(np.attr("float32"))) {
1721 return cast_attribute<float>(mesh, attr_id, name);
1722 } else if (dtype.is(np.attr("float64"))) {
1723 return cast_attribute<double>(mesh, attr_id, name);
1724 } else if (dtype.is(np.attr("int8"))) {
1725 return cast_attribute<int8_t>(mesh, attr_id, name);
1726 } else if (dtype.is(np.attr("int16"))) {
1727 return cast_attribute<int16_t>(mesh, attr_id, name);
1728 } else if (dtype.is(np.attr("int32"))) {
1729 return cast_attribute<int32_t>(mesh, attr_id, name);
1730 } else if (dtype.is(np.attr("int64"))) {
1731 return cast_attribute<int64_t>(mesh, attr_id, name);
1732 } else if (dtype.is(np.attr("uint8"))) {
1733 return cast_attribute<uint8_t>(mesh, attr_id, name);
1734 } else if (dtype.is(np.attr("uint16"))) {
1735 return cast_attribute<uint16_t>(mesh, attr_id, name);
1736 } else if (dtype.is(np.attr("uint32"))) {
1737 return cast_attribute<uint32_t>(mesh, attr_id, name);
1738 } else if (dtype.is(np.attr("uint64"))) {
1739 return cast_attribute<uint64_t>(mesh, attr_id, name);
1740 } else {
1741 throw nb::type_error("Unsupported `dtype`!");
1742 }
1743 } else {
1744 if (dtype.is(&PyFloat_Type)) {
1745 // Native python float is a C double.
1746 return cast_attribute_in_place<double>(mesh, attr_id);
1747 } else if (dtype.is(&PyLong_Type)) {
1748 // Native python int maps to int64.
1749 return cast_attribute_in_place<int64_t>(mesh, attr_id);
1750 } else if (dtype.is(np.attr("float32"))) {
1751 return cast_attribute_in_place<float>(mesh, attr_id);
1752 } else if (dtype.is(np.attr("float64"))) {
1753 return cast_attribute_in_place<double>(mesh, attr_id);
1754 } else if (dtype.is(np.attr("int8"))) {
1755 return cast_attribute_in_place<int8_t>(mesh, attr_id);
1756 } else if (dtype.is(np.attr("int16"))) {
1757 return cast_attribute_in_place<int16_t>(mesh, attr_id);
1758 } else if (dtype.is(np.attr("int32"))) {
1759 return cast_attribute_in_place<int32_t>(mesh, attr_id);
1760 } else if (dtype.is(np.attr("int64"))) {
1761 return cast_attribute_in_place<int64_t>(mesh, attr_id);
1762 } else if (dtype.is(np.attr("uint8"))) {
1763 return cast_attribute_in_place<uint8_t>(mesh, attr_id);
1764 } else if (dtype.is(np.attr("uint16"))) {
1765 return cast_attribute_in_place<uint16_t>(mesh, attr_id);
1766 } else if (dtype.is(np.attr("uint32"))) {
1767 return cast_attribute_in_place<uint32_t>(mesh, attr_id);
1768 } else if (dtype.is(np.attr("uint64"))) {
1769 return cast_attribute_in_place<uint64_t>(mesh, attr_id);
1770 } else {
1771 throw nb::type_error("Unsupported `dtype`!");
1772 }
1773 }
1774 };
1775
1776 if (std::holds_alternative<AttributeId>(input_attribute)) {
1777 return cast(std::get<AttributeId>(input_attribute));
1778 } else {
1779 AttributeId id = mesh.get_attribute_id(std::get<std::string_view>(input_attribute));
1780 return cast(id);
1781 }
1782 },
1783 "mesh"_a,
1784 "input_attribute"_a,
1785 "dtype"_a,
1786 "output_attribute_name"_a = nb::none(),
1787 R"(Cast an attribute to a new dtype.
1788
1789:param mesh: The input mesh.
1790:param input_attribute: The input attribute id or name.
1791:param dtype: The new dtype.
1792:param output_attribute_name: The output attribute name. If none, cast will replace the input attribute.
1793
1794:returns: The id of the new attribute.)");
1795
1796 m.def(
1797 "compute_mesh_covariance",
1798 [](MeshType& mesh,
1799 std::array<Scalar, 3> center,
1800 std::optional<std::string_view> active_facets_attribute_name)
1801 -> std::array<std::array<Scalar, 3>, 3> {
1802 MeshCovarianceOptions options;
1803 options.center = center;
1804 options.active_facets_attribute_name = active_facets_attribute_name;
1805 return compute_mesh_covariance<Scalar, Index>(mesh, options);
1806 },
1807 "mesh"_a,
1808 "center"_a,
1809 "active_facets_attribute_name"_a = nb::none(),
1810 R"(Compute the covariance matrix of a mesh w.r.t. a center (Pythonic API).
1811
1812:param mesh: Input mesh.
1813:param center: The center of the covariance computation.
1814:param active_facets_attribute_name: (optional) Attribute name of whether a facet should be considered in the computation.
1815
1816:returns: The 3 by 3 covariance matrix, which should be symmetric.)");
1817
1818 m.def(
1819 "select_facets_by_normal_similarity",
1820 [](MeshType& mesh,
1821 Index seed_facet_id,
1822 std::optional<double> flood_error_limit,
1823 std::optional<double> flood_second_to_first_order_limit_ratio,
1824 std::optional<std::string_view> facet_normal_attribute_name,
1825 std::optional<std::string_view> is_facet_selectable_attribute_name,
1826 std::optional<std::string_view> output_attribute_name,
1827 std::optional<std::string_view> search_type,
1828 std::optional<int> num_smooth_iterations) {
1829 // Set options in the C++ struct
1830 SelectFacetsByNormalSimilarityOptions options;
1831 if (flood_error_limit.has_value())
1832 options.flood_error_limit = flood_error_limit.value();
1833 if (flood_second_to_first_order_limit_ratio.has_value())
1834 options.flood_second_to_first_order_limit_ratio =
1835 flood_second_to_first_order_limit_ratio.value();
1836 if (facet_normal_attribute_name.has_value())
1837 options.facet_normal_attribute_name = facet_normal_attribute_name.value();
1838 if (is_facet_selectable_attribute_name.has_value()) {
1839 options.is_facet_selectable_attribute_name = is_facet_selectable_attribute_name;
1840 }
1841 if (output_attribute_name.has_value())
1842 options.output_attribute_name = output_attribute_name.value();
1843 if (search_type.has_value()) {
1844 if (search_type.value() == "BFS")
1846 else if (search_type.value() == "DFS")
1848 else
1849 throw std::runtime_error(
1850 fmt::format("Invalid search type: {}", search_type.value()));
1851 }
1852 if (num_smooth_iterations.has_value())
1853 options.num_smooth_iterations = num_smooth_iterations.value();
1854
1855 return select_facets_by_normal_similarity<Scalar, Index>(mesh, seed_facet_id, options);
1856 },
1857 "mesh"_a, /* `_a` is a literal for nanobind to create nb::args, a required argument */
1858 "seed_facet_id"_a,
1859 "flood_error_limit"_a = nb::none(),
1860 "flood_second_to_first_order_limit_ratio"_a = nb::none(),
1861 "facet_normal_attribute_name"_a = nb::none(),
1862 "is_facet_selectable_attribute_name"_a = nb::none(),
1863 "output_attribute_name"_a = nb::none(),
1864 "search_type"_a = nb::none(),
1865 "num_smooth_iterations"_a = nb::none(),
1866 R"(Select facets by normal similarity (Pythonic API).
1867
1868:param mesh: Input mesh.
1869:param seed_facet_id: Index of the seed facet.
1870:param flood_error_limit: Tolerance for normals of the seed and the selected facets. Higher limit leads to larger selected region.
1871:param flood_second_to_first_order_limit_ratio: Ratio of the flood_error_limit and the tolerance for normals of neighboring selected facets. Higher ratio leads to more curvature in selected region.
1872:param facet_normal_attribute_name: Attribute name of the facets normal. If the mesh doesn't have this attribute, it will call compute_facet_normal to compute it.
1873:param is_facet_selectable_attribute_name: If provided, this function will look for this attribute to determine if a facet is selectable.
1874:param output_attribute_name: Attribute name of whether a facet is selected.
1875:param search_type: Use 'BFS' for breadth-first search or 'DFS' for depth-first search.
1876:param num_smooth_iterations: Number of iterations to smooth the boundary of the selected region.
1877
1878:returns: Id of the attribute on whether a facet is selected.)",
1879 nb::sig(
1880 "def select_facets_by_normal_similarity(mesh: SurfaceMesh, "
1881 "seed_facet_id: int, "
1882 "flood_error_limit: float | None = None, "
1883 "flood_second_to_first_order_limit_ratio: float | None = None, "
1884 "facet_normal_attribute_name: str | None = None, "
1885 "is_facet_selectable_attribute_name: str | None = None, "
1886 "output_attribute_name: str | None = None, "
1887 "search_type: typing.Literal['BFS', 'DFS'] | None = None,"
1888 "num_smooth_iterations: int | None = None) -> int"));
1889
1890 m.def(
1891 "select_facets_in_frustum",
1892 [](MeshType& mesh,
1893 std::array<std::array<Scalar, 3>, 4> frustum_plane_points,
1894 std::array<std::array<Scalar, 3>, 4> frustum_plane_normals,
1895 std::optional<bool> greedy,
1896 std::optional<std::string_view> output_attribute_name) {
1897 // Set options in the C++ struct
1898 Frustum<Scalar> frustum;
1899 for (size_t i = 0; i < 4; ++i) {
1900 frustum.planes[i].point = frustum_plane_points[i];
1901 frustum.planes[i].normal = frustum_plane_normals[i];
1902 }
1903 FrustumSelectionOptions options;
1904 if (greedy.has_value()) options.greedy = greedy.value();
1905 if (output_attribute_name.has_value())
1906 options.output_attribute_name = output_attribute_name.value();
1907
1908 return select_facets_in_frustum<Scalar, Index>(mesh, frustum, options);
1909 },
1910 "mesh"_a,
1911 "frustum_plane_points"_a,
1912 "frustum_plane_normals"_a,
1913 "greedy"_a = nb::none(),
1914 "output_attribute_name"_a = nb::none(),
1915 R"(Select facets in a frustum (Pythonic API).
1916
1917:param mesh: Input mesh.
1918:param frustum_plane_points: Four points on each of the frustum planes.
1919:param frustum_plane_normals: Four normals of each of the frustum planes.
1920:param greedy: If true, the function returns as soon as the first facet is found.
1921:param output_attribute_name: Attribute name of whether a facet is selected.
1922
1923:returns: Whether any facets got selected.)");
1924
1925 m.def(
1926 "thicken_and_close_mesh",
1927 [](MeshType& mesh,
1928 std::optional<Scalar> offset_amount,
1929 std::variant<std::monostate, std::array<double, 3>, std::string_view> direction,
1930 std::optional<double> mirror_ratio,
1931 std::optional<size_t> num_segments,
1932 std::optional<std::vector<std::string>> indexed_attributes) {
1933 ThickenAndCloseOptions options;
1934
1935 if (auto array_val = std::get_if<std::array<double, 3>>(&direction)) {
1936 options.direction = *array_val;
1937 } else if (auto string_val = std::get_if<std::string_view>(&direction)) {
1938 options.direction = *string_val;
1939 }
1940 options.offset_amount = offset_amount.value_or(options.offset_amount);
1941 options.mirror_ratio = std::move(mirror_ratio);
1942 options.num_segments = num_segments.value_or(options.num_segments);
1943 options.indexed_attributes = indexed_attributes.value_or(options.indexed_attributes);
1944
1945 return thicken_and_close_mesh<Scalar, Index>(mesh, options);
1946 },
1947 "mesh"_a,
1948 "offset_amount"_a = nb::none(),
1949 "direction"_a = nb::none(),
1950 "mirror_ratio"_a = nb::none(),
1951 "num_segments"_a = nb::none(),
1952 "indexed_attributes"_a = nb::none(),
1953 R"(Thicken a mesh by offsetting it, and close the shape into a thick 3D solid.
1954
1955:param mesh: Input mesh.
1956:param direction: Direction of the offset. Can be an attribute name or a fixed 3D vector.
1957:param offset_amount: Amount of offset.
1958:param mirror_ratio: Ratio of the offset amount to mirror the mesh.
1959:param num_segments: Number of segments to use for the thickening.
1960:param indexed_attributes: List of indexed attributes to copy to the new mesh.
1961
1962:returns: The thickened and closed mesh.)");
1963
1964 m.def(
1965 "extract_boundary_loops",
1967 "mesh"_a,
1968 R"(Extract boundary loops from a mesh.
1969
1970:param mesh: Input mesh.
1971
1972:returns: A list of boundary loops, each represented as a list of vertex indices.)");
1973
1974 m.def(
1975 "extract_boundary_edges",
1976 [](MeshType& mesh) {
1977 mesh.initialize_edges();
1978 Index num_edges = mesh.get_num_edges();
1979 std::vector<Index> bd_edges;
1980 bd_edges.reserve(num_edges);
1981 for (Index ei = 0; ei < num_edges; ++ei) {
1982 if (mesh.is_boundary_edge(ei)) {
1983 bd_edges.push_back(ei);
1984 }
1985 }
1986 return bd_edges;
1987 },
1988 "mesh"_a,
1989 R"(Extract boundary edges from a mesh.
1990
1991:param mesh: Input mesh.
1992
1993:returns: A list of boundary edge indices.)");
1994
1995 m.def(
1996 "compute_uv_charts",
1997 [](MeshType& mesh,
1998 std::string_view uv_attribute_name,
1999 std::string_view output_attribute_name,
2000 std::string_view connectivity_type) {
2001 UVChartOptions options;
2002 options.uv_attribute_name = uv_attribute_name;
2003 options.output_attribute_name = output_attribute_name;
2004 if (connectivity_type == "Vertex") {
2005 options.connectivity_type = UVChartOptions::ConnectivityType::Vertex;
2006 } else if (connectivity_type == "Edge") {
2007 options.connectivity_type = UVChartOptions::ConnectivityType::Edge;
2008 } else {
2009 throw std::runtime_error(
2010 fmt::format("Invalid connectivity type: {}", connectivity_type));
2011 }
2012 return compute_uv_charts(mesh, options);
2013 },
2014 "mesh"_a,
2015 "uv_attribute_name"_a = UVChartOptions().uv_attribute_name,
2016 "output_attribute_name"_a = UVChartOptions().output_attribute_name,
2017 "connectivity_type"_a = "Edge",
2018 R"(Compute UV charts.
2019
2020@param mesh: Input mesh.
2021@param uv_attribute_name: Name of the UV attribute.
2022@param output_attribute_name: Name of the output attribute to store the chart ids.
2023@param connectivity_type: Type of connectivity to use for chart computation. Can be "Vertex" or "Edge".
2024
2025@returns: A list of chart ids for each vertex.)");
2026
2027 m.def(
2028 "uv_mesh_view",
2029 [](const MeshType& mesh, std::string_view uv_attribute_name) {
2030 UVMeshOptions options;
2031 options.uv_attribute_name = uv_attribute_name;
2032 return uv_mesh_view(mesh, options);
2033 },
2034 "mesh"_a,
2035 "uv_attribute_name"_a = UVMeshOptions().uv_attribute_name,
2036 R"(Extract a UV mesh view from a 3D mesh.
2037
2038:param mesh: Input mesh.
2039:param uv_attribute_name: Name of the (indexed or vertex) UV attribute.
2040
2041:return: A new mesh representing the UV mesh.)");
2042 m.def(
2043 "uv_mesh_ref",
2044 [](MeshType& mesh, std::string_view uv_attribute_name) {
2045 UVMeshOptions options;
2046 options.uv_attribute_name = uv_attribute_name;
2047 return uv_mesh_ref(mesh, options);
2048 },
2049 "mesh"_a,
2050 "uv_attribute_name"_a = UVMeshOptions().uv_attribute_name,
2051 R"(Extract a UV mesh reference from a 3D mesh.
2052
2053:param mesh: Input mesh.
2054:param uv_attribute_name: Name of the (indexed or vertex) UV attribute.
2055
2056:return: A new mesh representing the UV mesh.)");
2057
2058 m.def(
2059 "split_facets_by_material",
2061 "mesh"_a,
2062 "material_attribute_name"_a,
2063 R"(Split mesh facets based on a material attribute.
2064
2065@param mesh: Input mesh on which material segmentation will be applied in place.
2066@param material_attribute_name: Name of the material attribute to use for inserting boundaries.
2067
2068@note The material attribute should be n by k vertex attribute, where n is the number of vertices,
2069and k is the number of materials. The value at row i and column j indicates the probability of vertex
2070i belonging to material j. The function will insert boundaries between different materials based on
2071the material attribute.
2072)");
2073}
2074
2075} // namespace lagrange::python
bool is_boundary_edge(Index e) const
Determines whether the specified edge e is a boundary edge.
Definition SurfaceMesh.cpp:2621
Index get_dimension() const
Retrieves the dimension of the mesh vertices.
Definition SurfaceMesh.h:656
AttributeId get_attribute_id(std::string_view name) const
Retrieve an attribute id given its name.
Definition SurfaceMesh.cpp:345
Index get_num_edges() const
Retrieves the number of edges.
Definition SurfaceMesh.h:692
void initialize_edges(span< const Index > edges={})
Initializes attributes associated to mesh edges and connectivity.
Definition SurfaceMesh.cpp:2204
SurfaceMesh< Scalar, Index > unify_named_index_buffer(const SurfaceMesh< Scalar, Index > &mesh, const std::vector< std::string_view > &attribute_names)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition unify_index_buffer.cpp:277
AttributeId map_attribute_in_place(SurfaceMesh< Scalar, Index > &mesh, AttributeId id, AttributeElement new_element)
Map attribute values to a different element type.
Definition map_attribute.cpp:270
AttributeId map_attribute(SurfaceMesh< Scalar, Index > &mesh, AttributeId id, std::string_view new_name, AttributeElement new_element)
Map attribute values to a new attribute with a different element type.
Definition map_attribute.cpp:243
SurfaceMesh< Scalar, Index > unify_index_buffer(const SurfaceMesh< Scalar, Index > &mesh, const std::vector< AttributeId > &attribute_ids={})
Unify index buffers of the input mesh for all attributes specified in attribute_ids.
Definition unify_index_buffer.cpp:34
uint32_t AttributeId
Identified to be used to access an attribute.
Definition AttributeFwd.h:73
AttributeElement
Type of element to which the attribute is attached.
Definition AttributeFwd.h:26
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
@ Facet
Per-facet mesh attributes.
Definition AttributeFwd.h:31
AttributeId compute_normal(SurfaceMesh< Scalar, Index > &mesh, function_ref< bool(Index)> is_edge_smooth, span< const Index > cone_vertices={}, NormalOptions options={})
Compute smooth normals based on specified sharp edges and cone vertices.
Definition compute_normal.cpp:198
SurfaceMesh< Scalar, Index > trim_by_isoline(const SurfaceMesh< Scalar, Index > &mesh, const IsolineOptions &options={})
Trim a mesh by the isoline of an implicit function defined on the mesh vertices/corners.
Definition isoline.cpp:325
AttributeId cast_attribute_in_place(SurfaceMesh< Scalar, Index > &mesh, AttributeId attribute_id)
Cast an attribute in place to a different value type.
Definition cast_attribute.cpp:68
bool is_closed(const SurfaceMesh< Scalar, Index > &mesh)
Check if a mesh is closed.
Definition topology.cpp:51
int compute_euler(const SurfaceMesh< Scalar, Index > &mesh)
Compute Euler characteristic of a mesh.
Definition topology.cpp:35
bool select_facets_in_frustum(SurfaceMesh< Scalar, Index > &mesh, const Frustum< Scalar > &frustum, const FrustumSelectionOptions &options={})
Select all facets that intersect the cone/frustrum bounded by 4 planes defined by (n_i,...
Definition select_facets_in_frustum.cpp:44
AttributeId compute_greedy_coloring(SurfaceMesh< Scalar, Index > &mesh, const GreedyColoringOptions &options={})
Compute a greedy graph coloring of the mesh.
Definition compute_greedy_coloring.cpp:153
SurfaceMesh< Scalar, Index > combine_meshes(std::initializer_list< const SurfaceMesh< Scalar, Index > * > meshes, bool preserve_attributes=true)
Combine multiple meshes into a single mesh.
Definition combine_meshes.cpp:295
std::vector< SurfaceMesh< Scalar, Index > > separate_by_facet_groups(const SurfaceMesh< Scalar, Index > &mesh, size_t num_groups, span< const Index > facet_group_indices, const SeparateByFacetGroupsOptions &options={})
Extract a set of submeshes based on facet groups.
Definition separate_by_facet_groups.cpp:29
bool is_oriented(const SurfaceMesh< Scalar, Index > &mesh)
Check if a mesh is oriented.
Definition orientation.cpp:57
bool is_manifold(const SurfaceMesh< Scalar, Index > &mesh)
Check if a mesh is both vertex-manifold and edge-manifold.
Definition topology.h:98
void permute_facets(SurfaceMesh< Scalar, Index > &mesh, span< const Index > new_to_old)
Reorder facets of a mesh based on a given permutation.
Definition permute_facets.cpp:26
AttributeId compute_facet_normal(SurfaceMesh< Scalar, Index > &mesh, FacetNormalOptions options={})
Compute facet normals.
Definition compute_facet_normal.cpp:34
bool is_edge_manifold(const SurfaceMesh< Scalar, Index > &mesh)
Check if a mesh is edge-manifold.
Definition topology.cpp:125
AttributeId compute_facet_area(SurfaceMesh< Scalar, Index > &mesh, FacetAreaOptions options={})
Compute per-facet area.
Definition compute_area.cpp:304
AttributeId compute_edge_lengths(SurfaceMesh< Scalar, Index > &mesh, const EdgeLengthOptions &options={})
Computes edge lengths attribute.
Definition compute_edge_lengths.cpp:28
AttributeId cast_attribute(SurfaceMesh< Scalar, Index > &mesh, AttributeId source_id, std::string_view target_name)
Cast an attribute in place to a different value type.
Definition cast_attribute.cpp:25
std::optional< std::vector< Index > > compute_dijkstra_distance(SurfaceMesh< Scalar, Index > &mesh, const DijkstraDistanceOptions< Scalar, Index > &options={})
Computes dijkstra distance from a seed facet.
Definition compute_dijkstra_distance.cpp:24
Scalar compute_mesh_area(const SurfaceMesh< Scalar, Index > &mesh, MeshAreaOptions options={})
Compute mesh area.
Definition compute_area.cpp:361
AttributeId compute_vertex_valence(SurfaceMesh< Scalar, Index > &mesh, VertexValenceOptions options={})
Compute vertex valence.
Definition compute_vertex_valence.cpp:25
SurfaceMesh< Scalar, Index > extract_submesh(const SurfaceMesh< Scalar, Index > &mesh, span< const Index > selected_facets, const SubmeshOptions &options={})
Extract a submesh that consists of a subset of the facets of the source mesh.
Definition extract_submesh.cpp:26
void normalize_mesh(SurfaceMesh< Scalar, Index > &mesh, const TransformOptions &options={})
Normalize a mesh to fit in a unit box centered at the origin.
Definition normalize_meshes.cpp:52
AttributeId compute_facet_vector_area(SurfaceMesh< Scalar, Index > &mesh, FacetVectorAreaOptions options={})
Compute per-facet vector area.
Definition compute_area.cpp:322
void remap_vertices(SurfaceMesh< Scalar, Index > &mesh, span< const Index > forward_mapping, RemapVerticesOptions options={})
Remap vertices of a mesh based on provided forward mapping.
Definition remap_vertices.cpp:138
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:520
auto normalize_mesh_with_transform(SurfaceMesh< Scalar, Index > &mesh, const TransformOptions &options={}) -> Eigen::Transform< Scalar, Dimension, Eigen::Affine >
Normalize a mesh to fit in a unit box centered at the origin.
Definition normalize_meshes.cpp:25
void permute_vertices(SurfaceMesh< Scalar, Index > &mesh, span< const Index > new_to_old)
Reorder vertices of a mesh based on a given permutation.
Definition permute_vertices.cpp:26
AttributeId compute_vertex_normal(SurfaceMesh< Scalar, Index > &mesh, VertexNormalOptions options={})
Compute per-vertex normals based on specified weighting type.
Definition compute_vertex_normal.cpp:34
bool is_vertex_manifold(const SurfaceMesh< Scalar, Index > &mesh)
Check if a mesh is vertex-manifold.
Definition topology.cpp:98
AttributeId compute_facet_centroid(SurfaceMesh< Scalar, Index > &mesh, FacetCentroidOptions options={})
Compute per-facet centroid.
Definition compute_centroid.cpp:31
std::vector< SurfaceMesh< Scalar, Index > > separate_by_components(const SurfaceMesh< Scalar, Index > &mesh, const SeparateByComponentsOptions &options={})
Separate a mesh by connected components.
Definition separate_by_components.cpp:21
AttributeId select_facets_by_normal_similarity(SurfaceMesh< Scalar, Index > &mesh, const Index seed_facet_id, const SelectFacetsByNormalSimilarityOptions &options={})
Given a seed facet, selects facets around it based on the change in triangle normals.
Definition select_facets_by_normal_similarity.cpp:27
std::vector< std::vector< Index > > extract_boundary_loops(const SurfaceMesh< Scalar, Index > &mesh)
Extract boundary loops from a surface mesh.
Definition extract_boundary_loops.cpp:24
AttributeId compute_dihedral_angles(SurfaceMesh< Scalar, Index > &mesh, const DihedralAngleOptions &options={})
Computes dihedral angles for each edge in the mesh.
Definition compute_dihedral_angles.cpp:33
SurfaceMesh< ToScalar, ToIndex > cast(const SurfaceMesh< FromScalar, FromIndex > &source_mesh, const AttributeFilter &convertible_attributes={}, std::vector< std::string > *converted_attributes_names=nullptr)
Cast a mesh to a mesh of different scalar and/or index type.
TangentBitangentResult compute_tangent_bitangent(SurfaceMesh< Scalar, Index > &mesh, TangentBitangentOptions options={})
Compute mesh tangent and bitangent vectors orthogonal to the input mesh normals.
Definition compute_tangent_bitangent.cpp:534
SurfaceMesh< Scalar, Index > transformed_mesh(SurfaceMesh< Scalar, Index > mesh, const Eigen::Transform< Scalar, Dimension, Eigen::Affine > &transform, const TransformOptions &options={})
Apply an affine transform to a mesh and return the transformed mesh.
Definition transform_mesh.cpp:207
SurfaceMesh< Scalar, Index > filter_attributes(SurfaceMesh< Scalar, Index > source_mesh, const AttributeFilter &options={})
Filters the attributes of mesh according to user specifications.
Definition filter_attributes.cpp:116
void normalize_meshes(span< SurfaceMesh< Scalar, Index > * > meshes, const TransformOptions &options={})
Normalize a list of meshes to fit in a unit box centered at the origin.
Definition normalize_meshes.cpp:102
void transform_mesh(SurfaceMesh< Scalar, Index > &mesh, const Eigen::Transform< Scalar, Dimension, Eigen::Affine > &transform, const TransformOptions &options={})
Apply an affine transform to a mesh in-place.
Definition transform_mesh.cpp:198
AttributeId compute_uv_distortion(SurfaceMesh< Scalar, Index > &mesh, const UVDistortionOptions &options={})
Compute uv distortion using the selected distortion measure.
Definition compute_uv_distortion.cpp:31
DistortionMetric
UV distortion metric type.
Definition DistortionMetric.h:26
void compute_mesh_centroid(const SurfaceMesh< Scalar, Index > &mesh, span< Scalar > centroid, MeshCentroidOptions options={})
Compute mesh centroid, where mesh centroid is defined as the weighted sum of facet centroids.
Definition compute_centroid.cpp:74
size_t compute_components(SurfaceMesh< Scalar, Index > &mesh, ComponentOptions options={})
Compute connected components of an input mesh.
Definition compute_components.cpp:99
SurfaceMesh< Scalar, Index > extract_isoline(const SurfaceMesh< Scalar, Index > &mesh, const IsolineOptions &options={})
Extract the isoline of an implicit function defined on the mesh vertices/corners.
Definition isoline.cpp:333
auto normalize_meshes_with_transform(span< SurfaceMesh< Scalar, Index > * > meshes, const TransformOptions &options={}) -> Eigen::Transform< Scalar, Dimension, Eigen::Affine >
Normalize a list of meshes to fit in a unit box centered at the origin.
Definition normalize_meshes.cpp:62
@ Angle
Incident face normals are averaged weighted by incident angle of vertex.
Definition NormalWeightingType.h:36
@ CornerTriangleArea
Incident face normals are averaged weighted by area of the corner triangle.
Definition NormalWeightingType.h:33
@ Uniform
Incident face normals have uniform influence on vertex normal.
Definition NormalWeightingType.h:29
@ MIPS
UV triangle area / 3D triangle area.
Definition DistortionMetric.h:31
@ InverseDirichlet
Inverse Dirichlet energy.
Definition DistortionMetric.h:28
@ SymmetricDirichlet
Symmetric Dirichlet energy.
Definition DistortionMetric.h:29
@ Dirichlet
Dirichlet energy.
Definition DistortionMetric.h:27
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
::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
void map_attributes(const SurfaceMesh< Scalar, Index > &source_mesh, SurfaceMesh< Scalar, Index > &target_mesh, span< const Index > mapping_data, span< const Index > mapping_offsets={}, const MapAttributesOptions &options={})
Map attributes from the source mesh to the target mesh.
Definition map_attributes.cpp:26
std::array< std::array< Scalar, 3 >, 3 > compute_mesh_covariance(const SurfaceMesh< Scalar, Index > &mesh, const MeshCovarianceOptions &options={})
Compute the covariance matrix w.r.t.
Definition compute_mesh_covariance.cpp:98
SurfaceMesh< Scalar, Index > uv_mesh_view(const SurfaceMesh< Scalar, Index > &mesh, const UVMeshOptions &options={})
Extract a UV mesh view from an input mesh.
Definition uv_mesh.cpp:51
size_t compute_uv_charts(SurfaceMesh< Scalar, Index > &mesh, const UVChartOptions &options={})
Compute UV charts of an input mesh.
Definition compute_uv_charts.cpp:23
ConnectivityType
This type defines the condition when two facets are considered as "connected".
Definition ConnectivityType.h:19
@ Edge
Two facets are considered connected if they share an edge.
Definition ConnectivityType.h:21
ReorderingMethod
Mesh reordering method to apply before decimation.
Definition reorder_mesh.h:21
@ Lexicographic
Sort vertices/facets lexicographically.
Definition reorder_mesh.h:22
@ None
Do not reorder mesh vertices/facets.
Definition reorder_mesh.h:25
@ Hilbert
Spatial sort vertices/facets using Hilbert curve.
Definition reorder_mesh.h:24
@ Morton
Spatial sort vertices/facets using Morton encoding.
Definition reorder_mesh.h:23
SurfaceMesh< Scalar, Index > thicken_and_close_mesh(SurfaceMesh< Scalar, Index > input_mesh, const ThickenAndCloseOptions &options={})
Thicken a mesh by offsetting it, and close the shape into a thick 3D solid.
Definition thicken_and_close_mesh.cpp:270
void orient_outward(lagrange::SurfaceMesh< Scalar, Index > &mesh, const OrientOptions &options={})
Orient the facets of a mesh so that the signed volume of each connected component is positive or nega...
Definition orient_outward.cpp:126
void split_facets_by_material(SurfaceMesh< Scalar, Index > &mesh, std::string_view material_attribute_name)
Split mesh facets based on material labels.
Definition split_facets_by_material.cpp:57
PointcloudPCAOutput< Scalar > compute_pointcloud_pca(span< const Scalar > points, ComputePointcloudPCAOptions options={})
Finds the principal components for a pointcloud.
Definition compute_pointcloud_pca.cpp:23
AttributeId compute_facet_circumcenter(SurfaceMesh< Scalar, Index > &mesh, FacetCircumcenterOptions options={})
Compute per-facet circumcenter.
Definition compute_facet_circumcenter.cpp:32
SurfaceMesh< Scalar, Index > uv_mesh_ref(SurfaceMesh< Scalar, Index > &mesh, const UVMeshOptions &options={})
Extract a UV mesh reference from an input mesh.
Definition uv_mesh.cpp:21
AttributeId compute_seam_edges(SurfaceMesh< Scalar, Index > &mesh, AttributeId indexed_attribute_id, const SeamEdgesOptions &options={})
Computes the seam edges for a given indexed attribute.
Definition compute_seam_edges.cpp:35
@ KeepFirst
Keep the value of the first elements.
Definition MappingPolicy.h:23
@ Error
Throw an error if collision is detected.
Definition MappingPolicy.h:24
@ Average
Take the average of all involved elements.
Definition MappingPolicy.h:22
void reorder_mesh(SurfaceMesh< Scalar, Index > &mesh, ReorderingMethod method)
Mesh reordering to improve cache locality.
Definition reorder_mesh.cpp:178
std::variant< AttributeId, std::string > AttributeNameOrId
Variant identifying an attribute by its name or id.
Definition filter_attributes.h:39
ConnectivityType connectivity_type
Connectivity type used for component computation.
Definition compute_components.h:38
std::string_view output_attribute_name
Output component id attribute name.
Definition compute_components.h:35
std::string_view output_attribute_name
Output attribute name for facet area.
Definition compute_area.h:34
std::string_view output_attribute_name
Ouptut facet centroid attribute name.
Definition compute_centroid.h:33
std::string_view output_attribute_name
Output normal attribute name.
Definition compute_facet_normal.h:35
std::string_view input_attribute_name
Precomputed facet area attribute name.
Definition compute_area.h:119
bool use_signed_area
For 2D mesh only: whether the computed facet area (if any) should be signed.
Definition compute_area.h:122
std::string_view facet_centroid_attribute_name
Precomputed facet centroid attribute name.
Definition compute_centroid.h:66
@ Area
Per-facet centroid are weighted by facet area.
Definition compute_centroid.h:61
@ Uniform
Per-facet centroid are weighted uniformly.
Definition compute_centroid.h:60
std::string_view facet_area_attribute_name
Precomputed facet area attribute name.
Definition compute_centroid.h:70
bool keep_facet_normals
Whether to keep any newly added facet normal attribute.
Definition compute_normal.h:55
std::string_view facet_normal_attribute_name
Precomputed facet normal attribute name.
Definition compute_normal.h:48
bool recompute_facet_normals
Whether to recompute the facet normal attribute, or reuse existing cached values if present.
Definition compute_normal.h:51
std::string_view output_attribute_name
Output normal attribute name.
Definition compute_normal.h:41
float distance_tolerance
Tolerance for degenerate edge check. (only used to bypass degenerate edges in polygon facets)
Definition compute_normal.h:58
NormalWeightingType weight_type
Per-vertex normal averaging weighting type.
Definition compute_normal.h:44
CollisionPolicy collision_policy_integral
Collision policy for integral valued attributes.
Definition remap_vertices.h:39
CollisionPolicy collision_policy_float
Collision policy for float or double valued attributes.
Definition remap_vertices.h:36
@ BFS
Breadth-First Search.
Definition select_facets_by_normal_similarity.h:62
@ DFS
Depth-First Search.
Definition select_facets_by_normal_similarity.h:63
std::string_view bitangent_attribute_name
Output bitangent attribute name.
Definition compute_tangent_bitangent.h:41
bool keep_existing_tangent
Whether to recompute tangent if the tangent attribute (specified by tangent_attribute_name) already e...
Definition compute_tangent_bitangent.h:70
std::string_view normal_attribute_name
Normal attribute name used to compute the BTN frame.
Definition compute_tangent_bitangent.h:52
std::string_view tangent_attribute_name
Output tangent attribute name.
Definition compute_tangent_bitangent.h:38
AttributeElement output_element_type
Output element type. Can be either Corner or Indexed.
Definition compute_tangent_bitangent.h:55
bool pad_with_sign
Whether to pad the tangent/bitangent vectors with a 4th coordinate indicating the sign of the UV tria...
Definition compute_tangent_bitangent.h:59
bool orthogonalize_bitangent
Whether to compute the bitangent as sign * cross(normal, tangent) If false, the bitangent is computed...
Definition compute_tangent_bitangent.h:63
std::string_view uv_attribute_name
UV attribute name used to orient the BTN frame.
Definition compute_tangent_bitangent.h:45
AttributeId tangent_id
Tangent vector attribute id.
Definition compute_tangent_bitangent.h:77
AttributeId bitangent_id
Bitangent vector attribute id.
Definition compute_tangent_bitangent.h:80
@ Earcut
Use earcut algorithm to triangulate polygons.
Definition triangulate_polygonal_facets.h:31
@ CentroidFan
Connect facet centroid to polygon edges to form a fan of triangles.
Definition triangulate_polygonal_facets.h:32
Scheme scheme
Triangulation scheme to use.
Definition triangulate_polygonal_facets.h:35
bool keep_weighted_corner_normals
Whether to keep any newly added weighted corner normal attribute.
Definition compute_vertex_normal.h:56
std::string_view weighted_corner_normal_attribute_name
Precomputed weighted corner attribute name.
Definition compute_vertex_normal.h:47
std::string_view output_attribute_name
Output normal attribute name.
Definition compute_vertex_normal.h:39
float distance_tolerance
Tolerance for degenerate edge check. (only used to bypass degenerate edges in polygon facets)
Definition compute_vertex_normal.h:59
bool recompute_weighted_corner_normals
Whether to recompute the weighted corner normal attribute, or reuse existing cached values if present...
Definition compute_vertex_normal.h:51
NormalWeightingType weight_type
Per-vertex normal averaging weighting type.
Definition compute_vertex_normal.h:42
std::string_view induced_by_attribute
Optional per-edge attribute used as indicator function to restrict the graph used for vertex valence ...
Definition compute_vertex_valence.h:39
std::string_view output_attribute_name
Output vertex valence attribute name.
Definition compute_vertex_valence.h:42