Lagrange
Loading...
Searching...
No Matches
SimpleScene.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/SurfaceMesh.h>
15#include <lagrange/scene/api.h>
16#include <lagrange/utils/invalid.h>
17
18
19#include <Eigen/Geometry>
20
21#include <any>
22#include <vector>
23
24namespace lagrange::scene {
25
34template <typename Scalar, typename Index, size_t Dimension = 3>
35struct LA_SCENE_API MeshInstance
36{
38 using AffineTransform = Eigen::Transform<Scalar, static_cast<int>(Dimension), Eigen::Affine>;
39
42
44 AffineTransform transform = AffineTransform::Identity();
45
47 std::any user_data = {};
48
50 constexpr static size_t Dim = Dimension;
51};
52
60template <typename Scalar, typename Index, size_t Dimension = 3>
61class LA_SCENE_API SimpleScene
62{
63public:
66
69
72
74 constexpr static size_t Dim = Dimension;
75
76public:
82 Index get_num_meshes() const { return static_cast<Index>(m_meshes.size()); }
83
91 Index get_num_instances(Index mesh_index) const
92 {
93 return static_cast<Index>(m_instances[mesh_index].size());
94 }
95
102
110 const MeshType& get_mesh(Index mesh_index) const { return m_meshes[mesh_index]; }
111
119 MeshType& ref_mesh(Index mesh_index) { return m_meshes[mesh_index]; }
120
129 const InstanceType& get_instance(Index mesh_index, Index instance_index) const
130 {
131 return m_instances[mesh_index][instance_index];
132 }
133
142 InstanceType& ref_instance(Index mesh_index, Index instance_index)
143 {
144 return m_instances[mesh_index][instance_index];
145 }
146
152 void reserve_meshes(Index num_meshes);
153
161 Index add_mesh(MeshType mesh);
162
169 void reserve_instances(Index mesh_index, Index num_instances);
170
178 Index add_instance(InstanceType instance);
179
186 void foreach_instances_for_mesh(Index mesh_index, function_ref<void(const InstanceType&)> func)
187 const;
188
194 void foreach_instances(function_ref<void(const InstanceType&)> func) const;
195
196protected:
198 std::vector<MeshType> m_meshes;
199
201 std::vector<std::vector<InstanceType>> m_instances;
202};
203
204using SimpleScene32f2 = SimpleScene<float, uint32_t, 2u>;
205using SimpleScene32d2 = SimpleScene<double, uint32_t, 2u>;
206using SimpleScene64f2 = SimpleScene<float, uint64_t, 2u>;
207using SimpleScene64d2 = SimpleScene<double, uint64_t, 2u>;
208using SimpleScene32f3 = SimpleScene<float, uint32_t, 3u>;
209using SimpleScene32d3 = SimpleScene<double, uint32_t, 3u>;
210using SimpleScene64f3 = SimpleScene<float, uint64_t, 3u>;
211using SimpleScene64d3 = SimpleScene<double, uint64_t, 3u>;
212
213} // namespace lagrange::scene
A general purpose polygonal mesh class.
Definition SurfaceMesh.h:66
A lightweight non-owning reference to a callable.
Definition function_ref.h:47
Simple scene container for instanced meshes.
Definition SimpleScene.h:62
MeshInstance< Scalar, Index, Dimension > InstanceType
Instance type.
Definition SimpleScene.h:68
static constexpr size_t Dim
Definition SimpleScene.h:74
typename InstanceType::AffineTransform AffineTransform
Affine transform matrix.
Definition SimpleScene.h:71
Index get_num_instances(Index mesh_index) const
Gets the number of instances for a given mesh.
Definition SimpleScene.h:91
InstanceType & ref_instance(Index mesh_index, Index instance_index)
Get a reference to a mesh instance in the scene.
Definition SimpleScene.h:142
Index add_mesh(MeshType mesh)
Adds a mesh to the scene, possibly with existing instances.
Definition SimpleScene.cpp:37
MeshType & ref_mesh(Index mesh_index)
Gets a modifiable reference to a mesh in a scene.
Definition SimpleScene.h:119
void foreach_instances_for_mesh(Index mesh_index, function_ref< void(const InstanceType &)> func) const
Iterates over all instances of a specific mesh.
Definition SimpleScene.cpp:61
void foreach_instances(function_ref< void(const InstanceType &)> func) const
Iterates over all instances of the scene.
Definition SimpleScene.cpp:71
void reserve_meshes(Index num_meshes)
Pre-allocate a number of meshes in the scene.
Definition SimpleScene.cpp:30
const InstanceType & get_instance(Index mesh_index, Index instance_index) const
Get a const reference to a mesh instance in the scene.
Definition SimpleScene.h:129
std::vector< std::vector< InstanceType > > m_instances
Definition SimpleScene.h:201
std::vector< MeshType > m_meshes
Definition SimpleScene.h:198
const MeshType & get_mesh(Index mesh_index) const
Gets a const reference to a mesh in the scene.
Definition SimpleScene.h:110
Index get_num_meshes() const
Gets the number of meshes in the scene.
Definition SimpleScene.h:82
void reserve_instances(Index mesh_index, Index num_instances)
Pre-allocate a number of instances for a given mesh.
Definition SimpleScene.cpp:46
Index compute_num_instances() const
Calculates the total number instances for all meshes in the scene.
Definition SimpleScene.cpp:20
SurfaceMesh< Scalar, Index > MeshType
Mesh type.
Definition SimpleScene.h:65
Index add_instance(InstanceType instance)
Adds a new instance of an existing mesh.
Definition SimpleScene.cpp:52
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
A single mesh instance in a scene.
Definition SimpleScene.h:36
static constexpr size_t Dim
Definition SimpleScene.h:50
AffineTransform transform
Definition SimpleScene.h:44
Eigen::Transform< Scalar, static_cast< int >(Dimension), Eigen::Affine > AffineTransform
Affine transformation matrix.
Definition SimpleScene.h:38