-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProject.c
2165 lines (1852 loc) · 65.5 KB
/
Project.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
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 "sunclock.h"
#include <math.h>
#include "project.h"
#define PI 3.14159265358979323846
#define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0))) /* Fix angle */
#define fixangr(a) ((a) - (PI*2) * (floor((a) / (PI*2)))) /* Fix angle in radians*/
#define dtr(x) ((x) * (PI / 180.0)) /* Degree->Radian */
#define rtd(x) ((x) / (PI / 180.0)) /* Radian->Degree */
/* PI / 180 = .0174532925199 */
#define DCOS(x) (cos((x) * .0174532925199))
#define DSIN(x) (sin((x) * .0174532925199))
#define DTAN(x) (tan((x) * .0174532925199))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
mapwindow skywin, telwin, horwin;
mapwindow *mapwin[3] = { &skywin, &telwin, &horwin };
int numwins = 3;
/* exportable */
static double xf_west, xf_east, xf_north, xf_south, xf_bottom;
static int xf_xcen, xf_ycen, xf_ybot;
static int xf_w_left, xf_w_right, xf_w_top, xf_w_bot;
double xf_c_scale;
/* local storage */
static int xfs_proj_mode;
/* sin_dlcen = sin (phi0) = sin (declination of center), cos_dlcen similar */
static double xfs_ra_cen, xfs_dl_cen, sin_dlcen, cos_dlcen, chart_scale;
static double xfs_scale; /* Formerly yscale */
static double xfs_vinv, xfs_hinv;
static int xfs_wide_warn;
/* Forward functions. */
static void init_gt(mapwindow *win);
static void do_gt(double lat, double lon, double *xloc, double *yloc, double *r_theta);
static void inv_gt(double x, double y, double *latp, double *lonp);
/* return TRUE if a (in degrees) is west of b */
/* west is towards lower values of RA, e.g. 60 is west of 90 */
static int westof(double a, double b)
{
double diff;
diff = b - a;
if (diff > 180) diff -= 360;
if (diff < -180) diff += 360;
return diff > 0;
}
/* return TRUE if a (in degrees) is east of b */
/* east is towards higher values of RA, e.g. 90 is east of 60 */
static int eastof(double a, double b)
{
double diff;
diff = b - a;
if (diff > 180) diff -= 360;
if (diff < -180) diff += 360;
return diff < 0;
}
/* TRANSFORMATION FUNCTIONS */
/**
stereographic projection:
Imagine object projected on a sphere of radius 1.
Center of chart is against a piece of paper.
You are looking at the center of the chart through the
center of the sphere.
The objects are seen projected on the piece of paper.
to do it:
1) get angle from center to object, call this theta.
2) get direction from center to object, call this phi.
3) projection of object will be 2*tan(theta/2) from the center
of the paper, in the direction phi.
to do steps 1 & 2, use alt-azimuth formula, with theta = 90 - alt.
scale: when theta = scale, projected object = min(ww,wh)/2 from center.
i.e. 2*tan(scale/2)*xfs_scale = min(ww,wh)/2.
or xfs_scale = (min(ww,wh)/2)/(2*tan(scale/2))
= min(ww,wh)/(4*tan(scale/2))
put factor of 2 in xfs_scale to save a little computation
xfs_scale = min(ww,wh)/(2*tan(scale/2))
R = tan(theta/2)*xfs_scale.
*/
/**
alt-azimuth:
sin(alt) = sin(obs_lat)sin(obj_dl) + cos(obs_lat)cos(obj_dl)cos(hour)
cos(azi) = (cos(obs_lat)sin(obj_dl) - sin(obs_lat)cos(obj_dl)cos(hour))
/ cos(alt)
sin(azi) = (-cos(obj_dl)sin(hour)) / cos(alt)
tan(azi) = -cos((obj_dl)sin(hour)
/ (cos(obs_lat)sin(obj_dl) - sin(obs_lat)cos(obj_dl)cos(hour))
with alt = 90 - theta, azi = phi, hour = lon - racen, obj_dl =lat,
and obs_lat = dlcen, this becomes:
cos(theta) = sin(dlcen)sin(lat) + cos(dlcen)cos(lat)cos(lon - racen)
tan(phi) = -cos(lat)sin(lon - racen)
/ (cos(dlcen)sin(lat) - sin(dlcen)cos(lat)cos(lon - racen))
racen and dlcen are constants for a chart, so introduce variables
racen, sin_dlcen, cos_dlcen.
also add chart_scale which is chart->scale in radians.
initxform sets these static variables.
Other projections from book.
**/
int initxform(mapwindow *win)
{
double tscale = 0;
#ifdef NEEDED
double adj, xscale;
#endif
xfs_proj_mode = win->proj_mode;
if (win->scale <= 0.0)
return FALSE;
if (win->height == 0)
return FALSE;
xfs_ra_cen = win->racen;
xfs_dl_cen = win->dlcen;
xf_xcen = win->x_offset + (win->width) / 2;
xf_ycen = win->y_offset + (win->height) / 2;
xf_ybot = win->y_offset;
xf_north = (win->dlcen + win->scale / 2);
xf_south = (win->dlcen - win->scale / 2);
if (xf_north > 90.0)
xf_north = 90.0;
if (xf_south < -90.0)
xf_south = -90.0;
if (win->invert) {
xfs_vinv = -1.0;
// xfs_hinv = 1;
xf_bottom = xf_north;
} else {
xfs_vinv = 1.0;
// xfs_hinv = -1;
xf_bottom = xf_south;
}
if (win->mirror) {
xfs_hinv = -1;
} else {
xfs_hinv = 1;
}
#ifdef NEEDED
if (xfs_proj_mode == SANSONS) {
/*
* calculate xf_east and xf_west by calculating the widest range of lon
* which will be within the chart.
* xscale is other than win->scale in order to widen the horizontal viewing
* area, which otherwise shrinks near the poles under Sanson's projection
* this happens in polar maps which do not span the celestial equator
*/
adj = 1.0;
if (xf_north * xf_south > 0.0)
adj = MAX(DCOS(xf_north), DCOS(xf_south));
xscale = win->scale / adj;
tscale = xscale * win->width/win->height / 2.0;
if (tscale > 180.0)
tscale = 180.0;
} else if (xfs_proj_mode == RECTANGULAR) {
tscale = win->scale * win->width / (2 * win->height);
if (tscale > 180.0)
tscale = 180.0;
};
#endif // NEEDED
xf_east = win->racen + tscale;
xf_west = win->racen - tscale;
/* set warning, may have problems in SANSONS or RECTANGULAR
with lines which should wrap around */
#ifdef NEEDED
if (((xfs_proj_mode == SANSONS) || (xfs_proj_mode == RECTANGULAR))
&& (tscale > 90.0))
xfs_wide_warn = TRUE;
else
#endif
xfs_wide_warn = FALSE;
xf_w_left = win->x_offset;
xf_w_right = win->x_offset + win->width;
xf_w_bot = win->y_offset;
xf_w_top = win->y_offset + win->height;
switch (xfs_proj_mode) {
case GNOMONIC:
case ORTHOGR:
case STEREOGR:
sin_dlcen = DSIN(win->dlcen);
cos_dlcen = DCOS(win->dlcen);
chart_scale = win->scale * .0174532925199; /* Radians */
break;
#ifdef NEEDED
case SANSONS:
case RECTANGULAR:
default:
break;
#endif
}
/* xf_c_scale is the size in degrees which one pixel occupies on the map */
/* xfs_scale is the conversion factor for size of the picture
(= R in some formulas for stereographic,
gnomonic and orthographic projections) */
if (xfs_proj_mode == STEREOGR) {
xfs_scale = MIN(win->height, win->width) / (4.0 * DTAN(win->scale / 2.0));
xf_c_scale = win->c_scale = 1.0 / (2.0 * DTAN(0.5) * xfs_scale);
#ifdef NEEDED
} else if (xfs_proj_mode == SANSONS) {
xfs_scale = win->height / win->scale;
xf_c_scale = win->c_scale = win->scale / win->height;
#endif
} else if (xfs_proj_mode == GNOMONIC) {
xfs_scale = MIN(win->height, win->width) / (2.0 * DTAN(win->scale / 2.0));
xf_c_scale = win->c_scale = 1.0/(DTAN(1.0) * xfs_scale);
#ifdef NEEDED
} else if (xfs_proj_mode == ORTHOGR) {
xfs_scale = MIN(win->height, win->width) / (2.0 * DSIN(win->scale / 2.0));
xf_c_scale = win->c_scale = 1.0 / (DSIN(1.0) * xfs_scale);
} else if (xfs_proj_mode == RECTANGULAR) {
xfs_scale = win->height / win->scale;
xf_c_scale = win->c_scale = 1.0 / xfs_scale;
#endif
}
/* initialize gnomonic transform function */
init_gt(win);
return TRUE;
}
void xform(double lat, double lon, int *xloc, int *yloc, int *inregion)
{
double theta, actheta, rac_l;
double denom;
double Dcoslat, Dsinlat, Dcosrac_l, Dsinrac_l;
/* Dcoslat, Dsinlat: of object latitude in degrees = phi
Dcosrac_l, Dsinrac_l: of object ra - longditude of center = d(lambda) */
double xlocd, ylocd; /* double precision for xloc and yloc */
switch (xfs_proj_mode) {
#ifdef NEEDED
case SANSONS:
/*
* This is Sanson's Sinusoidal projection. Its properties:
* (1) area preserving
* (2) preserves linearity along y axis (declination/azimuth)
*/
/* because of the (xfs_ra_cen-lon) below, lon must be continuous across the
plotted region.
xf_west is xfs_ra_cen - (scale factor),
xf_east is xfs_ra_cen + (scale factor),
so xf_west may be negative, and xf_east may be > 360.0.
lon should be 0 <= lon < 360.0. we must bring lon into the range.
if xf_west < 0 and (xf_west + 360) < lon then lon is in the right half
of the chart and needs to be adjusted
if xf_east > 360 and (xf_east - 360) > lon then lon is in the left half
of the chart and needs to be adjusted
*/
if ((xf_west < 0.0) && (lon>(xf_west+360.0)))
lon -= 360.0;
if ((xf_east > 360.0) && (lon<(xf_east-360.0)))
lon += 360.0;
*xloc = (int) (xf_xcen + (int) xfs_hinv * ((xfs_ra_cen - lon) * xfs_scale * DCOS(lat)) + 0.5);
*yloc = (int) (xf_ybot + (int) xfs_vinv * ((lat - xf_bottom) * xfs_scale) + 0.5);
*inregion = ((lon >= xf_west) && (lon <= xf_east) &&
(lat >= xf_south) && (lat <= xf_north));
break;
#endif
case STEREOGR:
/* Stereographic projection */
rac_l = lon - xfs_ra_cen;
Dsinlat = DSIN(lat);
Dcoslat = DCOS(lat);
Dcosrac_l = DCOS(rac_l);
Dsinrac_l = DSIN(rac_l);
actheta = sin_dlcen * Dsinlat + cos_dlcen * Dcoslat * Dcosrac_l;
if (actheta > 1.0)
theta = 0.0;
else if (actheta < -1.0)
theta = 3.14159265358979323846;
else theta = acos(actheta);
*inregion = (theta <= chart_scale);
if (*inregion) {
denom = (1 + sin_dlcen * Dsinlat + cos_dlcen * Dcoslat * Dcosrac_l) / xfs_scale;
*xloc = (int) (xf_xcen - 2 * xfs_hinv * Dcoslat * Dsinrac_l / denom + 0.5);
*yloc = (int) (xf_ycen + 2 * xfs_vinv * (cos_dlcen * Dsinlat
- sin_dlcen * Dcoslat * Dcosrac_l) / denom + 0.5);
}
break;
case GNOMONIC:
/* Gnomonic projection */
rac_l = lon - xfs_ra_cen;
Dsinlat = DSIN(lat);
Dcoslat = DCOS(lat);
Dcosrac_l = DCOS(rac_l);
Dsinrac_l = DSIN(rac_l);
actheta = sin_dlcen * Dsinlat + cos_dlcen * Dcoslat * Dcosrac_l;
if (actheta > 1.0)
theta = 0.0;
else if (actheta < -1.0)
theta = 3.14159265358979323846;
else
theta = acos(actheta);
if (theta <= 1.57) { /* avoid wrapping */
denom = (sin_dlcen * Dsinlat + cos_dlcen * Dcoslat * Dcosrac_l) / xfs_scale;
*yloc = (int) (ylocd = xf_ycen +
(int) xfs_vinv *
(cos_dlcen * Dsinlat - sin_dlcen * Dcoslat * Dcosrac_l) / denom + 0.5);
*xloc = (int) (xlocd = xf_xcen - xfs_hinv * Dcoslat * Dsinrac_l / denom + 0.5);
*inregion = ((xlocd >= xf_w_left) && (xlocd <= xf_w_right)
&& (ylocd <= xf_w_top) && (ylocd >= xf_w_bot));
} else
*inregion = FALSE;
break;
#ifdef NEEDED
case ORTHOGR:
rac_l = lon - xfs_ra_cen;
Dsinlat = DSIN(lat);
Dcoslat = DCOS(lat);
Dcosrac_l = DCOS(rac_l);
Dsinrac_l = DSIN(rac_l);
actheta = sin_dlcen * Dsinlat + cos_dlcen * Dcoslat * Dcosrac_l;
if (actheta > 1.0)
theta = 0.0;
else if (actheta < -1.0)
theta = 3.14159265358979323846;
else
theta = acos(actheta);
if (theta <= 1.57) { /* avoid wrapping */
*yloc = (int) (ylocd = xf_ycen +
(int) xfs_vinv * xfs_scale
* (cos_dlcen * Dsinlat - sin_dlcen * Dcoslat * Dcosrac_l));
*xloc = (int) (xlocd = xf_xcen - xfs_hinv * xfs_scale * Dcoslat * Dsinrac_l);
*inregion = ((xlocd >= xf_w_left) && (xlocd <= xf_w_right)
&& (ylocd <= xf_w_top) && (ylocd >= xf_w_bot));
} else
*inregion = FALSE;
break;
case RECTANGULAR:
if ((xf_west < 0.0) && (lon>(xf_west+360.0)))
lon -= 360.0;
if ((xf_east > 360.0) && (lon<(xf_east-360.0)))
lon += 360.0;
*yloc = (int) (ylocd = xf_ycen + (int)xfs_vinv * xfs_scale * (lat - xfs_dl_cen));
*xloc = (int) (xlocd = xf_xcen + xfs_hinv * xfs_scale * (xfs_ra_cen - lon));
*inregion = ((xlocd >= xf_w_left) && (xlocd <= xf_w_right)
&& (ylocd <= xf_w_top) && (ylocd >= xf_w_bot));
break;
#endif // NEEDED
default:
break;
}
}
/* Given x and y of a point on the display,
return the latitude and longitude */
int invxform(int x, int y, double *latp, double *lonp)
{
int i;
int winnum;
mapwindow *win;
/* temporaries to hold values set by initxform */
double t_xf_west, t_xf_east, t_xf_north, t_xf_south, t_xf_bottom;
int t_xf_w_left, t_xf_w_right, t_xf_w_top, t_xf_w_bot;
int t_xf_xcen, t_xf_ycen, t_xf_ybot;
double t_xf_c_scale;
int t_xfs_proj_mode;
double t_xfs_ra_cen, t_sin_dlcen, t_cos_dlcen, t_chart_scale;
double t_xfs_scale;
double t_xfs_vinv, t_xfs_hinv;
double rho;
double R, theta;
double l, m, n;
double l_, m_, n_;
*latp = 0.0;
*lonp = 0.0;
/* First, find which mapwindow the point is in */
for (i = 0; i < numwins; i++) {
if ((x >= mapwin[i]->x_offset) && (y >= mapwin[i]->y_offset)
&& (x <= (mapwin[i]->x_offset+mapwin[i]->width))
&& (y <= (mapwin[i]->y_offset+mapwin[i]->height)))
/* point is in window i */
break;
}
if (i == numwins)
return -1; /* outside all windows */
winnum = i;
win = mapwin[winnum];
/* Now, initialize inverse transformation for window winnum */
t_xf_west = xf_west;
t_xf_east = xf_east;
t_xf_north = xf_north;
t_xf_south = xf_south;
t_xf_bottom = xf_bottom;
t_xf_xcen = xf_xcen;
t_xf_ycen = xf_ycen;
t_xf_ybot = xf_ybot;
t_xf_w_left = xf_w_left;
t_xf_w_right = xf_w_right;
t_xf_w_top = xf_w_top;
t_xf_w_bot = xf_w_bot;
t_xf_c_scale = xf_c_scale;
t_xfs_proj_mode = xfs_proj_mode;
t_xfs_ra_cen = xfs_ra_cen;
t_sin_dlcen = sin_dlcen;
t_cos_dlcen = cos_dlcen;
t_chart_scale = chart_scale;
t_xfs_scale = xfs_scale;
t_xfs_vinv = xfs_vinv;
t_xfs_hinv = xfs_hinv;
initxform(win);
/* Calculate lat and lon */
switch (win->proj_mode) {
#ifdef NEEDED
case SANSONS:
*latp = (y - xf_ybot) / xfs_scale * xfs_vinv + xf_bottom;
*lonp = -((x - xf_xcen) / (xfs_scale * xfs_hinv * DCOS(*latp)) - xfs_ra_cen);
break;
#endif
case GNOMONIC:
case ORTHOGR:
case STEREOGR:
x -= xf_xcen;
y -= xf_ycen;
y *= (int) xfs_vinv;
x *= (int) xfs_hinv;
R = sqrt((double) ((((long) x) * x) + (((long) y) * y)));
theta = atan2((double) y, (double) x);
/* rho is the angle from the center of the display to the object on the
unit sphere. */
switch (win->proj_mode) {
case STEREOGR:
rho = 2.0 * atan(R / (2.0 * xfs_scale));
break;
case GNOMONIC:
rho = atan(R / xfs_scale);
break;
#ifdef NEEDED
case ORTHOGR:
rho = asin(R / xfs_scale);
break;
#endif
}
/* transform from (rho, theta) to l m n direction cosines */
l = sin(rho) * cos(theta); /* rho and theta are in radians */
m = sin(rho) * sin(theta);
n = cos(rho);
/* transform to new declination at center
new axes rotated about x axis (l) */
l_ = l;
m_ = m * sin_dlcen - n * cos_dlcen;
n_ = m * cos_dlcen + n * sin_dlcen;
/* calculate lon and lat */
*lonp = atan2(l_, m_) / 0.0174532925199 + xfs_ra_cen - 180.0;
*latp = 90 - acos(n_) / 0.0174532925199;
break;
#ifdef NEEDED
case RECTANGULAR:
*latp = (y - xf_ycen) / (xfs_vinv * xfs_scale) + xfs_dl_cen;
*lonp = (xf_xcen - x) / (xfs_hinv * xfs_scale) + xfs_ra_cen;
break;
#endif
default: /* error */
winnum = -1;
}
/* restore initxform's variables */
xf_west = t_xf_west;
xf_east = t_xf_east;
xf_north = t_xf_north;
xf_south = t_xf_south;
xf_bottom = t_xf_bottom;
xf_xcen = t_xf_xcen;
xf_ycen = t_xf_ycen;
xf_ybot = t_xf_ybot;
xf_w_left = t_xf_w_left;
xf_w_right = t_xf_w_right;
xf_w_top = t_xf_w_top;
xf_w_bot = t_xf_w_bot;
xf_c_scale = t_xf_c_scale;
xfs_proj_mode = t_xfs_proj_mode;
xfs_ra_cen = t_xfs_ra_cen;
sin_dlcen = t_sin_dlcen;
cos_dlcen = t_cos_dlcen;
chart_scale = t_chart_scale;
xfs_scale = t_xfs_scale;
xfs_vinv = t_xfs_vinv;
xfs_hinv = t_xfs_hinv;
if (*lonp >= 360.0)
*lonp -= 360.0;
if (*lonp < 0.0)
*lonp += 360.0;
return winnum;
}
/* Gnomonic transformation
Used to draw vectors as great circles
in Gnomonic transform a great circle is projected to a line.
*/
static double gt_sin_dlcen, gt_cos_dlcen, gt_chart_scale;
static double gt_scale;
/* endpoints of west and east boundaries in SANSONS and RECTANGULAR */
static double gt_wx1, gt_wy1, gt_wx2, gt_wy2;
static double gt_ex1, gt_ey1, gt_ex2, gt_ey2;
/* midpoint, a and b for north and south boundaries.
y = a*x*x + y0 for parabola (b == 0.0)
1 = x*x/a + (y-y0)*(y-y0)/b for ellipse */
static double gt_ny0, gt_na, gt_nb;
static double gt_sy0, gt_sa, gt_sb;
/* radius for STEREOGRAPHIC, GNOMONIC, and ORTHOGRAPHIC */
static double gt_r;
/* Can we clip to boundaries analytically? */
static int gt_use_boundaries;
static void init_gt(mapwindow *win)
{
#ifdef NEEDED
double x_1, x_2, y_1, y_2;
double r_theta;
#endif
double adj;
gt_use_boundaries = TRUE;
gt_sin_dlcen = DSIN(win->dlcen);
gt_cos_dlcen = DCOS(win->dlcen);
gt_chart_scale = win->scale * .0174532925199; /* Radians */
/* gt_scale is the conversion factor for size of the picture ( = R) */
if (xfs_proj_mode == STEREOGR)
gt_scale = MIN(win->height, win->width) / (2.0 * DTAN(win->scale));
else
gt_scale = MIN(win->height, win->width) / (2.0 * DTAN(win->scale / 2.0));
adj = xf_c_scale * 0.9; /* use boundaries slightly
more restricted than full plot */
/* calculate boundaries of region */
switch (xfs_proj_mode) {
#ifdef NEEDED
case SANSONS:
case RECTANGULAR:
do_gt(xf_south+adj, xf_west+adj, >_wx1, >_wy1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
do_gt(xf_north-adj, xf_west+adj, >_wx2, >_wy2, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
do_gt(xf_south+adj, xf_east-adj, >_ex1, >_ey1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
do_gt(xf_north-adj, xf_east-adj, >_ex2, >_ey2, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
do_gt(xf_north-adj, xfs_ra_cen, &x_1, &y_1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_ny0 = y_1;
if (fabs(xf_north-adj) > (90 - fabs(win->dlcen))) {
/* ellipse */
do_gt(xf_north-adj, xfs_ra_cen + 180, &x_2, &y_2, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_nb = (y_2 - y_1)/2;
gt_ny0 = y_1 + gt_nb ;
gt_nb = gt_nb * gt_nb;
do_gt(xf_north-adj, xfs_ra_cen + 90, &x_1, &y_1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_na = x_1 * x_1 / (1 - (y_1 - gt_ny0) * (y_1 - gt_ny0) / gt_nb);
} else {
/* parabola */
if (gt_use_boundaries) {
gt_nb = 0.0;
gt_na = (gt_ey2 - gt_ny0) / (gt_ex2 * gt_ex2);
/* error if gt_ex2 == 0.0, as when r_theta was > 1.57 */
}
}
do_gt(xf_south+adj, xfs_ra_cen, &x_1, &y_1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_sy0 = y_1;
if (fabs(xf_south+adj) > (90 - fabs(win->dlcen))) {
/* ellipse */
do_gt(xf_south+adj, xfs_ra_cen + 180, &x_2, &y_2, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_sb = (y_2 - y_1) / 2;
gt_sy0 = y_1 - gt_sb ;
gt_sb = gt_sb * gt_sb;
do_gt(xf_south+adj, xfs_ra_cen + 90, &x_1, &y_1, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
do_gt(xf_south+adj, xfs_ra_cen + 270, &x_2, &y_2, &r_theta);
gt_use_boundaries &= (r_theta <= 1.57);
gt_sa = (x_2 - x_1) / 2;
gt_sa = gt_sa * gt_sa;
} else {
/* parabola */
if (gt_use_boundaries) {
gt_sb = 0.0;
gt_sa = (gt_ey1 - gt_sy0) / (gt_ex1 * gt_ex1);
/* error if gt_ex2 == 0.0, as when r_theta was > 1.57 */
}
}
break;
#endif // NEEDED
case STEREOGR:
gt_r = MIN(win->height, win->width) / 2.0 - 1;
break;
#ifdef NEEDED
case ORTHOGR:
gt_use_boundaries = FALSE; /* can't handle this analytically */
break;
#endif
case GNOMONIC:
gt_wx1 = gt_wx2 = xf_w_left - xf_xcen + 1;
gt_ex1 = gt_ex2 = xf_w_right - xf_xcen - 1;
gt_ey1 = gt_wy1 = xf_w_bot - xf_ycen + 1;
gt_ey2 = gt_wy2 = xf_w_top - xf_ycen - 1;
break;
default: /* error */
break;
}
}
/* Note, returns xloc and yloc as doubles */
static void do_gt(double lat, double lon, double *xloc, double *yloc, double *r_theta)
{
double theta, rac_l;
double denom;
double Dcoslat, Dsinlat, Dcosrac_l, Dsinrac_l;
/* Dcoslat, Dsinlat: of object latitude in degrees = phi
Dcosrac_l, Dsinrac_l: of object ra - longditude of center = d(lambda) */
rac_l = lon - xfs_ra_cen;
Dsinlat = DSIN(lat);
Dcoslat = DCOS(lat);
Dcosrac_l = DCOS(rac_l);
Dsinrac_l = DSIN(rac_l);
*r_theta =
theta = acos(gt_sin_dlcen*Dsinlat + gt_cos_dlcen * Dcoslat * Dcosrac_l);
if (theta <= 1.57) { /* avoid wrapping */
denom = (gt_sin_dlcen * Dsinlat + gt_cos_dlcen * Dcoslat * Dcosrac_l) / gt_scale;
*yloc = xfs_vinv *
(gt_cos_dlcen * Dsinlat - gt_sin_dlcen * Dcoslat * Dcosrac_l) / denom;
*xloc = xfs_hinv * (- Dcoslat * Dsinrac_l / denom);
};
}
/* Given x and y of a point on the display,
return the latitude and longitude */
static void inv_gt(double x, double y, double *latp, double *lonp)
{
double rho;
double R, theta;
double l, m, n;
double l_, m_, n_;
y *= xfs_vinv;
x *= xfs_hinv;
*latp = 0.0;
*lonp = 0.0;
/* Calculate lat and lon */
R = sqrt((double) ((((long) x) * x) + (((long) y) * y)));
theta = atan2((double) y, (double) x);
/* rho is the angle from the center of the display to the object on the
unit sphere. */
rho = atan(R / gt_scale);
/* transform from (rho, theta) to l m n direction cosines */
l = sin(rho) * cos(theta); /* rho and theta are in radians */
m = sin(rho) * sin(theta);
n = cos(rho);
/* transform to new declination at center
new axes rotated about x axis (l) */
l_ = l;
m_ = m * gt_sin_dlcen - n * gt_cos_dlcen;
n_ = m * gt_cos_dlcen + n * gt_sin_dlcen;
/* calculate lon and lat */
*lonp = atan2(l_, m_) / 0.0174532925199 + xfs_ra_cen - 180.0;
if (n_ > 1) n_ = 1;
if (n_ < -1) n_ = -1;
*latp = 90 - acos(n_) / 0.0174532925199;
if (*lonp >= 360.0)
*lonp -= 360.0;
if (*lonp < 0.0)
*lonp += 360.0;
}
/*
* clipping extentions (ccount)
*/
#define Fuz 0.1
static void quadrat(double a, double b, double c, double *x_1, double *x_2, int *n)
{
double t;
if (a == 0) {
*n = 0;
} else {
t = b * b - 4 * a * c;
if (t < 0) {
*n = 0;
} else if (t == 0) {
*x_1 = -b/(2*a);
*n = 1;
} else {
*x_1 = (-b + sqrt(t)) / (2 * a);
*x_2 = (-b - sqrt(t)) / (2 * a);
*n = 2;
};
};
}
static void gcmidpoint(double lat1, double lon1, double lat2, double lon2,
double *pmlat, double *pmlon)
{
double l1, m1, n1;
double l2, m2, n2;
double l3, m3, n3;
/* transform from (ra, dec) to l m n direction cosines */
l1 = DCOS(lat1) * DCOS(lon1);
m1 = DCOS(lat1) * DSIN(lon1);
n1 = DSIN(lat1);
l2 = DCOS(lat2) * DCOS(lon2);
m2 = DCOS(lat2) * DSIN(lon2);
n2 = DSIN(lat2);
l3 = l1 + l2;
m3 = m1 + m2;
n3 = n1 + n2;
n3 /= sqrt(l3 * l3 + m3 * m3 + n3 * n3);
*pmlon = atan2(m3, l3) / 0.0174532925199;
if ((*pmlon < 0) && (lon1 > 0) && (lon2 > 0))
*pmlon += 360.0;
*pmlat = asin(n3) / 0.0174532925199;
}
/* calculate and return the intersection point of two lines given
two points on each line */
static void line_intersect(double x_1, double y_1, double x_2, double y_2,
double x_3, double y_3, double x_4, double y_4,
double *x, double *y, int *int_1)
{
double a, b, c, d;
int x1, y1;
double lat_1, lon_1;
int in;
if (fabs(x_2 - x_1) > 1e-5) { /* Slope may be calculated */
a = (y_2 - y_1)/(x_2 - x_1);
b = y_1 - a * x_1;
if ((fabs(x_4 - x_3) < 1e-5)) { /* This slope is infinite */
/* calculate intersection */
*x = x_3;
*y = a*x_3 + b;
*int_1 = TRUE;
} else { /* Both slopes may be calculated */
c = (y_4 - y_3)/(x_4 - x_3);
d = y_3 - c * x_3;
if (fabs(a - c) < 1e-5) { /* Slopes the same, no intersection */
*int_1 = FALSE;
} else { /* calculate intersection */
*x = (d - b)/(a - c);
*y = (a*d - b*c)/(a - c);
*int_1 = TRUE;
};
};
} else { /* Slope is infinite */
if ((fabs(x_4 - x_3) < 1e-5)) { /* this slope is also infinite */
*int_1 = FALSE;
} else { /* There's an intersection */
c = (y_4 - y_3)/(x_4 - x_3);
d = y_3 - c * x_3;
*x = x_1;
*y = c*x_1 + d;
*int_1 = TRUE;
};
};
if (*int_1)
if (((((y_1 - Fuz) <= *y) && (*y <= (y_2 + Fuz)))
|| (((y_2 - Fuz) <= *y) && (*y <= (y_1 + Fuz))))
&& ((((x_1 - Fuz) <= *x) && (*x <= (x_2 + Fuz)))
|| (((x_2 - Fuz) <= *x) && (*x <= (x_1 + Fuz)))))
*int_1 = TRUE;
else
*int_1 = FALSE;
if (*int_1) {
inv_gt(*x, *y, &lat_1, &lon_1);
xform(lat_1, lon_1, &x1, &y1, &in);
if (!in)
*int_1 = FALSE;
}
}
#ifdef NEEDED
// y = a*x*x + b
static void para_intersect(double x_1, double y_1, double x_2, double y_2,
double a, double b, double *x, double *y, int *int_1)
{
double c, d;
double xroot1, xroot2;
double yr1, yr2, r1, r2;
int n;
int x1, y1;
double lat_1, lon_1;
int in;
if (fabs(x_2 - x_1) < 1e-5) { /* Line has infinite slope */
*x = x_1;
*y = a * x_1 * x_1 + b;
*int_1 = TRUE;
} else { /* Line slope may be calculated */
c = (y_2 - y_1) / (x_2 - x_1);
d = y_1 - c * x_1;
if (a < 1e-5) { /* virtually a straight line y = b */
if (fabs(c) < 1e-5) { /* Constant y */
n = 0;
} else {
xroot1 = (b - d) / c;
n = 1;
};
} else {
quadrat(a, -c, b - d, &xroot1, &xroot2, &n);
};
if (n == 0) { /* No intersection */
*int_1 = FALSE;
} else if (n == 1) { /* One intersection */
*x = xroot1;
*y = a * xroot1 * xroot1 + b;
*int_1 = TRUE;
} else { /* Two intersections */
yr1 = c * xroot1 + d;
yr2 = c * xroot2 + d;
r1 = (xroot1 - x_1) * (xroot1 - x_1) + (yr1 - y_1) * (yr1 - y_1)
+ (xroot1 - x_2) * (xroot1 - x_2) + (yr1 - y_2) * (yr1 - y_2);
r2 = (xroot2 - x_1) * (xroot2 - x_1) + (yr2 - y_1) * (yr2 - y_1)
+ (xroot2 - x_2) * (xroot2 - x_2) + (yr2 - y_2) * (yr2 - y_2);
if (r1 > r2) {
*x = xroot2;
*y = yr2;
*int_1 = TRUE;
} else {
*x = xroot1;
*y = yr1;
*int_1 = TRUE;
};
}
}
if (*int_1)
if (((((y_1 - Fuz) <= *y) && (*y <= (y_2 + Fuz)))
|| (((y_2 - Fuz) <= *y) && (*y <= (y_1 + Fuz))))
&& ((((x_1 - Fuz) <= *x) && (*x <= (x_2 + Fuz)))
|| (((x_2 - Fuz) <= *x) && (*x <= (x_1 + Fuz)))))
*int_1 = TRUE;
else
*int_1 = FALSE;
if (*int_1) {
inv_gt(*x, *y, &lat_1, &lon_1);
xform(lat_1, lon_1, &x1, &y1, &in);
if (!in)
*int_1 = FALSE;
}
}
// x*x/a + (y-y0)*(y-y0)/b - 1 = 0
static void ellip_intersect(double x_1, double y_1, double x_2, double y_2,
double a, double b, double y0, double *x, double *y, int *int_1)
{
double c, d;
double xroot1, xroot2;
double yr1, yr2, r1, r2;
int n;
int x1, y1;
double lat_1, lon_1;
int in;