@@ -137,6 +137,30 @@ double standardize_lat(double x){
137
137
}
138
138
return x ;
139
139
}
140
+ /**
141
+ * Returns the index of the 2d array in rot.
142
+ * @param index range from -3 to 3.
143
+ * @return the index into the rot 3d array.
144
+ */
145
+ static int get_rotate_index (int index ){
146
+ switch (index ){
147
+ case 0 :
148
+ return 0 ;
149
+ case 1 :
150
+ return 1 ;
151
+ case 2 :
152
+ return 2 ;
153
+ case 3 :
154
+ return 3 ;
155
+ case -1 :
156
+ return 4 ;
157
+ case -2 :
158
+ return 5 ;
159
+ case -3 :
160
+ return 6 ;
161
+ }
162
+ return 0 ;
163
+ }
140
164
/**
141
165
* Calculates if the point lies on or within the polygon.
142
166
* Very good explination of how this works: http://paulbourke.net/geometry/insidepoly/
@@ -369,7 +393,7 @@ static void dot_product(double a[2][2], double b[], double * ret){
369
393
for (i = 0 ; i < length ; i ++ ){
370
394
ret [i ] = 0 ;
371
395
for (j = 0 ; j < length ; j ++ ){
372
- ret [i ] += a [i ][j ]* b [i ];
396
+ ret [i ] += a [i ][j ]* b [j ];
373
397
}
374
398
}
375
399
}
@@ -426,12 +450,12 @@ static CapMap get_cap(double x, double y, double R, int npole, int spole, int in
426
450
double eps ;
427
451
if (y > R * PI /4.0 ){
428
452
capmap .region = north ;
429
- capmap .x = -1 * R * 3.0 * PI /4.0 + npole * R * PI /2.0 ;
453
+ capmap .x = R * ( - 3.0* PI /4.0 + npole * PI /2.0 ) ;
430
454
capmap .y = R * PI /2.0 ;
431
455
x = x - npole * R * PI /2.0 ;
432
456
}else if (y < -1 * R * PI /4.0 ){
433
457
capmap .region = south ;
434
- capmap .x = -1 * R * 3.0 * PI /4.0 + spole * R * PI /2 ;
458
+ capmap .x = R * ( - 3.0* PI /4.0 + spole * PI /2 ) ;
435
459
capmap .y = -1 * R * PI /2.0 ;
436
460
x = x - spole * R * PI /2.0 ;
437
461
}else {
@@ -444,23 +468,23 @@ static CapMap get_cap(double x, double y, double R, int npole, int spole, int in
444
468
eps = R * 1e-15 ; // Kludge. Fuzz to avoid some rounding errors.
445
469
if (capmap .region == north ){
446
470
if (y >= -1 * x - R * PI /4.0 - eps && y < x + R * 5.0 * PI /4.0 - eps ){
447
- capmap .cn = 1 ;
471
+ capmap .cn = ( npole + 1 ) % 4 ;
448
472
}else if (y > -1 * x - 1 * R * PI /4.0 + eps && y >= x + R * 5.0 * PI /4.0 - eps ){
449
- capmap .cn = 2 ;
473
+ capmap .cn = ( npole + 2 ) % 4 ;
450
474
}else if (y <= -1 * x - 1 * R * PI /4.0 + eps && y > x + R * 5.0 * PI /4.0 + eps ){
451
- capmap .cn = 3 ;
475
+ capmap .cn = ( npole + 3 ) % 4 ;
452
476
}else {
453
- capmap .cn = 0 ;
477
+ capmap .cn = npole ;
454
478
}
455
479
}else if (capmap .region == south ){
456
480
if (y <= x + R * PI /4.0 + eps && y > -1 * x - R * 5.0 * PI /4 + eps ){
457
- capmap .cn = 1 ;
481
+ capmap .cn = ( spole + 1 ) % 4 ;
458
482
}else if (y < x + R * PI /4.0 - eps && y <= -1 * x - R * 5.0 * PI /4.0 + eps ){
459
- capmap .cn = 2 ;
483
+ capmap .cn = ( spole + 2 ) % 4 ;
460
484
}else if (y >= x + R * PI /4.0 - eps && y < -1 * x - R * 5.0 * PI /4.0 - eps ){
461
- capmap .cn = 3 ;
485
+ capmap .cn = ( spole + 3 ) % 4 ;
462
486
}else {
463
- capmap .cn = 0 ;
487
+ capmap .cn = spole ;
464
488
}
465
489
}
466
490
return capmap ;
@@ -505,53 +529,57 @@ static XY combine_caps(double x, double y, double R, int npole, int spole, int i
505
529
double c [2 ] = {capmap .x ,capmap .y };
506
530
if (capmap .region == north ){
507
531
pole = npole ;
508
- tmpRot = rot [capmap .cn ];
509
- a [0 ] = R * -3.0 * PI /4.0 ;
510
- a [1 ] = PI /2.0 ;
532
+ a [0 ] = R * (-3.0 * PI /4.0 + pole * PI /2 );
533
+ a [1 ] = R * (PI /2.0 + pole * 0 );
534
+
535
+ tmpRot = rot [get_rotate_index (capmap .cn - pole )];
536
+ vector_sub (v ,c ,v_min_c );
537
+ dot_product (tmpRot ,v_min_c ,ret_dot );
538
+ vector_add (ret_dot , a , vector );
539
+
511
540
}else {
512
541
pole = spole ;
513
- tmpRot = rot [capmap .cn + RFACTOR ];
514
- a [0 ] = R * -3.0 * PI /4.0 ;
515
- a [1 ] = PI /-2.0 ;
542
+ a [0 ] = R * (-3.0 * PI /4.0 + pole * PI /2 );
543
+ a [1 ] = R * (PI /-2.0 + pole * 0 );
544
+
545
+ tmpRot = rot [get_rotate_index (-1 * (capmap .cn - pole ))];
546
+ vector_sub (v ,c ,v_min_c );
547
+ dot_product (tmpRot ,v_min_c ,ret_dot );
548
+
549
+ vector_add (ret_dot , a , vector );
516
550
}
517
551
518
- tmpVect [0 ] = R * pole * PI /2.0 ;
519
- tmpVect [1 ] = 0 ;
520
- // translate, rotate, then shift
521
- vector_sub (v ,c ,v_min_c );
522
- dot_product (tmpRot ,v_min_c ,ret_dot );
523
- vector_add (a ,tmpVect ,ret_add );
524
- vector_add (ret_dot , ret_add , vector );
525
552
xy .x = vector [0 ];
526
553
xy .y = vector [1 ];
527
554
return xy ;
528
555
}else {
529
556
// compute inverse function.
530
557
// get the current position of rHEALPix polar squares
531
- int pole = floor ( (capmap .x + R * 3.0 * PI /4.0 ) / (R * PI /2.0 ));
532
- double tmpVect [2 ] = {R * pole * PI /2.0 ,0 };
533
- double coord [2 ] = {x ,y };
534
- double (* tmpRot )[2 ];
535
558
int cn ;
536
- // translate polar square to position 0
537
- vector_sub (coord ,tmpVect ,v );
559
+ int pole = 0 ;
560
+ double (* tmpRot )[2 ];
561
+ double c [2 ] = {capmap .x ,capmap .y };
538
562
// disassemble
539
563
if (capmap .region == north ){
540
- cn = capmap .cn + RFACTOR ;
541
- a [0 ] = R * -3 * PI /4.0 ;
542
- a [1 ] = PI /2.0 ;
564
+ pole = npole ;
565
+ a [0 ] = R * (-3.0 * PI /4.0 + capmap .cn * PI /2 );
566
+ a [1 ] = R * (PI /2.0 + capmap .cn * 0 );
567
+
568
+ tmpRot = rot [get_rotate_index (-1 * (capmap .cn - pole ))];
569
+ vector_sub (v ,c ,v_min_c );
570
+ dot_product (tmpRot ,v_min_c ,ret_dot );
571
+ vector_add (ret_dot , a , vector );
572
+
543
573
}else {
544
- cn = capmap .cn ;
545
- a [0 ] = R * -3 * PI /4.0 ;
546
- a [1 ] = PI /-2.0 ;
574
+ pole = spole ;
575
+ a [0 ] = R * (-3.0 * PI /4.0 + capmap .cn * PI /2 );
576
+ a [1 ] = R * (PI /-2.0 + capmap .cn * 0 );
577
+
578
+ tmpRot = rot [get_rotate_index (capmap .cn - pole )];
579
+ vector_sub (v ,c ,v_min_c );
580
+ dot_product (tmpRot ,v_min_c ,ret_dot );
581
+ vector_add (ret_dot , a , vector );
547
582
}
548
- tmpVect [0 ] = R * capmap .cn * PI /2.0 ;
549
- tmpVect [1 ] = 0 ;
550
- // Math: Rotate Matrix * v-a + a + R*CN*{PI/2,0}
551
- vector_sub (v ,a ,v_min_c );
552
- dot_product (rot [cn ],v_min_c ,ret_dot );
553
- vector_add (ret_dot ,a ,ret_add );
554
- vector_add (ret_add ,tmpVect ,vector );
555
583
xy .x = vector [0 ];
556
584
xy .y = vector [1 ];
557
585
return xy ;
0 commit comments