-
Notifications
You must be signed in to change notification settings - Fork 23
/
bfs_orient.h
93 lines (85 loc) · 3.18 KB
/
bfs_orient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Source: https://github.com/libigl/libigl/blob/main/include/igl/bfs_orient.cpp
// SPDX-License-Identifier: MPL-2.0
//
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
//
// This file has been modified by Adobe.
//
// All modifications are Copyright 2022 Adobe.
#pragma once
#include <lagrange/internal/orientable_patches.h>
namespace lagrange::internal {
template <typename DerivedF, typename DerivedFF, typename DerivedC>
void bfs_orient(
const Eigen::MatrixBase<DerivedF>& F,
Eigen::PlainObjectBase<DerivedFF>& FF,
Eigen::PlainObjectBase<DerivedC>& C)
{
Eigen::SparseMatrix<typename DerivedF::Scalar> A;
lagrange::internal::orientable_patches(F, C, A);
// number of faces
const int m = static_cast<int>(F.rows());
// number of patches
const int num_cc = C.maxCoeff() + 1;
Eigen::VectorXi seen = Eigen::VectorXi::Zero(m);
// Edge sets
const int ES[3][2] = {{1, 2}, {2, 0}, {0, 1}};
if (((void*)&FF) != ((void*)&F)) {
FF = F;
}
// loop over patches
tbb::parallel_for(0, num_cc, [&](int c) {
std::queue<typename DerivedF::Scalar> Q;
// find first member of patch c
for (int f = 0; f < FF.rows(); f++) {
if (C(f) == c) {
Q.push(f);
break;
}
}
assert(!Q.empty());
while (!Q.empty()) {
const typename DerivedF::Scalar f = Q.front();
Q.pop();
if (seen(f) > 0) {
continue;
}
seen(f)++;
// loop over neighbors of f
for (typename Eigen::SparseMatrix<typename DerivedF::Scalar>::InnerIterator it(A, f);
it;
++it) {
// might be some lingering zeros, and skip self-adjacency
if (it.value() != 0 && it.row() != f) {
const int n = static_cast<int>(it.row());
assert(n != f);
// loop over edges of f
for (int efi = 0; efi < 3; efi++) {
// efi'th edge of face f
Eigen::Vector2i ef(FF(f, ES[efi][0]), FF(f, ES[efi][1]));
// loop over edges of n
for (int eni = 0; eni < 3; eni++) {
// eni'th edge of face n
Eigen::Vector2i en(FF(n, ES[eni][0]), FF(n, ES[eni][1]));
// Match (half-edges go same direction)
if (ef(0) == en(0) && ef(1) == en(1)) {
// flip face n
FF.row(n) = FF.row(n).reverse().eval();
}
}
}
// add neighbor to queue
Q.push(n);
}
}
}
});
// make sure flip is OK if &FF = &F
}
} // namespace lagrange::internal