16#include <lagrange/Attribute.h>
17#include <lagrange/AttributeTypes.h>
18#include <lagrange/IndexedAttribute.h>
19#include <lagrange/Logger.h>
20#include <lagrange/SurfaceMeshTypes.h>
21#include <lagrange/cast_attribute.h>
22#include <lagrange/compute_uv_orientation.h>
23#include <lagrange/find_matching_attributes.h>
24#include <lagrange/map_attribute.h>
25#include <lagrange/solver/DirectSolver.h>
26#include <lagrange/triangulate_polygonal_facets.h>
27#include <lagrange/utils/Error.h>
28#include <lagrange/utils/fmt_eigen.h>
29#include <lagrange/views.h>
30#include <lagrange/weld_indexed_attribute.h>
33#include "ThreadPool.h"
34#define MULTI_THREADING_INCLUDED
35using namespace lagrange::texproc::threadpool;
38#include <lagrange/utils/warnoff.h>
39#include <Misha/RegularGrid.h>
40#include <Src/PreProcessing.h>
41#include <Src/GradientDomain.h>
42#include <lagrange/utils/warnon.h>
43#include <lagrange/utils/fmt/format.h>
44#include <lagrange/utils/fmt/join.h>
47#include <Eigen/Sparse>
51namespace lagrange::texproc {
55using namespace MishaK;
58template <
typename T,
unsigned N>
59using Vector = MishaK::Point<T, N>;
62static const unsigned int Dim = 3;
65static const unsigned int K = 2;
68using Solver = lagrange::solver::SolverLDLT<Eigen::SparseMatrix<double>>;
72enum class RequiresIndexedTexcoords { Yes, No };
73enum class CheckFlippedUV { Yes, No };
77template <
unsigned int NumChannels,
typename ValueType>
79 image::experimental::View3D<ValueType> texture,
82 unsigned int num_channels =
static_cast<unsigned int>(texture.extent(2));
83 if (num_channels != NumChannels)
la_debug_assert(
"Number of channels don't match");
86 grid.resize(texture.extent(0), texture.extent(1));
87 for (
unsigned int j = 0; j < grid.res(1); j++) {
88 for (
unsigned int i = 0; i < grid.res(0); i++) {
89 for (
unsigned int c = 0; c < NumChannels; c++) {
90 grid(i, j)[c] = texture(i, j, c);
96template <
unsigned int NumChannels,
typename ValueType>
99 image::experimental::View3D<ValueType> texture)
102 for (
unsigned int j = 0; j < grid.res(1); j++) {
103 for (
unsigned int i = 0; i < grid.res(0); i++) {
104 for (
unsigned int c = 0; c < NumChannels; c++) {
105 texture(i, j, c) = grid(i, j)[c];
111template <
typename ValueType>
113 const RegularGrid<K, double>& grid,
114 image::experimental::View3D<ValueType> texture)
117 for (
unsigned int j = 0; j < grid.res(1); j++) {
118 for (
unsigned int i = 0; i < grid.res(0); i++) {
119 texture(i, j, 0) = grid(i, j);
124template <
unsigned int NumChannels>
125void clamp_out_of_range(
127 const MishaK::TSP::GradientDomain<double>& gd,
128 std::pair<double, double>
range = {0.0, 1.0},
129 bool mark_out_of_range =
false)
131 size_t num_interior_out_of_range = 0;
132 size_t num_exterior_out_of_range = 0;
136 for (
unsigned int c = 0; c < NumChannels; c++) {
137 red[c] = (c == 0 ? 1.0 : 0.0);
138 green[c] = (c == 1 ? 1.0 : 0.0);
142 constexpr double eps = 1e-6;
143 for (
unsigned int c = 0; c < NumChannels; c++) {
144 if (p[c] <
range.first - eps || p[c] >
range.second + eps) {
151 for (
size_t n = 0; n < gd.numNodes(); ++n) {
152 const bool is_strictly_out = is_out_of_range(x[n]);
153 for (
unsigned int c = 0; c < NumChannels; c++) {
154 x[n][c] = std::clamp(x[n][c],
range.first,
range.second);
156 if (is_strictly_out) {
157 if (gd.isCovered(n)) {
158 num_interior_out_of_range++;
159 if (mark_out_of_range) {
163 num_exterior_out_of_range++;
164 if (mark_out_of_range) {
170 if (num_interior_out_of_range || num_exterior_out_of_range) {
172 "{} interior and {} exterior texels were out of range and have been clamped.",
173 num_interior_out_of_range,
174 num_exterior_out_of_range);
201template <
typename Scalar>
206 double epsilon = 1e-4)
208 if (std::abs(epsilon) < std::numeric_limits<double>().denorm_min()) {
212 Scalar jitter_scale =
static_cast<Scalar>(epsilon / std::max<unsigned int>(width, height));
214 std::uniform_real_distribution<Scalar> dist(-jitter_scale, jitter_scale);
215 for (
auto& x : texcoords_buffer) {
221template <
typename Scalar>
222Eigen::SparseMatrix<Scalar> laplacian_regularization(Eigen::SparseMatrix<Scalar> S,
Scalar weight)
224 if (std::abs(weight) > std::numeric_limits<Scalar>().denorm_min()) {
229 tbb::parallel_for(
size_t(0),
size_t(S.outerSize()), [&S, weight](
size_t c) {
231 for (typename Eigen::SparseMatrix<Scalar>::InnerIterator it(S, c); it; ++it) {
232 if (it.row() != it.col()) {
233 it.valueRef() -= weight;
237 for (
typename Eigen::SparseMatrix<Scalar>::InnerIterator it(S, c); it; ++it) {
238 if (it.row() == it.col()) {
239 it.valueRef() += weight * count;
248template <
typename Scalar,
typename Index>
255 size_t num_simplices()
const {
return static_cast<size_t>(mesh.get_num_facets()); }
256 size_t num_vertices()
const {
return static_cast<size_t>(mesh.get_num_vertices()); }
257 size_t num_texcoords()
const {
return texcoords.size() / K; }
262 for (
unsigned int d = 0; d < Dim; d++) {
263 p[d] =
static_cast<double>(vertices[i * Dim + d]);
271 for (
unsigned int k = 0; k < K; k++) {
272 q[k] =
static_cast<double>(texcoords[i * K + k]);
280 for (
unsigned int k = 0; k < K; k++) {
281 q[k] =
static_cast<double>(texcoords[i * K + k]);
287 int vertex_index(
size_t f,
unsigned int k)
const
289 return static_cast<int>(vertex_indices[f * (K + 1) + k]);
292 int texture_index(
size_t f,
unsigned int k)
const
294 switch (texture_element) {
298 default:
la_debug_assert(
"Unsupported texture element type");
return 0;
302 Simplex<double, K, K> simplex_texcoords(
size_t f)
const
304 Simplex<double, K, K> s;
305 for (
unsigned int k = 0; k <= K; k++) {
306 s[k] = texcoord(texture_index(f, k));
311 Simplex<double, K, K> vflipped_simplex_texcoords(
size_t f)
const
313 Simplex<double, K, K> s;
314 for (
unsigned int k = 0; k <= K; k++) {
315 s[k] = vflipped_texcoord(texture_index(f, k));
320 Simplex<double, Dim, K> simplex_vertices(
size_t f)
const
322 Simplex<double, Dim, K> s;
323 for (
unsigned int k = 0; k <= K; k++) {
324 s[k] = vertex(vertex_index(f, k));
329 SimplexIndex<K> facet_indices(
size_t f)
const
331 SimplexIndex<K> simplex;
332 for (
unsigned int k = 0; k <= K; ++k) {
333 simplex[k] =
static_cast<int>(vertex_indices[f * (K + 1) + k]);
346template <
typename Scalar,
typename Index>
349 RequiresIndexedTexcoords requires_indexed_texcoords,
350 CheckFlippedUV check_flipped_uv)
362 texcoord_id = res.value();
367 if (!_mesh.template is_attribute_type<Scalar>(texcoord_id)) {
369 "Input uv coordinates do not have the same scalar type as the input points. Casting "
375 if (requires_indexed_texcoords == RequiresIndexedTexcoords::Yes &&
377 logger().warn(
"UV coordinates are not indexed. Welding.");
379 weld_indexed_attribute(_mesh, texcoord_id);
385 "Number of corners doesn't match the number of simplices");
387 if (check_flipped_uv == CheckFlippedUV::Yes) {
389 UVOrientationOptions orient_options;
392 if (orient_counts.negative > 0) {
394 "The input mesh has {} flipped UV triangle(s). Please fix the input mesh "
395 "before proceeding.",
396 orient_counts.negative));
403 auto& uv_attr = _mesh.template ref_indexed_attribute<Scalar>(texcoord_id);
404 wrapper.texcoords = uv_attr.values().ref_all();
405 wrapper.texture_indices = uv_attr.indices().get_all();
408 auto& uv_attr = _mesh.template ref_attribute<Scalar>(texcoord_id);
409 wrapper.texcoords = uv_attr.ref_all();
410 wrapper.texture_indices = {};
411 wrapper.texture_element = uv_attr.get_element_type();
419template <
typename Scalar,
typename Index>
420Padding create_padding(MeshWrapper<Scalar, Index>& wrapper,
unsigned int width,
unsigned int height)
422 static_assert(
sizeof(std::array<Scalar, 2>) == 2 *
sizeof(
Scalar));
424 reinterpret_cast<std::array<Scalar, 2>*
>(wrapper.texcoords.data()),
425 wrapper.num_texcoords());
427 padding = Padding::init<Scalar>(width, height, texcoords);
428 padding.pad(width, height, texcoords);
AttributeElement get_element_type() const
Gets the attribute element type.
Definition Attribute.h:122
lagrange::span< const ValueType > get_all() const
Returns a read-only view of the buffer spanning num elements x num channels.
Definition Attribute.cpp:526
A general purpose polygonal mesh class.
Definition SurfaceMesh.h:73
std::string_view get_attribute_name(AttributeId id) const
Retrieve attribute name from its id.
Definition SurfaceMesh.cpp:358
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:1274
bool is_attribute_indexed(std::string_view name) const
Determines whether the specified attribute is indexed.
Definition SurfaceMesh.cpp:1233
const Attribute< Index > & get_corner_to_vertex() const
Gets a read-only reference to the corner -> vertex id attribute.
Definition SurfaceMesh.cpp:1397
Index get_num_facets() const
Retrieves the number of facets.
Definition SurfaceMesh.h:694
const Attribute< Scalar > & get_vertex_to_position() const
Gets a read-only reference to the vertex -> positions attribute.
Definition SurfaceMesh.cpp:1385
Index get_num_corners() const
Retrieves the number of corners.
Definition SurfaceMesh.h:701
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:292
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:533
UVOrientationCount compute_uv_orientation(SurfaceMesh< Scalar, Index > &mesh, const UVOrientationOptions &options={})
Compute a per-facet orientation attribute using Shewchuk's exact orient2D predicate.
Definition compute_uv_orientation.cpp:96
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Type alias for one-dimensional column Eigen vectors.
Definition views.h:79
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:175
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:195
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition range.h:176
::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
std::string_view uv_attribute_name
Input UV attribute name.
Definition compute_uv_orientation.h:30
Definition mesh_utils.h:250