Lagrange
vertex_components.h
1// Source: https://github.com/libigl/libigl/blob/main/include/igl/vertex_components.cpp
2// SPDX-License-Identifier: MPL-2.0
3//
4// This file is part of libigl, a simple c++ geometry processing library.
5//
6// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
7//
8// This Source Code Form is subject to the terms of the Mozilla Public License
9// v. 2.0. If a copy of the MPL was not distributed with this file, You can
10// obtain one at http://mozilla.org/MPL/2.0/.
11//
12// This file has been modified by Adobe.
13//
14// All modifications are Copyright 2022 Adobe.
15#pragma once
16
17#include <Eigen/Dense>
18#include <Eigen/Sparse>
19
20#include <queue>
21#include <vector>
22
23namespace lagrange::internal {
24
25template <typename DerivedA, typename DerivedC, typename Derivedcounts>
26void vertex_components(
27 const Eigen::SparseCompressedBase<DerivedA>& A,
28 Eigen::PlainObjectBase<DerivedC>& C,
29 Eigen::PlainObjectBase<Derivedcounts>& counts)
30{
31 assert(A.rows() == A.cols() && "A should be square.");
32 const size_t n = A.rows();
33 Eigen::Array<bool, Eigen::Dynamic, 1> seen = Eigen::Array<bool, Eigen::Dynamic, 1>::Zero(n, 1);
34 C.resize(n, 1);
35 typename DerivedC::Scalar id = 0;
36 std::vector<typename Derivedcounts::Scalar> vcounts;
37 // breadth first search
38 for (int k = 0; k < A.outerSize(); ++k) {
39 if (seen(k)) {
40 continue;
41 }
42 std::queue<int> Q;
43 Q.push(k);
44 vcounts.push_back(0);
45 while (!Q.empty()) {
46 const int f = Q.front();
47 Q.pop();
48 if (seen(f)) {
49 continue;
50 }
51 seen(f) = true;
52 C(f, 0) = id;
53 vcounts[id]++;
54 // Iterate over inside
55 for (typename DerivedA::InnerIterator it(A, f); it; ++it) {
56 const int g = it.index();
57 if (!seen(g) && it.value()) {
58 Q.push(g);
59 }
60 }
61 }
62 id++;
63 }
64 assert((size_t)id == vcounts.size());
65 const size_t ncc = vcounts.size();
66 assert((size_t)C.maxCoeff() + 1 == ncc);
67 counts.resize(ncc, 1);
68 for (size_t i = 0; i < ncc; i++) {
69 counts(i) = vcounts[i];
70 }
71}
72
73template <typename DerivedA, typename DerivedC>
74void vertex_components(
75 const Eigen::SparseCompressedBase<DerivedA>& A,
76 Eigen::PlainObjectBase<DerivedC>& C)
77{
78 Eigen::VectorXi counts;
79 return vertex_components(A, C, counts);
80}
81
82} // namespace lagrange::internal
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition: attribute_string_utils.h:21