-
Notifications
You must be signed in to change notification settings - Fork 19
/
coordinate.h
271 lines (235 loc) · 10.1 KB
/
coordinate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2018
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* For further information, visit https://knossos.app
* or contact knossosteam@gmail.com
*/
#pragma once
#include <QDebug>
#include <QList>
#include <QVector>
#include <QVector3D>
#include <QMetaType>
#include <cmath>
#include <cstddef>
#include <boost/functional/hash.hpp>
template<typename ComponentType, typename CoordinateDerived>
class CoordinateBase {
public:
ComponentType x = 0;
ComponentType y = 0;
ComponentType z = 0;
constexpr CoordinateBase() = default;
CoordinateBase(QList<ComponentType> l) {
if (l.size() != 3) {
throw std::runtime_error("QList too short for coordinate");
}
std::tie(x, y, z) = std::make_tuple(l[0], l[1], l[2]);
}
CoordinateBase(QVector<ComponentType> l) {
if (l.size() != 3) {
throw std::runtime_error("QVector too short for coordinate");
}
std::tie(x, y, z) = std::make_tuple(l[0], l[1], l[2]);
}
constexpr CoordinateBase(ComponentType x, ComponentType y, ComponentType z) : x(x), y(y), z(z) {}
constexpr ComponentType sum() const {
return x+y+z;
}
QList<ComponentType> list() const {
return {x, y, z};
}
QVector<ComponentType> vector() const {
return {x, y, z};
}
constexpr bool operator==(const CoordinateDerived & rhs) const {
return x == rhs.x && y == rhs.y && z == rhs.z;
}
constexpr bool operator!=(const CoordinateDerived & rhs) const {
return !(*this == rhs);
}
template<typename T>
using CoordinateDerived_if_valid_t = typename std::enable_if<std::is_convertible<T, CoordinateDerived>::value || std::is_convertible<T, ComponentType>::value, CoordinateDerived>::type;
constexpr CoordinateDerived operator+(const CoordinateDerived & rhs) const {
return CoordinateDerived(x + rhs.x, y + rhs.y, z + rhs.z);
}
constexpr CoordinateDerived operator+(const ComponentType scalar) const {
return CoordinateDerived(x + scalar, y + scalar, z + scalar);
}
template<typename T>
constexpr CoordinateDerived_if_valid_t<T> & operator+=(const T & rhs) {
return static_cast<CoordinateDerived&>(*this = *this + rhs);
}
constexpr CoordinateDerived operator-(const CoordinateDerived & rhs) const {
return CoordinateDerived(x - rhs.x, y - rhs.y, z - rhs.z);
}
constexpr CoordinateDerived operator-(const ComponentType scalar) const {
return CoordinateDerived(x - scalar, y - scalar, z - scalar);
}
template<typename T>
constexpr CoordinateDerived_if_valid_t<T> & operator-=(const T & rhs) {
return static_cast<CoordinateDerived&>(*this = *this - rhs);
}
template<typename T>
constexpr CoordinateDerived componentMul(const T & rhs) const {
return CoordinateDerived(x * rhs.x, y * rhs.y, z * rhs.z);
}
constexpr CoordinateDerived componentMul(const ComponentType x, const ComponentType y, const ComponentType z) const {
return CoordinateDerived(this->x * x, this->y * y, this->z * z);
}
constexpr ComponentType dot(const CoordinateDerived & rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
constexpr CoordinateDerived cross(const CoordinateDerived & rhs) const {
return CoordinateDerived(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
}
constexpr CoordinateDerived operator*(const ComponentType scalar) const {
return CoordinateDerived(x * scalar, y * scalar, z * scalar);
}
template<typename T>
constexpr CoordinateDerived_if_valid_t<T> & operator*=(const T & rhs) {
return static_cast<CoordinateDerived&>(*this = *this * rhs);
}
constexpr CoordinateDerived operator/(const CoordinateDerived & rhs) const {
return CoordinateDerived(x / rhs.x, y / rhs.y, z / rhs.z);
}
constexpr CoordinateDerived operator/(const ComponentType scalar) const {
return CoordinateDerived(x / scalar, y / scalar, z / scalar);
}
template<typename T>
constexpr CoordinateDerived_if_valid_t<T> & operator/=(const T & rhs) {
return static_cast<CoordinateDerived&>(*this = *this / rhs);
}
ComponentType prod() const {
return x * y * z;
}
float length() const {
return std::sqrt(x*x + y*y + z*z);
}
float angleRad(CoordinateDerived & other) const {
return std::acos(static_cast<float>(this->dot(other)) / (this->length() * other.length()));
}
constexpr bool normalize() {
const ComponentType norm = length();
if (norm != 0) {
*this /= norm;
return true;
}
return false;
}
constexpr CoordinateDerived capped(const CoordinateDerived & min, const CoordinateDerived & max) const {
return CoordinateDerived{
std::max(min.x, std::min(x, max.x - 1))
, std::max(min.y, std::min(y, max.y - 1))
, std::max(min.z, std::min(z, max.z - 1))
};
}
constexpr CoordinateDerived toWorldFrom(const CoordinateDerived & v1, const CoordinateDerived & v2, const CoordinateDerived & n) const {
return x * v1 + y * v2 + z * n;
}
constexpr CoordinateDerived toLocal(const CoordinateDerived & v1, const CoordinateDerived & v2, const CoordinateDerived & n) const {
return CoordinateDerived(v1.dot({x, y, z}), v2.dot({x, y, z}), n.dot({x, y, z}));
}
};
template<typename ComponentType, typename CoordinateDerived>
constexpr CoordinateDerived operator*(const ComponentType scalar, const CoordinateBase<ComponentType, CoordinateDerived> & coord) {
return static_cast<CoordinateDerived>(coord * scalar);
}
template<typename ComponentType, typename CoordinateDerived>
QDebug operator<<(QDebug stream, const CoordinateBase<ComponentType, CoordinateDerived> & coord) {
return stream << "(" << coord.x << coord.y << coord.z << ")";
}
template<typename ComponentType = int, std::size_t tag = 0>
class Coord : public CoordinateBase<ComponentType, Coord<ComponentType, tag>> {
public:
using CoordinateBase<ComponentType, Coord<ComponentType, tag>>::CoordinateBase;
constexpr Coord() = default;
template<typename T>
constexpr Coord(const Coord<T, tag> & rhs) : CoordinateBase<ComponentType, Coord<ComponentType, tag>>(rhs.x, rhs.y, rhs.z) {}
template<typename T>
operator Coord<T, tag>() const {
return Coord<T, tag>(std::round(this->x), std::round(this->y), std::round(this->z));
}
constexpr operator QVector3D() const {
return QVector3D{this->x, this->y, this->z};
}
};
using Coordinate = Coord<int, 0>;
using CoordOfCube = Coord<int, 1>;
using CoordInCube = Coord<int, 2>;
using CoordOfGPUCube = Coord<int, 3>;
using floatCoordinate = Coord<float>;
template<>
class Coord<int, 0> : public CoordinateBase<int, Coordinate> {
public:
using CoordinateBase<int, Coordinate>::CoordinateBase;
inline CoordOfCube cube(const Coordinate & size, const floatCoordinate scale = {1,1,1}) const;
constexpr CoordInCube insideCube(const Coordinate & size, const floatCoordinate scale) const;
};
template<>
class Coord<int, 1> : public CoordinateBase<int, CoordOfCube> {
public:
using CoordinateBase<int, CoordOfCube>::CoordinateBase;
constexpr Coordinate cube2Global(const Coordinate & cubeShape, const floatCoordinate scale = {1,1,1}) const;
};
template<>
class Coord<int, 2> : public CoordinateBase<int, CoordInCube> {
public:
using CoordinateBase<int, CoordInCube>::CoordinateBase;
inline Coordinate insideCube2Global(const CoordOfCube & cube, const Coordinate & cubeShape, const floatCoordinate scale) const;
};
template<>
class Coord<int, 3> : public CoordinateBase<int, CoordOfGPUCube> {
public:
using CoordinateBase<int, CoordOfGPUCube>::CoordinateBase;
constexpr Coordinate cube2Global(const Coordinate & cubeShape, const floatCoordinate scale = {1,1,1}) const;
};
inline CoordOfCube Coordinate::cube(const Coordinate & size, const floatCoordinate scale) const {
return CoordOfCube(std::floor(x / scale.x / size.x), std::floor(y / scale.y / size.y), std::floor(z / scale.z / size.z));
}
constexpr CoordInCube Coordinate::insideCube(const Coordinate & size, const floatCoordinate scale) const {
return CoordInCube(static_cast<int>(x / scale.x) % size.x, static_cast<int>(y / scale.y) % size.y, static_cast<int>(z / scale.z) % size.z);
}
constexpr Coordinate CoordOfCube::cube2Global(const Coordinate & size, const floatCoordinate scale) const {
return Coordinate(x * size.x * scale.x, y * size.y * scale.y, z * size.z * scale.z);
}
constexpr Coordinate CoordOfGPUCube::cube2Global(const Coordinate & size, const floatCoordinate scale) const {
return Coordinate(x * size.x * scale.x, y * size.y * scale.y, z * size.z * scale.z);
}
inline Coordinate CoordInCube::insideCube2Global(const CoordOfCube & cube, const Coordinate & size, const floatCoordinate scale) const {
return cube.cube2Global(size, scale) + scale.componentMul(Coordinate{x, y, z});
}
Q_DECLARE_METATYPE(Coordinate)
Q_DECLARE_METATYPE(CoordOfCube)
Q_DECLARE_METATYPE(floatCoordinate)
namespace std {
template<typename T, std::size_t x>
struct hash<Coord<T, x>> {
std::size_t operator()(const Coord<T, x> & cord) const {
return boost::hash_value(std::make_tuple(cord.x, cord.y, cord.z));
}
};
}
namespace std {
template<>
struct hash<CoordOfGPUCube> {
std::size_t operator()(const CoordOfGPUCube & cord) const {
return boost::hash_value(std::make_tuple(cord.x, cord.y, cord.z));
}
};
}