Lagrange
mesh.h
1/*
2 * Copyright 2021 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/ui/Entity.h>
15#include <lagrange/ui/components/MeshData.h>
16#include <lagrange/ui/components/MeshGeometry.h>
17#include <lagrange/ui/types/AABB.h>
18#include <lagrange/ui/types/Frustum.h>
19#include <lagrange/ui/types/RayFacetHit.h>
20#include <lagrange/ui/utils/math.h>
21#include <lagrange/ui/api.h>
22#include <optional>
23
27
28namespace lagrange {
29namespace ui {
30
31struct GPUBuffer;
32class Camera;
33
34template <typename MeshType>
35Entity register_mesh(Registry& r, std::shared_ptr<MeshType> mesh)
36{
37 auto e = r.create();
38 MeshData d;
39 d.mesh = mesh;
40 d.type = entt::type_id<MeshType>();
41 r.emplace<MeshData>(e, std::move(d));
42 return e;
43}
44
45template <typename MeshType>
46Entity register_mesh(Registry& r, std::unique_ptr<MeshType> mesh)
47{
48 return register_mesh(r, std::shared_ptr<MeshType>(mesh.release()));
49}
50
51
53// Getters
55
56
57template <typename V, typename F>
58lagrange::Mesh<V, F>& cast_mesh(MeshData& mesh_data);
59template <typename V, typename F>
60lagrange::Mesh<V, F>& cast_mesh(MeshData& mesh_data);
61template <typename MeshType>
62MeshType& get_mesh(Registry& r, Entity e);
63
64LA_UI_API MeshData& get_mesh_data(Registry& r, Entity e);
65LA_UI_API const MeshData& get_mesh_data(const Registry& r, Entity e);
66
67LA_UI_API bool has_mesh_component(const Registry& r, Entity e);
68
69LA_UI_API size_t get_num_vertices(const MeshData& d);
70LA_UI_API size_t get_num_facets(const MeshData& d);
71LA_UI_API size_t get_num_edges(const MeshData& d);
72LA_UI_API RowMajorMatrixXf get_mesh_vertices(const MeshData& d);
73LA_UI_API RowMajorMatrixXi get_mesh_facets(const MeshData& d);
74LA_UI_API RowMajorMatrixXf get_mesh_vertex_attribute(const MeshData& d, const std::string& name);
75LA_UI_API RowMajorMatrixXf get_mesh_corner_attribute(const MeshData& d, const std::string& name);
76LA_UI_API RowMajorMatrixXf get_mesh_facet_attribute(const MeshData& d, const std::string& name);
77LA_UI_API RowMajorMatrixXf get_mesh_edge_attribute(const MeshData& d, const std::string& name);
78LA_UI_API RowMajorMatrixXf get_mesh_attribute(const MeshData& d, IndexingMode mode, const std::string& name);
79std::pair<Eigen::VectorXf, Eigen::VectorXf>
80LA_UI_API get_mesh_attribute_range(const MeshData& d, IndexingMode mode, const std::string& name);
81LA_UI_API AABB get_mesh_bounds(const MeshData& d);
82
83
85// Ensure existence of mesh attributes for rendering
87
88LA_UI_API void ensure_uv(MeshData& d);
89LA_UI_API void ensure_normal(MeshData& d);
90LA_UI_API void ensure_tangent_bitangent(MeshData& d);
91LA_UI_API void ensure_is_selected_attribute(MeshData& d);
92LA_UI_API void map_indexed_attribute_to_corner_attribute(MeshData& d, const std::string& name);
93LA_UI_API void map_corner_attribute_to_vertex_attribute(MeshData& d, const std::string& name);
94
96// Mesh to GPU upload
98
99LA_UI_API void upload_mesh_vertices(const MeshData& d, GPUBuffer& gpu);
100LA_UI_API void upload_mesh_triangles(const MeshData& d, GPUBuffer& gpu);
101
102LA_UI_API void upload_mesh_vertex_attribute(const MeshData& d, const RowMajorMatrixXf& data, GPUBuffer& gpu);
103LA_UI_API void upload_mesh_corner_attribute(const MeshData& d, const RowMajorMatrixXf& data, GPUBuffer& gpu);
104LA_UI_API void upload_mesh_facet_attribute(const MeshData& d, const RowMajorMatrixXf& data, GPUBuffer& gpu);
105LA_UI_API void upload_mesh_edge_attribute(const MeshData& d, const RowMajorMatrixXf& data, GPUBuffer& gpu);
106
107LA_UI_API std::unordered_map<entt::id_type, std::shared_ptr<GPUBuffer>> upload_submesh_indices(
108 const MeshData& d,
109 const std::string& facet_attrib_name);
110
112// Has attribute
114LA_UI_API bool has_mesh_vertex_attribute(const MeshData& d, const std::string& name);
115LA_UI_API bool has_mesh_corner_attribute(const MeshData& d, const std::string& name);
116LA_UI_API bool has_mesh_facet_attribute(const MeshData& d, const std::string& name);
117LA_UI_API bool has_mesh_edge_attribute(const MeshData& d, const std::string& name);
118LA_UI_API bool has_mesh_indexed_attribute(const MeshData& d, const std::string& name);
119
121// Picking
123
125LA_UI_API std::optional<RayFacetHit>
126intersect_ray(const MeshData& d, const Eigen::Vector3f& origin, const Eigen::Vector3f& dir);
127LA_UI_API bool select_facets_in_frustum(MeshData& d, SelectionBehavior sel_behavior, const Frustum& frustum);
128LA_UI_API void select_vertices_in_frustum(
129 MeshData& d,
130 SelectionBehavior sel_behavior,
131 const Frustum& frustum);
132LA_UI_API void select_edges_in_frustum(MeshData& d, SelectionBehavior sel_behavior, const Frustum& frustum);
133LA_UI_API void propagate_corner_selection(MeshData& d, const std::string& attrib_name);
134LA_UI_API void propagate_vertex_selection(MeshData& d, const std::string& attrib_name);
135LA_UI_API void propagate_facet_selection(MeshData& d, const std::string& attrib_name);
136LA_UI_API void combine_vertex_and_corner_selection(MeshData& d, const std::string& attrib_name);
137LA_UI_API void select_facets_by_color(
138 MeshData& d,
139 const std::string& attrib_name,
140 SelectionBehavior sel_behavior,
141 const unsigned char* color_bytes,
142 size_t colors_byte_size);
143LA_UI_API void select_edges_by_color(
144 MeshData& d,
145 const std::string& attrib_name,
146 SelectionBehavior sel_behavior,
147 const unsigned char* color_bytes,
148 size_t colors_byte_size);
149LA_UI_API void select_vertices_by_color(
150 MeshData& d,
151 const std::string& attrib_name,
152 SelectionBehavior sel_behavior,
153 const unsigned char* color_bytes,
154 size_t colors_byte_size);
155LA_UI_API void select_facets(
156 MeshData& d,
157 SelectionBehavior sel_behavior,
158 const std::vector<int>& facet_indices);
159
160LA_UI_API void filter_closest_vertex(
161 MeshData& d,
162 const std::string& attrib_name,
163 SelectionBehavior sel_behavior,
164 const Camera& camera,
165 const Eigen::Vector2i& viewport_pos);
166
167template <typename V, typename F>
168lagrange::Mesh<V, F>& cast_mesh(const Registry& r, Entity e)
169{
170 return cast_mesh<V, F>(r.get<const MeshData>(e));
171}
172
173
174template <typename V, typename F>
175lagrange::Mesh<V, F>& cast_mesh(MeshData& mesh_data)
176{
179 mesh_data.type == entt::type_id<MeshType>() && mesh_data.mesh,
180 "Incorrect mesh type");
181 return reinterpret_cast<MeshType&>(*mesh_data.mesh);
182}
183
184template <typename MeshType>
185MeshType& get_mesh(Registry& r, Entity e)
186{
187 la_runtime_assert(r.all_of<MeshData>(e), "No MeshData component");
188 return cast_mesh<typename MeshType::VertexArray, typename MeshType::FacetArray>(
189 r.get<MeshData>(e));
190}
191
192inline Entity get_mesh_entity(Registry& r, Entity e)
193{
194 if (r.all_of<MeshData>(e)) return e;
195 if (r.all_of<MeshGeometry>(e)) return r.get<MeshGeometry>(e).entity;
196 return NullEntity;
197}
198
199} // namespace ui
200} // namespace lagrange
Definition: Mesh.h:48
bool select_facets_in_frustum(SurfaceMesh< Scalar, Index > &mesh, const Frustum< Scalar > &frustum, const FrustumSelectionOptions &options={})
Select all facets that intersect the cone/frustrum bounded by 4 planes defined by (n_i,...
Definition: select_facets_in_frustum.cpp:32
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
LA_UI_API std::optional< std::pair< Entity, RayFacetHit > > intersect_ray(Registry &r, const Eigen::Vector3f &origin, const Eigen::Vector3f &dir, ui::Entity root=ui::NullEntity, Layer visible_layers=Layer(true), Layer hidden_layers=Layer(false))
Intersect ray with meshes in root's hierarchy Returns a pair of intersected entity and the correspond...
Definition: default_entities.cpp:502
Main namespace for Lagrange.
Definition: AABBIGL.h:30