forked from matthewarcus/mmps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.cpp
1341 lines (1214 loc) · 35.3 KB
/
transform.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
// $Revision: 1.5 $
// transform.cpp
// (C) 2004 by Matthew Arcus
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include "utils.h"
#include "constants.h"
#include "matrix.h"
#include "image.h"
#include "equations.h"
#include "transform.h"
using namespace std;
const double nightdim = 0.5;
// Apply matrix to phi, lambda, and put the resulting
// cartesion coords in x,y,z.
void convertlatlong(double &phi, double &lambda,
double& x, double& y, double& z,
const Matrix3& m)
{
x = cos(lambda) * cos(phi);
y = sin(lambda) * cos(phi);
z = sin(phi);
if (!m.IsIdentity()) {
m.Apply(x,y,z);
phi = asin(z);
lambda = atan2(y,x);
}
}
double distance(double phi0, double lambda0, double phi1, double lambda1)
{
double x0 = cos(lambda0) * cos(phi0);
double y0 = sin(lambda0) * cos(phi0);
double z0 = sin(phi0);
double x1 = cos(lambda1) * cos(phi1);
double y1 = sin(lambda1) * cos(phi1);
double z1 = sin(phi1);
double d = acos((x0*x1 + y0*y1 + z0*z1));
return d;
}
inline double cot(double theta) { return 1.0/tan(theta); }
TransformParams::TransformParams()
: tilt(0.0), turn(0.0), rotate(0.0), lat(0.0), lon(0.0),
scale(1.0), radius(0.0), a(1.0), aw(radians(20)),
x(8.0), y(0.0), z(0.0), ox(1.0), oy(1.0), oz(1.0),
sun(false), p(0.0), time(0.0), date(0.0), noback(false),
conic(1.0), conicr(0.0), xoff(0.0), yoff(0.0), background(Rgb(0,0,0)),
gridx(15), gridy(10), gridcolor(Rgb(0,0,0)), gridoff(0),
analemma(false), analemmacolor(Rgb(255,0,0)),
dial(false), dialcolor(Rgb(255,255,0)),
dlat(0.0),dlon(0.0)
{
}
void Transform::Init(const TransformParams& params)
{
m.Identity();
m = m * Matrix3(ZAxis, params.turn);
m = m * Matrix3(XAxis, params.tilt);
m = m * Matrix3(YAxis, params.lat);
m = m * Matrix3(ZAxis, params.lon);
// Construct the inverse matrix
minv.Identity();
minv = minv * Matrix3(ZAxis,-params.lon);
minv = minv * Matrix3(YAxis,-params.lat);
minv = minv * Matrix3(XAxis,-params.tilt);
minv = minv * Matrix3(ZAxis,-params.turn);
// Construct the sun parameters
if (!params.sun) {
sunx = 0.0;
suny = 0.0;
sunz = 0.0;
} else {
// We will use the astronomical day, starting at midnight (at Greenwich)
// And start the year at the spring equinox
// Need the vector to the sun.
double date = params.date-80;
while (date < 0) date += 365;
while (date >= 365) date -= 365;
date = 2 * pi *date / 365;
double sheight = sunheight(date);
double q = sqrt(1-sqr(sheight));
// params.time is mean time, convert to apparent time
double eot = equationoftime(date) * 2.0 * pi/(60.0 * 60.0 * 24.0);
// AT = MT + EOT
double atime = params.time + eot;
sunx = -cos(atime) * q;
suny = sin(atime) * q;
sunz = sheight;
// fprintf (stderr, "Sun: %f %f\n", degrees(asin(sunz)), hours(atan2(suny,-sunx)));
}
}
void Transform::SetData(const Image& inImage, Image& outImage,
const TransformParams& params,
double x, double y, double z,
double phi, double lambda,
unsigned int outIndex)
{
unsigned int iw = inImage.Width();
unsigned int ih = inImage.Height();
double hh = ih * 0.5;
double hw = iw * 0.5;
double sh = ih * oneoverpi;
double sw = iw * oneovertwopi;
// Use unsigned so we don't have to test for negative indices
unsigned int sx = unsigned(floor(hw + lambda * sw));
// Clamp in case of rounding errors
if (sx >= iw) sx = 0;
// Use unsigned so we don't have to test for negative indices
unsigned int sy = unsigned(floor(hh - phi * sh));
if (sy >= ih) sy = ih-1;
Rgb rgb;
unsigned int inIndex = sy * iw + sx;
inImage.GetRgb(inIndex, rgb);
if (params.sun) {
// Is where we are sunny?
// Compute pi/2 - angle of the x-axis with the normal at the relevant point.
// This should be asin(...) but we are only interested in small angles
// so assume sin x = x
double sunaltitude = sunx * x + suny * y + sunz * z;
#if 1
if (sunaltitude < sunriseangle) {
rgb.Dim(nightdim);
} else {
rgb.Dim(0.8 + 0.2 * sunaltitude);
}
#else
rgb.Dim(0.6 + 0.5 * sunaltitude * sunaltitude);
#endif
}
outImage.SetRgb(outIndex, rgb);
}
bool Transform::CheckPoint(const TransformParams ¶ms, double phi, double lambda) const
{
if (params.radius != 0 &&
distance(phi,lambda,params.lat,params.lon) > params.radius) {
return false;
}
return true;
}
void applyRotation(double r, double &x, double &y)
{
double x1 = x * cos(r) + y * sin(r);
double y1 = -x * sin(r) + y * cos(r);
//double x1 = -y;
//double y1 = x;
//fprintf(stderr,"%g %g:", x, y);
x = x1;
y = y1;
//fprintf(stderr,"%g %g\n", x, y);
}
void Transform::TransformImage(const Image& inImage, Image& outImage,
const TransformParams& params)
{
int ow = outImage.Width();
int oh = outImage.Height();
double orgx = 0.5 * ow;
double orgy = 0.5 * oh;
// Scale so that half width is 1
double scaleFactor = BasicScale(ow,oh) / params.scale;
int ox, oy; int outIndex = 0;
//fprintf(stderr,"Rotation %g %g %g %g\n",
// params.rotate, params.xoff, params.yoff, scaleFactor);
for (oy = 0; oy < oh; oy++) {
double y = scaleFactor * (orgy - oy - 0.5) + params.yoff;
if (params.rotate == 0) SetY(y);
for (ox = 0; ox < ow; ox++, outIndex++) {
// Really ought to just apply a 3x2 matrix to the point
double x = scaleFactor * (ox + 0.5 - orgx) + params.xoff;
double x1 = x;
double y1 = y;
if (params.rotate != 0) {
applyRotation(-params.rotate,x1,y1);
SetY(y1);
}
double x0 = 0.0, y0 = 0.0, z0 = 0.0;
double phi = 0.0, lambda = 0.0;
if (Project(params, x1, y1, x0, y0, z0, phi, lambda) &&
CheckPoint(params,phi, lambda)) {
SetData(inImage, outImage, params, x0, y0, z0, phi, lambda, outIndex);
} else if (!params.noback) {
outImage.SetRgb(outIndex, params.background);
}
}
}
}
void Transform::TransformImageInv(const Image& inImage, Image& outImage,
const TransformParams& params)
{
int ow = outImage.Width();
int oh = outImage.Height();
double orgx = 0.5 * ow;
double orgy = 0.5 * oh;
// Now scan across the output image
int ox, oy; int outIndex = 0;
for (oy = 0; oy < oh; oy++) {
for (ox = 0; ox < ow; ox++, outIndex++) {
// Compute lat and long
double phi = (orgy - oy - 0.5) * pi / oh;
double lambda = (ox + 0.5 - orgx) * twopi / ow;
// Compute the scaled x,y coordinates for <phi,lambda>
double x = 0.0, y = 0.0;
if (!CheckPoint(params,phi,lambda) ||
!ProjectInv(params, phi, lambda, x, y) ||
!SetDataInv(inImage, outImage, params, ox, oy, x, y, outIndex)) {
if (!params.noback) {
outImage.SetRgb(outIndex, params.background);
}
}
}
}
}
bool Transform::SetDataInv(const Image& inImage, Image& outImage,
const TransformParams& params,
double ox, double oy, // Coordinates in output image
double x, double y, // scaled coordinates in input image
unsigned int outIndex)
{
(void(ox));
(void(oy));
int iw = inImage.Width();
int ih = inImage.Height();
double orgx = 0.5 * iw;
double orgy = 0.5 * ih;
// Scale so that half width is 1
double scaleFactor = BasicScale(iw,ih) / params.scale;
// This is what we are inverting
//double x = scaleFactor * (ix - orgx) + params.xoff;
//double y = scaleFactor * (orgy - iy) + params.yoff;
//applyRotation(-params.rotate,x,y)
if (params.rotate != 0) {
applyRotation(params.rotate,x,y);
}
int ix = int(floor(orgx + (x - params.xoff) / scaleFactor));
int iy = int(floor(orgy - (y - params.yoff) / scaleFactor));
if (ix < 0 || ix >= iw || iy < 0 || iy >= ih) {
return false;
} else {
Rgb rgb;
unsigned int inIndex = iy * iw + ix;
inImage.GetRgb(inIndex, rgb);
outImage.SetRgb(outIndex, rgb);
return true;
}
}
bool Transform::MapXY(const Image& outImage,
const TransformParams& params,
double phi, double lambda, double& x, double& y) const
{
bool result = false;
// Set x and y to where phi and lambda are mapped to
// x, y are in image coordinates
// Get projection coordinates for x and y
if (CheckPoint(params,phi,lambda) &&
ProjectInv(params, phi,lambda, x, y)) {
// Now x and y are in 2pi scale
int ow = outImage.Width();
int oh = outImage.Height();
double orgx = 0.5 * ow;
double orgy = 0.5 * oh;
double scaleFactor = BasicScale(ow,oh) / params.scale;
if (params.rotate != 0) {
applyRotation(params.rotate,x,y);
}
x = orgx + (x - params.xoff) / scaleFactor;
y = orgy + (y - params.yoff) / -scaleFactor;
result = true;
}
return result;
}
Transform* Transform::GetTransform(const char *type)
{
Transform* transform = NULL;
if (strcmp(type, "latlong") == 0) {
transform = new LatLong();
} else if (strcmp(type, "equalarea") == 0) {
transform = new EqualArea();
} else if (strcmp(type, "sinusoidal") == 0) {
transform = new Sinusoidal();
} else if (strcmp(type, "sinusoidal2") == 0) {
transform = new Sinusoidal2();
} else if (strcmp(type, "mollweide") == 0) {
transform = new Mollweide();
} else if (strcmp(type, "mercator") == 0) {
transform = new Mercator();
} else if (strcmp(type, "gallpeters") == 0) {
transform = new GallPeters();
} else if (strcmp(type, "cylindrical") == 0) {
transform = new Cylindrical();
} else if (strcmp(type, "azimuthal") == 0) {
transform = new Azimuthal();
} else if (strcmp(type, "orthographic") == 0) {
transform = new Rectilinear();
} else if (strcmp(type, "rectilinear") == 0) {
transform = new Rectilinear();
} else if (strcmp(type, "stereographic") == 0) {
transform = new Stereographic();
} else if (strcmp(type, "gnomonic") == 0) {
transform = new Gnomonic();
} else if (strcmp(type, "perspective") == 0) {
transform = new Perspective();
} else if (strcmp(type, "bonne") == 0) {
transform = new Bonne();
} else if (strcmp(type, "hammer") == 0) {
transform = new Hammer();
}
return transform;
}
void PolarTransform::Init (const TransformParams& params)
{
Transform::Init(params);
// Polar transforms have the north pole as the centre.
// For consistency with other transforms, we rotate to set lat = 0
m = Matrix3(YAxis,-piovertwo) * m;
minv = minv * Matrix3(YAxis,piovertwo);
k = params.conic;
}
bool PolarTransform::Project(const TransformParams& params,
double x0, double y0,
double& x, double& y, double& z,
double& phi, double& lambda) const
{
bool result = false;
double r = sqrt(sqr(x0) + sqr(y0)) - params.conicr;
lambda = params.conic * atan2(x0, -y0);
if (r > 0.0 && lambda >= -pi && lambda <= pi) {
GetPhi(r, phi);
if (phi >= -piovertwo && phi <= piovertwo) {
convertlatlong(phi,lambda,x,y,z,m);
result = true;
}
}
return result;
}
bool PolarTransform::ProjectInv(const TransformParams& params,
double phi, double lambda,
double& x, double& y) const
{
bool result = false;
// Set x and y to where phi and lambda are mapped to
// x, y are in image coordinates
// Get projection coordinates for x and y
double x0,y0,z0;
convertlatlong(phi,lambda,x0,y0,z0,minv);
double r = 0.0;
if (GetR(phi, r)) {
x = (r + params.conicr) * sin(lambda / params.conic);
y = -(r + params.conicr) * cos(lambda / params.conic);
result = true;
}
return result;
}
bool CylindricalTransform::Project(const TransformParams& params,
double x0, double y0,
double& x, double& y, double& z,
double& phi, double& lambda) const
{
(void(params));
(void(y0));
bool result = false;
phi = phi0;
lambda = GetLong(x0);
if (lambda >= -pi && lambda <= pi &&
phi >= -piovertwo && phi <= piovertwo) {
// Transform to new lat and long.
// cartesian coords from latlong
convertlatlong(phi,lambda,x,y,z,m);
result = true;
}
return result;
}
bool CylindricalTransform::ProjectInv(const TransformParams& params,
double phi, double lambda,
double& x, double& y) const
{
(void(params));
minv.ApplyLatLong(phi,lambda);
return GetXY(phi,lambda, x, y);
}
bool SimpleTransform::Project(const TransformParams& params,
double x, double y,
double& x1, double& y1, double& z1,
double& phi, double& lambda) const
{
(void(params));
bool result = ProjectSimple(x,y,phi,lambda);
convertlatlong(phi,lambda,x1,y1,z1,m);
return result;
}
bool SimpleTransform::ProjectInv(const TransformParams& params,
double phi, double lambda, double& x, double& y) const
{
(void(params));
minv.ApplyLatLong(phi,lambda);
return ProjectInvSimple(phi,lambda,x,y);
}
double LatLong::GetMaxHeight(const TransformParams& params)
{
(void(params));
return piovertwo;
}
double LatLong::GetLat(double y)
{
return y;
}
double LatLong::GetLong(double x) const
{
return x;
}
bool LatLong::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda;
y = phi;
return true;
}
// f(phi) = sin(phi)/k, g(phi) = 1, where k = sqr(cos(p))
double EqualArea::GetMaxHeight(const TransformParams& params)
{
return 1 / (cos(params.p) * cos(params.p));
}
void EqualArea::Init(const TransformParams& params)
{
CylindricalTransform::Init(params);
k = cos(params.p) * cos(params.p);
}
double EqualArea::GetLat(double y)
{
return asin(y * k);
}
double EqualArea::GetLong(double x) const
{
return x;
}
bool EqualArea::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda;
y = sin(phi)/k;
return true;
}
// f(phi) = phi, g(phi) = cos(phi)
double Sinusoidal::GetMaxHeight (const TransformParams& params)
{
(void(params));
return piovertwo;
}
double Sinusoidal::GetLat(double y)
{
// Latitude is just y
m = 1/cos(y); // Cache the cos
return y;
}
double Sinusoidal::GetLong(double x) const
{
return x * m;
}
bool Sinusoidal::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda * cos(phi);
y = phi;
return true;
}
double Mercator::GetLat(double y)
{
double k = exp(fabs(y));
double phi = acos(2*k/(k*k+1));
if (y < 0) phi = -phi;
return phi;
}
double Mercator::GetLong(double x) const
{
return x;
}
bool Mercator::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda;
y = log((1 + sin(fabs(phi))) / cos(fabs(phi)));
if (phi < 0) {
y = -y;
}
return true;
}
double GallPeters::GetMaxHeight(const TransformParams& params)
{
(void(params));
return 2;
}
double GallPeters::GetLat(double y)
{
return asin(y / 2);
}
double GallPeters::GetLong(double x) const
{
return x;
}
bool GallPeters::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda;
y = 2 * sin(phi);
return true;
}
double Cylindrical::GetLat(double y)
{
return atan(y);
}
double Cylindrical::GetLong(double x) const
{
return x;
}
bool Cylindrical::GetXY(double phi, double lambda, double& x, double& y) const
{
x = lambda;
y = tan(phi);
return true;
}
void Azimuthal::AdjustSize (int& w, int& h, TransformParams& params)
{
int w1 = (int (params.scale * h));
if (w1 < w) { w = w1; }
}
void Azimuthal::GetPhi(double r, double& phi) const
{
phi = pi * (0.5 - r);
}
bool Azimuthal::GetR(double phi, double& r) const
{
r = 0.5 - phi / pi;
return true;
}
void Rectilinear::GetPhi(double r, double& phi) const
{
phi = acos(r);
}
bool Rectilinear::GetR(double phi, double& r) const
{
bool result = false;
if (phi >= 0.0) {
r = cos(phi);
result = true;
}
return result;
}
void Stereographic::GetPhi(double r, double& phi) const
{
double sr = pow(r,k);
phi = asin((1-sqr(sr)) / (1+sqr(sr)));
}
bool Stereographic::GetR(double phi, double& r) const
{
r = cos(phi) / (1 + sin(phi));
r = pow(r,1/k);
return true;
}
void Gnomonic::GetPhi(double r, double& phi) const
{
phi = atan(1 / (2 * r));
}
bool Gnomonic::GetR(double phi, double& r) const
{
bool result = false;
if (phi > 0) {
r = 1 / (2 * tan(phi));
result = true;
}
return result;
}
void Hammer::Init(const TransformParams& params)
{
Transform::Init(params);
}
bool Hammer::ProjectSimple(double x, double y,
double& phi, double& lambda) const
{
bool result = false;
double z2 = 2-sqr(x)-sqr(2*y);
double z = sqrt(z2);
double t1 = 2*y*z;
if (t1 >= -1.0 && t1 <= 1.0) {
phi = asin(t1);
lambda = 2 * atan2(x*z,z2 - 1);
if (lambda >= -pi && lambda <= pi) {
result = true;
}
}
return result;
}
bool Hammer::ProjectInvSimple(double phi, double lambda, double& x, double& y) const
{
double z = sqrt(1 + cos(phi)*cos(lambda/2));
x = cos(phi)*sin(lambda/2)/z;
y = sin(phi)/(2 * z);
return true;
}
void Bonne::Init(const TransformParams& params)
{
Transform::Init(params);
p = params.p;
cotp = cot(p);
}
bool Bonne::ProjectSimple(double x, double y,
double& phi, double& lambda) const
{
bool result = false;
double rho = sqrt(sqr(x) + sqr(cotp - y));
if (p > 0) {
phi = cotp + p - rho;
lambda = rho * atan2(x, cotp - y)/cos(phi);
} else if (p < 0) {
phi = cotp + p + rho;
lambda = rho * atan2(x, y - cotp)/cos(phi);
} else {
// Degenerate case - the sinusoidal projection
phi = y;
lambda = x/cos(phi);
}
if (phi >= -piovertwo && phi <= +piovertwo &&
lambda >= -pi && lambda <= pi) {
result = true;
}
return result;
}
bool Bonne::ProjectInvSimple(double phi, double lambda, double& x, double& y) const
{
if (p == 0.0) {
x = lambda * cos(phi);
y = phi;
} else {
double rho = cotp + p - phi;
double e = lambda * cos(phi)/rho;
x = rho * sin(e);
y = cotp - rho * cos(e);
}
return true;
}
void Perspective::Init(const TransformParams& params)
{
Transform::Init(params);
vx = params.x; vy = params.y; vz = params.z;
rx = vx; ry = vy; rz = vz;
m.Apply(rx,ry,rz);
scalefactor = (vx + 1) * tan(params.aw/2);
iscalefactor = 1/scalefactor;
a = params.ox;
b = params.oy;
c = params.oz;
a2 = sqr(a);
b2 = sqr(b);
c2 = sqr(c);
ia2 = 1/a2;
ib2 = 1/b2;
ic2 = 1/c2;
}
bool Perspective::Project(const TransformParams& params,
double x0, double y0,
double& x, double& y, double& z,
double& phi, double& lambda) const
{
(void(params));
bool result = false;
// Apply our rotation to the point we are projecting to
double x1 = -1;
double y1 = scalefactor * x0 + vy;
double z1 = scalefactor * y0 + vz;
m.Apply(x1,y1,z1);
// Projecting from (rx, ry, rz) to point (x1, y1, z1)
// Solve a quadratic obtained from equating line equation with r = 1
double qa = a2 * sqr(rx-x1) + b2 * sqr(ry-y1) + c2 * sqr(rz-z1);
double qb = 2 * (a2 * x1 * (rx-x1) + b2 * y1 * (ry-y1) + c2 * z1 * (rz-z1));
double qc = a2 * sqr(x1) + b2 * sqr(y1) + c2 * sqr(z1) - 1;
double qm = qb * qb - 4 * qa * qc;
if (qm >= 0) {
// Since qa is always positive, the + solution is nearest to the point
// of view, so we always use that one.
double k = (-qb + sqrt(qm)) / (2*qa);
x = k * rx + (1-k) * x1;
y = k * ry + (1-k) * y1;
z = k * rz + (1-k) * z1;
if (a == 1.0 && b == 1.0 && c == 1.0) {
phi = asin(z);
lambda = atan2(y, x);
} else {
// This is a point on the ellipsoid, so convert to lat long
double r = sqrt(sqr(a2 * x) + sqr(b2 * y));
phi = atan(c2 * z / r);
lambda = atan2(b2*y, a2*x);
// Now return the spherical x, y, z corresponding to phi, lambda
x = cos(lambda) * cos(phi);
y = sin(lambda) * cos(phi);
z = sin(phi);
}
result = true;
}
return result;
}
bool Perspective::ProjectInv(const TransformParams& params,
double phi, double lambda, double& x, double& y) const
{
(void(params));
bool result = false;
// Find where phi, lambda project to on the ellipsoid
double x0,y0,z0;
double nx,ny,nz;
if (a == 1.0 && b == 1.0 && c == 1.0) {
// Just a sphere
x0 = cos(lambda) * cos(phi);
y0 = sin(lambda) * cos(phi);
z0 = sin(phi);
minv.Apply(x0,y0,z0);
nx = x0; ny = y0; nz = z0;
} else {
// The normal vector
nx = cos(lambda);
ny = sin(lambda);
nz = tan(phi);
// The actual vector
double r = sqrt(1/(ia2 * sqr(nx) + ib2 * sqr(ny) + ic2 * sqr(nz)));
x0 = nx * r * ia2;
y0 = ny * r * ib2;
z0 = nz * r * ic2;
// And apply rotations
minv.Apply(x0,y0,z0);
minv.Apply(nx,ny,nz);
}
// Test for visibility - dot product of nx,ny,nz and the line
// towards to viewpoint must be positive
double p = (vx-x0) * nx + (vy-y0) * ny + (vz-z0) * nz;
if (p >= 0) {
// Project from (vx, vy, vz) through (x0,y0,z0) to (-1, y, z)
double t = (1 + x0)/(x0 - vx);
x = t * vy + (1-t) * y0; // Note change of axes
y = t * vz + (1-t) * z0;
x = (x - vy) * iscalefactor;
y = (y - vz) * iscalefactor;
result = true;
}
return result;
}
void CylindricalTransform::AdjustSize (int& w, int& h,
TransformParams& params)
{
if (params.scale < 1.0) {
w = int(w * params.scale);
params.scale = 1.0;
}
// If a parallel has been given, check it's valid
if (params.p < 0 || params.p >= piovertwo) {
error ("Invalid parallel");
}
double h0 = GetMaxHeight(params);
if (h0 > 0.0) { // Return <= 0 for don't adjust
int newh = int(h0 * w * params.scale / pi);
if (newh < h) { h = newh; };
}
}
// f(phi) = bt, g(phi) = (cos(t) + a)/(a+1), where sin(t) + at = k(a+1)sin(phi)
// and b + (a * pi)/2 = k*(a+1)
double Sinusoidal2::GetMaxHeight(const TransformParams& params)
{
double k0, b0;
CalcParams(params.a, params.p, k0, b0);
return b0 * piovertwo;
}
void Sinusoidal2::CalcParams(double a, double phi, double& k, double& b)
{
// Find the t value for phi
double t1 = 0;
double t2 = pi/2.0;
double epsilon = 1e-6;
double k0 = 0.0, b0 = 0.0;
while (fabs(t2 - t1) > epsilon) {
double t0 = (t1 + t2)/2.0;
k0 = sqr((cos(t0) + a) / ((a+1)*cos(phi)));
b0 = (k0 * (a+1))/(1 + a * pi / 2.0);
double p0 = b0 * sin(t0) + a * t0 - k0 * (a+1) * sin(phi);
if (p0 <= 0.0) t1 = t0;
else t2 = t0;
}
k = k0; b = b0;
}
void Sinusoidal2::Init(const TransformParams& params)
{
CylindricalTransform::Init(params);
a = params.a;
CalcParams(params.a, params.p, k, b);
}
double Sinusoidal2::GetLat(double y)
{
double t = y/b;
double phi = asin((b * (sin(t) + a * t)) / (k * (a+1)));
m = (a+1) / (cos(t) + a);
return phi;
}
double Sinusoidal2::GetLong(double x) const
{
return m * x;
}
class Sinusoidal2Equation : public Equation
{
public:
double operator() (double t) const {
// b(sin(t) + at) / k(a+1)-sin(phi);
return b * (sin(t) + a * t) / (k * (a+1)) - sinphi;
}
public:
double b;
double a;
double k;
double sinphi;
};
bool Sinusoidal2::GetXY(double phi, double lambda, double& x, double& y) const
{
bool result = false;
Sinusoidal2Equation equation;
equation.b = b; equation.a = a; equation.k = k; equation.sinphi = sin(phi);
double t = 0.0;
if (equation.FindRoot(-pi/2, pi/2, 1e-5, t)) {
y = b * t;
x = lambda * (cos(t) + a) / (a+1);
result = true;
}
return result;
}
// f(phi) = 1/a sin t, g(phi) = cos t, where t + 0.5 sin(2t) = 0.5 pi sin(phi)
double Mollweide::GetMaxHeight(const TransformParams& params)
{
double a0;
CalcParams(params.p, a0);
return 1.0/a0;
}
void Mollweide::CalcParams(double phi, double& a)
{
// Now we need to find a suitable t
// Good old binary search - note we cavalierly fail to check if there's
// a solution in the interval. Hell, we're going to terminate with something.
double t = 0; // phi = 0;
double t1 = pi/2; // phi = pi/2;
double b = 0.5 * pi * sin(phi);
double epsilon = 1e-6;
while (fabs(t1 - t) > epsilon) {
double t2 = (t + t1)/2.0;
double p = t2 + 0.5 * sin (2 * t2) - b;
if (p <= 0.0) t = t2;
else t1 = t2;
}
// t is our selected value
a = pi / (4 * sqr(cos (t)/cos(phi)));
}
void Mollweide::Init(const TransformParams& params)
{
CylindricalTransform::Init(params);
double phi = params.p;
CalcParams(phi, a);
}
double Mollweide::GetLat(double y)
{
// find t;
double t = asin(y*a);
m = 1/cos(t);
return asin((t + 0.5 * sin(2 * t)) * twooverpi);
}
double Mollweide:: GetLong(double x) const
{
return m * x;
}
class MollweideEquation : public Equation
{
public:
double operator() (double t) const {
return (t + 0.5 * sin(2 * t)) * twooverpi - sinphi;
}
public:
double sinphi;
};
bool Mollweide::GetXY(double phi, double lambda, double& x, double& y) const
{
bool result = false;
MollweideEquation equation;
equation.sinphi = sin(phi);
double t = 0.0;
if (equation.FindRoot(-pi/2, pi/2, 1e-5, t)) {
y = sin(t)/a;
x = lambda * cos(t);
result = true;
}
return result;
}
class TransformPlotter : public LinePlotter
{
public:
TransformPlotter(const Image& image_, const TransformParams& params_,
const Transform& transform_)
: image(image_), params(params_), transform(transform_) {}