75 const SourceMeshType& source,
76 TargetMeshType& target,
77 const std::vector<std::string>& names,
78 const Eigen::MatrixBase<DerivedVector>& direction,
81 DefaultScalar default_value = DefaultScalar(0),
82 std::function<
void(
typename TargetMeshType::Index,
bool)> user_callback =
nullptr,
84 std::function<
bool(IndexOf<TargetMeshType>)> skip_vertex =
nullptr)
86 static_assert(MeshTrait<SourceMeshType>::is_mesh(),
"Input type is not Mesh");
87 static_assert(MeshTrait<TargetMeshType>::is_mesh(),
"Output type is not Mesh");
91 using Scalar =
typename SourceMeshType::Scalar;
92 using Index =
typename TargetMeshType::Index;
93 using SourceArray =
typename SourceMeshType::AttributeArray;
94 using TargetArray =
typename SourceMeshType::AttributeArray;
95 using Point =
typename EmbreeRayCaster<Scalar>::Point;
96 using Direction =
typename EmbreeRayCaster<Scalar>::Direction;
97 using RayCasterIndex =
typename EmbreeRayCaster<Scalar>::Index;
98 using Scalar4 =
typename EmbreeRayCaster<Scalar>::Scalar4;
99 using Index4 =
typename EmbreeRayCaster<Scalar>::Index4;
100 using Point4 =
typename EmbreeRayCaster<Scalar>::Point4;
101 using Direction4 =
typename EmbreeRayCaster<Scalar>::Direction4;
102 using Mask4 =
typename EmbreeRayCaster<Scalar>::Mask4;
105 std::unique_ptr<EmbreeRayCaster<Scalar>> engine;
113 engine->add_mesh(mesh, Eigen::Matrix<Scalar, 4, 4>::Identity());
117 engine->cast(Point(0, 0, 0), Direction(0, 0, 1));
118 ray_caster = engine.get();
120 logger().debug(
"Using provided ray-caster");
124 std::vector<const SourceArray*> source_attrs;
125 source_attrs.reserve(names.size());
126 std::vector<TargetArray> target_attrs(names.size());
127 for (
size_t k = 0; k < names.size(); ++k) {
128 const auto& name = names[k];
130 source_attrs.push_back(&source.get_vertex_attribute(name));
131 if (target.has_vertex_attribute(name)) {
132 target.export_vertex_attribute(name, target_attrs[k]);
134 target_attrs[k].resize(target.get_num_vertices(), source_attrs[k]->cols());
138 auto diag = igl::bounding_box_diagonal(source.get_vertices());
141 std::vector<char> is_hit;
143 is_hit.assign(target.get_num_vertices(),
false);
146 Index num_vertices = target.get_num_vertices();
147 Index num_packets = (num_vertices + 3) / 4;
150 dirs.row(0) = direction.normalized().transpose();
151 for (
int i = 1; i < 4; ++i) {
152 dirs.row(i) = dirs.row(0);
154 Direction4 dirs2 = -dirs;
156 tbb::parallel_for(Index(0), num_packets, [&](Index packet_index) {
157 Index batchsize = std::min(num_vertices - (packet_index << 2), 4);
158 Mask4 mask = Mask4::Constant(-1);
161 int num_skipped_in_packet = 0;
162 for (Index b = 0; b < batchsize; ++b) {
163 Index i = (packet_index << 2) + b;
164 if (skip_vertex && skip_vertex(i)) {
165 logger().trace(
"skipping vertex: {}", i);
166 if (!is_hit.empty()) {
170 ++num_skipped_in_packet;
174 origins.row(b) = target.get_vertices().row(i);
177 if (num_skipped_in_packet == batchsize)
return;
179 for (Index b = batchsize; b < 4; ++b) {
184 Index4 instance_indices;
185 Index4 facet_indices;
189 uint8_t hits = ray_caster->cast4(
203 Point4 origins2 = origins +
Scalar(1e-6) * diag * dirs;
204 Index4 mesh_indices2;
205 Index4 instance_indices2;
206 Index4 facet_indices2;
210 uint8_t hits2 = ray_caster->cast4(
222 for (Index b = 0; b < batchsize; ++b) {
223 if (!mask(b))
continue;
224 auto len2 = std::abs(
Scalar(1e-6) * diag - ray_depths2(b));
225 bool hit = hits & (1 << b);
226 bool hit2 = hits2 & (1 << b);
227 if (hit2 && (!hit || len2 < ray_depths(b))) {
229 mesh_indices(b) = mesh_indices2(b);
230 facet_indices(b) = facet_indices2(b);
231 barys.row(b) = barys2.row(b);
235 for (Index b = 0; b < batchsize; ++b) {
236 if (!mask(b))
continue;
237 bool hit = hits & (1 << b);
238 Index i = (packet_index << 2) + b;
242 facet_indices(b) >= 0 &&
243 facet_indices(b) <
static_cast<RayCasterIndex
>(source.get_num_facets()));
244 auto face = source.get_facets().row(facet_indices(b));
245 for (
size_t k = 0; k < source_attrs.size(); ++k) {
246 target_attrs[k].row(i).setZero();
247 for (
int lv = 0; lv < 3; ++lv) {
248 target_attrs[k].row(i) += source_attrs[k]->row(face[lv]) * barys(b, lv);
254 for (
size_t k = 0; k < source_attrs.size(); ++k) {
263 if (!is_hit.empty()) {
267 user_callback(i, hit);
274 for (
size_t k = 0; k < names.size(); ++k) {
275 const auto& name = names[k];
276 target.add_vertex_attribute(name);
277 target.import_vertex_attribute(name, target_attrs[k]);
282 bool all_hit = std::all_of(is_hit.begin(), is_hit.end(), [](
char x) { return bool(x); });
286 return bool(is_hit[i]);
289 ::lagrange::bvh::project_attributes_closest_vertex(
293 [&](Index i) {
return bool(is_hit[i]); });
295 throw std::runtime_error(
"not implemented");
void project_attributes_directional(const SourceMeshType &source, TargetMeshType &target, const std::vector< std::string > &names, const Eigen::MatrixBase< DerivedVector > &direction, CastMode cast_mode=CastMode::BOTH_WAYS, WrapMode wrap_mode=WrapMode::CONSTANT, DefaultScalar default_value=DefaultScalar(0), std::function< void(typename TargetMeshType::Index, bool)> user_callback=nullptr, EmbreeRayCaster< ScalarOf< SourceMeshType > > *ray_caster=nullptr, std::function< bool(IndexOf< TargetMeshType >)> skip_vertex=nullptr)
Project vertex attributes from one mesh to another, by projecting target vertices along a prescribed ...
Definition project_attributes_directional.h:74