15#include <lagrange/Attribute.h>
16#include <lagrange/Logger.h>
17#include <lagrange/SurfaceMesh.h>
18#include <lagrange/foreach_attribute.h>
19#include <lagrange/map_attribute.h>
20#include <lagrange/views.h>
23#include <lagrange/utils/warnoff.h>
24#include <catch2/catch_test_macros.hpp>
25#include <lagrange/utils/warnon.h>
28namespace lagrange::testing {
48template <
typename ValueType1,
typename ValueType2,
typename Scalar,
typename Index>
49bool attribute_is_approx_equivalent(
55 std::string_view name =
"testing_attribute_is_approx_equivalent";
59 const auto& attr1 = mesh1.template get_attribute<ValueType1>(id1);
60 const auto& attr2 = mesh2.template get_attribute<ValueType2>(id2);
62 REQUIRE(attr1.get_usage() == attr2.get_usage());
63 REQUIRE(attr1.get_num_channels() == attr2.get_num_channels());
64 REQUIRE(attr1.get_num_elements() == attr2.get_num_elements());
69 constexpr Scalar rel_tol =
static_cast<Scalar
>(1e-6);
70 bool match = data1.template cast<Scalar>().isApprox(data2.template cast<Scalar>(), rel_tol);
91template <AttributeUsage usage,
typename Scalar,
typename Index>
92void ensure_approx_equivalent_usage(
97 using ValueType1 =
typename std::decay_t<
decltype(attr1)>::ValueType;
98 if (attr1.get_usage() != usage)
return;
101 bool has_equivalent =
false;
103 using ValueType2 =
typename std::decay_t<
decltype(attr2)>::ValueType;
104 if (attr2.get_usage() != usage)
return;
106 if (attribute_is_approx_equivalent<ValueType1, ValueType2>(mesh1, mesh2, id1, id2))
107 has_equivalent =
true;
110 REQUIRE(has_equivalent);
133template <
typename Scalar,
typename Index>
134void ensure_approx_equivalent_mesh(
139 REQUIRE(testing::attribute_is_approx_equivalent<Scalar, Scalar>(
146 testing::ensure_approx_equivalent_usage<AttributeUsage::Normal>(mesh1, mesh2);
147 testing::ensure_approx_equivalent_usage<AttributeUsage::UV>(mesh1, mesh2);
148 testing::ensure_approx_equivalent_usage<AttributeUsage::Color>(mesh1, mesh2);
149 testing::ensure_approx_equivalent_usage<AttributeUsage::Position>(mesh1, mesh2);
150 testing::ensure_approx_equivalent_usage<AttributeUsage::Tangent>(mesh1, mesh2);
151 testing::ensure_approx_equivalent_usage<AttributeUsage::Bitangent>(mesh1, mesh2);
156 auto usage = attr1.get_usage();
158 auto element = attr1.get_element_type();
164 using ValueType1 =
typename std::decay_t<
decltype(attr1)>::ValueType;
168 details::internal_foreach_named_attribute<
170 details::Ordering::Sequential,
171 details::Access::Read>(
173 [&](std::string_view name2,
auto&& attr2) {
174 if (mesh2.attr_name_is_reserved(name2))
return;
176 using ValueType2 =
typename std::decay_t<
decltype(attr2)>::ValueType;
177 REQUIRE(testing::attribute_is_approx_equivalent<ValueType1, ValueType2>(
static constexpr BitField all()
Named constructor returning a bitfield set to 1.
Definition: BitField.h:76
A general purpose polygonal mesh class.
Definition: SurfaceMesh.h:66
AttributeId attr_id_vertex_to_position() const
Attribute id for vertex -> positions.
Definition: SurfaceMesh.h:2028
void delete_attribute(std::string_view name, AttributeDeletePolicy policy=AttributeDeletePolicy::ErrorIfReserved)
Delete an attribute given by name.
Definition: SurfaceMesh.cpp:1036
AttributeId get_attribute_id(std::string_view name) const
Retrieve an attribute id given its name.
Definition: SurfaceMesh.cpp:341
static bool attr_name_is_reserved(std::string_view name)
Check whether the given name corresponds to a reserved attribute.
Definition: SurfaceMesh.cpp:1403
AttributeId map_attribute(SurfaceMesh< Scalar, Index > &mesh, AttributeId id, std::string_view new_name, AttributeElement new_element)
Map attribute values to a new attribute with a different element type.
Definition: map_attribute.cpp:241
uint32_t AttributeId
Identified to be used to access an attribute.
Definition: AttributeFwd.h:73
@ Vector
Mesh attribute can have any number of channels (including 1 channel).
@ Scalar
Mesh attribute must have exactly 1 channel.
@ Value
Values that are not attached to a specific element.
Definition: AttributeFwd.h:42
@ Edge
Per-edge mesh attributes.
Definition: AttributeFwd.h:34
@ Corner
Per-corner mesh attributes.
Definition: AttributeFwd.h:37
void seq_foreach_named_attribute_read(const SurfaceMesh< Scalar, Index > &mesh, Visitor &&vis)
Applies a function to each attribute of a mesh.
Definition: foreach_attribute.h:207
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