-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.h
95 lines (75 loc) · 2.32 KB
/
common.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
#pragma once
////////////////////////////////////////////////////////////////////////////////
#include <Eigen/Dense>
////////////////////////////////////////////////////////////////////////////////
namespace Math {
inline bool isPowerOfTwo(int x) {
return (x > 0) && !(x & (x - 1));
}
inline int nextPowerOfTwo(int x) {
// http://stackoverflow.com/questions/364985/algorithm-for-finding-the-smallest-power-of-two-thats-greater-or-equal-to-a-giv
if (x < 0) { return 0; }
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x+1;
}
} // namespace Math
////////////////////////////////////////////////////////////////////////////////
struct Layout3D {
static int toIndex(Eigen::Vector3i x, Eigen::Vector3i dims) {
return x[0] + dims[0] * (x[1] + dims[1] * x[2]);
}
static Eigen::Vector3i toGrid(int i, Eigen::Vector3i dims) {
return Eigen::Vector3i(
i % dims[0],
(i / dims[0]) % dims[1],
(i / dims[0]) / dims[1]
);
}
/*
static int toint(Eigen::Vector3i x, Eigen::Vector3i dims) {
return x[2] + dims[2] * (x[1] + dims[1] * x[0]);
}
static Eigen::Vector3i toVector(int i, Eigen::Vector3i dims) {
return {{
(i / dims[2]) / dims[1],
(i / dims[2]) % dims[1],
i % dims[2],
}};
}
*/
static bool isValid(Eigen::Vector3i x, Eigen::Vector3i dims) {
return (x.array() >= 0).all() && (x.array() < dims.array()).all();
}
static bool isValid(int i, Eigen::Vector3i dims) {
return isValid(toGrid(i, dims), dims);
}
static Eigen::Vector3i clamp(Eigen::Vector3i x, Eigen::Vector3i dims) {
return Eigen::Vector3i(
std::max(0, std::min(x[0], dims[0]-1)),
std::max(0, std::min(x[1], dims[1]-1)),
std::max(0, std::min(x[2], dims[2]-1))
);
}
};
////////////////////////////////////////////////////////////////////////////////
namespace Cube {
// Map local corner index (between 0 and 7) to the actual corner of the unit
// cube [0,1]^3. The corners are obtained in this order:
// (0,0,0), (0,1,0), (1,1,0), (1,0,0), (0,0,1), (0,1,1), (1,1,1), (1,0,1)
inline Eigen::Vector3i delta(int i) {
return Eigen::Vector3i((i & 1) ^ ((i >> 1) & 1), (i >> 1) & 1, (i >> 2) & 1);
}
// Inverse mapping (delta -> corner index)
inline int invDelta(Eigen::Vector3i u) {
if (u[1]) {
return 4*u[2] + 3 - u[0];
} else {
return 4*u[2] + u[0];
}
}
} // namespace Cube