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")
34 "Creates a new mesh instance with identity transform and mesh_index of 0.")
37 &MeshInstance3D::mesh_index,
38 "Index of the mesh in the scene's mesh array.")
41 [](MeshInstance3D& self) {
42 auto& M = self.transform.matrix();
43 using MatrixType = std::decay_t<
decltype(M)>;
44 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
47 size_t shape[2]{
static_cast<size_t>(M.rows()),
static_cast<size_t>(M.cols())};
48 int64_t stride[2]{1, 4};
49 return span_to_tensor(data, shape, stride, nb::cast(&self));
51 [](MeshInstance3D& self, Tensor<Scalar> tensor) {
52 auto [values, shape, stride] = tensor_to_span(tensor);
53 auto& M = self.transform.matrix();
54 using MatrixType = std::decay_t<
decltype(M)>;
55 static_assert(!MatrixType::IsRowMajor,
"Transformation matrix is not column major");
61 std::copy(values.begin(), values.end(), M.data());
85 R
"(4x4 transformation matrix for this instance.
87The transformation matrix is stored in column-major order. Both row-major and column-major
88input tensors are supported for setting the transform.)");
90 using SimpleScene3D = lagrange::scene::SimpleScene<Scalar, Index, 3>;
91 nb::class_<SimpleScene3D>(m,
"SimpleScene3D",
"Simple scene container for instanced meshes")
92 .def(nb::init<>(),
"Creates an empty scene with no meshes or instances.")
93 .def_prop_ro(
"num_meshes", &SimpleScene3D::get_num_meshes,
"Number of meshes in the scene")
96 &SimpleScene3D::get_num_instances,
98 R
"(Gets the number of instances for a specific mesh.
100:param mesh_index: Index of the mesh.
102:return: Number of instances of the specified mesh.)")
104 "total_num_instances",
105 &SimpleScene3D::compute_num_instances,
106 "Total number of instances for all meshes in the scene")
109 &SimpleScene3D::get_mesh,
111 R
"(Gets a copy of the mesh at the specified index.
113:param mesh_index: Index of the mesh.
115:return: Copy of the mesh.)")
118 &SimpleScene3D::ref_mesh,
120 R
"(Gets a reference to the mesh at the specified index.
122:param mesh_index: Index of the mesh.
124:return: Reference to the mesh.)")
127 &SimpleScene3D::get_instance,
130 R
"(Gets a specific instance of a mesh.
132:param mesh_index: Index of the mesh.
133:param instance_index: Index of the instance for that mesh.
135:return: The mesh instance.)")
138 &SimpleScene3D::reserve_meshes,
140 R
"(Reserves storage for meshes.
142:param num_meshes: Number of meshes to reserve space for.)")
145 &SimpleScene3D::add_mesh,
147 R
"(Adds a mesh to the scene.
149:param mesh: Mesh to add.
151:return: Index of the newly added mesh.)")
154 &SimpleScene3D::reserve_instances,
157 R
"(Reserves storage for instances of a specific mesh.
159:param mesh_index: Index of the mesh.
160:param num_instances: Number of instances to reserve space for.)")
163 &SimpleScene3D::add_instance,
165 R
"(Adds an instance to the scene.
167:param instance: Mesh instance to add.
169:return: Index of the newly added instance for its mesh.)");
174 "simple_scene_to_mesh",
175 [](
const SimpleScene3D& scene,
176 bool normalize_normals,
177 bool normalize_tangents_bitangents,
178 bool preserve_attributes) {
179 TransformOptions transform_options;
180 transform_options.normalize_normals = normalize_normals;
181 transform_options.normalize_tangents_bitangents = normalize_tangents_bitangents;
182 return scene::simple_scene_to_mesh(scene, transform_options, preserve_attributes);
185 "normalize_normals"_a = TransformOptions{}.normalize_normals,
186 "normalize_tangents_bitangents"_a = TransformOptions{}.normalize_tangents_bitangents,
187 "preserve_attributes"_a =
true,
188 R
"(Converts a scene into a concatenated mesh with all the transforms applied.
190:param scene: Scene to convert.
191:param normalize_normals: If enabled, normals are normalized after transformation.
192:param normalize_tangents_bitangents: If enabled, tangents and bitangents are normalized after transformation.
193:param preserve_attributes: Preserve shared attributes and map them to the output mesh.
195:return: Concatenated mesh.)");
197 using MeshType = lagrange::SurfaceMesh<Scalar, Index>;
199 "mesh_to_simple_scene",
200 [](
const MeshType& mesh) {
return scene::mesh_to_simple_scene<3>(mesh); },
202 R
"(Converts a single mesh into a simple scene with a single identity instance of the input mesh.
204:param mesh: Input mesh to convert.
206:return: Simple scene containing the input mesh.)");
209 "meshes_to_simple_scene",
210 [](std::vector<MeshType> meshes) {
211 return scene::meshes_to_simple_scene<3>(std::move(meshes));
214 R
"(Converts a list of meshes into a simple scene with a single identity instance of each input mesh.
216:param meshes: Input meshes to convert.
218: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