-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoordinateTransformations.cpp
1354 lines (1073 loc) · 42.7 KB
/
CoordinateTransformations.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StdAfx.h"
#include "CoordinateTransformations.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// These are the Geodesy Fundation Classes (Sam Blackburn)
CEarthCoordinate::CEarthCoordinate(void)
{
m_X_CoordinateInMeters = 0.0;
m_Y_CoordinateInMeters = 0.0;
m_Z_CoordinateInMeters = 0.0;
}
CEarthCoordinate::CEarthCoordinate(const CEarthCoordinate& source)
{
Copy(source);
}
CEarthCoordinate::~CEarthCoordinate(void)
{
m_X_CoordinateInMeters = 0.0;
m_Y_CoordinateInMeters = 0.0;
m_Z_CoordinateInMeters = 0.0;
}
void CEarthCoordinate::Copy(const CEarthCoordinate& source)
{
m_X_CoordinateInMeters = source.m_X_CoordinateInMeters;
m_Y_CoordinateInMeters = source.m_Y_CoordinateInMeters;
m_Z_CoordinateInMeters = source.m_Z_CoordinateInMeters;
}
void CEarthCoordinate::Get(double& x_coordinate,double& y_coordinate,double& z_coordinate) const
{
x_coordinate = m_X_CoordinateInMeters;
y_coordinate = m_Y_CoordinateInMeters;
z_coordinate = m_Z_CoordinateInMeters;
}
double CEarthCoordinate::GetXCoordinateInMeters(void) const
{
return( m_X_CoordinateInMeters );
}
double CEarthCoordinate::GetYCoordinateInMeters(void) const
{
return( m_Y_CoordinateInMeters );
}
double CEarthCoordinate::GetZCoordinateInMeters(void) const
{
return( m_Z_CoordinateInMeters );
}
void CEarthCoordinate::Set(double x_coordinate,double y_coordinate,double z_coordinate)
{
m_X_CoordinateInMeters = x_coordinate;
m_Y_CoordinateInMeters = y_coordinate;
m_Z_CoordinateInMeters = z_coordinate;
}
void CEarthCoordinate::SetXCoordinateInMeters(double x_coordinate)
{
m_X_CoordinateInMeters = x_coordinate;
}
void CEarthCoordinate::SetYCoordinateInMeters(double y_coordinate)
{
m_Y_CoordinateInMeters = y_coordinate;
}
void CEarthCoordinate::SetZCoordinateInMeters(double z_coordinate)
{
m_Z_CoordinateInMeters = z_coordinate;
}
CEarthCoordinate& CEarthCoordinate::operator=(const CEarthCoordinate& source)
{
Copy(source);
return(*this);
}
CPolarCoordinate::CPolarCoordinate(void)
{
m_UpDownAngleInDegrees = 0.0;
m_LeftRightAngleInDegrees = 0.0;
m_DistanceFromSurfaceInMeters = 0.0;
}
CPolarCoordinate::CPolarCoordinate(const CPolarCoordinate& source)
{
Copy(source);
}
CPolarCoordinate::~CPolarCoordinate(void)
{
m_UpDownAngleInDegrees = 0.0;
m_LeftRightAngleInDegrees = 0.0;
m_DistanceFromSurfaceInMeters = 0.0;
}
void CPolarCoordinate::Copy(const CPolarCoordinate& source)
{
m_UpDownAngleInDegrees = source.m_UpDownAngleInDegrees;
m_LeftRightAngleInDegrees = source.m_LeftRightAngleInDegrees;
m_DistanceFromSurfaceInMeters = source.m_DistanceFromSurfaceInMeters;
}
void CPolarCoordinate::Get(double& up_down_angle,double& left_right_angle,double& length) const
{
up_down_angle = m_UpDownAngleInDegrees;
left_right_angle = m_LeftRightAngleInDegrees;
length = m_DistanceFromSurfaceInMeters;
}
double CPolarCoordinate::GetUpDownAngleInDegrees(void) const
{
return(m_UpDownAngleInDegrees);
}
double CPolarCoordinate::GetLeftRightAngleInDegrees(void) const
{
return(m_LeftRightAngleInDegrees);
}
double CPolarCoordinate::GetDistanceFromSurfaceInMeters(void) const
{
return(m_DistanceFromSurfaceInMeters);
}
void CPolarCoordinate::Set(double up_down_angle,double left_right_angle,double length)
{
m_UpDownAngleInDegrees = up_down_angle;
m_LeftRightAngleInDegrees = left_right_angle;
m_DistanceFromSurfaceInMeters = length;
}
void CPolarCoordinate::SetUpDownAngleInDegrees(double up_down_angle)
{
m_UpDownAngleInDegrees = up_down_angle;
}
void CPolarCoordinate::SetLeftRightAngleInDegrees(double left_right_angle)
{
m_LeftRightAngleInDegrees = left_right_angle;
}
void CPolarCoordinate::SetDistanceFromSurfaceInMeters(double distance_from_surface)
{
m_DistanceFromSurfaceInMeters = distance_from_surface;
}
CPolarCoordinate& CPolarCoordinate::operator=(const CPolarCoordinate& source)
{
Copy(source);
return(*this);
}
CEarth::CEarth(int ellipsoid_identifier)
{
m_Initialize();
SetEllipsoid(ellipsoid_identifier);
}
CEarth::~CEarth()
{
m_Initialize();
}
void CEarth::AddLineOfSightDistanceAndDirectionToCoordinate(const CPolarCoordinate& point_1,
double distance,
double direction,
CPolarCoordinate& point_2,
double height_above_surface_of_point_2)
{
// The method used here is to convert the straight (line-of-sight) distance to
// a surface distance and then find out the position using the surface distance.
// This is a translation of the MMDIST routine found in the FORWRD3D program at
// ftp://ftp.ngs.noaa.gov/pub/pcsoft/for_inv.3d/source/forwrd3d.for
double c = 0.0;
double cosine_of_latitude_squared = 0.0;
double cosine_of_direction_squared = 0.0;
double cosine_of_point_1_latitude = 0.0;
double difference_in_height = 0.0;
double direction_in_radians = 0.0;
double distance_adjusted_for_differences_in_height = 0.0;
double height_above_surface_of_point_1 = 0.0;
double n = 0.0;
double point_1_latitude_in_radians = 0.0;
double polar_eccentricity_squared = 0.0;
double polar_flattening = 0.0;
double r = 0.0;
double sine_of_point_1_latitude = 0.0;
double surface_distance = 0.0;
double term_1 = 0.0;
double term_2 = 0.0;
double term_3 = 0.0;
double two_r = 0.0;
double x = 0.0;
// Many thanks to Peter Dana (pdana@mail.utexas.edu) for educating me
// on the finer points of Geodesy, one of which was how to compute
// "second eccentricity squared"
polar_eccentricity_squared = ((m_EquatorialRadiusInMeters * m_EquatorialRadiusInMeters) - (m_PolarRadiusInMeters * m_PolarRadiusInMeters )) / ( m_PolarRadiusInMeters * m_PolarRadiusInMeters);
//printf( "polar_eccentricity_squared is %.23lf\n", polar_eccentricity_squared );
point_1_latitude_in_radians = CMath::ConvertDegreesToRadians(point_1.GetUpDownAngleInDegrees());
direction_in_radians = CMath::ConvertDegreesToRadians(direction);
cosine_of_point_1_latitude = CMath::Cosine(point_1_latitude_in_radians);
cosine_of_latitude_squared = cosine_of_point_1_latitude * cosine_of_point_1_latitude;
cosine_of_direction_squared = CMath::Cosine(direction_in_radians) * CMath::Cosine(direction_in_radians);
c = (m_EquatorialRadiusInMeters * m_EquatorialRadiusInMeters) / m_PolarRadiusInMeters;
n = c / CMath::SquareRoot(1.0 + (polar_eccentricity_squared * cosine_of_latitude_squared));
r = n / (1.0 + (polar_eccentricity_squared * cosine_of_latitude_squared * cosine_of_direction_squared));
height_above_surface_of_point_1 = point_1.GetDistanceFromSurfaceInMeters();
difference_in_height = height_above_surface_of_point_2 - height_above_surface_of_point_1;
term_1 = (distance * distance) - (difference_in_height * difference_in_height);
term_2 = 1.0 + (height_above_surface_of_point_1 / r);
term_3 = 1.0 + (height_above_surface_of_point_2 / r);
distance_adjusted_for_differences_in_height = CMath::SquareRoot(term_1 / (term_2 * term_3));
// printf( "distance_adjusted_for_differences_in_height is %.11lf\n", distance_adjusted_for_differences_in_height );
two_r = 2.0 * r;
surface_distance = two_r * CMath::ArcSine(distance_adjusted_for_differences_in_height / two_r);
// printf( "surface_distance is %.11lf\n", surface_distance );
AddSurfaceDistanceAndDirectionToCoordinate(point_1,surface_distance,direction,point_2);
}
void CEarth::AddSurfaceDistanceAndDirectionToCoordinate(const CEarthCoordinate& point_1,
double distance,
double direction,
CPolarCoordinate& point_2)
{
CPolarCoordinate polar_point_1;
Convert(point_1, polar_point_1);
AddSurfaceDistanceAndDirectionToCoordinate(polar_point_1,distance,direction,point_2);
}
void CEarth::AddSurfaceDistanceAndDirectionToCoordinate(const CEarthCoordinate& point_1,
double distance,
double direction,
CEarthCoordinate& point_2)
{
CPolarCoordinate polar_point_1;
CPolarCoordinate polar_point_2;
Convert(point_1,polar_point_1);
AddSurfaceDistanceAndDirectionToCoordinate(polar_point_1,distance,direction,polar_point_2);
Convert(polar_point_2,point_2);
}
void CEarth::AddSurfaceDistanceAndDirectionToCoordinate(const CPolarCoordinate& point_1,
double distance,
double direction,
CEarthCoordinate& point_2)
{
CPolarCoordinate polar_coordinate;
AddSurfaceDistanceAndDirectionToCoordinate(point_1,distance,direction,polar_coordinate);
Convert(polar_coordinate,point_2);
}
void CEarth::AddSurfaceDistanceAndDirectionToCoordinate(const CPolarCoordinate& point_1,
double distance,
double direction,
CPolarCoordinate& point_2)
{
// This is a translation of the Fortran routine DIRCT1 found in the
// FORWRD3D program at:
// ftp://ftp.ngs.noaa.gov/pub/pcsoft/for_inv.3d/source/forwrd3d.for
double c = 0.0;
double c2a = 0.0;
double cosine_of_direction = 0.0;
double cosine_of_y = 0.0;
double cu = 0.0;
double cz = 0.0;
double d = 0.0;
double e = 0.0;
double direction_in_radians = 0.0;
double eps = 0.0;
double heading_from_point_2_to_point_1_in_radians = 0.0;
double point_1_latitude_in_radians = 0.0;
double point_1_longitude_in_radians = 0.0;
double point_2_latitude_in_radians = 0.0;
double point_2_longitude_in_radians = 0.0;
double r = 0.0;
double sa = 0.0;
double sine_of_direction = 0.0;
double sine_of_y = 0.0;
double su = 0.0;
double tangent_u = 0.0;
double term_1 = 0.0;
double term_2 = 0.0;
double term_3 = 0.0;
double x = 0.0;
double y = 0.0;
direction_in_radians = CMath::ConvertDegreesToRadians(direction);
eps = 0.000000000000005;
r = 1.0 - m_Flattening;
point_1_latitude_in_radians = CMath::ConvertDegreesToRadians(point_1.GetUpDownAngleInDegrees());
point_1_longitude_in_radians = CMath::ConvertDegreesToRadians(point_1.GetLeftRightAngleInDegrees());
tangent_u = (r * CMath::Sine(point_1_latitude_in_radians)) / CMath::Cosine(point_1_latitude_in_radians);
sine_of_direction = CMath::Sine(direction_in_radians);
cosine_of_direction = CMath::Cosine(direction_in_radians);
heading_from_point_2_to_point_1_in_radians = 0.0;
if (cosine_of_direction != 0.0)
{
heading_from_point_2_to_point_1_in_radians = CMath::ArcTangentOfYOverX(tangent_u, cosine_of_direction) * 2.0;
}
cu = 1.0 / CMath::SquareRoot((tangent_u * tangent_u) + 1.0);
su = tangent_u * cu;
sa = cu * sine_of_direction;
c2a = ((-sa) * sa) + 1.0;
x = CMath::SquareRoot((((1.0 / r / r) - 1.0) * c2a) + 1.0) + 1.0;
x = (x - 2.0) / x;
c = 1.0 - x;
c = (((x * x) / 4.0) + 1.0) / c;
d = ((0.375 * (x * x)) -1.0) * x;
tangent_u = distance / r / m_EquatorialRadiusInMeters / c;
y = tangent_u;
bool exit_loop = false;
while(exit_loop != true)
{
sine_of_y = CMath::Sine(y);
cosine_of_y = CMath::Cosine(y);
cz = CMath::Cosine(heading_from_point_2_to_point_1_in_radians + y);
e = (cz * cz * 2.0) - 1.0;
c = y;
x = e * cosine_of_y;
y = (e + e) - 1.0;
term_1 = (sine_of_y * sine_of_y * 4.0) - 3.0;
term_2 = ((term_1 * y * cz * d) / 6.0) + x;
term_3 = ((term_2 * d) / 4.0) - cz;
y = (term_3 * sine_of_y * d) + tangent_u;
if (CMath::AbsoluteValue(y - c) > eps)
{
exit_loop = false;
}
else
{
exit_loop = true;
}
}
heading_from_point_2_to_point_1_in_radians = (cu * cosine_of_y * cosine_of_direction) - (su * sine_of_y);
c = r * CMath::SquareRoot((sa * sa) + (heading_from_point_2_to_point_1_in_radians * heading_from_point_2_to_point_1_in_radians));
d = (su * cosine_of_y) + (cu * sine_of_y * cosine_of_direction);
point_2_latitude_in_radians = CMath::ArcTangentOfYOverX(d,c);
c = (cu * cosine_of_y) - (su * sine_of_y * cosine_of_direction);
x = CMath::ArcTangentOfYOverX(sine_of_y * sine_of_direction,c);
c = (((((-3.0 * c2a) + 4.0) * m_Flattening) + 4.0) * c2a * m_Flattening) / 16.0;
d = ((((e * cosine_of_y * c) + cz) * sine_of_y * c) + y) * sa;
point_2_longitude_in_radians = (point_1_longitude_in_radians + x) - ((1.0 - c) * d * m_Flattening);
heading_from_point_2_to_point_1_in_radians = CMath::ArcTangentOfYOverX(sa,heading_from_point_2_to_point_1_in_radians) + CMath::Pi();
point_2.SetUpDownAngleInDegrees(CMath::ConvertRadiansToDegrees(point_2_latitude_in_radians));
point_2.SetLeftRightAngleInDegrees(CMath::ConvertRadiansToDegrees(point_2_longitude_in_radians));
}
void CEarth::Convert(const CEarthCoordinate& cartesian_coordinate,
CPolarCoordinate& polar_coordinate) const
{
// convert from cartesian to polar
double equatorial_radius_times_eccentricity_squared = 0.0;
equatorial_radius_times_eccentricity_squared = m_EquatorialRadiusInMeters * m_EccentricitySquared;
double p = 0.0;
p = CMath::SquareRoot((cartesian_coordinate.GetXCoordinateInMeters() * cartesian_coordinate.GetXCoordinateInMeters()) +
(cartesian_coordinate.GetYCoordinateInMeters() * cartesian_coordinate.GetYCoordinateInMeters()));
double temp_latitude = 0.0;
double z_coordinate = cartesian_coordinate.GetZCoordinateInMeters(); // for convienance
double one_minus_eccentricity_squared = 1.0 - m_EccentricitySquared;
temp_latitude = z_coordinate / p / one_minus_eccentricity_squared;
double old_value = 0.0;
double temp_value = 0.0;
double part_a = 0.0;
double part_b = 0.0;
double part_c = 0.0;
unsigned long loop_index = 0;
unsigned long maximum_number_of_tries = 1024;
bool convergence_was_acheived = false;
while(convergence_was_acheived != true && loop_index < maximum_number_of_tries)
{
old_value = temp_latitude;
part_a = one_minus_eccentricity_squared * temp_latitude * temp_latitude;
part_b = equatorial_radius_times_eccentricity_squared / CMath::SquareRoot(1.0 + part_a);
part_c = p - part_b;
temp_latitude = z_coordinate / part_c;
loop_index++;
if (CMath::AbsoluteValue(temp_latitude - old_value) > 0.000000000000000000001)
{
// Oh well, try again...
}
else
{
// YIPEE!! We've reached convergence!
convergence_was_acheived = true;
}
}
if (convergence_was_acheived == true)
{
double latitude_angle_in_radians = 0.0;
// Save the UpDown angle in degrees
latitude_angle_in_radians = CMath::ArcTangent(temp_latitude);
polar_coordinate.SetUpDownAngleInDegrees(CMath::ConvertRadiansToDegrees(latitude_angle_in_radians)); // Latitude
double sine_of_latitude_in_radians = 0.0;
double cosine_of_latitude_in_radians = 0.0;
sine_of_latitude_in_radians = CMath::Sine(latitude_angle_in_radians);
cosine_of_latitude_in_radians = CMath::Cosine(latitude_angle_in_radians);
double longitude_in_radians = 0.0;
longitude_in_radians = CMath::ArcTangentOfYOverX(cartesian_coordinate.GetYCoordinateInMeters(),cartesian_coordinate.GetXCoordinateInMeters());
polar_coordinate.SetLeftRightAngleInDegrees(CMath::ConvertRadiansToDegrees(longitude_in_radians)); // Longitude
double w = 0.0;
w = CMath::SquareRoot(1.0 - (m_EccentricitySquared * sine_of_latitude_in_radians * sine_of_latitude_in_radians));
double distance_from_center_to_surface_of_the_ellipsoid = 0.0;
distance_from_center_to_surface_of_the_ellipsoid = m_EquatorialRadiusInMeters / w;
double distance_from_surface = 0.0;
if (CMath::AbsoluteValue(latitude_angle_in_radians) < 0.7854)
{
//printf( "fabs( %14.10lf ) < 0.7854\n", latitude_angle_in_radians );
distance_from_surface = (p / cosine_of_latitude_in_radians) - distance_from_center_to_surface_of_the_ellipsoid;
}
else
{
//printf( "fabs( %14.10lf ) >= 0.7854\n", latitude_angle_in_radians );
distance_from_surface = (z_coordinate / sine_of_latitude_in_radians)
- distance_from_center_to_surface_of_the_ellipsoid
+ (m_EccentricitySquared * distance_from_center_to_surface_of_the_ellipsoid);
}
// printf( "CEarth::Convert() : First method produced a length of %14.10lf\n", distance_from_surface );
polar_coordinate.SetDistanceFromSurfaceInMeters(distance_from_surface);
}
else
{
// Oh well, we gave it a shot..
polar_coordinate.Set( 0.0, 0.0, 0.0 );
}
}
void CEarth::Convert(const CPolarCoordinate& polar_coordinate,
CEarthCoordinate& cartesian_coordinate) const
{
// convert from polar to cartesian
double up_down_radians = 0.0; // latitude
double left_right_radians = 0.0; // longitude angle
up_down_radians = CMath::ConvertDegreesToRadians(polar_coordinate.GetUpDownAngleInDegrees());
left_right_radians = CMath::ConvertDegreesToRadians(polar_coordinate.GetLeftRightAngleInDegrees());
double sine_of_up_down_radians = 0.0;
double cosine_of_left_right_radians = 0.0; // cosine_of_longitude
double cosine_of_up_down_radians = 0.0; // cosine_of_latitude
sine_of_up_down_radians = CMath::Sine(up_down_radians);
cosine_of_left_right_radians = CMath::Cosine(left_right_radians);
cosine_of_up_down_radians = CMath::Cosine(up_down_radians);
// Now we need to calculate the distance from the center of the ellipsoid to the surface of the ellipsoid
double w = 0.0;
w = CMath::SquareRoot(1.0 - (m_EccentricitySquared * sine_of_up_down_radians * sine_of_up_down_radians));
double distance_from_center_to_surface_of_the_ellipsoid = 0.0;
distance_from_center_to_surface_of_the_ellipsoid = m_EquatorialRadiusInMeters / w;
// printf( "en = %.25lf\n", distance_from_center_to_surface_of_the_ellipsoid );
double coordinate = 0.0;
coordinate = (distance_from_center_to_surface_of_the_ellipsoid + polar_coordinate.GetDistanceFromSurfaceInMeters())
* cosine_of_up_down_radians * cosine_of_left_right_radians;
cartesian_coordinate.SetXCoordinateInMeters(coordinate);
coordinate = (distance_from_center_to_surface_of_the_ellipsoid + polar_coordinate.GetDistanceFromSurfaceInMeters())
* cosine_of_up_down_radians * CMath::Sine(left_right_radians);
cartesian_coordinate.SetYCoordinateInMeters(coordinate);
coordinate = (distance_from_center_to_surface_of_the_ellipsoid * (1.0 - m_EccentricitySquared)
+ polar_coordinate.GetDistanceFromSurfaceInMeters()) * sine_of_up_down_radians;
cartesian_coordinate.SetZCoordinateInMeters(coordinate);
}
double CEarth::GetDistanceToHorizon(const CEarthCoordinate& point_1) const
{
CPolarCoordinate polar_coordinate;
Convert(point_1,polar_coordinate);
return(GetDistanceToHorizon(polar_coordinate));
}
double CEarth::GetDistanceToHorizon(const CPolarCoordinate& point_1) const
{
double distance_to_horizon = 0.0;
// d = ::sqrt( feet ) * 1.144 for nmi
// optical horizon is 1.317 * sqrt( h );
// d= ::sqrt( 17 * height_in_meters ); d is in meters
distance_to_horizon = CMath::SquareRoot(17.0 * point_1.GetDistanceFromSurfaceInMeters());
return(distance_to_horizon);
}
double CEarth::GetEquatorialRadiusInMeters(void) const
{
return(m_EquatorialRadiusInMeters);
}
double CEarth::GetPolarRadiusInMeters(void) const
{
return(m_PolarRadiusInMeters);
}
double CEarth::GetLineOfSightDistanceFromCourse(const CEarthCoordinate& current_location,
const CEarthCoordinate& point_a,
const CEarthCoordinate& point_b) const
{
// This function tells you how far off course you are from a straight line between
// point_a and point_b.
/*
** References:
** I got the formula from:
** Engineering Mathematics Handbook
** Jan J. Tuma, Ph.D.
** McGraw-Hill Book Company
** 1970
** Library of Congress Catalog Number 78-101174
** page 19, (a) Oblique triangle
**
** Teach Yourself Trigonometry
** P. Abbott, B.A.
** English Universities Press Ltd.
** 102 Newgate Street
** London, E.C.I
** Originally published 1940
** I used the 1964 printing.
** Page 22, Figure 12 calls this "the altitude from the vertex A"
*/
double distance_from_current_location_to_point_a = 0.0;
double distance_from_current_location_to_point_b = 0.0;
double distance_from_point_a_to_point_b = 0.0;
distance_from_current_location_to_point_a = GetLineOfSightDistance(current_location, point_a);
distance_from_current_location_to_point_b = GetLineOfSightDistance(current_location, point_b);
distance_from_point_a_to_point_b = GetLineOfSightDistance(point_a, point_b);
double p = 0.0;
p = distance_from_current_location_to_point_a;
p += distance_from_current_location_to_point_b;
p += distance_from_point_a_to_point_b;
p /= 2.0;
double temp_double = 0.0;
temp_double = p;
temp_double *= (double) (p - distance_from_current_location_to_point_a);
temp_double *= (double) (p - distance_from_current_location_to_point_b);
temp_double *= (double) (p - distance_from_point_a_to_point_b);
double area = 0.0;
area = CMath::SquareRoot(temp_double);
double distance_from_course = 0.0;
// The altitude from the vertex A is two times the area of the triangle divided by the baseline
distance_from_course = (2.0 * area) / distance_from_point_a_to_point_b;
return(distance_from_course);
}
double CEarth::GetLineOfSightDistance(const CEarthCoordinate& point_1,
const CEarthCoordinate& point_2) const
{
// This function implements the Pythagoras method of computing the distance
// between two points.
// This is a line-of-sight algorithm. It does not take into acccount the
// curvature of the Earth. It is not a distance on the surface algorithm.
// If you had a laser and connected the two points, this algorithm tells
// you how long the laser beam is.
double distance = 0.0;
double x_coordinate = 0.0;
double y_coordinate = 0.0;
double z_coordinate = 0.0;
x_coordinate = point_1.GetXCoordinateInMeters() - point_2.GetXCoordinateInMeters();
y_coordinate = point_1.GetYCoordinateInMeters() - point_2.GetYCoordinateInMeters();
z_coordinate = point_1.GetZCoordinateInMeters() - point_2.GetZCoordinateInMeters();
// Square the coordinates
x_coordinate *= x_coordinate;
y_coordinate *= y_coordinate;
z_coordinate *= z_coordinate;
distance = CMath::SquareRoot(x_coordinate + y_coordinate + z_coordinate);
return(distance);
}
double CEarth::GetLineOfSightDistance(const CEarthCoordinate& point_1,
const CPolarCoordinate& point_2) const
{
CEarthCoordinate earth_center_earth_fixed_point_2;
Convert(point_2,earth_center_earth_fixed_point_2);
return(GetLineOfSightDistance(point_1,earth_center_earth_fixed_point_2));
}
double CEarth::GetLineOfSightDistance(const CPolarCoordinate& point_1,
const CEarthCoordinate& point_2) const
{
CEarthCoordinate earth_center_earth_fixed_point_1;
Convert(point_1,earth_center_earth_fixed_point_1);
return(GetLineOfSightDistance(earth_center_earth_fixed_point_1,point_2));
}
double CEarth::GetLineOfSightDistance(const CPolarCoordinate& point_1,const CPolarCoordinate& point_2) const
{
CEarthCoordinate earth_center_earth_fixed_point_1;
CEarthCoordinate earth_center_earth_fixed_point_2;
Convert(point_1,earth_center_earth_fixed_point_1);
Convert(point_2,earth_center_earth_fixed_point_2);
return(GetLineOfSightDistance(earth_center_earth_fixed_point_1,earth_center_earth_fixed_point_2));
}
double CEarth::GetSurfaceDistance(const CEarthCoordinate& point_1,
const CEarthCoordinate& point_2,
double * heading_from_point_1_to_point_2_p,
double * heading_from_point_2_to_point_1_p) const
{
CPolarCoordinate polar_point_1;
CPolarCoordinate polar_point_2;
Convert(point_1,polar_point_1);
Convert(point_2,polar_point_2);
return(GetSurfaceDistance(polar_point_1,
polar_point_2,
heading_from_point_1_to_point_2_p,
heading_from_point_2_to_point_1_p));
}
double CEarth::GetSurfaceDistance(const CEarthCoordinate& point_1,
const CPolarCoordinate& point_2,
double * heading_from_point_1_to_point_2_p,
double * heading_from_point_2_to_point_1_p) const
{
CPolarCoordinate polar_point_1;
Convert(point_1,polar_point_1);
return(GetSurfaceDistance(polar_point_1,
point_2,
heading_from_point_1_to_point_2_p,
heading_from_point_2_to_point_1_p));
}
double CEarth::GetSurfaceDistance(const CPolarCoordinate& point_1,
const CEarthCoordinate& point_2,
double * heading_from_point_1_to_point_2_p,
double * heading_from_point_2_to_point_1_p) const
{
CPolarCoordinate polar_point_2;
Convert(point_2,polar_point_2);
return(GetSurfaceDistance(point_1,
polar_point_2,
heading_from_point_1_to_point_2_p,
heading_from_point_2_to_point_1_p));
}
double CEarth::GetSurfaceDistance(const CPolarCoordinate& point_1,
const CPolarCoordinate& point_2,
double * heading_from_point_1_to_point_2_p,
double * heading_from_point_2_to_point_1_p) const
{
// This is a translation of the Fortran routine INVER1 found in the
// INVERS3D program at:
// ftp://ftp.ngs.noaa.gov/pub/pcsoft/for_inv.3d/source/invers3d.for
// The ton of variables used...
double c = 0.0;
double c_value_1 = 0.0;
double c_value_2 = 0.0;
double c2a = 0.0;
double cosine_of_x = 0.0;
double cy = 0.0;
double cz = 0.0;
double d = 0.0;
double e = 0.0;
double r_value = 0.0;
double s = 0.0;
double s_value_1 = 0.0;
double sa = 0.0;
double sine_of_x = 0.0;
double sy = 0.0;
double tangent_1 = 0.0;
double tangent_2 = 0.0;
double x = 0.0;
double y = 0.0;
int loop_count = 0;
double heading_from_point_1_to_point_2 = 0.0;
double heading_from_point_2_to_point_1 = 0.0;
// UpDown == Latitude
// LeftRight == Longitude
double point_1_latitude_in_radians = CMath::ConvertDegreesToRadians(point_1.GetUpDownAngleInDegrees());
double point_1_longitude_in_radians = CMath::ConvertDegreesToRadians(point_1.GetLeftRightAngleInDegrees());
double point_2_latitude_in_radians = CMath::ConvertDegreesToRadians(point_2.GetUpDownAngleInDegrees());
double point_2_longitude_in_radians = CMath::ConvertDegreesToRadians(point_2.GetLeftRightAngleInDegrees());
r_value = 1.0 - m_Flattening;
tangent_1 = (r_value * CMath::Sine(point_1_latitude_in_radians)) / CMath::Cosine(point_1_latitude_in_radians);
tangent_2 = (r_value * CMath::Sine(point_2_latitude_in_radians)) / CMath::Cosine(point_2_latitude_in_radians);
c_value_1 = 1.0 / CMath::SquareRoot((tangent_1 * tangent_1) + 1.0);
s_value_1 = c_value_1 * tangent_1;
c_value_2 = 1.0 / CMath::SquareRoot((tangent_2 * tangent_2) + 1.0);
s = c_value_1 * c_value_2;
heading_from_point_2_to_point_1 = s * tangent_2; // backward_azimuth
heading_from_point_1_to_point_2 = heading_from_point_2_to_point_1 * tangent_1;
x = point_2_longitude_in_radians - point_1_longitude_in_radians;
bool exit_loop = false;
while(exit_loop != true)
{
sine_of_x = CMath::Sine(x);
cosine_of_x = CMath::Cosine(x);
tangent_1 = c_value_2 * sine_of_x;
tangent_2 = heading_from_point_2_to_point_1 - (s_value_1 * c_value_2 * cosine_of_x);
sy = CMath::SquareRoot((tangent_1 * tangent_1) + (tangent_2 * tangent_2));
cy = (s * cosine_of_x) + heading_from_point_1_to_point_2;
y = CMath::ArcTangentOfYOverX(sy,cy);
// Thanks to John Werner (werner@tij.wb.xerox.com) for
// finding a bug where sy could be zero. Here's his fix:
if ((s * sine_of_x ) == 0.0 && (sy == 0.0))
{
sa = 1.0;
}
else
{
sa = (s * sine_of_x) / sy;
}
c2a = ((-sa) * sa) + 1.0;
cz = heading_from_point_1_to_point_2 + heading_from_point_1_to_point_2;
if (c2a > 0.0)
{
cz = ((-cz) / c2a) + cy;
}
e = (cz * cz * 2.0) - 1.0;
c = (((((-3.0 * c2a) + 4.0) * m_Flattening) + 4.0) * c2a * m_Flattening) / 16.0;
d = x;
x = ((((e * cy * c) + cz) * sy * c) + y) * sa;
x = ((1.0 - c) * x * m_Flattening) + point_2_longitude_in_radians - point_1_longitude_in_radians;
if (CMath::AbsoluteValue(d - x) > 0.00000000000000000000005)
{
exit_loop = false;
}
else
{
exit_loop = true;
}
}
heading_from_point_1_to_point_2 = CMath::ArcTangentOfYOverX(tangent_1,tangent_2);
double temp_degrees = 0.0;
double temp_minutes = 0.0;
double temp_seconds = 0.0;
double temp_decimal_degrees = 0.0;
temp_decimal_degrees = CMath::ConvertRadiansToDegrees(heading_from_point_1_to_point_2);
if (temp_decimal_degrees < 0.0)
{
temp_decimal_degrees += 360.0;
}
if (heading_from_point_1_to_point_2_p != NULL)
{
// The user passed us a pointer, don't trust it.
// If you are using Visual C++ on Windows NT, the following
// try/catch block will ensure you won't blow up when random
// pointers are passed to you. If you are on a legacy operating
// system like Unix, you are screwed.
try
{
*heading_from_point_1_to_point_2_p = temp_decimal_degrees;
}
catch( ... )
{
// Do Nothing
}
}
heading_from_point_2_to_point_1 = CMath::ArcTangentOfYOverX(c_value_1 * sine_of_x,
((heading_from_point_2_to_point_1 * cosine_of_x) - (s_value_1 * c_value_2))) + CMath::Pi();
temp_decimal_degrees = CMath::ConvertRadiansToDegrees(heading_from_point_2_to_point_1);
if (temp_decimal_degrees < 0)
{
temp_decimal_degrees += 360.0;
}
if (heading_from_point_2_to_point_1_p != NULL)
{
// The user passed us a pointer, don't trust it.
// If you are using Visual C++ on Windows NT, the following
// try/catch block will ensure you won't blow up when random
// pointers are passed to you. If you are on a legacy operating
// system like Unix, you are screwed.
try
{
*heading_from_point_2_to_point_1_p = temp_decimal_degrees;
}
catch( ... )
{
// Do Nothing
}
}
x = CMath::SquareRoot((((1.0 / r_value / r_value) - 1) * c2a) + 1.0) + 1.0;
x = (x - 2.0) / x;
c = 1.0 - x;
c = (((x * x) / 4.0) + 1.0) / c;
d = ((0.375 * (x * x)) - 1.0) * x;
// 1998-09-01
// Thanks go to Gerard Murphy (bjmillar@dera.gov.uk) for finding a typo here.
x = e * cy;
s = (1.0 - e) - e;
double term_1 = 0.0;
double term_2 = 0.0;
double term_3 = 0.0;
double term_4 = 0.0;
double term_5 = 0.0;
term_1 = (sy * sy * 4.0) - 3.0;
term_2 = ((s * cz * d) / 6.0) - x;
term_3 = term_1 * term_2;
term_4 = ((term_3 * d) / 4.0) + cz;
term_5 = (term_4 * sy * d) + y;
s = term_5 * c * m_EquatorialRadiusInMeters * r_value;
return( s );
}
void CEarth::m_ComputeEccentricitySquared(void)
{
if (m_Flattening == 0.0)
{
m_EccentricitySquared = 0.0;
return;
}
m_EccentricitySquared = (2.0 * m_Flattening) - (m_Flattening * m_Flattening);
//printf( "Eccentricity Squared = %.23lf\n", m_EccentricitySquared );
}
void CEarth::m_ComputeFlattening(void)
{
if (m_EquatorialRadiusInMeters == 0.0 || m_PolarRadiusInMeters == 0.0)
{
return;
}
m_Flattening = CMath::AbsoluteValue(m_EquatorialRadiusInMeters - m_PolarRadiusInMeters) / m_EquatorialRadiusInMeters;
//printf( "Flattening = %.23lf\n", m_Flattening );
}
void CEarth::m_Initialize(void)
{
m_EllipsoidID = 0;
m_PolarRadiusInMeters = 0.0;
m_EquatorialRadiusInMeters = 0.0;
m_Flattening = 0.0;
m_EccentricitySquared = 0.0;
}
void CEarth::SetEllipsoid(int ellipsoid_identifier)
{
m_EllipsoidID = ellipsoid_identifier;
switch(ellipsoid_identifier)
{
case Perfect_Sphere:
m_EquatorialRadiusInMeters = 6378137.0;
m_PolarRadiusInMeters = 6378137.0;