-
Notifications
You must be signed in to change notification settings - Fork 6
/
Octohedral.java
358 lines (291 loc) · 15.4 KB
/
Octohedral.java
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
/**
* MIT License
*
* Copyright (c) 2017 Justin Kunimune
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package maps;
import de.jtem.mfc.field.Complex;
import maps.Projection.Property;
import utils.Math2;
/**
* A class of maps that use octohedral octants. Very similar to Polyhedral, but much faster since
* it takes advantage of the fact that everything is orthogonal.
*
* @author jkunimune
*/
public class Octohedral {
public static final Projection WATERMAN = new OctohedralProjection(
"Waterman Butterfly", "A simple Cahill-esque octohedral map arrangement, with Antarctica left on.",
2*Math.sqrt(3), (Math.sqrt(3)-1)/2, 0b1010, Property.COMPROMISE, 3,
Configuration.BUTTERFLY) {
protected double[] faceProject(double lat, double lon) {
return Waterman.faceProject(lat, lon);
}
protected double[] faceInverse(double x, double y) {
return Waterman.faceInverse(x, y);
}
};
public static final Projection KEYES_BUTTERFLY = new OctohedralProjection(
"Cahill\u2013Keyes Butterfly", "A simple Cahill-esque octohedral map arrangement, with Antarctica left on.",
CahillKeyes.lMG, CahillKeyes.lMA, 0b1010, Property.COMPROMISE, 4,
Configuration.BUTTERFLY) {
protected double[] faceProject(double lat, double lon) {
return CahillKeyes.faceProjectD(Math.toDegrees(lat), Math.toDegrees(lon));
}
protected double[] faceInverse(double x, double y) {
double[] coords = CahillKeyes.faceInverseD(x, y);
return (coords == null) ? null :
new double[] {Math.toRadians(coords[0]), Math.toRadians(coords[1])};
}
};
public static final Projection KEYES_BASIC_M = new OctohedralProjection(
"Cahill\u2013Keyes Basic", "A simple M-shaped octohedral projection, with Antarctica broken into three pieces.",
CahillKeyes.lMG, CahillKeyes.lMA, 0b1010, Property.COMPROMISE, 3,
Configuration.M_PROFILE) {
protected double[] faceProject(double lat, double lon) {
return CahillKeyes.faceProjectD(Math.toDegrees(lat), Math.toDegrees(lon));
}
protected double[] faceInverse(double x, double y) {
double[] coords = CahillKeyes.faceInverseD(x, y);
return (coords == null) ? null :
new double[] {Math.toRadians(coords[0]), Math.toRadians(coords[1])};
}
};
public static final Projection CAHILL_KEYES = new OctohedralProjection(
"Cahill\u2013Keyes", "An M-shaped octohedral projection with Antarctica assembled in the center.",
CahillKeyes.lMG, CahillKeyes.lMA, 0b1010, Property.COMPROMISE, 4,
Configuration.M_W_S_POLE) {
public double[] project(double lat, double lon) {
return super.project(lat, lon + Math.PI/9); // apply the central meridian manually
}
protected double[] faceProject(double lat, double lon) {
return CahillKeyes.faceProjectD(Math.toDegrees(lat), Math.toDegrees(lon));
}
public double[] inverse(double x, double y) {
double[] coords = super.inverse(x, y);
if (coords == null) return null;
coords[1] = Math2.floorMod(coords[1] - Math.PI/9 + Math.PI, 2*Math.PI) - Math.PI; // apply the central meridian manually
return coords;
}
protected double[] faceInverse(double x, double y) {
double[] coords = CahillKeyes.faceInverseD(x, y);
return (coords == null) ? null :
new double[] {Math.toRadians(coords[0]), Math.toRadians(coords[1])};
}
};
public static final OctohedralProjection CONFORMAL_CAHILL = new OctohedralProjection(
"Cahill Conformal", "The conformal and only reproducable variant of Cahill's original map.",
Math.sqrt(3)/2, 0, 0b1000, Property.CONFORMAL, 3, Configuration.BUTTERFLY) {
private final double HEXAGON_SCALE = 1.112913; //this is 2^(2/3)/6*\int_0^\pi sin^(-1/3) x dx
private final double TOLERANCE = 1e-3;
private final double[] VERTEX = {0, Math.PI/4, -3*Math.PI/4}; // TODO this needs to be tilted a bit
protected double[] faceProject(double lat, double lon) {
double[] poleCoords = {lat, lon};
double[] vertCoords = obliquifySphc(lat, lon, VERTEX); //look at an oblique aspect from the nearest vertex
if (poleCoords[0] > vertCoords[0]) { //if this point is closer to the pole
Complex w = Complex.fromPolar(Math.pow(Math.tan(Math.PI/4-lat/2), 2/3.), lon*2/3.);
Complex z = polynomial(w); //project it as normal
return new double[] {z.getRe(), z.getIm()};
}
else { //if it is closer to the vertex
Complex w = Complex.fromPolar(
Math.pow(Math.tan(Math.PI/4-vertCoords[0]/2), 2/3.), vertCoords[1]*2/3.);
Complex zSkew = polynomial(w); //use the maclaurin series centred there
return new double[] {
-1/2.*zSkew.getRe() + Math.sqrt(3)/2*zSkew.getIm() + Math.sqrt(3)/2,
-Math.sqrt(3)/2*zSkew.getRe() - 1/2.*zSkew.getIm() + 1/2. };
}
}
protected double[] faceInverse(double x, double y) {
Complex z;
if (x < (1-y)/Math.sqrt(3)) //do the Newton Raphson from whichever vertex to which it is closest
z = new Complex(x, y);
else
z = new Complex(-1/2.*(x-Math.sqrt(3)/2) - Math.sqrt(3)/2*(y-1/2.),
Math.sqrt(3)/2*(x-Math.sqrt(3)/2) - 1/2.*(y-1/2.));
Complex w = z.divide(HEXAGON_SCALE);
Complex error = polynomial(w).minus(z);
for (int i = 0; i < 8 && error.abs() > TOLERANCE; i ++) {
Complex dzdw = derivative(w);
w = w.minus(error.divide(dzdw));
error = polynomial(w).minus(z);
}
double[] latLon = { Math.PI/2 - 2*Math.atan(Math.pow(w.abs(), 3/2.)), w.arg()*3/2. }; //inverse conic it back to spherical coordinates
if (x < (1-y)/Math.sqrt(3)) //if it was closest to that vertex, the result is easy
return latLon;
else //if it was closer to the other vertex, do some obliquifying
return obliquifyPlnr(latLon, VERTEX);
}
private Complex polynomial(Complex w) { //an approximation of the true conformal mapping function
w = w.times(Complex.fromPolar(1, -Math.PI/6));
Complex z = w.plus(w.pow(7).divide(21))
.plus(w.pow(11).divide(99)).plus(w.pow(13).divide(1287/16.));
return z.divide(Complex.fromPolar(HEXAGON_SCALE, -Math.PI/6));
}
private Complex derivative(Complex w) { //the derivative of polynomial()
w = w.times(Complex.fromPolar(1, -Math.PI/6));
Complex z = new Complex(1).plus(w.pow(6).divide(3))
.plus(w.pow(10).divide(9)).plus(w.pow(12).divide(99/16.));
return z.divide(Complex.fromPolar(HEXAGON_SCALE, -Math.PI/6));
}
};
public static final Projection CAHILL_CONCIALDI = new OctohedralProjection(
"Cahill\u2013Concialdi Bat", "A conformal octohedral projection with no extra cuts and a unique arrangement.",
Math.sqrt(3)/2, 0, 0b1000, Property.CONFORMAL, 4, Configuration.BAT_SHAPE) {
private final double lon0 = Math.toRadians(20);
public double[] project(double lat, double lon) {
lon = Math2.floorMod(lon - lon0 + Math.PI, 2*Math.PI) - Math.PI; // change the central meridian
return super.project(lat, lon);
}
protected double[] faceProject(double lat, double lon) {
return CONFORMAL_CAHILL.faceProject(lat, lon);
}
public double[] inverse(double x, double y) {
double[] coords = super.inverse(x, y);
if (coords != null)
coords[1] = Math2.floorMod(coords[1] + lon0 + Math.PI, 2*Math.PI) - Math.PI; // change the central meridian
return coords;
}
protected double[] faceInverse(double x, double y) {
return CONFORMAL_CAHILL.faceInverse(x, y);
}
};
private static abstract class OctohedralProjection extends Projection {
protected final double size;
private Configuration config;
public OctohedralProjection(String name, String desc, double altitude, double cutSize,
int fisc, Property property, int rating, Configuration config) {
super(name, desc,
config.fullWidth*altitude-config.cutWidth*cutSize,
config.fullHeight*altitude-config.cutHeight*cutSize, fisc,
(cutSize == 0) ? Type.OCTOHEDRAL : Type.TETRADECAHEDRAL, property, rating,
new String[] {}, new double[][] {}, config.hasAspect);
this.size = altitude;
this.config = config;
}
protected abstract double[] faceProject(double lat, double lon);
protected abstract double[] faceInverse(double x, double y);
public double[] project(double lat, double lon) {
for (double[] octant: config.octants) { // try each octant
double lonr = Math2.floorMod(lon - octant[3] + Math.PI, 2*Math.PI) - Math.PI; // relative longitude
if (Math.abs(lonr) > Math.PI/4 || lat < octant[4] || lat > octant[5]) // if it doesn't fit...
continue; // check the next one
if (octant.length > 7 && (lonr < octant[6] || lonr > octant[7])) // also check the longitude restriction
continue;
double xP = octant[0]*size, yP = octant[1]*size; // vertex coordinates
double th = octant[2]; // rotation angle
double[] coords = this.faceProject(Math.abs(lat), Math.abs(lonr));
double xMj = coords[0], yMj = coords[1]; //relative octant coordinates (Mj stands for "Mary Jo Graca")
if (lat < 0) //reflect the southern hemisphere over the equator
xMj = 2*size - xMj;
if (lonr < 0)
yMj = -yMj;
return new double[] {
xP + Math.sin(th)*xMj + Math.cos(th)*yMj,
yP - Math.cos(th)*xMj + Math.sin(th)*yMj + config.fullHeight*size/2 };
}
return new double[] {Double.NaN, Double.NaN}; // if none of the octants fit, return null
}
public double[] inverse(double x, double y) {
y -= config.fullHeight*size/2;
for (double[] octant: config.octants) { // try each octant
double xV = octant[0]*size, yV = octant[1]*size; // vertex coordinates
double th = octant[2]; // rotation angle
double xMj = Math.sin(th)*(x-xV) - Math.cos(th)*(y-yV); // do the coordinate change
double yMj = Math.cos(th)*(x-xV) + Math.sin(th)*(y-yV);
if (Math.sqrt(3)*Math.abs(yMj) > Math.min(xMj, 2*size-xMj) + 1e-12) // if the angle is wrong,
continue; // check the next one
double[] coords = this.faceInverse(Math.min(xMj, 2*size-xMj), Math.abs(yMj));
if (coords == null)
continue; // if you got nothing, keep looking
double lat = coords[0], lon = coords[1]; // project
lat *= Math.signum(size-xMj); // undo the reflections
lon *= Math.signum(yMj);
if (lat < octant[4] - 1e-6 || lat > octant[5] + 1e-6) // if the resulting coordinates are wrong
continue; // move on
if (octant.length > 7 && (lon < octant[6] - 1e-6 || lon > octant[7] + 1e-6)) // also check the longitude restriction
continue;
lon = Math2.floorMod(lon + octant[3] + Math.PI, 2*Math.PI)- Math.PI;
return new double[] { lat, lon };
}
return null;
}
}
private enum Configuration {
BUTTERFLY(4, 2, 4/Math.sqrt(3), Math.sqrt(3), true, new double[][] { //the classic four quadrants splayed out in a nice butterfly shape, with Antarctica divided and attached
{ 0, -1/Math.sqrt(3), -Math.PI/2, -3*Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 0, -1/Math.sqrt(3), -Math.PI/6, -Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 0, -1/Math.sqrt(3), Math.PI/6, Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 0, -1/Math.sqrt(3), Math.PI/2, 3*Math.PI/4, -Math.PI/2, Math.PI/2 },
}),
M_PROFILE(4, 0, Math.sqrt(3), Math.sqrt(3), true, new double[][] { //The more compact zigzag configuration with Antarctica divided and attached
{ -1, 0, -Math.PI/6, -3*Math.PI/4, -Math.PI/2, Math.PI/2 },
{ -1, 0, Math.PI/6, -Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 1, 0, -Math.PI/6, Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 1, 0, Math.PI/6, 3*Math.PI/4, -Math.PI/2, Math.PI/2 },
}),
M_W_S_POLE(4, 0, 2.008, Math.sqrt(3), false, new double[][] { //Keyes's current configuration, with Antarctica reassembled in the center
{ -1, 0, -Math.PI/6, -3*Math.PI/4, Math.toRadians(-64), Math.PI/2 },
{ -1, 0, Math.PI/6, -Math.PI/4, -Math.PI/2, Math.PI/2 },
{ 1, 0, -Math.PI/6, Math.PI/4, Math.toRadians(-64), Math.PI/2 },
{ 1, 0, Math.PI/6, 3*Math.PI/4, Math.toRadians(-64), Math.PI/2 },
{ -1.6976, -2.6036, 2*Math.PI/3, -3*Math.PI/4, -Math.PI/2, Math.toRadians(-64) },
{ 1.6036, -0.6976, -Math.PI/3, Math.PI/4, -Math.PI/2, Math.toRadians(-64) },
{ 0.9060, -3.3013, -5*Math.PI/6, 3*Math.PI/4, -Math.PI/2, Math.toRadians(-64) },
}),
BAT_SHAPE(3.47, 0, 2.03, 0, false, rotate(0, -1.55, Math.toRadians(5), new double[][] { //Luca Concialdi's obscure "Bat" arrangement that I liked.
{ 0, -0.5, -2*Math.PI/3, -Math.PI , 0, Math.PI/2, Math.toRadians(-9), 1 },
{ -3/Math.sqrt(3), 0.5, 0, -Math.PI , -Math.PI/2, 0, Math.toRadians( 9), 1 },
{ 0, -0.5, -Math.PI/3, -Math.PI/2, -Math.PI/2, Math.PI/2 },
{ 0, -0.5, 0, 0, -Math.PI/4, Math.PI/2 },
{ 0, -2.5, -2*Math.PI/3, 0, -Math.PI/2, -Math.PI/4, -1, 0 },
{ 0, -2.5, 2*Math.PI/3, 0, -Math.PI/2, -Math.PI/4, 0, 1 },
{ 0, -0.5, Math.PI/3, Math.PI/2, -Math.PI/2, Math.PI/2 },
{ 0, -0.5, 2*Math.PI/3, Math.PI , 0, Math.PI/2, -1, Math.toRadians(-9) },
{ 3/Math.sqrt(3), 0.5, 0, Math.PI , -Math.PI/2, 0, -1, Math.toRadians( 9) },
}));
public final double fullWidth, cutWidth, fullHeight, cutHeight;
public final boolean hasAspect;
public final double[][] octants; // array of {x, y (from top), rotation, \lambda_0, \phi_min, \phi_max[, \lambda_min, \lambda_max]}
// public double cutRatio; //this variable should be set by the map projection so that the configuration knows how big the cuts actually are
private Configuration(double fullWidth, double cutWidth,
double fullHeight, double cutHeight, boolean hasAspect,
double[][] octants) {
this.fullWidth = fullWidth; //the size of the configuration in altitudes ignoring cuts
this.cutWidth = cutWidth; //the change in width due to the cut in cut lengths
this.fullHeight = fullHeight; //and the cut sizes
this.cutHeight = cutHeight;
this.hasAspect = hasAspect;
this.octants = octants;
}
private static final double[][] rotate(double xP, double yP, double th, double[][] in) { // apply that rotation to the bat
double[][] out = new double[in.length][];
for (int i = 0; i < in.length; i ++) {
out[i] = new double[in[i].length];
System.arraycopy(in[i], 0, out[i], 0, in[i].length);
out[i][0] = Math.cos(th)*(in[i][0]-xP) - Math.sin(th)*(in[i][1]-yP) + xP;
out[i][1] = Math.sin(th)*(in[i][0]-xP) + Math.cos(th)*(in[i][1]-yP) + yP;
out[i][2] = in[i][2] + th;
}
return out;
}
}
}