-
Notifications
You must be signed in to change notification settings - Fork 5
/
PJ_qsc.c
381 lines (363 loc) · 13.4 KB
/
PJ_qsc.c
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
/*
* This implements the Quadrilateralized Spherical Cube (QSC) projection.
*
* Copyright (c) 2011, 2012 Martin Lambers <marlam@marlam.de>
*
* The QSC projection was introduced in:
* [OL76]
* E.M. O'Neill and R.E. Laubscher, "Extended Studies of a Quadrilateralized
* Spherical Cube Earth Data Base", Naval Environmental Prediction Research
* Facility Tech. Report NEPRF 3-76 (CSC), May 1976.
*
* The preceding shift from an ellipsoid to a sphere, which allows to apply
* this projection to ellipsoids as used in the Ellipsoidal Cube Map model,
* is described in
* [LK12]
* M. Lambers and A. Kolb, "Ellipsoidal Cube Maps for Accurate Rendering of
* Planetary-Scale Terrain Data", Proc. Pacfic Graphics (Short Papers), Sep.
* 2012
*
* You have to choose one of the following projection centers,
* corresponding to the centers of the six cube faces:
* phi0 = 0.0, lam0 = 0.0 ("front" face)
* phi0 = 0.0, lam0 = 90.0 ("right" face)
* phi0 = 0.0, lam0 = 180.0 ("back" face)
* phi0 = 0.0, lam0 = -90.0 ("left" face)
* phi0 = 90.0 ("top" face)
* phi0 = -90.0 ("bottom" face)
* Other projection centers will not work!
*
* In the projection code below, each cube face is handled differently.
* See the computation of the face parameter in the ENTRY0(qsc) function
* and the handling of different face values (FACE_*) in the forward and
* inverse projections.
*
* Furthermore, the projection is originally only defined for theta angles
* between (-1/4 * PI) and (+1/4 * PI) on the current cube face. This area
* of definition is named AREA_0 in the projection code below. The other
* three areas of a cube face are handled by rotation of AREA_0.
*/
#define PROJ_PARMS__ \
int face; \
double a_squared; \
double b; \
double one_minus_f; \
double one_minus_f_squared;
#define PJ_LIB__
#include <projects.h>
PROJ_HEAD(qsc, "Quadrilateralized Spherical Cube") "\n\tAzi, Sph.";
#define EPS10 1.e-10
/* The six cube faces. */
#define FACE_FRONT 0
#define FACE_RIGHT 1
#define FACE_BACK 2
#define FACE_LEFT 3
#define FACE_TOP 4
#define FACE_BOTTOM 5
/* The four areas on a cube face. AREA_0 is the area of definition,
* the other three areas are counted counterclockwise. */
#define AREA_0 0
#define AREA_1 1
#define AREA_2 2
#define AREA_3 3
/* Helper function for forward projection: compute the theta angle
* and determine the area number. */
static double
qsc_fwd_equat_face_theta(double phi, double y, double x, int *area) {
double theta;
if (phi < EPS10) {
*area = AREA_0;
theta = 0.0;
} else {
theta = atan2(y, x);
if (fabs(theta) <= FORTPI) {
*area = AREA_0;
} else if (theta > FORTPI && theta <= HALFPI + FORTPI) {
*area = AREA_1;
theta -= HALFPI;
} else if (theta > HALFPI + FORTPI || theta <= -(HALFPI + FORTPI)) {
*area = AREA_2;
theta = (theta >= 0.0 ? theta - PI : theta + PI);
} else {
*area = AREA_3;
theta += HALFPI;
}
}
return (theta);
}
/* Helper function: shift the longitude. */
static double
qsc_shift_lon_origin(double lon, double offset) {
double slon = lon + offset;
if (slon < -PI) {
slon += TWOPI;
} else if (slon > +PI) {
slon -= TWOPI;
}
return slon;
}
/* Forward projection, ellipsoid */
FORWARD(e_forward);
double lat, lon;
double theta, phi;
double t, mu; /* nu; */
int area;
/* Convert the geodetic latitude to a geocentric latitude.
* This corresponds to the shift from the ellipsoid to the sphere
* described in [LK12]. */
if (P->es) {
lat = atan(P->one_minus_f_squared * tan(lp.phi));
} else {
lat = lp.phi;
}
/* Convert the input lat, lon into theta, phi as used by QSC.
* This depends on the cube face and the area on it.
* For the top and bottom face, we can compute theta and phi
* directly from phi, lam. For the other faces, we must use
* unit sphere cartesian coordinates as an intermediate step. */
lon = lp.lam;
if (P->face == FACE_TOP) {
phi = HALFPI - lat;
if (lon >= FORTPI && lon <= HALFPI + FORTPI) {
area = AREA_0;
theta = lon - HALFPI;
} else if (lon > HALFPI + FORTPI || lon <= -(HALFPI + FORTPI)) {
area = AREA_1;
theta = (lon > 0.0 ? lon - PI : lon + PI);
} else if (lon > -(HALFPI + FORTPI) && lon <= -FORTPI) {
area = AREA_2;
theta = lon + HALFPI;
} else {
area = AREA_3;
theta = lon;
}
} else if (P->face == FACE_BOTTOM) {
phi = HALFPI + lat;
if (lon >= FORTPI && lon <= HALFPI + FORTPI) {
area = AREA_0;
theta = -lon + HALFPI;
} else if (lon < FORTPI && lon >= -FORTPI) {
area = AREA_1;
theta = -lon;
} else if (lon < -FORTPI && lon >= -(HALFPI + FORTPI)) {
area = AREA_2;
theta = -lon - HALFPI;
} else {
area = AREA_3;
theta = (lon > 0.0 ? -lon + PI : -lon - PI);
}
} else {
double q, r, s;
double sinlat, coslat;
double sinlon, coslon;
if (P->face == FACE_RIGHT) {
lon = qsc_shift_lon_origin(lon, +HALFPI);
} else if (P->face == FACE_BACK) {
lon = qsc_shift_lon_origin(lon, +PI);
} else if (P->face == FACE_LEFT) {
lon = qsc_shift_lon_origin(lon, -HALFPI);
}
sinlat = sin(lat);
coslat = cos(lat);
sinlon = sin(lon);
coslon = cos(lon);
q = coslat * coslon;
r = coslat * sinlon;
s = sinlat;
if (P->face == FACE_FRONT) {
phi = acos(q);
theta = qsc_fwd_equat_face_theta(phi, s, r, &area);
} else if (P->face == FACE_RIGHT) {
phi = acos(r);
theta = qsc_fwd_equat_face_theta(phi, s, -q, &area);
} else if (P->face == FACE_BACK) {
phi = acos(-q);
theta = qsc_fwd_equat_face_theta(phi, s, -r, &area);
} else if (P->face == FACE_LEFT) {
phi = acos(-r);
theta = qsc_fwd_equat_face_theta(phi, s, q, &area);
} else {
/* Impossible */
phi = theta = 0.0;
area = AREA_0;
}
}
/* Compute mu and nu for the area of definition.
* For mu, see Eq. (3-21) in [OL76], but note the typos:
* compare with Eq. (3-14). For nu, see Eq. (3-38). */
mu = atan((12.0 / PI) * (theta + acos(sin(theta) * cos(FORTPI)) - HALFPI));
t = sqrt((1.0 - cos(phi)) / (cos(mu) * cos(mu)) / (1.0 - cos(atan(1.0 / cos(theta)))));
/* nu = atan(t); We don't really need nu, just t, see below. */
/* Apply the result to the real area. */
if (area == AREA_1) {
mu += HALFPI;
} else if (area == AREA_2) {
mu += PI;
} else if (area == AREA_3) {
mu += HALFPI + PI;
}
/* Now compute x, y from mu and nu */
/* t = tan(nu); */
xy.x = t * cos(mu);
xy.y = t * sin(mu);
return (xy);
}
/* Inverse projection, ellipsoid */
INVERSE(e_inverse);
double mu, nu, cosmu, tannu;
double tantheta, theta, cosphi, phi;
double t;
int area;
/* Convert the input x, y to the mu and nu angles as used by QSC.
* This depends on the area of the cube face. */
nu = atan(sqrt(xy.x * xy.x + xy.y * xy.y));
mu = atan2(xy.y, xy.x);
if (xy.x >= 0.0 && xy.x >= fabs(xy.y)) {
area = AREA_0;
} else if (xy.y >= 0.0 && xy.y >= fabs(xy.x)) {
area = AREA_1;
mu -= HALFPI;
} else if (xy.x < 0.0 && -xy.x >= fabs(xy.y)) {
area = AREA_2;
mu = (mu < 0.0 ? mu + PI : mu - PI);
} else {
area = AREA_3;
mu += HALFPI;
}
/* Compute phi and theta for the area of definition.
* The inverse projection is not described in the original paper, but some
* good hints can be found here (as of 2011-12-14):
* http://fits.gsfc.nasa.gov/fitsbits/saf.93/saf.9302
* (search for "Message-Id: <9302181759.AA25477 at fits.cv.nrao.edu>") */
t = (PI / 12.0) * tan(mu);
tantheta = sin(t) / (cos(t) - (1.0 / sqrt(2.0)));
theta = atan(tantheta);
cosmu = cos(mu);
tannu = tan(nu);
cosphi = 1.0 - cosmu * cosmu * tannu * tannu * (1.0 - cos(atan(1.0 / cos(theta))));
if (cosphi < -1.0) {
cosphi = -1.0;
} else if (cosphi > +1.0) {
cosphi = +1.0;
}
/* Apply the result to the real area on the cube face.
* For the top and bottom face, we can compute phi and lam directly.
* For the other faces, we must use unit sphere cartesian coordinates
* as an intermediate step. */
if (P->face == FACE_TOP) {
phi = acos(cosphi);
lp.phi = HALFPI - phi;
if (area == AREA_0) {
lp.lam = theta + HALFPI;
} else if (area == AREA_1) {
lp.lam = (theta < 0.0 ? theta + PI : theta - PI);
} else if (area == AREA_2) {
lp.lam = theta - HALFPI;
} else /* area == AREA_3 */ {
lp.lam = theta;
}
} else if (P->face == FACE_BOTTOM) {
phi = acos(cosphi);
lp.phi = phi - HALFPI;
if (area == AREA_0) {
lp.lam = -theta + HALFPI;
} else if (area == AREA_1) {
lp.lam = -theta;
} else if (area == AREA_2) {
lp.lam = -theta - HALFPI;
} else /* area == AREA_3 */ {
lp.lam = (theta < 0.0 ? -theta - PI : -theta + PI);
}
} else {
/* Compute phi and lam via cartesian unit sphere coordinates. */
double q, r, s, t;
q = cosphi;
t = q * q;
if (t >= 1.0) {
s = 0.0;
} else {
s = sqrt(1.0 - t) * sin(theta);
}
t += s * s;
if (t >= 1.0) {
r = 0.0;
} else {
r = sqrt(1.0 - t);
}
/* Rotate q,r,s into the correct area. */
if (area == AREA_1) {
t = r;
r = -s;
s = t;
} else if (area == AREA_2) {
r = -r;
s = -s;
} else if (area == AREA_3) {
t = r;
r = s;
s = -t;
}
/* Rotate q,r,s into the correct cube face. */
if (P->face == FACE_RIGHT) {
t = q;
q = -r;
r = t;
} else if (P->face == FACE_BACK) {
q = -q;
r = -r;
} else if (P->face == FACE_LEFT) {
t = q;
q = r;
r = -t;
}
/* Now compute phi and lam from the unit sphere coordinates. */
lp.phi = acos(-s) - HALFPI;
lp.lam = atan2(r, q);
if (P->face == FACE_RIGHT) {
lp.lam = qsc_shift_lon_origin(lp.lam, -HALFPI);
} else if (P->face == FACE_BACK) {
lp.lam = qsc_shift_lon_origin(lp.lam, -PI);
} else if (P->face == FACE_LEFT) {
lp.lam = qsc_shift_lon_origin(lp.lam, +HALFPI);
}
}
/* Apply the shift from the sphere to the ellipsoid as described
* in [LK12]. */
if (P->es) {
int invert_sign;
double tanphi, xa;
invert_sign = (lp.phi < 0.0 ? 1 : 0);
tanphi = tan(lp.phi);
xa = P->b / sqrt(tanphi * tanphi + P->one_minus_f_squared);
lp.phi = atan(sqrt(P->a * P->a - xa * xa) / (P->one_minus_f * xa));
if (invert_sign) {
lp.phi = -lp.phi;
}
}
return (lp);
}
FREEUP; if (P) pj_dalloc(P); }
ENTRY0(qsc)
P->inv = e_inverse;
P->fwd = e_forward;
/* Determine the cube face from the center of projection. */
if (P->phi0 >= HALFPI - FORTPI / 2.0) {
P->face = FACE_TOP;
} else if (P->phi0 <= -(HALFPI - FORTPI / 2.0)) {
P->face = FACE_BOTTOM;
} else if (fabs(P->lam0) <= FORTPI) {
P->face = FACE_FRONT;
} else if (fabs(P->lam0) <= HALFPI + FORTPI) {
P->face = (P->lam0 > 0.0 ? FACE_RIGHT : FACE_LEFT);
} else {
P->face = FACE_BACK;
}
/* Fill in useful values for the ellipsoid <-> sphere shift
* described in [LK12]. */
if (P->es) {
P->a_squared = P->a * P->a;
P->b = P->a * sqrt(1.0 - P->es);
P->one_minus_f = 1.0 - (P->a - P->b) / P->a;
P->one_minus_f_squared = P->one_minus_f * P->one_minus_f;
}
ENDENTRY(P)