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