forked from libigl/libigl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexComparison.h
117 lines (109 loc) · 2.73 KB
/
IndexComparison.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2013 Alec Jacobson <[email protected]>
//
// 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/.
#ifndef IGL_INDEXCOMPARISON_H
#define IGL_INDEXCOMPARISON_H
#include <iostream>
namespace igl{
// Comparison struct used by sort
// http://bytes.com/topic/c/answers/132045-sort-get-index
// For use with functions like std::sort
template<class T> struct IndexLessThan
{
IndexLessThan(const T arr) : arr(arr) {}
bool operator()(const size_t a, const size_t b) const
{
return arr[a] < arr[b];
}
const T arr;
};
// For use with functions like std::unique
template<class T> struct IndexEquals
{
IndexEquals(const T arr) : arr(arr) {}
bool operator()(const size_t a, const size_t b) const
{
return arr[a] == arr[b];
}
const T arr;
};
// For use with functions like std::sort
template<class T> struct IndexVectorLessThan
{
IndexVectorLessThan(const T & vec) : vec ( vec) {}
bool operator()(const size_t a, const size_t b) const
{
return vec(a) < vec(b);
}
const T & vec;
};
// For use with functions like std::sort
template<class T> struct IndexDimLessThan
{
IndexDimLessThan(const T & mat,const int & dim, const int & j) :
mat(mat),
dim(dim),
j(j)
{}
bool operator()(const size_t a, const size_t b) const
{
if(dim == 1)
{
return mat(a,j) < mat(b,j);
}else
{
return mat(j,a) < mat(j,b);
}
}
const T & mat;
const int & dim;
const int & j;
};
// For use with functions like std::sort
template<class T> struct IndexRowLessThan
{
IndexRowLessThan(const T & mat) : mat ( mat) {}
bool operator()(const size_t a, const size_t b) const
{
const int cols = mat.cols();
// Lexicographical order
for(int j = 0;j<cols;j++)
{
if(mat(a,j) > mat(b,j))
{
return false;
} else if(mat(a,j) < mat(b,j))
{
return true;
}
}
// equality is false
return false;
}
const T & mat;
};
// For use with functions like std::sort
template<class T> struct IndexRowEquals
{
IndexRowEquals(const T & mat) : mat ( mat) {}
bool operator()(const size_t a, const size_t b) const
{
const int cols = mat.cols();
// Lexicographical order
for(int j = 0;j<cols;j++)
{
if(mat(a,j) != mat(b,j))
{
return false;
}
}
return true;
}
const T & mat;
};
}
#endif