Lagrange
orientable_patches.h
1// Source: https://github.com/libigl/libigl/blob/main/include/igl/orientable_patches.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 <lagrange/internal/unique_rows.h>
18#include <lagrange/internal/vertex_components.h>
19
20namespace lagrange::internal {
21
22template <typename DerivedF, typename DerivedC, typename AScalar>
23void orientable_patches(
24 const Eigen::MatrixBase<DerivedF>& F,
25 Eigen::PlainObjectBase<DerivedC>& C,
26 Eigen::SparseMatrix<AScalar>& A)
27{
28 // simplex size
29 assert(F.cols() == 3);
30
31 // List of all "half"-edges: 3*#F by 2
32 Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, 2> allE, uE;
33 allE.resize(F.rows() * 3, 2);
34 Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, 2> IX;
35 Eigen::VectorXi IA, IC;
36 allE.block(0 * F.rows(), 0, F.rows(), 1) = F.col(1);
37 allE.block(0 * F.rows(), 1, F.rows(), 1) = F.col(2);
38 allE.block(1 * F.rows(), 0, F.rows(), 1) = F.col(2);
39 allE.block(1 * F.rows(), 1, F.rows(), 1) = F.col(0);
40 allE.block(2 * F.rows(), 0, F.rows(), 1) = F.col(0);
41 allE.block(2 * F.rows(), 1, F.rows(), 1) = F.col(1);
42 // Sort each row
43 for (auto row : allE.rowwise()) {
44 std::sort(row.begin(), row.end());
45 }
46 // IC(i) tells us where to find sortallE(i,:) in uE:
47 // so that sortallE(i,:) = uE(IC(i),:)
48 lagrange::internal::unique_rows(allE, uE, IA, IC);
49 // uE2FT(e,f) = 1 means face f is adjacent to unique edge e
50 std::vector<Eigen::Triplet<AScalar>> uE2FTijv(IC.rows());
51 for (int e = 0; e < IC.rows(); e++) {
52 uE2FTijv[e] = Eigen::Triplet<AScalar>(e % F.rows(), IC(e), 1);
53 }
54 Eigen::SparseMatrix<AScalar> uE2FT(F.rows(), uE.rows());
55 uE2FT.setFromTriplets(uE2FTijv.begin(), uE2FTijv.end());
56 // kill non-manifold edges
57 for (int j = 0; j < (int)uE2FT.outerSize(); j++) {
58 int degree = 0;
59 for (typename Eigen::SparseMatrix<AScalar>::InnerIterator it(uE2FT, j); it; ++it) {
60 degree++;
61 }
62 // Iterate over inside
63 if (degree > 2) {
64 for (typename Eigen::SparseMatrix<AScalar>::InnerIterator it(uE2FT, j); it; ++it) {
65 uE2FT.coeffRef(it.row(), it.col()) = 0;
66 }
67 }
68 }
69 // Face-face Adjacency matrix
70 Eigen::SparseMatrix<AScalar> uE2F;
71 uE2F = uE2FT.transpose().eval();
72 A = uE2FT * uE2F;
73 // All ones
74 for (int j = 0; j < A.outerSize(); j++) {
75 // Iterate over inside
76 for (typename Eigen::SparseMatrix<AScalar>::InnerIterator it(A, j); it; ++it) {
77 if (it.value() > 1) {
78 A.coeffRef(it.row(), it.col()) = 1;
79 }
80 }
81 }
82 //% Connected components are patches
83 //%C = vertex_components(A); % alternative to graphconncomp from matlab_bgl
84 //[~,C] = graphconncomp(A);
85 // graph connected components
86 lagrange::internal::vertex_components(A, C);
87}
88
89template <typename DerivedF, typename DerivedC>
90void orientable_patches(const Eigen::MatrixBase<DerivedF>& F, Eigen::PlainObjectBase<DerivedC>& C)
91{
92 Eigen::SparseMatrix<typename DerivedF::Scalar> A;
93 return lagrange::internal::orientable_patches(F, C, A);
94}
95
96} // 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