forked from xw0/spherical_voronoi_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvMath.cpp
287 lines (250 loc) · 9.03 KB
/
svMath.cpp
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
//
// svMath.cpp
// SphericalVoronoi
//
// Created by Home on 2014-06-28.
// Copyright (c) 2014 whenitsdone.org. All rights reserved.
//
//#include "svPrefix.h"
#include "svMath.h"
#include <numeric>
#include <glm/gtc/matrix_transform.hpp>
namespace sv
{
std::tuple<Real3, CubeFaceBitSet> Point::cubeCoord() const
{
Real3 absPos = glm::abs(position);
Real3 result;
CubeFaceBitSet faceSet;
Real ratio;
if (absPos.z >= glm::max(absPos.x, absPos.y))
{
ratio = 1.0 / absPos.z;
faceSet |= (position.z > 0 ? CF_POSZ_BITMASK : CF_NEGZ_BITMASK);
if (absPos.z == absPos.x)
{
faceSet |= (position.x > 0 ? CF_POSX_BITMASK : CF_NEGX_BITMASK);
}
if (absPos.z == absPos.y)
{
faceSet |= (position.y > 0 ? CF_POSY_BITMASK : CF_NEGY_BITMASK);
}
}
else if (absPos.y >= glm::max(absPos.x, absPos.z))
{
ratio = 1.0 / absPos.y;
faceSet |= (position.y > 0 ? CF_POSY_BITMASK : CF_NEGY_BITMASK);
if (absPos.y == absPos.x)
{
faceSet |= (position.x > 0 ? CF_POSX_BITMASK : CF_NEGX_BITMASK);
}
if (absPos.y == absPos.z)
{
faceSet |= (position.z > 0 ? CF_POSZ_BITMASK : CF_NEGZ_BITMASK);
}
}
else if (absPos.x >= glm::max(absPos.y, absPos.z))
{
ratio = 1.0 / absPos.x;
faceSet |= (position.x > 0 ? CF_POSX_BITMASK : CF_NEGX_BITMASK);
if (absPos.x == absPos.y)
{
faceSet |= (position.y > 0 ? CF_POSY_BITMASK : CF_NEGY_BITMASK);
}
if (absPos.x == absPos.z)
{
faceSet |= (position.z > 0 ? CF_POSZ_BITMASK : CF_NEGZ_BITMASK);
}
}
result = position * ratio;
return std::tie(result, faceSet);
}
Real3 Point::tangent() const
{
CubeFaceBitSet bitSet;
Real3 binormal(1, 0, 0);
if (bitSet.test(CF_POSX))
{
binormal = Real3(0, 1, 0);
}
else if (bitSet.test(CF_NEGX))
{
binormal = Real3(0, 1, 0);
}
else if (bitSet.test(CF_POSY))
{
binormal = Real3(1, 0, 0);
}
else if (bitSet.test(CF_NEGY))
{
binormal = Real3(-1, 0, 0);
}
else if (bitSet.test(CF_POSZ))
{
binormal = Real3(0, 1, 0);
}
else if (bitSet.test(CF_NEGZ))
{
binormal = Real3(0, 1, 0);
}
else
{
assert(false);
}
Real3 normal = position;
Real3 result = glm::normalize(glm::cross(binormal, normal));
return result;
}
Real3 Point::binormal() const
{
Real3 t = tangent();
Real3 normal = position;
Real3 result = glm::normalize(glm::cross(normal, t));
return result;
}
// http://www.cgafaq.info/wiki/Intersection_of_three_planes
bool threePlanesIntersection(const Plane& planeA, const Plane& planeB, const Plane& planeC, Real3& result)
{
Real3 bcCross = glm::cross(planeB.normal(), planeC.normal());
Real denom = glm::dot(planeA.normal(), bcCross);
if (denom == 0) {
result = Real3(0);
return false;
}
else {
result = (-planeA.distance() * bcCross
- planeB.distance() * glm::cross(planeC.normal(), planeA.normal())
- planeC.distance() * glm::cross(planeA.normal(), planeB.normal())) / denom;
return true;
}
}
// http://tavianator.com/2011/05/fast-branchless-raybounding-box-intersections/
bool rayAabbIntersection(const Ray& ray, const AABB& aabb)
{
Real3 n_inv = Real3(1.0) / ray.direction();
double tx1 = (aabb.min().x - ray.origin().x)*n_inv.x;
double tx2 = (aabb.max().x - ray.origin().x)*n_inv.x;
double tmin = glm::min(tx1, tx2);
double tmax = glm::max(tx1, tx2);
double ty1 = (aabb.min().y - ray.origin().y)*n_inv.y;
double ty2 = (aabb.max().y - ray.origin().y)*n_inv.y;
tmin = glm::max(tmin, glm::min(ty1, ty2));
tmax = glm::min(tmax, glm::max(ty1, ty2));
double tz1 = (aabb.min().z - ray.origin().z)*n_inv.z;
double tz2 = (aabb.max().z - ray.origin().z)*n_inv.z;
tmin = glm::max(tmin, glm::min(tz1, tz2));
tmax = glm::min(tmax, glm::max(tz1, tz2));
return tmax >= glm::max(tmin, 0.0);
}
namespace Util
{
std::vector<Real3> splitSphericalLineSegment(const Point& start, const Point& end, Real deltaAngle)
{
std::vector<Real3> result;
assert(start.position != -end.position);
auto direction = glm::normalize(glm::cross(start.position, end.position));
float distance = glm::acos(glm::dot(start.position, end.position));
result.push_back(start.position);
for (auto angle=deltaAngle; angle<distance; angle+=deltaAngle)
{
Mat4 rotation = glm::rotate(Mat4(1.0), angle, direction);
Real3 pos = glm::normalize(Real3(rotation * Real4(start.position, 1.0)));
result.push_back(pos);
}
result.push_back(end.position);
return result;
}
Real lagrangeInterpolate(Real x, const std::vector<Real>& xArray, const std::vector<Real>& yArray)
{
assert(xArray.size() == yArray.size());
Real sum = 0.0;
for (unsigned int i = 0; i < xArray.size(); ++i)
{
Real Xi, Yi;
Xi = xArray[i];
Yi = yArray[i];
Real factor = 1.0;
for (unsigned int j = 0; j < xArray.size(); ++j)
{
if (i != j)
{
Real Xj = xArray[j];
factor *= (x - Xj) / (Xi - Xj);
}
}
sum += factor * Yi;
}
return sum;
}
Real interpolateSphericalSamples(const Point& p0, const std::vector<Point>& points, const std::vector<Real>& values)
{
Real totalSqrDistance = std::accumulate(points.begin(), points.end(), 0.0, [p0](Real res, const Point& p) {
Real d = p.sphericalDistance(p0);
return res + d * d;
});
Real sum = 0.0;
Real weight = 0.0;
for (size_t i = 0; i < points.size(); ++i)
{
const Point& p = points[i];
Real d = p.sphericalDistance(p0);
Real w = (totalSqrDistance - d*d) / totalSqrDistance;
sum += w * values[i];
weight += w;
}
return sum / weight;
}
Real computeTriangleArea(const Real3& p0, const Real3& p1, const Real3& p2)
{
Real3 v12 = p2 - p1;
Real3 v02 = p2 - p0;
Real3 v12n = glm::normalize(v12);
Real t = glm::dot(v02, v12n);
Real3 c = p2 - v12n * t;
Real d = glm::distance(p0, c);
Real l12 = glm::length(v12);
return l12 * d * 0.5;
}
void faceAxisDirection(ECubeFace face, Real3& s_dir, Real3& t_dir, Real3& p_dir)
{
switch (face)
{
case CF_POSX:
p_dir = Real3(1, 0, 0);
s_dir = Real3(0, 0, -1);
t_dir = Real3(0, 1, 0);
break;
case CF_NEGX:
p_dir = Real3(-1, 0, 0);
s_dir = Real3(0, 0, 1);
t_dir = Real3(0, 1, 0);
break;
case CF_POSY:
p_dir = Real3(0, 1, 0);
s_dir = Real3(0, 0, 1);
t_dir = Real3(1, 0, 0);
break;
case CF_NEGY:
p_dir = Real3(0, -1, 0);
s_dir = Real3(0, 0, 1);
t_dir = Real3(-1, 0, 0);
break;
case CF_POSZ:
p_dir = Real3(0, 0, 1);
s_dir = Real3(1, 0, 0);
t_dir = Real3(0, 1, 0);
break;
case CF_NEGZ:
p_dir = Real3(0, 0, -1);
s_dir = Real3(-1, 0, 0);
t_dir = Real3(0, 1, 0);
break;
default:
assert(0);
p_dir = Real3(1, 0, 0);
s_dir = Real3(0, 0, -1);
t_dir = Real3(0, 1, 0);
}
}
}
}