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>
23namespace lagrange::python {
25namespace nb = nanobind;
27template <
typename Scalar,
typename Index>
28void bind_simple_scene(nb::module_& m)
30 using MeshInstance3D = lagrange::scene::MeshInstance<Scalar, Index, 3>;
31 nb::class_<MeshInstance3D>(m,
"MeshInstance3D",
"A single mesh instance in a scene")
33 .def_rw(
"mesh_index", &MeshInstance3D::mesh_index)
36 [](MeshInstance3D& self) {
37 auto& M = self.transform.matrix();
38 using MatrixType = std::decay_t<
decltype(M)>;
39 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
42 size_t shape[2]{
static_cast<size_t>(M.rows()),
static_cast<size_t>(M.cols())};
43 int64_t stride[2]{1, 4};
44 return span_to_tensor(data, shape, stride, nb::cast(&self));
46 [](MeshInstance3D& self, Tensor<Scalar> tensor) {
47 auto [values, shape, stride] = tensor_to_span(tensor);
48 auto& M = self.transform.matrix();
49 using MatrixType = std::decay_t<
decltype(M)>;
50 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
56 std::copy(values.begin(), values.end(), M.data());
81 using SimpleScene3D = lagrange::scene::SimpleScene<Scalar, Index, 3>;
82 nb::class_<SimpleScene3D>(m,
"SimpleScene3D",
"Simple scene container for instanced meshes")
84 .def_prop_ro(
"num_meshes", &SimpleScene3D::get_num_meshes,
"Number of meshes in the scene")
85 .def(
"num_instances", &SimpleScene3D::get_num_instances,
"mesh_index"_a)
87 "total_num_instances",
88 &SimpleScene3D::compute_num_instances,
89 "Total number of instances for all meshes in the scene")
90 .def(
"get_mesh", &SimpleScene3D::get_mesh,
"mesh_index"_a)
91 .def(
"ref_mesh", &SimpleScene3D::ref_mesh,
"mesh_index"_a)
92 .def(
"get_instance", &SimpleScene3D::get_instance,
"mesh_index"_a,
"instance_index"_a)
93 .def(
"reserve_meshes", &SimpleScene3D::reserve_meshes,
"num_meshes"_a)
94 .def(
"add_mesh", &SimpleScene3D::add_mesh,
"mesh"_a)
97 &SimpleScene3D::reserve_instances,
100 .def(
"add_instance", &SimpleScene3D::add_instance,
"instance"_a);
105 "simple_scene_to_mesh",
106 [](
const SimpleScene3D& scene,
107 bool normalize_normals,
108 bool normalize_tangents_bitangents,
109 bool preserve_attributes) {
110 TransformOptions transform_options;
111 transform_options.normalize_normals = normalize_normals;
112 transform_options.normalize_tangents_bitangents = normalize_tangents_bitangents;
113 return scene::simple_scene_to_mesh(scene, transform_options, preserve_attributes);
116 "normalize_normals"_a = TransformOptions{}.normalize_normals,
117 "normalize_tangents_bitangents"_a = TransformOptions{}.normalize_tangents_bitangents,
118 "preserve_attributes"_a =
true,
119 R
"(Converts a scene into a concatenated mesh with all the transforms applied.
121:param scene: Scene to convert.
122:param normalize_normals: If enabled, normals are normalized after transformation.
123:param normalize_tangents_bitangents: If enabled, tangents and bitangents are normalized after transformation.
124:param preserve_attributes: Preserve shared attributes and map them to the output mesh.
126:return: Concatenated mesh.)");
128 using MeshType = lagrange::SurfaceMesh<Scalar, Index>;
130 "mesh_to_simple_scene",
131 [](
const MeshType& mesh) {
return scene::mesh_to_simple_scene<3>(mesh); },
133 R
"(Converts a single mesh into a simple scene with a single identity instance of the input mesh.
135:param mesh: Input mesh to convert.
137:return: Simple scene containing the input mesh.)");
140 "meshes_to_simple_scene",
141 [](std::vector<MeshType> meshes) {
142 return scene::meshes_to_simple_scene<3>(std::move(meshes));
145 R
"(Converts a list of meshes into a simple scene with a single identity instance of each input mesh.
147:param meshes: Input meshes to convert.
149:return: Simple scene containing the input meshes.)");
#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