-
Notifications
You must be signed in to change notification settings - Fork 1
/
fundamentaldomaingenerator.cpp
220 lines (177 loc) · 6.1 KB
/
fundamentaldomaingenerator.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
#include "fundamentaldomaingenerator.h"
#include <QTime>
FundamentalDomainGenerator::FundamentalDomainGenerator(const GroupRepresentation<H2Isometry> &rho) : rho(rho)
{
assert(rho.getDiscreteGroup().isClosedSurfaceGroup());
tol = 0.00001;
xInit.setDiskCoordinate(Complex(0.0, 0.0));
delta = 0.0000001;
delX = Complex(1.0, 0.0);
delY = Complex(0.0, 1.0);
maxIterations = 100;
maxLineSearchIterations = 50;
}
H2Polygon FundamentalDomainGenerator::getOptimalFundamentalDomain() const
{
H2Point firstVertex = optimalStepGradientDescent(xInit);
H2Polygon out = rho.getFundamentalDomain(firstVertex);
if (!out.isConvex())
{
std::cout << "Warning in FundamentalDomainGenerator::getOptimalFundamentalDomain(): the polygon found is not convex" << std::endl;
}
return rho.getFundamentalDomain(firstVertex);
}
double FundamentalDomainGenerator::F(const H2Point &x) const
{
return diameterNorm(rho.getFundamentalDomain(x));
//return squareDiameterNorm(rho.getFundamentalDomain(x));
//return minAngleNorm(rho.getFundamentalDomain(x));
//return maxAngleNorm(rho.getFundamentalDomain(x));
//return minDistanceToNonNeighborSideNorm(rho.getFundamentalDomain(x));
//return energyNorm(rho.getFundamentalDomain(x));
//return distanceToIdealBoundaryNorm(rho.getFundamentalDomain(x));
//return isoperimetricNorm(rho.getFundamentalDomain(x));
}
double FundamentalDomainGenerator::Phi(const H2Point &x0, const Complex &u, const double &t) const
{
return F(H2Point::exponentialMap(x0, u, t));
}
Complex FundamentalDomainGenerator::gradF(const H2Point &x) const
{
double delXF = dPhi(x, delX, 0);
double delYF = dPhi(x, delY, 0);
Complex z = x.getDiskCoordinate();
double g = (1 - norm(z))*(1-norm(z))/4;
return g*Complex(delXF, delYF);
}
double FundamentalDomainGenerator::dPhi(const H2Point &x0, const Complex &u, const double &t) const
{
return (Phi(x0, u, t+delta)-Phi(x0, u, t-delta))/(2*delta);
}
double FundamentalDomainGenerator::diameterNorm(const H2Polygon &polygon)
{
return polygon.diameter();
}
double FundamentalDomainGenerator::squareDiameterNorm(const H2Polygon &polygon)
{
double diameter = polygon.diameter();
return diameter*diameter;
}
double FundamentalDomainGenerator::minAngleNorm(const H2Polygon &polygon)
{
return 2*M_PI - polygon.smallestAngle();
}
double FundamentalDomainGenerator::maxAngleNorm(const H2Polygon &polygon)
{
std::vector<double> angles = polygon.getInteriorAngles();
return *std::max_element(angles.begin(), angles.end());
}
double FundamentalDomainGenerator::minDistanceToNonNeighborSideNorm(const H2Polygon &polygon)
{
std::vector<H2GeodesicArc> sides = polygon.getSides();
std::vector<double> sideDistances, sideLengths;
for (const auto & side : sides)
{
for (const auto & otherSide : sides)
{
if (!H2GeodesicArc::shareEndpoint(side,otherSide))
{
sideDistances.push_back(H2Geodesic::distanceGeodesics(side.getGeodesic(),otherSide.getGeodesic()));
}
}
sideLengths.push_back(side.length());
}
return -*std::min_element(sideDistances.begin(),sideDistances.end());
}
double FundamentalDomainGenerator::energyNorm(const H2Polygon &polygon)
{
std::vector<H2Point> vertices = polygon.getVertices();
uint i, j, N = vertices.size();
double sum = 0, distance;
for (i=0; i+1!=N; ++i)
{
for (j=i+1; j!=N; ++j)
{
distance = H2Point::distance(vertices[i], vertices[j]);
sum += distance*distance;
}
}
return sum;
}
double FundamentalDomainGenerator::distanceToIdealBoundaryNorm(const H2Polygon &polygon)
{
std::vector<H2Point> vertices = polygon.getVertices();
std::vector<double> distances;
distances.reserve(vertices.size());
for (const auto &vertex : vertices)
{
distances.push_back(norm(vertex.getDiskCoordinate()));
}
return *std::max_element(distances.begin(), distances.end());
}
double FundamentalDomainGenerator::isoperimetricNorm(const H2Polygon &polygon)
{
std::vector<H2GeodesicArc> sides = polygon.getSides();
double sum = 0;
for (const auto &side : sides)
{
sum += side.length();
}
return sum*sum;
}
H2Point FundamentalDomainGenerator::lineSearch(const H2Point &x0, const Complex &u, double t0) const
{
double tol1 = tol;
double error = 1 + tol1;
double tk = t0, previous = t0;
double PhiL, PhiC, PhiR;
uint counter = 0;
while((error>tol1) && (counter!=maxLineSearchIterations))
{
PhiL = Phi(x0, u, previous - delta);
PhiC = Phi(x0, u, previous);
PhiR = Phi(x0, u, previous + delta);
if ((std::abs((PhiR - PhiL)/(2))<tol1) || (std::abs((PhiR + PhiL - 2*PhiC)/(delta)) < tol1))
{
break;
}
tk = previous - (1/2)*(PhiR - PhiL)/((PhiR + PhiL - 2*PhiC)/delta);
error = std::abs(tk - previous);
previous = tk;
++counter;
}
return H2Point::exponentialMap(x0, u, tk);
}
H2Point FundamentalDomainGenerator::optimalStepGradientDescent(const H2Point &x0) const
{
//clock_t start = clock();
H2Point xk = x0, previous = x0;
double error = 1.0 + tol;
uint counter = 0;
Complex z, gradFk;
double gradLength;
while((error>tol) && (counter!=maxIterations))
{
z = previous.getDiskCoordinate();
gradFk = gradF(previous);
gradLength = (2.0/(1.0 - norm(z)))*std::abs(gradFk);
if (gradLength < tol)
{
break;
}
xk = lineSearch(previous, -gradFk, gradLength);
error = H2Point::distance(xk, previous);
previous = xk;
++counter;
}
if (counter==maxIterations)
{
qDebug() << "Warning in FundamentalDomainGenerator::optimalStepGradientDescent: optimalStepGradientDescent failed, final error: " << error;
}
/*else
{
std::cout << "Success of optimalStepGradientDescent in " << counter << " iterations" << std::endl;
}
std::cout<< "Time elapsed: " << (clock()-start)*1.0/CLOCKS_PER_SEC << std::endl;*/
return xk;
}