14#include <lagrange/python/tensor_utils.h>
15#include <lagrange/scene/SimpleScene.h>
16#include <lagrange/scene/simple_scene_convert.h>
17#include <lagrange/utils/assert.h>
20#include <lagrange/utils/warnoff.h>
22#include <nanobind/nanobind.h>
23#include <nanobind/stl/optional.h>
24#include <lagrange/utils/warnon.h>
29namespace lagrange::python {
31namespace nb = nanobind;
33template <
typename Scalar,
typename Index>
34void bind_simple_scene(nb::module_& m)
37 nb::class_<MeshInstance3D>(m,
"MeshInstance3D",
"A single mesh instance in a scene")
39 .def_rw(
"mesh_index", &MeshInstance3D::mesh_index)
42 [](MeshInstance3D& self) {
43 auto& M = self.transform.matrix();
44 using MatrixType = std::decay_t<
decltype(M)>;
45 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
47 span<Scalar> data(M.data(), M.size());
48 size_t shape[2]{
static_cast<size_t>(M.rows()),
static_cast<size_t>(M.cols())};
49 int64_t stride[2]{1, 4};
50 return span_to_tensor(data, shape, stride,
nb::cast(&self));
52 [](MeshInstance3D& self, Tensor<Scalar> tensor) {
53 auto [values, shape, stride] = tensor_to_span(tensor);
54 auto& M = self.transform.matrix();
55 using MatrixType = std::decay_t<
decltype(M)>;
56 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
62 std::copy(values.begin(), values.end(), M.data());
88 nb::class_<SimpleScene3D>(m,
"SimpleScene3D",
"Simple scene container for instanced meshes")
90 .def_prop_ro(
"num_meshes", &SimpleScene3D::get_num_meshes,
"Number of meshes in the scene")
91 .def(
"num_instances", &SimpleScene3D::get_num_instances,
"mesh_index"_a)
93 "total_num_instances",
94 &SimpleScene3D::compute_num_instances,
95 "Total number of instances for all meshes in the scene")
96 .def(
"get_mesh", &SimpleScene3D::get_mesh,
"mesh_index"_a)
97 .def(
"ref_mesh", &SimpleScene3D::ref_mesh,
"mesh_index"_a)
98 .def(
"get_instance", &SimpleScene3D::get_instance,
"mesh_index"_a,
"instance_index"_a)
99 .def(
"reserve_meshes", &SimpleScene3D::reserve_meshes,
"num_meshes"_a)
100 .def(
"add_mesh", &SimpleScene3D::add_mesh,
"mesh"_a)
103 &SimpleScene3D::reserve_instances,
106 .def(
"add_instance", &SimpleScene3D::add_instance,
"instance"_a);
111 "simple_scene_to_mesh",
112 [](
const SimpleScene3D& scene,
113 bool normalize_normals,
114 bool normalize_tangents_bitangents,
115 bool preserve_attributes) {
116 TransformOptions transform_options;
117 transform_options.normalize_normals = normalize_normals;
118 transform_options.normalize_tangents_bitangents = normalize_tangents_bitangents;
119 return scene::simple_scene_to_mesh(scene, transform_options, preserve_attributes);
122 "normalize_normals"_a = TransformOptions{}.normalize_normals,
123 "normalize_tangents_bitangents"_a = TransformOptions{}.normalize_tangents_bitangents,
124 "preserve_attributes"_a =
true,
125 R
"(Converts a scene into a concatenated mesh with all the transforms applied.
127:param scene: Scene to convert.
128:param normalize_normals: If enabled, normals are normalized after transformation.
129:param normalize_tangents_bitangents: If enabled, tangents and bitangents are normalized after transformation.
130:param preserve_attributes: Preserve shared attributes and map them to the output mesh.
132:return: Concatenated mesh.)");
136 "mesh_to_simple_scene",
137 [](
const MeshType& mesh) {
return scene::mesh_to_simple_scene<3>(mesh); },
139 R
"(Converts a single mesh into a simple scene with a single identity instance of the input mesh.
141:param mesh: Input mesh to convert.
143:return: Simple scene containing the input mesh.)");
146 "meshes_to_simple_scene",
147 [](std::vector<MeshType> meshes) {
148 return scene::meshes_to_simple_scene<3>(std::move(meshes));
151 R
"(Converts a list of meshes into a simple scene with a single identity instance of each input mesh.
153:param meshes: Input meshes to convert.
155:return: Simple scene containing the input meshes.)");
Simple scene container for instanced meshes.
Definition: SimpleScene.h:62
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.
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
A single mesh instance in a scene.
Definition: SimpleScene.h:36