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 if (name.empty()) {
74 return;
75 }
76 mesh.template create_attribute<uint8_t>(
77 name,
79 1,
81 auto values = attribute_vector_ref<uint8_t>(mesh, name);
82 values.setConstant(static_cast<uint8_t>(label));
83}
84
95template <typename Scalar, typename Index>
96void normalize_uv(
97 SurfaceMesh<Scalar, Index>& mesh,
98 Eigen::Matrix<Scalar, 1, 2> min_uv,
99 Eigen::Matrix<Scalar, 1, 2> max_uv)
100{
101 auto uv_mesh = uv_mesh_ref(mesh);
102 auto uvs = vertex_ref(uv_mesh);
103
104 Eigen::Matrix<Scalar, 1, 2> bbox_min = uvs.colwise().minCoeff();
105 Eigen::Matrix<Scalar, 1, 2> bbox_max = uvs.colwise().maxCoeff();
106
107 uvs =
108 (((uvs.rowwise() - bbox_min).array().rowwise() / (bbox_max - bbox_min).array()).rowwise() *
109 (max_uv - min_uv).array())
110 .rowwise() +
111 min_uv.array();
112}
113
122template <typename Scalar, typename Index, typename ValueType>
123void center_mesh(SurfaceMesh<Scalar, Index>& mesh, std::array<ValueType, 3> center)
124{
125 Eigen::Transform<Scalar, 3, Eigen::Affine> transform;
126 transform.setIdentity();
127 transform.translate(
128 Eigen::Matrix<Scalar, 3, 1>(
129 static_cast<Scalar>(center[0]),
130 static_cast<Scalar>(center[1]),
131 static_cast<Scalar>(center[2])));
132 transform_mesh(mesh, transform);
133}
134
149template <typename Scalar, typename Index>
150SurfaceMesh<Scalar, Index> boundary_to_mesh(
151 span<Scalar> boundary,
152 std::string_view uv_attribute_name,
153 std::string_view normal_attribute_name,
154 bool flipped = false)
155{
156 la_debug_assert(boundary.size() % 2 == 0);
157
158 Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, 2, Eigen::RowMajor>> boundary_map(
159 boundary.data(),
160 boundary.size() / 2,
161 2);
162
163 Index num_vertices = static_cast<Index>(boundary.size() / 2);
164 SurfaceMesh<Scalar, Index> mesh;
165 mesh.add_vertices(num_vertices);
166 auto vertices = vertex_ref(mesh);
167 vertices.setZero();
168 vertices.template leftCols<2>() = boundary_map;
169
170 std::vector<Index> indices(num_vertices);
171 std::iota(indices.begin(), indices.end(), 0);
172 if (flipped) {
173 std::reverse(indices.begin(), indices.end());
174 }
175 mesh.add_polygon({indices.data(), indices.size()});
176
177 // Generate UV coordinates
178 if (!uv_attribute_name.empty()) {
179 auto uv_attr_id = mesh.template create_attribute<Scalar>(
180 uv_attribute_name,
182 2,
184 auto& uv_attr = mesh.template ref_indexed_attribute<Scalar>(uv_attr_id);
185 auto& uv_values = uv_attr.values();
186 auto& uv_indices = uv_attr.indices();
187 uv_values.resize_elements(num_vertices);
188 auto uv_indices_ref = uv_indices.ref_all();
189
190 matrix_ref(uv_values) = vertices.template leftCols<2>();
191 if (flipped) {
192 matrix_ref(uv_values).col(0) *= -1; // Flip UVs if the mesh is flipped
193 }
194 std::copy(indices.begin(), indices.end(), uv_indices_ref.begin());
195 }
196
197 // Generate normals
198 if (!normal_attribute_name.empty()) {
199 auto normal_attr_id = mesh.template create_attribute<Scalar>(
200 normal_attribute_name,
202 3,
204 auto& normal_attr = mesh.template ref_indexed_attribute<Scalar>(normal_attr_id);
205 auto& normal_values = normal_attr.values();
206 auto& normal_indices = normal_attr.indices();
207 normal_values.resize_elements(1);
208
209 matrix_ref(normal_values).row(0) << 0, 0, (flipped ? -1 : 1);
210 vector_ref(normal_indices).setZero(); // All facets share the same normal
211 }
212
213 return mesh;
214}
215
216} // namespace lagrange::primitive
@ 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:137
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:40
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:195
::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