forked from xw0/spherical_voronoi_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svMath.hpp
322 lines (273 loc) · 7.83 KB
/
svMath.hpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// svMath.hpp
// SphericalVoronoi
//
// Created by Home on 2014-06-27.
// Copyright (c) 2014 whenitsdone.org. All rights reserved.
//
#ifndef SphericalVoronoi_svMath_hpp
#define SphericalVoronoi_svMath_hpp
// cereal serialization
template <class Archive>
inline void save(Archive& archive, glm::vec3 const& v)
{
archive(v.x, v.y, v.z);
}
template <class Archive>
inline void load(Archive& archive, glm::vec3& v)
{
archive(v.x, v.y, v.z);
}
template <class Archive>
inline void save(Archive& archive, glm::vec2 const& v)
{
archive(v.x, v.y);
}
template <class Archive>
inline void load(Archive& archive, glm::vec2& v)
{
archive(v.x, v.y);
}
namespace sv
{
inline AABB::AABB()
{
reset();
}
inline AABB::AABB(const Real3& p)
: m_min(p), m_max(p)
{
}
inline AABB::AABB(const Real3& min, const Real3& max)
: m_min(min), m_max(max)
{
assert(m_min.x < m_max.x);
assert(m_min.y < m_max.y);
assert(m_min.z < m_max.z);
}
inline void AABB::reset()
{
m_min = Real3(FLT_MAX);
m_max = Real3(-FLT_MAX);
}
inline bool AABB::isValid() const
{
bool result = m_min.x <= m_max.x;
if (result)
{
assert(m_min.y <= m_max.y);
assert(m_min.z <= m_max.z);
}
return result;
}
inline bool AABB::isEmpty() const
{
return m_min == m_max;
}
inline void AABB::unionWith(const Real3& p)
{
if (isValid())
{
m_min = glm::min(m_min, p);
m_max = glm::max(m_max, p);
}
else
{
m_min = m_max = p;
}
}
inline void AABB::unionWith(const AABB& aabb)
{
if (isValid())
{
m_min = glm::min(m_min, aabb.m_min);
m_max = glm::max(m_max, aabb.m_max);
}
else
{
*this = aabb;
}
}
inline bool AABB::contains(const Real3& p) const
{
if (!isValid()) return false;
return
(m_min.x <= p.x && p.x <= m_max.x) &&
(m_min.y <= p.y && p.y <= m_max.y) &&
(m_min.z <= p.z && p.z <= m_max.z);
}
inline bool AABB::contains(const AABB& aabb) const
{
if (!isValid() || !aabb.isValid()) return false;
return
(m_min.x <= aabb.m_min.x && aabb.m_max.x <= m_max.x) &&
(m_min.y <= aabb.m_min.y && aabb.m_max.y <= m_max.y) &&
(m_min.z <= aabb.m_min.z && aabb.m_max.z <= m_max.z);
}
inline void AABB::getMajorVertices(const Real3& direction, Real3& P, Real3& N) const
{
if (direction.x >= 0)
{
P.x = m_max.x;
N.x = m_min.x;
}
else
{
P.x = m_min.x;
N.x = m_max.x;
}
if (direction.y >= 0)
{
P.y = m_max.y;
N.y = m_min.y;
}
else
{
P.y = m_min.y;
N.y = m_max.y;
}
if (direction.z >= 0)
{
P.z = m_max.z;
N.z = m_min.z;
}
else
{
P.z = m_min.z;
N.z = m_max.z;
}
}
inline bool AABB::operator == (const AABB& aabb) const
{
if (!isValid() || !aabb.isValid()) return false;
return (m_min == aabb.m_min && m_max == aabb.m_max);
}
inline Ray::Ray()
: Ray(Real3(0.0), Real3(1.0, 0, 0))
{
}
inline Ray::Ray(const Real3& origin, const Real3& direction)
{
setOrigin(origin);
setDirection(direction);
}
inline Plane::Plane() {}
inline Plane::Plane(const Plane& other)
: m_normal(other.m_normal),
m_distance(other.m_distance)
{}
inline Plane::Plane(const Real3& normal, Real distance)
: m_normal(normal),
m_distance(distance)
{}
inline Plane::Plane(const Real4& vec)
: m_normal(vec),
m_distance(vec.w)
{}
inline Plane::Plane(const Real3& a, const Real3& b, const Real3& c)
{
m_normal = glm::normalize(glm::cross(b - a, c - a));
m_distance = - glm::dot(a, m_normal);
}
inline Plane Plane::normalize() const {
Plane res;
Real denom = 1 / glm::length(m_normal);
res.m_normal = m_normal * denom;
res.m_distance = m_distance * denom;
return res;
}
// The transform passed in should be the inverse transpose or be an orthogonal matrix.
// http://stackoverflow.com/questions/7685495/transforming-a-3d-plane-by-4x4-matrix
inline Plane Plane::transform(const glm::mat4 transform) const {
Plane res;
res.m_normal = Real3(Mat4(transform) * Real4(m_normal, 0));
res.m_distance = m_distance - glm::dot(getTransformPosition(Mat4(transform)), res.m_normal);
return res;
}
inline Real Plane::distance(const Real3& point) const {
return glm::dot(m_normal, point) + m_distance;
}
inline bool Plane::pointOnSide(const Real3& point) const {
return distance(point) >= 0;
}
// http://paulbourke.net/geometry/planeline/
inline bool Plane::lineIntersection(const Real3& ptA, const Real3& ptB, Real3& resultDestination) const {
Real3 vec = m_normal * (ptA - ptB);
Real denom = vec.x + vec.y + vec.z;
if(denom == 0) {
//line is perpendicular to plane
return false;
}
vec = m_normal * ptA;
Real dist = (vec.x + vec.y + vec.z + m_distance) / denom;
if(dist >= (Real)0 && dist <= (Real)1) {
resultDestination = ptA + (ptB - ptA) * dist;
return true;
}
else {
return false;
}
}
template <typename T>
inline PositionT<T>::PositionT()
: m_face(CF_INVALID), m_height(1.0f), m_surfacePoint(0), m_spacePosition(0)
{
}
template <typename T>
inline PositionT<T>::PositionT(ECubeFace face, T s, T t, T p)
: m_face(face), m_height(p)
{
switch (face)
{
case CF_POSX:
m_surfacePoint = Vec3(1.0, t, -s);
break;
case CF_NEGX:
m_surfacePoint = Vec3(-1.0, t, s);
break;
case CF_POSY:
m_surfacePoint = Vec3(t, 1.0, s);
break;
case CF_NEGY:
m_surfacePoint = Vec3(-t, -1.0, s);
break;
case CF_POSZ:
m_surfacePoint = Vec3(s, t, 1.0);
break;
case CF_NEGZ:
m_surfacePoint = Vec3(-s, t, -1.0);
break;
default:
assert(false);
}
m_spacePosition = glm::normalize(m_surfacePoint) * m_height;
}
template <typename T>
inline PositionT<T>::PositionT(ECubeFace face, const typename PositionT<T>::Vec3& stp)
: Position(face, stp.s, stp.t, stp.p)
{
}
template <typename T>
inline typename PositionT<T>::Vec3 PositionT<T>::stpCoords() const
{
switch (m_face)
{
case CF_POSX:
return Vec3(-m_surfacePoint.z, m_surfacePoint.y, m_height);
case CF_NEGX:
return Vec3(m_surfacePoint.z, m_surfacePoint.y, m_height);
case CF_POSY:
return Vec3(m_surfacePoint.z, m_surfacePoint.x, m_height);
case CF_NEGY:
return Vec3(m_surfacePoint.z, -m_surfacePoint.x, m_height);
case CF_POSZ:
return Vec3(m_surfacePoint.x, m_surfacePoint.y, m_height);
case CF_NEGZ:
return Vec3(-m_surfacePoint.x, m_surfacePoint.y, m_height);
default:
assert(false);
return Vec3(0, 0, 0);
}
}
}
#endif