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>
26namespace lagrange::primitive {
39template <
typename Scalar>
40std::vector<Scalar> generate_ring(
Scalar radius,
size_t num_segments,
Scalar angle_offset = 0)
42 std::vector<Scalar> ring(num_segments * 2 + 2);
43 for (
size_t i = 0; i < num_segments; ++i) {
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);
51 ring[num_segments * 2] = ring[0];
52 ring[num_segments * 2 + 1] = ring[1];
67template <
typename Scalar,
typename Index>
68void add_semantic_label(
69 SurfaceMesh<Scalar, Index>& mesh,
70 std::string_view name,
73 mesh.template create_attribute<uint8_t>(
79 values.setConstant(
static_cast<uint8_t
>(label));
92template <
typename Scalar,
typename Index>
94 SurfaceMesh<Scalar, Index>& mesh,
95 Eigen::Matrix<Scalar, 1, 2> min_uv,
96 Eigen::Matrix<Scalar, 1, 2> max_uv)
101 Eigen::Matrix<Scalar, 1, 2> bbox_min = uvs.colwise().minCoeff();
102 Eigen::Matrix<Scalar, 1, 2> bbox_max = uvs.colwise().maxCoeff();
105 (((uvs.rowwise() - bbox_min).array().rowwise() / (bbox_max - bbox_min).array()).rowwise() *
106 (max_uv - min_uv).array())
119template <
typename Scalar,
typename Index,
typename ValueType>
120void center_mesh(SurfaceMesh<Scalar, Index>& mesh, std::array<ValueType, 3> center)
122 Eigen::Transform<Scalar, 3, Eigen::Affine> transform;
123 transform.setIdentity();
125 Eigen::Matrix<Scalar, 3, 1>(
126 static_cast<Scalar>(center[0]),
127 static_cast<Scalar>(center[1]),
128 static_cast<Scalar>(center[2])));
146template <
typename Scalar,
typename Index>
147SurfaceMesh<Scalar, Index> boundary_to_mesh(
149 std::string_view uv_attribute_name,
150 std::string_view normal_attribute_name,
151 bool flipped =
false)
155 Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, 2, Eigen::RowMajor>> boundary_map(
160 Index num_vertices =
static_cast<Index
>(boundary.size() / 2);
161 SurfaceMesh<Scalar, Index> mesh;
165 vertices.template leftCols<2>() = boundary_map;
167 std::vector<Index> indices(num_vertices);
168 std::iota(indices.begin(), indices.end(), 0);
170 std::reverse(indices.begin(), indices.end());
172 mesh.
add_polygon({indices.data(), indices.size()});
175 auto uv_attr_id = mesh.template create_attribute<Scalar>(
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();
186 matrix_ref(uv_values) = vertices.template leftCols<2>();
190 std::copy(indices.begin(), indices.end(), uv_indices_ref.begin());
193 auto normal_attr_id = mesh.template create_attribute<Scalar>(
194 normal_attribute_name,
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);
203 matrix_ref(normal_values).row(0) << 0, 0, (flipped ? -1 : 1);
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