forked from philipperemy/digital-setting-circles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.cpp
506 lines (389 loc) · 11.1 KB
/
Calc.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "MathLib.h"
#include "Calc.h"
#include <iostream>
double Calc::sqr(double x)
{
return x*x;
}
void Calc::printDebugMsg(std::string msg)
{
std::cerr << msg << std::endl;
}
void Calc::VVtoW()
{
W = VV[1][1]*VV[2][2]*VV[3][3]+VV[1][2]*VV[2][3]*VV[3][1];
W = W+VV[1][3]*VV[3][2]*VV[2][1];
W = W-VV[1][3]*VV[2][2]*VV[3][1]-VV[1][1]*VV[3][2]*VV[2][3];
W = W-VV[1][2]*VV[2][1]*VV[3][3];
}
void Calc::YY1toFH(bool flipped)
{
double C = MathLib::sqrt(sqr(YY[1][1])+sqr(YY[2][1]));
// Since C is always non-negative, H will be between -90 and 90 degrees.
if (C==0 && YY[3][1]==0)
H = 0; // Undefined result!
else
H = MathLib::atan2(YY[3][1], C);
if (C==0)
F = 1; // Elevation is +-90 deg, so az is arbitrary.
else {
// Note: Since C>0, both YY[2][1] and YY[1][1] must be != 0.
F = MathLib::atan2(YY[2][1], YY[1][1]);
// The atan2() function provides a 4-quadrant result, but in
// the -p...pi range instead of 0...2*pi.
if (F<0)
F += (2*pi);
}
// No longer any need to clip F to 0..2*pi.
// If desired, convert the result to flipped mode.
if (flipped) {
// Flip the elevation to the other side of the zenith.
if (H >= 0)
H = pi - H;
else
H = -pi - H;
// Rotate (not flip) the azimuth 180 degrees.
F -= pi;
if (F<0)
F += (2*pi);
}
}
void Calc::AzEltoYY0()
{
H += Z3;
YY[1][0] = MathLib::cos(F)*MathLib::cos(H) - MathLib::sin(F)*Z2 + MathLib::sin(F)*MathLib::sin(H)*Z1;
YY[2][0] = MathLib::sin(F)*MathLib::cos(H) + MathLib::cos(F)*Z2 - MathLib::cos(F)*MathLib::sin(H)*Z1;
YY[3][0] = MathLib::sin(H);
}
void Calc::AzElToYY1(void)
{
YY[1][1] = MathLib::cos(F)*MathLib::cos(H) + MathLib::sin(F)*Z2 - MathLib::sin(F)*MathLib::sin(H)*Z1;
YY[2][1] = MathLib::sin(F)*MathLib::cos(H) - MathLib::cos(F)*Z2 + MathLib::cos(F)*MathLib::sin(H)*Z1;
YY[3][1] = MathLib::sin(H);
}
void Calc::TrueToApparentAzEl(bool flipped)
{
AzElToYY1();
YY1toFH(flipped);
H -= Z3;
}
void Calc::ApparentToTrueAzEl()
{
int J;
AzEltoYY0();
for (J=1; J<=3; J++)
YY[J][1] = YY[J][0];
YY1toFH(false);
}
double Calc::calcApparentSeparation()
{
double corrAz1, corrAz2, corrEl1, corrEl2;
F = alignAz[1];
H = alignEl[1];
ApparentToTrueAzEl();
corrAz1 = F;
corrEl1 = H;
F = alignAz[2];
H = alignEl[2];
ApparentToTrueAzEl();
corrAz2 = F;
corrEl2 = H;
return calcSeparation(corrAz1, corrEl1, corrAz2, corrEl2);
}
bool Calc::optimizeZ3()
{
double maxGuess;
double upperGuess, lowerGuess;
double upperError, lowerError;
// Our residual error threshold
const double maxErr = 0.01*radPerDeg;
int ntries;
// Calculate the true angular separation of the two stars,
// based on their RA/Dec. This need be calculated only once.
const double trueSep = calcSeparation(alignRA[1], alignDec[1], alignRA[2], alignDec[2]);
if (!MathLib::finite(trueSep)) {
printDebugMsg("Can't calculate star separation!");
return false;
}
if (trueSep < 5.0*radPerDeg) {
printDebugMsg("Stars too close together!");
return false;
}
maxGuess = 10.0*radPerDeg;
if (maxGuess > trueSep/2)
maxGuess = trueSep/2;
Z3 = upperGuess = maxGuess;
upperError = calcApparentSeparation() - trueSep;
if (MathLib::fabs(upperError) <= maxErr) {
printDebugMsg("Lucky pos. first guess");
return true; // Lucky guess!
}
Z3 = lowerGuess = -maxGuess;
lowerError = calcApparentSeparation() - trueSep;
if (MathLib::fabs(lowerError) <= maxErr) {
printDebugMsg("Lucky neg. first guess");
return true; // Lucky guess!
}
// For the interpolation to succeed, the upper and
// lower errors must be of opposite sign.
if (upperError * lowerError > 0.0) {
printDebugMsg("Correct Z3 out of range");
return false;
}
ntries = 0;
for (;;) {
double newGuess, newError;
ntries++;
// Interpolate between the upper and lower guesses to
// get a new guess.
Z3 = newGuess = lowerGuess +
((lowerError/(lowerError-upperError))*(upperGuess-lowerGuess));
newError = calcApparentSeparation() - trueSep;
if (MathLib::fabs(newError) <= maxErr) {
std::cout << "Converged after " << ntries << " tries: Z3= " << Z3*degPerRad << std::endl;
return true; // We have converged! Z3 is set.
}
if (ntries > 25) {
printDebugMsg("No convergence after 25 tries");
return false; // Failure to converge
}
// NewGuess replaces either lowerGuess or upperGuess, depending
// on the sign of the newError.
if (lowerError * newError > 0) {
// newError has the same sign as lowerError
lowerGuess = newGuess;
lowerError = newError;
} else {
upperGuess = newGuess;
upperError = newError;
}
}
// We should never get here!
printDebugMsg("Broke out of loop!?!");
return false;
}
/////////////////////////////////////////////////////////////////
// Externally callable functions ///////////////////////////////
/////////////////////////////////////////////////////////////////
// A utility function. Has no affect on main conversion routines.
// Works equally well for any polar coordinate system: altaz or
// equatorial.
double Calc::calcSeparation(double az1, double el1, double az2, double el2)
{
double cossep;
cossep = (MathLib::sin(el1)*MathLib::sin(el2)) + (MathLib::cos(el1)*MathLib::cos(el2)*MathLib::cos(az1-az2));
// Avoid a domain error for acos due to rounding.
if (cossep >= 1.0)
return 0.0;
if (cossep <= -1.0)
return pi;
return MathLib::acos(cossep);
}
// Same as above, but values in degrees.
double Calc::calcDegSeparation(double az1, double el1, double az2, double el2)
{
return calcSeparation(az1*radPerDeg, el1*radPerDeg,
az2*radPerDeg, el2*radPerDeg);
}
// No longer necessary to call this before setAlignmentData().
void Calc::setZ123(double z1, double z2, double z3)
{
// All are converted to radians.
Z1 = z1 * radPerDeg;
Z2 = z2 * radPerDeg;
Z3 = z3 * radPerDeg;
}
double Calc::getZ3()
{
return Z3*degPerRad;
}
// Angles in degrees, time in seconds.
void Calc::setAlignmentData(int i, double ra, double dec,
double az, double el, double time)
{
if (i<0 || i>1)
return;
i++; // I must be 1 or 2 to index the arrays!
// This entire package measures azimuth backwards!
alignAz[i] = (360.0-az)*radPerDeg;
alignEl[i] = el*radPerDeg;
alignRA[i] = (ra-degreesPerMinute*time)/degPerRad;
alignDec[i] = dec/degPerRad;
}
// Call after calling setAlignmentData twice.
bool Calc::finalizeAlignment(bool optimize_z3)
{
int I, J, L, M, N;
double A;
double E;
bool status;
if (optimize_z3) {
Z3 = 0.0; // If optimization fails, leave Z3 set to 0.0
status = optimizeZ3();
} else
status = true;
// Keep going even if the Z3 optimization fails.
// Initialize columns 1 and 2 of YY with the alignment az/el data,
// corrected with Z123.
F = alignAz[1];
H = alignEl[1];
AzEltoYY0();
YY[1][1] = YY[1][0]; YY[2][1] = YY[2][0]; YY[3][1] = YY[3][0];
F = alignAz[2];
H = alignEl[2];
AzEltoYY0();
YY[1][2] = YY[1][0]; YY[2][2] = YY[2][0]; YY[3][2] = YY[3][0];
// Set third column of array YY.
YY[1][3]=YY[2][1]*YY[3][2]-YY[3][1]*YY[2][2];
YY[2][3]=YY[3][1]*YY[1][2]-YY[1][1]*YY[3][2];
YY[3][3]=YY[1][1]*YY[2][2]-YY[2][1]*YY[1][2];
// Normalize third column of array YY.
A=MathLib::sqrt(sqr(YY[1][3])+sqr(YY[2][3])+sqr(YY[3][3]));
if (A > 0.0) {
// Avoid 0/0 error if third column is all zeros
for (I=1; I<=3; I++) {
YY[I][3] /= A;
}
}
// Initialize columns 1 and 2 of XX with the star RA/Dec data.
XX[1][1] = MathLib::cos(alignDec[1])*MathLib::cos(alignRA[1]);
XX[2][1] = MathLib::cos(alignDec[1])*MathLib::sin(alignRA[1]);
XX[3][1] = MathLib::sin(alignDec[1]);
XX[1][2] = MathLib::cos(alignDec[2])*MathLib::cos(alignRA[2]);
XX[2][2] = MathLib::cos(alignDec[2])*MathLib::sin(alignRA[2]);
XX[3][2] = MathLib::sin(alignDec[2]);
// Set third column of array XX.
XX[1][3]=XX[2][1]*XX[3][2]-XX[3][1]*XX[2][2];
XX[2][3]=XX[3][1]*XX[1][2]-XX[1][1]*XX[3][2];
XX[3][3]=XX[1][1]*XX[2][2]-XX[2][1]*XX[1][2];
// Normalize third column of array XX.
A=MathLib::sqrt(sqr(XX[1][3])+sqr(XX[2][3])+sqr(XX[3][3]));
if (A > 0.0) {
// Avoid 0/0 error if third column is all zeros
for (I=1; I<=3; I++) {
XX[I][3] /= A;
}
}
// TRANSFORM MATRIX
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
VV[I][J]=XX[I][J];
}
}
VVtoW(); E=W;
for (M=1; M<=3; M++) {
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
VV[I][J]=XX[I][J];
}
}
for (N=1; N<= 3; N++) {
VV[1][M]=0; VV[2][M]=0; VV[3][M]=0; VV[N][M]=1;
VVtoW();
// Avoid problems with divide-by-zero. Am not sure
// under what conditions this could actually happen...
if (W==0 && E==0) {
QQ[M][N]=1;
} else if (E==0) {
QQ[M][N]=1;
status = false;
} else {
QQ[M][N]=W/E;
}
}
}
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
RR[I][J]=0;
}
}
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
for (L=1; L<=3; L++) {
RR[I][J] += (YY[I][L]*QQ[L][J]);
}
}
}
for (M=1; M<=3; M++) {
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
VV[I][J]=RR[I][J];
}
}
VVtoW(); E=W;
for (N=1; N<=3; N++) {
VV[1][M]=0; VV[2][M]=0; VV[3][M]=0; VV[N][M]=1;
VVtoW();
// Avoid problems with divide-by-zero. Am not sure
// under what conditions this could actually happen...
if (W==0 && E==0) {
QQ[M][N]=1;
} else if (E==0) {
QQ[M][N]=1;
status = false;
} else {
QQ[M][N]=W/E;
}
}
}
return status;
}
// CONVERT EQUATORIAL --> TELESCOPE
void Calc::EqToAzEl(double ra, double dec, double time, bool flipped, double *azptr, double *elptr)
{
int I, J;
double RA = ra;
double DEC = dec;
DEC=DEC/degPerRad;
RA=(RA-degreesPerMinute*time)/degPerRad;
XX[1][1]=MathLib::cos(DEC)*MathLib::cos(RA);
XX[2][1]=MathLib::cos(DEC)*MathLib::sin(RA);
XX[3][1]=MathLib::sin(DEC);
YY[1][1]=0; YY[2][1]=0; YY[3][1]=0;
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
YY[I][1] += (RR[I][J]*XX[J][1]);
}
}
YY1toFH(flipped);
TrueToApparentAzEl(flipped);
F *= degPerRad;
H *= degPerRad;
// This code measures azimuth backwards!
F = 360.0-F;
if (azptr)
*azptr = F;
if (elptr)
*elptr = H;
}
// CONVERT TELESCOPE --> EQUATORIAL
void Calc::AzElToEq(double az, double el, double time, double *raptr, double *decptr)
{
int I, J;
F = 360.0 - az; // This code measures azimuth backwards!
H = el;
F *= radPerDeg;
H *= radPerDeg;
AzEltoYY0();
XX[1][1]=YY[1][0]; XX[2][1]=YY[2][0]; XX[3][1]=YY[3][0];
YY[1][1]=0; YY[2][1]=0; YY[3][1]=0;
for (I=1; I<=3; I++) {
for (J=1; J<=3; J++) {
YY[I][1] += (QQ[I][J]*XX[J][1]);
}
}
YY1toFH(false);
F *= degPerRad;
H *= degPerRad;
F += degreesPerMinute*time;
F=F-MathLib::floor(F/360)*360;
//F = fmod(F, 360.0);
if (raptr)
*raptr = F;
if (decptr)
*decptr = H;
}
// *****************************************
// FROM SKY & TELESCOPE, FEBRUARY, 1989,
// PAGES 194-196
// *****************************************