forked from seub/Harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h2point.cpp
411 lines (346 loc) · 10.6 KB
/
h2point.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "h2point.h"
#include "h2isometry.h"
#include <Eigen/Dense>
H2Point::H2Point()
{
}
Complex H2Point::getUpperHalfPlaneCoordinate() const
{
Complex I(0.0, 1.0);
return -I*((z + I)/(z - I));
}
Complex H2Point::getDiskCoordinate() const
{
return z;
}
Complex H2Point::getKleinCoordinate() const
{
// complex z = getDiskCoordinate();
return 2.0*z / (1.0 + norm(z));
}
void H2Point::getHyperboloidCoordinate(double &x, double &y, double &z) const
{
double s = 1.0 - norm(this->z);
z = (2.0 / s) - 1.0;
x = (2.0*real(this->z) / s);
y = (2.0*imag(this->z) / s);
}
Complex H2Point::getHyperboloidProjection() const
{
return 2.0*z / (1.0 - norm(z));
}
void H2Point::setUpperHalfPlaneCoordinate(Complex z)
{
Complex I(0.0, 1.0);
this->z = I*((z - I)/(z + I));
}
void H2Point::setDiskCoordinate(Complex z)
{
this->z = z;
}
void H2Point::setHyperboloidProjection(Complex z)
{
this->z = z / (1.0 + sqrt(1.0 + norm(z)));
}
void H2Point::setKleinCoordinate(Complex z)
{
this->z = z / (1.0 + sqrt(1.0 - norm(z)));
}
double H2Point::distance(const H2Point & p1, const H2Point & p2)
{
double s;
Complex z1,z2;
z1 = p1.z;
z2 = p2.z;
s = 2.0 * (norm(z1 - z2) / ((1 - norm(z1))*(1 - norm(z2))));
return acosh(1.0 + s);
}
H2Point H2Point::midpoint(const H2Point &p1, const H2Point &p2)
{
Complex w1 = (p2.z - p1.z)/(1.0 - conj(p1.z)*p2.z);
Complex w2 = w1*sqrt((1.0 - sqrt(1.0 - norm(w1)))/(norm(w1)*(1.0 + sqrt(1.0 - norm(w1)))));
H2Point res;
res.setDiskCoordinate((w2 + p1.z)/(1.0 + conj(p1.z)*w2));
return res;
}
H2Point H2Point::exponentialMap(const H2Point &p0, const Complex &u, const double &t)
{
assert(norm(u)>0);
Complex z1 = tanh(t/2)*u/std::abs(u);
// NB: When translating to the origin via the map (z -z0)/(1 - bar(z0) z), u is only scaled by a positive factor.
Complex z0 = p0.getDiskCoordinate();
Complex z2 = (z1 + z0)/(1.0 + conj(z0)*z1);
H2Point out;
out.setDiskCoordinate(z2);
return out;
}
H2Point H2Point::proportionalPoint(const H2Point &p1, const H2Point &p2, const double &s)
{
// assert that 0 < s < 1
Complex w1 = (p2.z - p1.z)/(1.0 - conj(p1.z)*p2.z);
double d = log ((1+std::abs(w1))/(1-std::abs(w1)));
Complex w2 = w1*(exp(s*d) - 1.0)/(std::abs(w1)*(exp(s*d) + 1.0));
H2Point res;
res.setDiskCoordinate((w2 + p1.z)/(1.0 + conj(p1.z)*w2));
return res;
}
double H2Point::angle(const H2Point &previous, const H2Point &point, const H2Point &next)
{
Complex u = (previous.z - point.z) / (1.0 - conj(point.z)*previous.z);
Complex v = (next.z - point.z) / (1.0 - conj(point.z)*next.z);
return Tools::mod2Pi(arg(v*conj(u)));
}
double H2Point::tanHalfAngle(const H2Point &previous, const H2Point &point, const H2Point &next)
{
Complex u = (previous.z - point.z) / (1.0 - conj(point.z)*previous.z);
Complex v = (next.z - point.z) / (1.0 - conj(point.z)*next.z);
Complex zed = v*conj(u) / std::abs(v*conj(u));
double x = real(zed), y = imag(zed);
return (1-x)/y;
}
void H2Point::computeAffineWeights(const std::vector<H2Point> &neighbors, std::vector<double> &outputWeights) const
{
H2Point previous, current, next;
std::vector<double> preNeighborWeights;
double r, tan1, tan2, sum;
std::vector<double> distances;
outputWeights.clear();
previous = neighbors.back();
current = neighbors.front();
next = neighbors[1];
for(std::vector<H2Point>::size_type j=1; j+1<neighbors.size(); ++j)
{
r=H2Point::distance(*this, current);
distances.push_back(r);
tan1=H2Point::tanHalfAngle(current, *this, next);
tan2=H2Point::tanHalfAngle(previous, *this, current);
preNeighborWeights.push_back((1.0/(3*M_PI*r))*(tan1 + tan2));
previous = current;
current = next;
next = neighbors[j+1];
}
r=H2Point::distance(*this, current);
distances.push_back(r);
tan1=H2Point::tanHalfAngle(current, *this, next);
tan2=H2Point::tanHalfAngle(previous, *this, current);
preNeighborWeights.push_back((1.0/(3*M_PI*r))*(tan1 + tan2));
previous = current;
current = next;
next = neighbors.front();
r=H2Point::distance(*this, current);
distances.push_back(r);
tan1=H2Point::tanHalfAngle(current, *this, next);
tan2=H2Point::tanHalfAngle(previous, *this, current);
preNeighborWeights.push_back((1.0/(3*M_PI*r))*(tan1 + tan2));
sum = 0.0;
for (auto x : preNeighborWeights)
{
sum += x;
}
double epsilon = 1/sum;
for (auto preweight : preNeighborWeights)
{
outputWeights.push_back(epsilon*preweight);
}
}
void H2Point::computeNaiveWeights(const std::vector<H2Point> &neighbors, std::vector<double> &outputWeights) const
{
H2Point previous, current, next;
double r, sum;
std::vector<double> distances, angles;
outputWeights.clear();
previous = neighbors.back();
current = neighbors.front();
next = neighbors[1];
for(std::vector<H2Point>::size_type j=1; j+1<neighbors.size(); ++j)
{
r=H2Point::distance(*this, current);
distances.push_back(r);
angles.push_back(H2Point::angle(previous, *this, next)/2.0);
previous = current;
current = next;
next = neighbors[j+1];
}
r=H2Point::distance(*this, current);
distances.push_back(r);
angles.push_back(H2Point::angle(previous, *this, next)/2.0);
previous = current;
current = next;
next = neighbors.front();
r=H2Point::distance(*this, current);
distances.push_back(r);
angles.push_back(H2Point::angle(previous, *this, next)/2.0);
for (std::vector<double>::size_type l=0; l<angles.size(); ++l)
{
outputWeights.push_back(angles[l]/(2*M_PI*distances[l]));
}
sum = 0.0;
for (auto neighborWeight : outputWeights)
{
sum += neighborWeight;
}
for (auto & neighborWeight : outputWeights)
{
neighborWeight = neighborWeight/sum;
}
}
void H2Point::computeQuadraticWeights(const std::vector<H2Point> &neighbors, std::vector<double> &outputWeights) const
{
if (neighbors.size() != 6)
{
QString errorMessage = QString("Error in H2Point::computeQuadraticWeights: there needs to be 6 neighbors (you gave me %1) ").arg(QString::number(neighbors.size()));
throw(errorMessage);
}
std::vector<double> neighborsInTangentSpaceX, neighborsInTangentSpaceY;
double d, r, xj, yj;
Complex z;
using namespace Eigen;
MatrixXd m(6,6);
VectorXd vec(6), out(6);
H2Isometry A;
outputWeights.clear();
A.setByMappingPointToOrigin(*this);
std::vector<H2Point> translatedNeighbors = A*neighbors;
double epsilon = -1.0;
for(const auto & point : translatedNeighbors)
{
z = point.getDiskCoordinate();
d = std::abs(z);
r = log((1.0+d)/(1.0-d));
epsilon = ((epsilon < r) && epsilon > 0) ? epsilon : r;
neighborsInTangentSpaceX.push_back(r*real(z)/d);
neighborsInTangentSpaceY.push_back(r*imag(z)/d);
}
epsilon *= 0.1;
vec(0) = 1; vec(1) = 0; vec(2) = 0; vec(3) = 0;
vec(4) = epsilon*epsilon/4.0; vec(5) = epsilon*epsilon/4.0;
for(uint j=0; j!=6; ++j)
{
xj = neighborsInTangentSpaceX[j];
yj = neighborsInTangentSpaceY[j];
m(0,j) = 1;
m(1,j) = xj;
m(2,j) = yj;
m(3,j) = xj*yj;
m(4,j) = xj*xj;
m(5,j) = yj*yj;
}
out = m.colPivHouseholderQr().solve(vec);
for(uint j=0; j!=6; ++j)
{
outputWeights.push_back(out(j));
}
//Tests
/*int counter=0;
qDebug() << "List of weights: ";
for(int j=0; j<6; ++j)
{
if (out(j)<0.0)
{
++counter;
}
qDebug() << out(j);
}
qDebug() << "Number of negative weights:" << counter;
sum=0.0;
for(int j=0; j<6; ++j)
{
sum += out(j)*neighborsInTangentSpaceX[j];
}
qDebug() << "A mesh point has weighted sum of X coordinates " << sum;
sum=0.0;
for(int j=0; j<6; ++j)
{
sum += out(j)*neighborsInTangentSpaceY[j];
}
qDebug() << "A mesh point has weighted sum of Y coordinates " << sum;
sum=0.0;
for(int j=0; j<6; ++j)
{
sum += out(j)*neighborsInTangentSpaceX[j]*neighborsInTangentSpaceY[j];
}
qDebug() << "A mesh point has weighted sum of X*Y coordinates " << sum;
sum=0.0;
for(int j=0; j<6; ++j)
{
sum += out(j)*neighborsInTangentSpaceX[j]*neighborsInTangentSpaceX[j];
}
qDebug() << "A mesh point has weighted sum of X^2 coordinates " << sum-(epsilon*epsilon/4.0);
sum=0.0;
for(int j=0; j<6; ++j)
{
sum += out(j)*neighborsInTangentSpaceY[j]*neighborsInTangentSpaceY[j];
}
qDebug() << "A mesh point has weighted sum of Y^2 coordinates " << sum-(epsilon*epsilon/4.0);
sum = 0.0;
for (auto neighborWeight : outputWeights)
{
sum += neighborWeight;
}
qDebug() << "sum of weights = " << sum;*/
}
std::ostream & operator<<(std::ostream & out, const H2Point &p)
{
out << "H2Point with disk coordinate " << p.z;
return out;
}
bool operator ==(H2Point & p1, H2Point & p2)
{
return p1.z == p2.z;
}
H2Point H2Point::centroid(const std::vector<H2Point> &points, const std::vector<double> &weights)
{
if (points.size() != weights.size())
{
throw(QString("Error in H2Point::centroid: number of points does not match number of weights"));
}
std::vector<double> X, Y, Z;
double a, b, c, xOut=0.0, yOut=0.0, zOut=0.0;
H2Point out;
X.reserve(points.size());
Y.reserve(points.size());
Z.reserve(points.size());
for(const auto & p: points)
{
p.getHyperboloidCoordinate(a,b,c);
X.push_back(a);
Y.push_back(b);
Z.push_back(c);
}
for(unsigned int j=0; j<points.size(); ++j)
{
xOut += weights[j]*X[j];
yOut += weights[j]*Y[j];
zOut += weights[j]*Z[j];
}
double s = 1/ sqrt(zOut*zOut - xOut*xOut - yOut*yOut);
xOut = s*xOut;
yOut = s*yOut;
out.setHyperboloidProjection(Complex(xOut,yOut));
return out;
}
bool H2Point::compareAngles(const H2Point &p1, const H2Point &p2)
{
Complex z1 = p1.z - z;
Complex z2 = p2.z - z;
double x1,x2,y1,y2;
x1 = real(z1); y1 = imag(z1);
x2 = real(z2); y2 = imag(z2);
if (x1>=0.0)
{
if (y1>=0.0)
{
if (x2>=0.0 && y2>=0.0)
{
return (y1*x2 < x1*y2);
}
else return true;
}
else return (x2>0.0 && y2<0.0 && (y1*x2 < x1*y2));
}
else if (x2<0.0)
{
return (y1*x2 < x1*y2);
}
else return (y2<0.0);
}