Lagrange
project_attributes_closest_point.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
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>
19
20#include <tbb/parallel_for.h>
21
22#include <string>
23#include <type_traits>
24#include <vector>
25
26namespace lagrange {
27namespace raycasting {
28
48template <typename SourceMeshType, typename TargetMeshType>
50 const SourceMeshType &source,
51 TargetMeshType &target,
52 const std::vector<std::string> &names,
53 EmbreeRayCaster<ScalarOf<SourceMeshType>> *ray_caster = nullptr,
54 std::function<bool(IndexOf<TargetMeshType>)> skip_vertex = nullptr)
55{
56 static_assert(MeshTrait<SourceMeshType>::is_mesh(), "Input type is not Mesh");
57 static_assert(MeshTrait<TargetMeshType>::is_mesh(), "Output type is not Mesh");
58 la_runtime_assert(source.get_vertex_per_facet() == 3);
59
60 // Typedef festival because templates...
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;
67
68 // We need to convert to a shared_ptr AND the ray caster will make another copy of the data..
69 std::unique_ptr<EmbreeRayCaster<Scalar>> engine;
70 if (!ray_caster) {
71 auto mesh = lagrange::to_shared_ptr(
72 lagrange::create_mesh(source.get_vertices(), source.get_facets()));
73 // Robust mode gives slightly more accurate results...
74 engine = create_ray_caster<Scalar>(EMBREE_ROBUST, BUILD_QUALITY_HIGH);
75
76 // Gosh why do I need to specify a transform here?
77 engine->add_mesh(mesh, Eigen::Matrix<Scalar, 4, 4>::Identity());
78
79 // Do a dummy raycast to trigger scene update, otherwise `cast()` will not work in
80 // multithread mode... (this is why we need const-safety...)
81 engine->cast(Point(0, 0, 0), Direction(0, 0, 1));
82 ray_caster = engine.get();
83 } else {
84 logger().debug("Using provided ray-caster");
85 }
86
87 // Store pointer to source/target arrays
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];
92 la_runtime_assert(source.has_vertex_attribute(name));
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]);
96 } else {
97 target_attrs[k].resize(target.get_num_vertices(), source_attrs[k]->cols());
98 }
99 }
100
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);
104 return;
105 }
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;
111
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];
116 }
117 }
118 });
119
120 // Not super pretty way, we still need to separately add/create the attribute,
121 // THEN import it without copy. Would be better if we could get a ref to it.
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]);
126 }
127}
128
129} // namespace raycasting
130} // namespace lagrange
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