16#include <lagrange/Attribute.h>
17#include <lagrange/AttributeTypes.h>
18#include <lagrange/ExactPredicatesShewchuk.h>
19#include <lagrange/IndexedAttribute.h>
20#include <lagrange/Logger.h>
21#include <lagrange/SurfaceMeshTypes.h>
22#include <lagrange/cast_attribute.h>
23#include <lagrange/find_matching_attributes.h>
24#include <lagrange/internal/get_uv_attribute.h>
25#include <lagrange/map_attribute.h>
26#include <lagrange/solver/DirectSolver.h>
27#include <lagrange/triangulate_polygonal_facets.h>
28#include <lagrange/utils/Error.h>
29#include <lagrange/utils/fmt_eigen.h>
30#include <lagrange/views.h>
31#include <lagrange/weld_indexed_attribute.h>
34#include "ThreadPool.h"
35#define MULTI_THREADING_INCLUDED
36using namespace lagrange::texproc::threadpool;
39#include <lagrange/utils/warnoff.h>
40#include <Misha/RegularGrid.h>
41#include <lagrange/utils/warnon.h>
44#include <Eigen/Sparse>
48namespace lagrange::texproc {
52using namespace MishaK;
55template <
typename T,
unsigned N>
56using Vector = MishaK::Point<T, N>;
59static const unsigned int Dim = 3;
62static const unsigned int K = 2;
65using Solver = lagrange::solver::SolverLDLT<Eigen::SparseMatrix<double>>;
71template <
unsigned int NumChannels,
typename ValueType>
73 image::experimental::View3D<ValueType> texture,
76 unsigned int num_channels =
static_cast<unsigned int>(texture.extent(2));
77 if (num_channels != NumChannels)
la_debug_assert(
"Number of channels don't match");
80 grid.resize(texture.extent(0), texture.extent(1));
81 for (
unsigned int j = 0; j < grid.res(1); j++) {
82 for (
unsigned int i = 0; i < grid.res(0); i++) {
83 for (
unsigned int c = 0; c < NumChannels; c++) {
84 grid(i, j)[c] = texture(i, j, c);
90template <
unsigned int NumChannels,
typename ValueType>
93 image::experimental::View3D<ValueType> texture)
96 for (
unsigned int j = 0; j < grid.res(1); j++) {
97 for (
unsigned int i = 0; i < grid.res(0); i++) {
98 for (
unsigned int c = 0; c < NumChannels; c++) {
99 texture(i, j, c) = grid(i, j)[c];
105template <
typename ValueType>
107 const RegularGrid<K, double>& grid,
108 image::experimental::View3D<ValueType> texture)
111 for (
unsigned int j = 0; j < grid.res(1); j++) {
112 for (
unsigned int i = 0; i < grid.res(0); i++) {
113 texture(i, j, 0) = grid(i, j);
118template <
typename Scalar,
typename Index>
119void check_for_flipped_uv(
const SurfaceMesh<Scalar, Index>& mesh,
AttributeId id)
124 const auto& uv_attr = mesh.template get_indexed_attribute<Scalar>(
id);
127 return {uv_values, uv_indices};
129 const auto& uv_attr = mesh.template get_attribute<Scalar>(
id);
133 "UV attribute must be per-vertex or per-corner.");
139 : std::optional<ConstRowMatrixView<Index>>(
facet_view(mesh))};
143 auto uv_index = [&](Index f,
unsigned int k) {
144 if (uv_mesh.second.has_value()) {
145 return (*uv_mesh.second)(f, k);
151 ExactPredicatesShewchuk predicates;
153 Eigen::RowVector2d p0 = uv_mesh.first.row(uv_index(f, 0)).template
cast<double>();
154 Eigen::RowVector2d p1 = uv_mesh.first.row(uv_index(f, 1)).template
cast<double>();
155 Eigen::RowVector2d p2 = uv_mesh.first.row(uv_index(f, 2)).template
cast<double>();
156 auto r = predicates.
orient2D(p0.data(), p1.data(), p2.data());
160 "The input mesh has flipped UVs:\n p0=({:.3g})\n p1=({:.3g})\n p2=("
162 "Please fix the input mesh before proceeding.",
165 fmt::join(p2,
", ")));
193template <
typename Scalar>
198 double epsilon = 1e-4)
200 if (std::abs(epsilon) < std::numeric_limits<double>().denorm_min()) {
204 Scalar jitter_scale =
static_cast<Scalar>(epsilon / std::max<unsigned int>(width, height));
206 std::uniform_real_distribution<Scalar> dist(-jitter_scale, jitter_scale);
207 for (
auto& x : texcoords_buffer) {
212template <
typename Scalar,
typename Index>
219 size_t num_simplices()
const {
return static_cast<size_t>(mesh.get_num_facets()); }
220 size_t num_vertices()
const {
return static_cast<size_t>(mesh.get_num_vertices()); }
221 size_t num_texcoords()
const {
return texcoords.size() / K; }
226 for (
unsigned int d = 0; d < Dim; d++) {
227 p[d] =
static_cast<double>(vertices[i * Dim + d]);
235 for (
unsigned int k = 0; k < K; k++) {
236 q[k] =
static_cast<double>(texcoords[i * K + k]);
241 int vertex_index(
size_t f,
unsigned int k)
const
243 return static_cast<int>(vertex_indices[f * (K + 1) + k]);
246 int texture_index(
size_t f,
unsigned int k)
const
248 switch (texture_element) {
252 default:
la_debug_assert(
"Unsupported texture element type");
return 0;
256 Simplex<double, K, K> simplex_texcoords(
size_t f)
const
258 Simplex<double, K, K> s;
259 for (
unsigned int k = 0; k <= K; k++) {
260 s[k] = texcoord(texture_index(f, k));
265 Simplex<double, Dim, K> simplex_vertices(
size_t f)
const
267 Simplex<double, Dim, K> s;
268 for (
unsigned int k = 0; k <= K; k++) {
269 s[k] = vertex(vertex_index(f, k));
274 SimplexIndex<K> facet_indices(
size_t f)
const
276 SimplexIndex<K> simplex;
277 for (
unsigned int k = 0; k <= K; ++k) {
278 simplex[k] =
static_cast<int>(vertex_indices[f * (K + 1) + k]);
291template <
typename Scalar,
typename Index>
294 bool needs_indexed_texcoords =
true,
295 bool check_flipped_uv =
true)
307 texcoord_id = res.value();
312 if (!_mesh.template is_attribute_type<Scalar>(texcoord_id)) {
314 "Input uv coordinates do not have the same scalar type as the input points. Casting "
320 if (needs_indexed_texcoords &&
322 logger().warn(
"UV coordinates are not indexed. Welding.");
324 weld_indexed_attribute(_mesh, texcoord_id);
330 "Numer of corners doesn't match the number of simplices");
332 if (check_flipped_uv) {
333 check_for_flipped_uv(_mesh, texcoord_id);
339 auto& uv_attr = _mesh.template ref_indexed_attribute<Scalar>(texcoord_id);
340 wrapper.texcoords = uv_attr.values().ref_all();
341 wrapper.texture_indices = uv_attr.indices().get_all();
344 auto& uv_attr = _mesh.template ref_attribute<Scalar>(texcoord_id);
345 wrapper.texcoords = uv_attr.ref_all();
346 wrapper.texture_indices = {};
347 wrapper.texture_element = uv_attr.get_element_type();
355template <
typename Scalar,
typename Index>
358 static_assert(
sizeof(std::array<Scalar, 2>) == 2 *
sizeof(
Scalar));
360 reinterpret_cast<std::array<Scalar, 2>*
>(wrapper.texcoords.data()),
361 wrapper.num_texcoords());
363 padding = Padding::init<Scalar>(width, height, texcoords);
364 padding.pad(width, height, texcoords);
AttributeElement get_element_type() const
Gets the attribute element type.
Definition Attribute.h:101
lagrange::span< const ValueType > get_all() const
Returns a read-only view of the buffer spanning num elements x num channels.
Definition Attribute.cpp:512
virtual short orient2D(double p1[2], double p2[2], double p3[2]) const
Exact 2D orientation test.
Definition ExactPredicatesShewchuk.cpp:37
A general purpose polygonal mesh class.
Definition SurfaceMesh.h:66
const AttributeBase & get_attribute_base(std::string_view name) const
Gets a read-only reference to the base class of attribute given its name.
Definition SurfaceMesh.cpp:1270
bool is_attribute_indexed(std::string_view name) const
Determines whether the specified attribute is indexed.
Definition SurfaceMesh.cpp:1229
const Attribute< Index > & get_corner_to_vertex() const
Gets a read-only reference to the corner -> vertex id attribute.
Definition SurfaceMesh.cpp:1393
Index get_num_facets() const
Retrieves the number of facets.
Definition SurfaceMesh.h:678
const Attribute< Scalar > & get_vertex_to_position() const
Gets a read-only reference to the vertex -> positions attribute.
Definition SurfaceMesh.cpp:1381
Index get_num_corners() const
Retrieves the number of corners.
Definition SurfaceMesh.h:685
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
AttributeId map_attribute_in_place(SurfaceMesh< Scalar, Index > &mesh, AttributeId id, AttributeElement new_element)
Map attribute values to a different element type.
Definition map_attribute.cpp:270
uint32_t AttributeId
Identified to be used to access an attribute.
Definition AttributeFwd.h:73
AttributeElement
Type of element to which the attribute is attached.
Definition AttributeFwd.h:26
@ UV
Mesh attribute must have exactly 2 channels.
Definition AttributeFwd.h:62
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
@ Value
Values that are not attached to a specific element.
Definition AttributeFwd.h:42
@ Indexed
Indexed mesh attributes.
Definition AttributeFwd.h:45
@ Corner
Per-corner mesh attributes.
Definition AttributeFwd.h:37
@ Vertex
Per-vertex mesh attributes.
Definition AttributeFwd.h:28
AttributeId cast_attribute_in_place(SurfaceMesh< Scalar, Index > &mesh, AttributeId attribute_id)
Cast an attribute in place to a different value type.
Definition cast_attribute.cpp:68
std::optional< AttributeId > find_matching_attribute(const SurfaceMesh< Scalar, Index > &mesh, const AttributeMatcher &options)
Finds the first attribute with the specified usage/element type/number of channels.
Definition find_matching_attributes.cpp:37
void triangulate_polygonal_facets(SurfaceMesh< Scalar, Index > &mesh, const TriangulationOptions &options={})
Triangulate polygonal facets of a mesh using a prescribed set of rules.
Definition triangulate_polygonal_facets.cpp:520
SurfaceMesh< ToScalar, ToIndex > cast(const SurfaceMesh< FromScalar, FromIndex > &source_mesh, const AttributeFilter &convertible_attributes={}, std::vector< std::string > *converted_attributes_names=nullptr)
Cast a mesh to a mesh of different scalar and/or index type.
ConstRowMatrixView< ValueType > matrix_view(const Attribute< ValueType > &attribute)
Returns a read-only view of a given attribute in the form of an Eigen matrix.
Definition views.cpp:35
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Type alias for one-dimensional column Eigen vectors.
Definition views.h:79
ConstRowMatrixView< ValueType > reshaped_view(const Attribute< ValueType > &attribute, size_t num_cols)
Returns a read-only view of a given single-channel attribute in the form of an Eigen matrix with a pr...
Definition views.cpp:71
ConstRowMatrixView< Index > facet_view(const SurfaceMesh< Scalar, Index > &mesh)
Returns a read-only view of a mesh facets in the form of an Eigen matrix.
Definition views.cpp:170
const Eigen::Map< const RowMatrix< Scalar >, Eigen::Unaligned > ConstRowMatrixView
Type alias for row-major const matrix view.
Definition views.h:75
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
#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
@ Error
Throw an error if collision is detected.
Definition MappingPolicy.h:24
Definition mesh_utils.h:214