14#include <lagrange/MeshTrait.h>
15#include <lagrange/compute_barycentric_coordinates.h>
16#include <lagrange/create_mesh.h>
17#include <lagrange/raycasting/create_ray_caster.h>
18#include <lagrange/utils/safe_cast.h>
20#include <tbb/parallel_for.h>
48template <
typename SourceMeshType,
typename TargetMeshType>
50 const SourceMeshType &source,
51 TargetMeshType &target,
52 const std::vector<std::string> &names,
54 std::function<
bool(IndexOf<TargetMeshType>)> skip_vertex =
nullptr)
61 using Scalar =
typename SourceMeshType::Scalar;
62 using Index =
typename TargetMeshType::Index;
63 using SourceArray =
typename SourceMeshType::AttributeArray;
64 using TargetArray =
typename SourceMeshType::AttributeArray;
65 using Point =
typename EmbreeRayCaster<Scalar>::Point;
66 using Direction =
typename EmbreeRayCaster<Scalar>::Direction;
69 std::unique_ptr<EmbreeRayCaster<Scalar>> engine;
77 engine->add_mesh(mesh, Eigen::Matrix<Scalar, 4, 4>::Identity());
81 engine->cast(Point(0, 0, 0), Direction(0, 0, 1));
82 ray_caster = engine.get();
84 logger().debug(
"Using provided ray-caster");
88 std::vector<const SourceArray *> source_attrs(names.size());
89 std::vector<TargetArray> target_attrs(names.size());
90 for (
size_t k = 0; k < names.size(); ++k) {
91 const auto &name = names[k];
93 source_attrs[k] = &source.get_vertex_attribute(name);
94 if (target.has_vertex_attribute(name)) {
95 target.export_vertex_attribute(name, target_attrs[k]);
97 target_attrs[k].resize(target.get_num_vertices(), source_attrs[k]->cols());
101 tbb::parallel_for(Index(0), target.get_num_vertices(), [&](Index i) {
102 if (skip_vertex && skip_vertex(i)) {
103 logger().trace(
"skipping vertex: {}", i);
106 Point query = target.get_vertices().row(i).transpose();
107 auto res = ray_caster->query_closest_point(query);
108 la_runtime_assert(res.facet_index >= 0 && res.facet_index < (
unsigned)source.get_num_facets());
109 auto face = source.get_facets().row(res.facet_index).eval();
110 Point bary = res.barycentric_coord;
112 for (
size_t k = 0; k < source_attrs.size(); ++k) {
113 target_attrs[k].row(i).setZero();
114 for (
int lv = 0; lv < 3; ++lv) {
115 target_attrs[k].row(i) += source_attrs[k]->row(face[lv]) * bary[lv];
122 for (
size_t k = 0; k < names.size(); ++k) {
123 const auto &name = names[k];
124 target.add_vertex_attribute(name);
125 target.import_vertex_attribute(name, target_attrs[k]);
A wrapper for Embree's raycasting API to compute ray intersections with (instances of) meshes.
Definition: EmbreeRayCaster.h:47
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
@ EMBREE_ROBUST
Corresponds to RTC_SCENE_FLAG_ROBUST.
Definition: create_ray_caster.h:25
void project_attributes_closest_point(const SourceMeshType &source, TargetMeshType &target, const std::vector< std::string > &names, EmbreeRayCaster< ScalarOf< SourceMeshType > > *ray_caster=nullptr, std::function< bool(IndexOf< TargetMeshType >)> skip_vertex=nullptr)
Project vertex attributes from one mesh to another, by copying attributes from the closest point on t...
Definition: project_attributes_closest_point.h:49
@ BUILD_QUALITY_HIGH
Corresponds to RTC_BUILD_QUALITY_HIGH.
Definition: create_ray_caster.h:32
Main namespace for Lagrange.
Definition: AABBIGL.h:30
auto create_mesh(const Eigen::MatrixBase< DerivedV > &vertices, const Eigen::MatrixBase< DerivedF > &facets)
This function create a new mesh given the vertex and facet arrays by copying data into the Mesh objec...
Definition: create_mesh.h:39
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T > &&ptr)
Helper for automatic type deduction for unique_ptr to shared_ptr conversion.
Definition: common.h:84
MeshTrait class provide compiler check for different mesh types.
Definition: MeshTrait.h:108