Lagrange
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
41 Index mesh_index = invalid<Index>();
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
101 Index compute_num_instances() const;
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
212
213} // namespace lagrange::scene
Definition: Mesh.h:48
A lightweight non-owning reference to a callable.
Definition: function_ref.h:47
Simple scene container for instanced meshes.
Definition: SimpleScene.h:62
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
MeshType & ref_mesh(Index mesh_index)
Gets a modifiable reference to a mesh in a scene.
Definition: SimpleScene.h:119
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
List of mesh instances in the scene. Stored as a list of instance per parent mesh.
Definition: SimpleScene.h:201
std::vector< MeshType > m_meshes
List of meshes in the scene.
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
A single mesh instance in a scene.
Definition: SimpleScene.h:36
Eigen::Transform< Scalar, static_cast< int >(Dimension), Eigen::Affine > AffineTransform
Affine transformation matrix.
Definition: SimpleScene.h:38