Lagrange
unique_rows.h
1// Source: https://github.com/libigl/libigl/blob/main/include/igl/unique_rows.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) 2017 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/sortrows.h>
18
19namespace lagrange::internal {
20
21template <typename DerivedA, typename DerivedC, typename DerivedIA, typename DerivedIC>
22void unique_rows(
23 const Eigen::DenseBase<DerivedA>& A,
24 Eigen::PlainObjectBase<DerivedC>& C,
25 Eigen::PlainObjectBase<DerivedIA>& IA,
26 Eigen::PlainObjectBase<DerivedIC>& IC)
27{
28 Eigen::VectorXi IM;
29 DerivedA sortA;
30 lagrange::internal::sortrows(A, true, sortA, IM);
31
32 const int num_rows = static_cast<int>(sortA.rows());
33 const int num_cols = static_cast<int>(sortA.cols());
34 std::vector<int> vIA(num_rows);
35 for (int i = 0; i < num_rows; i++) {
36 vIA[i] = i;
37 }
38
39 auto index_equal = [&](const size_t i, const size_t j) {
40 for (size_t c = 0; c < num_cols; c++) {
41 if (sortA(i, c) != sortA(j, c)) return false;
42 }
43 return true;
44 };
45 vIA.erase(std::unique(vIA.begin(), vIA.end(), index_equal), vIA.end());
46
47 IC.resize(A.rows(), 1);
48 {
49 int j = 0;
50 for (int i = 0; i < num_rows; i++) {
51 if (sortA.row(vIA[j]) != sortA.row(i)) {
52 j++;
53 }
54 IC(IM(i, 0), 0) = j;
55 }
56 }
57 const int unique_rows = static_cast<int>(vIA.size());
58 C.resize(unique_rows, A.cols());
59 IA.resize(unique_rows, 1);
60 // Reindex IA according to IM
61 for (int i = 0; i < unique_rows; i++) {
62 IA(i, 0) = IM(vIA[i], 0);
63 C.row(i) << A.row(IA(i, 0));
64 }
65}
66
67} // 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