Lagrange
Loading...
Searching...
No Matches
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/legacy/inline.h>
18#include <lagrange/raycasting/create_ray_caster.h>
19#include <lagrange/utils/safe_cast.h>
20
21#include <tbb/parallel_for.h>
22
23#include <string>
24#include <type_traits>
25#include <vector>
26
27namespace lagrange {
28namespace raycasting {
29LAGRANGE_LEGACY_INLINE
30namespace legacy {
31
51template <typename SourceMeshType, typename TargetMeshType>
52void project_attributes_closest_point(
53 const SourceMeshType& source,
54 TargetMeshType& target,
55 const std::vector<std::string>& names,
56 EmbreeRayCaster<ScalarOf<SourceMeshType>>* ray_caster = nullptr,
57 std::function<bool(IndexOf<TargetMeshType>)> skip_vertex = nullptr)
58{
59 static_assert(MeshTrait<SourceMeshType>::is_mesh(), "Input type is not Mesh");
60 static_assert(MeshTrait<TargetMeshType>::is_mesh(), "Output type is not Mesh");
61 la_runtime_assert(source.get_vertex_per_facet() == 3);
62
63 // Typedef festival because templates...
64 using Scalar = typename SourceMeshType::Scalar;
65 using Index = typename TargetMeshType::Index;
66 using SourceArray = typename SourceMeshType::AttributeArray;
67 using TargetArray = typename SourceMeshType::AttributeArray;
68 using Point = typename EmbreeRayCaster<Scalar>::Point;
69 using Direction = typename EmbreeRayCaster<Scalar>::Direction;
70
71 // We need to convert to a shared_ptr AND the ray caster will make another copy of the data..
72 std::unique_ptr<EmbreeRayCaster<Scalar>> engine;
73 if (!ray_caster) {
74 auto mesh = lagrange::to_shared_ptr(
75 lagrange::create_mesh(source.get_vertices(), source.get_facets()));
76 // Robust mode gives slightly more accurate results...
77 engine = create_ray_caster<Scalar>(EMBREE_ROBUST, BUILD_QUALITY_HIGH);
78
79 // Gosh why do I need to specify a transform here?
80 engine->add_mesh(mesh, Eigen::Matrix<Scalar, 4, 4>::Identity());
81
82 // Do a dummy raycast to trigger scene update, otherwise `cast()` will not work in
83 // multithread mode... (this is why we need const-safety...)
84 engine->cast(Point(0, 0, 0), Direction(0, 0, 1));
85 ray_caster = engine.get();
86 } else {
87 logger().debug("Using provided ray-caster");
88 }
89
90 // Store pointer to source/target arrays
91 std::vector<const SourceArray*> source_attrs(names.size());
92 std::vector<TargetArray> target_attrs(names.size());
93 for (size_t k = 0; k < names.size(); ++k) {
94 const auto& name = names[k];
95 la_runtime_assert(source.has_vertex_attribute(name));
96 source_attrs[k] = &source.get_vertex_attribute(name);
97 if (target.has_vertex_attribute(name)) {
98 target.export_vertex_attribute(name, target_attrs[k]);
99 } else {
100 target_attrs[k].resize(target.get_num_vertices(), source_attrs[k]->cols());
101 }
102 }
103
104 tbb::parallel_for(Index(0), target.get_num_vertices(), [&](Index i) {
105 if (skip_vertex && skip_vertex(i)) {
106 logger().trace("skipping vertex: {}", i);
107 return;
108 }
109 Point query = target.get_vertices().row(i).transpose();
110 auto res = ray_caster->query_closest_point(query);
112 res.facet_index >= 0 && res.facet_index < (unsigned)source.get_num_facets());
113 auto face = source.get_facets().row(res.facet_index).eval();
114 Point bary = res.barycentric_coord;
115
116 for (size_t k = 0; k < source_attrs.size(); ++k) {
117 target_attrs[k].row(i).setZero();
118 for (int lv = 0; lv < 3; ++lv) {
119 target_attrs[k].row(i) += source_attrs[k]->row(face[lv]) * bary[lv];
120 }
121 }
122 });
123
124 // Not super pretty way, we still need to separately add/create the attribute,
125 // THEN import it without copy. Would be better if we could get a ref to it.
126 for (size_t k = 0; k < names.size(); ++k) {
127 const auto& name = names[k];
128 target.add_vertex_attribute(name);
129 target.import_vertex_attribute(name, target_attrs[k]);
130 }
131}
132
133} // namespace legacy
134} // namespace raycasting
135} // namespace lagrange
A wrapper for Embree's raycasting API to compute ray intersections with (instances of) meshes.
Definition EmbreeRayCaster.h:59
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
Raycasting operations.
Definition ClosestPointResult.h:22
Main namespace for Lagrange.
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:88