Lagrange
Loading...
Searching...
No Matches
primitive_utils.h
1/*
2 * Copyright 2025 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
13#include <lagrange/Logger.h>
14#include <lagrange/primitive/SemanticLabel.h>
15#include <lagrange/transform_mesh.h>
16#include <lagrange/utils/assert.h>
17#include <lagrange/uv_mesh.h>
18#include <lagrange/views.h>
19
20#include <algorithm>
21#include <numeric>
22#include <vector>
23
24#include <Eigen/Core>
25
26namespace lagrange::primitive {
27
39template <typename Scalar>
40std::vector<Scalar> generate_ring(Scalar radius, size_t num_segments, Scalar angle_offset = 0)
41{
42 std::vector<Scalar> ring(num_segments * 2 + 2);
43 for (size_t i = 0; i < num_segments; ++i) {
44 Scalar angle =
45 static_cast<Scalar>(i) * (2 * lagrange::internal::pi / num_segments) + angle_offset;
46 ring[i * 2] = radius * std::cos(angle);
47 ring[i * 2 + 1] = radius * std::sin(angle);
48 }
49
50 // Close the ring by repeating the first point
51 ring[num_segments * 2] = ring[0];
52 ring[num_segments * 2 + 1] = ring[1];
53
54 return ring;
55}
56
67template <typename Scalar, typename Index>
68void add_semantic_label(
69 SurfaceMesh<Scalar, Index>& mesh,
70 std::string_view name,
71 const SemanticLabel label)
72{
73 mesh.template create_attribute<uint8_t>(
74 name,
76 1,
78 auto values = attribute_vector_ref<uint8_t>(mesh, name);
79 values.setConstant(static_cast<uint8_t>(label));
80}
81
92template <typename Scalar, typename Index>
93void normalize_uv(
94 SurfaceMesh<Scalar, Index>& mesh,
95 Eigen::Matrix<Scalar, 1, 2> min_uv,
96 Eigen::Matrix<Scalar, 1, 2> max_uv)
97{
98 auto uv_mesh = uv_mesh_ref(mesh);
99 auto uvs = vertex_ref(uv_mesh);
100
101 Eigen::Matrix<Scalar, 1, 2> bbox_min = uvs.colwise().minCoeff();
102 Eigen::Matrix<Scalar, 1, 2> bbox_max = uvs.colwise().maxCoeff();
103
104 uvs =
105 (((uvs.rowwise() - bbox_min).array().rowwise() / (bbox_max - bbox_min).array()).rowwise() *
106 (max_uv - min_uv).array())
107 .rowwise() +
108 min_uv.array();
109}
110
119template <typename Scalar, typename Index, typename ValueType>
120void center_mesh(SurfaceMesh<Scalar, Index>& mesh, std::array<ValueType, 3> center)
121{
122 Eigen::Transform<Scalar, 3, Eigen::Affine> transform;
123 transform.setIdentity();
124 transform.translate(
125 Eigen::Matrix<Scalar, 3, 1>(
126 static_cast<Scalar>(center[0]),
127 static_cast<Scalar>(center[1]),
128 static_cast<Scalar>(center[2])));
129 transform_mesh(mesh, transform);
130}
131
146template <typename Scalar, typename Index>
147SurfaceMesh<Scalar, Index> boundary_to_mesh(
148 span<Scalar> boundary,
149 std::string_view uv_attribute_name,
150 std::string_view normal_attribute_name,
151 bool flipped = false)
152{
153 la_debug_assert(boundary.size() % 2 == 0);
154
155 Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, 2, Eigen::RowMajor>> boundary_map(
156 boundary.data(),
157 boundary.size() / 2,
158 2);
159
160 Index num_vertices = static_cast<Index>(boundary.size() / 2);
161 SurfaceMesh<Scalar, Index> mesh;
162 mesh.add_vertices(num_vertices);
163 auto vertices = vertex_ref(mesh);
164 vertices.setZero();
165 vertices.template leftCols<2>() = boundary_map;
166
167 std::vector<Index> indices(num_vertices);
168 std::iota(indices.begin(), indices.end(), 0);
169 if (flipped) {
170 std::reverse(indices.begin(), indices.end());
171 }
172 mesh.add_polygon({indices.data(), indices.size()});
173
174 // Generate UV coordinates
175 auto uv_attr_id = mesh.template create_attribute<Scalar>(
176 uv_attribute_name,
178 2,
180 auto& uv_attr = mesh.template ref_indexed_attribute<Scalar>(uv_attr_id);
181 auto& uv_values = uv_attr.values();
182 auto& uv_indices = uv_attr.indices();
183 uv_values.resize_elements(num_vertices);
184 auto uv_indices_ref = uv_indices.ref_all();
185
186 matrix_ref(uv_values) = vertices.template leftCols<2>();
187 if (flipped) {
188 matrix_ref(uv_values).col(0) *= -1; // Flip UVs if the mesh is flipped
189 }
190 std::copy(indices.begin(), indices.end(), uv_indices_ref.begin());
191
192 // Generate normals
193 auto normal_attr_id = mesh.template create_attribute<Scalar>(
194 normal_attribute_name,
196 3,
198 auto& normal_attr = mesh.template ref_indexed_attribute<Scalar>(normal_attr_id);
199 auto& normal_values = normal_attr.values();
200 auto& normal_indices = normal_attr.indices();
201 normal_values.resize_elements(1);
202
203 matrix_ref(normal_values).row(0) << 0, 0, (flipped ? -1 : 1);
204 vector_ref(normal_indices).setZero(); // All facets share the same normal
205
206 return mesh;
207}
208
209} // namespace lagrange::primitive
void add_vertices(Index num_vertices, span< const Scalar > coordinates={})
Adds multiple vertices to the mesh.
Definition SurfaceMesh.cpp:1579
void add_polygon(Index facet_size)
Adds a single (uninitialized) polygonal facet to the mesh.
Definition SurfaceMesh.cpp:1632
@ Normal
Mesh attribute can have dim or dim + 1 channels.
Definition AttributeFwd.h:58
@ UV
Mesh attribute must have exactly 2 channels.
Definition AttributeFwd.h:62
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
@ Indexed
Indexed mesh attributes.
Definition AttributeFwd.h:45
@ Facet
Per-facet mesh attributes.
Definition AttributeFwd.h:31
void transform_mesh(SurfaceMesh< Scalar, Index > &mesh, const Eigen::Transform< Scalar, Dimension, Eigen::Affine > &transform, const TransformOptions &options={})
Apply an affine transform to a mesh in-place.
Definition transform_mesh.cpp:199
VectorView< ValueType > vector_ref(Attribute< ValueType > &attribute)
Returns a writable view of a scalar attribute in the form of an Eigen vector.
Definition views.cpp:44
VectorView< ValueType > attribute_vector_ref(SurfaceMesh< Scalar, Index > &mesh, std::string_view name)
Returns a writable view of a mesh attribute in the form of an Eigen vector.
Definition views.cpp:104
RowMatrixView< Scalar > vertex_ref(SurfaceMesh< Scalar, Index > &mesh)
Returns a writable view of the mesh vertices in the form of an Eigen matrix.
Definition views.cpp:150
RowMatrixView< ValueType > matrix_ref(Attribute< ValueType > &attribute)
Returns a writable view of a given attribute in the form of an Eigen matrix.
Definition views.cpp:26
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:194
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
SemanticLabel
Semantic labels are used to classify different parts of a primitive mesh.
Definition SemanticLabel.h:22
SurfaceMesh< UVScalar, Index > uv_mesh_ref(SurfaceMesh< Scalar, Index > &mesh, const UVMeshOptions &options={})
Extract a UV mesh reference from an input mesh.
Definition uv_mesh.cpp:21