Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
project_attributes_closest_vertex.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/bvh/BVHNanoflann.h>
16#include <lagrange/common.h>
17
18#include <tbb/parallel_for.h>
19
20#include <string>
21#include <vector>
22
23namespace lagrange {
24namespace bvh {
25
43template <typename SourceMeshType, typename TargetMeshType>
44void project_attributes_closest_vertex(
45 const SourceMeshType &source,
46 TargetMeshType &target,
47 const std::vector<std::string> &names,
48 std::function<bool(IndexOf<TargetMeshType>)> skip_vertex = nullptr)
49{
50 static_assert(MeshTrait<SourceMeshType>::is_mesh(), "Input type is not Mesh");
51 static_assert(MeshTrait<TargetMeshType>::is_mesh(), "Output type is not Mesh");
52
53 using VertexArray = typename SourceMeshType::VertexArray;
54 using BVH_t = BVHNanoflann<VertexArray>;
55 using Index = typename TargetMeshType::Index;
56 using SourceArray = typename SourceMeshType::AttributeArray;
57 using TargetArray = typename SourceMeshType::AttributeArray;
58
59 auto engine = std::make_unique<BVH_t>();
60 engine->build(source.get_vertices());
61
62 // Store pointer to source/target arrays
63 std::vector<const SourceArray *> source_attrs(names.size());
64 std::vector<TargetArray> target_attrs(names.size());
65 for (size_t k = 0; k < names.size(); ++k) {
66 const auto &name = names[k];
67 la_runtime_assert(source.has_vertex_attribute(name));
68 source_attrs[k] = &source.get_vertex_attribute(name);
69 if (target.has_vertex_attribute(name)) {
70 target.export_vertex_attribute(name, target_attrs[k]);
71 } else {
72 target_attrs[k].resize(target.get_num_vertices(), source_attrs[k]->cols());
73 }
74 }
75
76 tbb::parallel_for(Index(0), target.get_num_vertices(), [&](Index i) {
77 if (skip_vertex && skip_vertex(i)) {
78 logger().trace("skipping vertex: {}", i);
79 return;
80 }
81 auto res = engine->query_closest_point(target.get_vertices().row(i));
82 for (size_t k = 0; k < source_attrs.size(); ++k) {
83 target_attrs[k].row(i) = source_attrs[k]->row(res.closest_vertex_idx);
84 }
85 });
86
87 // Not super pretty way, we still need to separately add/create the attribute,
88 // THEN import it without copy. Would be better if we could get a ref to it.
89 for (size_t k = 0; k < names.size(); ++k) {
90 const auto &name = names[k];
91 target.add_vertex_attribute(name);
92 target.import_vertex_attribute(name, target_attrs[k]);
93 }
94}
95
96} // namespace bvh
97} // namespace lagrange
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30