This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
forked from segasai/q3c
-
Notifications
You must be signed in to change notification settings - Fork 2
/
q3cube.c
3036 lines (2634 loc) · 89.3 KB
/
q3cube.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
/*
Copyright (C) 2004-2021 Sergey Koposov
Email: skoposov AT ed DOT ac DOT uk
This file is part of Q3C.
Q3C 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.
Q3C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Q3C; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "my_bits.h"
static char __q3c_version[] = Q3C_VERSION;
static int q3c_setup_square_stack(struct q3c_square *stack, q3c_coord_t xmin,
q3c_coord_t ymin, q3c_coord_t xmax, q3c_coord_t ymax,
int n0);
static void q3c_stack_expand(struct q3c_square* work_stack, int *work_nstack,
struct q3c_square* out_stack, int *out_nstack,
int cur_depth, int res_depth);
static void q3c_output_stack( struct q3c_prm *hprm,
struct q3c_square *out_stack, int out_nstack,
struct q3c_square *work_stack, int work_nstack,
int face_num, int nside,
q3c_ipix_t *out_ipix_arr_fulls,
int *out_ipix_arr_fulls_pos,
q3c_ipix_t *out_ipix_arr_partials,
int *out_ipix_arr_partials_pos);
static void q3c_fast_get_equatorial_ellipse_xy_minmax(q3c_coord_t alpha,
q3c_coord_t delta,
q3c_coord_t d, q3c_coord_t e,
q3c_coord_t PA,
q3c_coord_t *ymin,
q3c_coord_t *ymax,
q3c_coord_t *zmin,
q3c_coord_t *zmax);
static void q3c_fast_get_equatorial_ellipse_xy_minmax_and_poly_coefs(q3c_coord_t alpha,
q3c_coord_t delta,
q3c_coord_t d, q3c_coord_t e,
q3c_coord_t PA,
q3c_coord_t *ymin,
q3c_coord_t *ymax,
q3c_coord_t *zmin,
q3c_coord_t *zmax,
q3c_coord_t *ayy,
q3c_coord_t *azz,
q3c_coord_t *ayz,
q3c_coord_t *ay,
q3c_coord_t *az,
q3c_coord_t *a);
static void q3c_fast_get_polar_ellipse_xy_minmax_and_poly_coefs(q3c_coord_t alpha,
q3c_coord_t delta,
q3c_coord_t d, q3c_coord_t e,
q3c_coord_t PA,
q3c_coord_t *ymin,
q3c_coord_t *ymax,
q3c_coord_t *zmin,
q3c_coord_t *zmax,
q3c_coord_t *ayy,
q3c_coord_t *azz,
q3c_coord_t *ayz,
q3c_coord_t *ay,
q3c_coord_t *az,
q3c_coord_t *a);
static void q3c_fast_get_ellipse_xy_minmax_and_poly_coefs(char face_num,
q3c_coord_t ra0,
q3c_coord_t dec0,
q3c_coord_t d0,
q3c_coord_t e,
q3c_coord_t PA0,
q3c_coord_t *ymin,
q3c_coord_t *ymax,
q3c_coord_t *zmin,
q3c_coord_t *zmax,
q3c_coord_t *ayy,
q3c_coord_t *azz,
q3c_coord_t *ayz,
q3c_coord_t *ay,
q3c_coord_t *az,
q3c_coord_t *a);
static void q3c_fast_get_polar_ellipse_xy_minmax(q3c_coord_t alpha,
q3c_coord_t delta, q3c_coord_t d,
q3c_coord_t e, q3c_coord_t PA,
q3c_coord_t *ymin,
q3c_coord_t *ymax,
q3c_coord_t *zmin,
q3c_coord_t *zmax);
static void q3c_fast_get_xy_minmax(char, q3c_region, void *, q3c_coord_t *,
q3c_coord_t *, q3c_coord_t *,
q3c_coord_t *);
static void q3c_fast_get_circle_xy_minmax(char, q3c_coord_t, q3c_coord_t, q3c_coord_t,
q3c_coord_t *, q3c_coord_t *, q3c_coord_t *,
q3c_coord_t *);
static void q3c_fast_get_ellipse_xy_minmax(char, q3c_coord_t, q3c_coord_t,
q3c_coord_t,q3c_coord_t, q3c_coord_t,
q3c_coord_t *, q3c_coord_t *, q3c_coord_t *,
q3c_coord_t *);
static void array_filler(q3c_ipix_t *fulls, int fullpos,
q3c_ipix_t *parts, int partpos);
void q3c_get_version(char *out, int maxchar)
{
strncpy(out,__q3c_version,maxchar);
}
/* Distance calculation routine, inputs and outputs are in degrees */
q3c_coord_t q3c_dist(q3c_coord_t ra1, q3c_coord_t dec1,
q3c_coord_t ra2, q3c_coord_t dec2)
{
return 2 * q3c_asin (q3c_sqrt (q3c_sindist(ra1, dec1, ra2, dec2))) * Q3C_RADEG;
}
/* sin(Distance/2)^2 calculation routine, inputs and outputs are in degrees */
q3c_coord_t q3c_sindist(q3c_coord_t ra1, q3c_coord_t dec1,
q3c_coord_t ra2, q3c_coord_t dec2)
{
/* Fast and precise way to compute the distance on the sphere
* it uses just 3 evaluations of trigonometric functions
*/
q3c_coord_t x, y, z;
x = q3c_sin ((ra1 - ra2) / 2 * Q3C_DEGRA);
x *= x;
y = q3c_sin ((dec1 - dec2) / 2 * Q3C_DEGRA);
y *= y;
/* Seem to be more precise :) */
z = q3c_cos ((dec1 + dec2) / 2 * Q3C_DEGRA);
z *= z;
return x * (z - y) + y;
}
/* convert angular coordinates (ra,dec) -> ipix
* ang2ipix is also outputting x,y on the cube face
* Coordinates on the cube face are x[-0.5,0.5] y[-0.5,0.5]
* ipix structure is the following:
* Bit 0(leftmost) : unused
* Bit 1-3: Cube face id (0 is the top face, 5 is the bottom face,
* 1-4 are faces looking towards (x,y)=(1,0), (0,1), (-1,0), (0,-1)
* Bit 4-63: 60-bit long location in the quadtree on the cube face,
* encoded by z-order (e.g. two interleaved bit strings on for x,
* another for y)
* The mapping between x,y,z to (ra,dec) is such that
* (x,y,z)=(1,0,0) corresponds to (ra,dec)=(0,0)
* (x,y,z)=(0,0,1) corresponds to (ra,dec)=(0,90)
*/
void q3c_ang2ipix_xy (struct q3c_prm *hprm, q3c_coord_t ra0, q3c_coord_t dec0,
char *out_face_num, q3c_ipix_t *ipix, q3c_coord_t *x_out,
q3c_coord_t *y_out)
/* ra in degrees, dec in degrees */
/* strictly 0<=ra<360 and -90<=dec<=90 */
{
q3c_coord_t x0 = 0, y0 = 0, ra1, dec1, tmp0, td1;
q3c_coord_t ra,dec;
q3c_ipix_t nside = hprm->nside, *xbits = hprm->xbits,
*ybits = hprm->ybits, xi, yi;
char face_num;
/* We check against crazy right ascensions */
ra = UNWRAP_RA(ra0);
/* protection against wrong declinations */
if (dec0 > 90)
{
dec = 90;
}
else if (dec0 < -90)
{
dec = -90;
}
else
{
dec = dec0;
}
face_num = q3c_fmod ((ra + 45) / 90, 4);
/* for equatorial pixels we'll have face_num from 1 to 4 */
ra1 = Q3C_DEGRA * (ra - 90 * (q3c_coord_t)face_num);
dec1 = Q3C_DEGRA * dec;
x0 = q3c_tan (ra1);
td1 = q3c_tan(dec1);
y0 = td1 / q3c_cos (ra1);
face_num++;
if (y0 > 1)
{
face_num = 0;
ra1 = Q3C_DEGRA * ra;
tmp0 = 1 / td1;
q3c_sincos (ra1, x0, y0);
x0 *= tmp0;
y0 *= (-tmp0);
/*x0 = q3c_sin(ra1) / q3c_tan(dec1);*/
/*y0 = -q3c_cos(ra1) / q3c_tan(dec1);*/
/* I don't know
* Probably I should write (sin(ra)/sin(dec))*cos(dec) to
* not loose the precision in the region where dec ~=90deg
*/
}
else if (y0 < -1)
{
face_num = 5;
ra1 = Q3C_DEGRA * ra;
tmp0 = 1 / td1;
q3c_sincos (ra1, x0, y0);
x0 *= (-tmp0);
y0 *= (-tmp0);
/*x0 = -q3c_sin(ra1) / q3c_tan(dec1);*/
/*y0 = -q3c_cos(ra1) / q3c_tan(dec1);*/
}
*x_out = x0 / 2;
*y_out = y0 / 2;
x0 = (x0 + 1) / 2;
y0 = (y0 + 1) / 2;
/* Now I produce the final pixel value by converting x and y values
* to bitfields and combining them by interleaving, using the
* predefined arrays xbits and ybits
*/
xi = (q3c_ipix_t)(x0 * nside);
yi = (q3c_ipix_t)(y0 * nside);
/* This two following statements are written to handle the
* case of upper right corner of base square */
if (xi == nside)
{
xi--;
}
if (yi == nside)
{
yi--;
}
*ipix = q3c_xiyi2ipix(nside, xbits, ybits, face_num, xi, yi);
*out_face_num = face_num;
}
/* convert coordinates (ra,dec) -> ipix
* ra, dec in degrees
* and strictly 0<=ra<360 and -90<=dec<=90
*/
void q3c_ang2ipix(struct q3c_prm *hprm, q3c_coord_t ra0, q3c_coord_t dec0,
q3c_ipix_t *ipix)
{
q3c_coord_t tmpx, tmpy;
char face;
q3c_ang2ipix_xy(hprm, ra0, dec0, &face, ipix, &tmpx, &tmpy);
}
/* get the cube face number for a given coordinates
* ra, dec in degrees
* and strictly 0<=ra<360 and -90<=dec<=90
*/
char q3c_get_facenum(q3c_coord_t ra, q3c_coord_t dec)
{
q3c_coord_t y0 = 0;
char face_num;
if (dec >= 90)
/* Poles */
{
return 0;
}
else if (dec <= -90)
{
return 5;
}
face_num = q3c_fmod ((ra + 45) / 90, 4);
/*for equatorial pixels we'll have face_num from 1 to 4 */
y0 = q3c_tan(dec * Q3C_DEGRA) /
q3c_cos(Q3C_DEGRA * (ra - 90 * (q3c_coord_t)face_num));
face_num++;
if (y0 > 1)
{
return 0;
}
else if (y0 < -1)
{
return 5;
}
else
{
return face_num;
}
}
/* get the main cube face number for a given region
* CIRCLE/ELLIPSE/POLYGON
*/
char q3c_get_region_facenum(q3c_region region, void *data)
{
switch(region)
{
case Q3C_CIRCLE:
{
return q3c_get_facenum(((q3c_circle_region*)data)->ra,
((q3c_circle_region*)data)->dec);
}
case Q3C_ELLIPSE:
{
return q3c_get_facenum(((q3c_ellipse_region*)data)->ra,
((q3c_ellipse_region*)data)->dec);
}
case Q3C_POLYGON:
{
return q3c_get_facenum_poly((q3c_poly *)data);
}
default:
return 1;
}
}
/* Check that the given point (alpha, delta0)
* is within the ellipse specified by
* center, maj_ax, axis ratio and positional angle
*/
char q3c_in_ellipse(q3c_coord_t alpha, q3c_coord_t delta0,
q3c_coord_t alpha1, q3c_coord_t delta01, q3c_coord_t d0,
q3c_coord_t e, q3c_coord_t PA0)
{
q3c_coord_t d_alpha = (alpha1 - alpha) * Q3C_DEGRA;
q3c_coord_t delta1 = delta01 * Q3C_DEGRA;
q3c_coord_t delta = delta0 * Q3C_DEGRA;
q3c_coord_t PA = PA0 * Q3C_DEGRA;
q3c_coord_t d = d0 * Q3C_DEGRA;
q3c_coord_t t1 = q3c_cos(d_alpha);
q3c_coord_t t22 = q3c_sin(d_alpha);
q3c_coord_t t3 = q3c_cos(delta1);
q3c_coord_t t32 = q3c_sin(delta1);
q3c_coord_t t6 = q3c_cos(delta);
q3c_coord_t t26 = q3c_sin(delta);
q3c_coord_t t9 = q3c_cos(d);
q3c_coord_t t55 = q3c_sin(d);
q3c_coord_t t2;
q3c_coord_t t4;
q3c_coord_t t5;
q3c_coord_t t7;
q3c_coord_t t8;
q3c_coord_t t10;
q3c_coord_t t11;
q3c_coord_t t13;
q3c_coord_t t14;
q3c_coord_t t15;
q3c_coord_t t18;
q3c_coord_t t19;
q3c_coord_t t24;
q3c_coord_t t31;
q3c_coord_t t36;
q3c_coord_t t37;
q3c_coord_t t38;
q3c_coord_t t45;
q3c_coord_t t56;
q3c_coord_t t57;
q3c_coord_t t60;
q3c_coord_t t61;
q3c_coord_t t63;
if ((t3 * t6 * t1 + t32 * t26) < 0)
{
return 0;
}
t2 = t1 * t1;
t4 = t3 * t3;
t5 = t2 * t4;
t7 = t6 * t6;
t8 = t5 * t7;
t10 = t9 * t9;
t11 = t7 * t10;
t13 = q3c_cos(PA);
t14 = t13 * t13;
t15 = t14 * t10;
t18 = t7 * t14;
t19 = t18 * t10;
t24 = q3c_sin(PA);
t31 = t1 * t3;
t36 = 2.0 * t31 * t32 * t26 * t6;
t37 = t31 * t32;
t38 = t26 * t6;
t45 = t4 * t10;
t56 = t55 * t55;
t57 = t4 * t7;
t60 = -t8 + t5 * t11 + 2.0 * t5 * t15 - t5 * t19 - 2.0 * t1 * t4 * t22 * t10 * t24 * t13 * t26 - t36 + 2.0 * t37 * t38 * t10 - 2.0 * t37 * t38 * t15 - t45 * t14 - t45 * t2 + 2.0 * t22 * t3 * t32 * t6 * t24 * t10 * t13 - t56 + t7 - t11 + t4 - t57 + t57 * t10 + t19 - t18 * t45;
t61 = e * e;
t63 = t60 * t61 + t8 + t57 - t4 - t7 + t56 + t36;
return t63 > 0;
}
/* Checking whether the box (xmin,ymin,xmax,ymax) intersects other faces or
* not. If yes, I setup the array "points" designed to help us work on
* other faces ( points array will then have the coordinates on a main face
* which should be mapped to other faces
* !!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* !!!!!!! It does change the arguments (xmin,xmax, ymin, ymax) !!!!!!!!
*/
void q3c_multi_face_check(q3c_coord_t *xmin0, q3c_coord_t *ymin0,
q3c_coord_t *xmax0, q3c_coord_t *ymax0,
q3c_coord_t *points, char *multi_flag)
{
const q3c_coord_t xmin = *xmin0,
xmax = *xmax0,
ymin = *ymin0,
ymax = *ymax0;
if (xmin < -Q3C_HALF)
{
if (ymin < -Q3C_HALF)
{
points[0] = xmax;
points[1] = ymin;
points[2] = xmin;
points[3] = ymax;
*multi_flag = 2;
*xmin0 = -Q3C_HALF;
*ymin0 = -Q3C_HALF;
}
else
{
if (ymax > Q3C_HALF)
{
points[0] = xmax;
points[1] = ymax;
points[2] = xmin;
points[3] = ymin;
*multi_flag = 2;
*xmin0 = -Q3C_HALF;
*ymax0 = Q3C_HALF;
}
else
{
points[0] = xmin;
points[1] = (ymin + ymax) / 2;
*multi_flag = 1;
*xmin0 = -Q3C_HALF;
}
}
}
else
{
if (xmax > Q3C_HALF)
{
if (ymin < -Q3C_HALF)
{
points[0] = xmin;
points[1] = ymin;
points[2] = xmax;
points[3] = ymax;
*multi_flag = 2;
*xmax0 = Q3C_HALF;
*ymin0 = -Q3C_HALF;
}
else
{
if (ymax > Q3C_HALF)
{
points[0] = xmin;
points[1] = ymax;
points[2] = xmax;
points[3] = ymin;
*multi_flag = 2;
*xmax0 = Q3C_HALF;
*ymax0 = Q3C_HALF;
}
else
{
points[0] = xmax;
points[1] = (ymin + ymax) / 2;
*multi_flag = 1;
*xmax0 = Q3C_HALF;
}
}
}
else
{
if (ymin < -Q3C_HALF)
{
points[0] = (xmin + xmax) / 2;
points[1] = ymin;
*multi_flag = 1;
*ymin0 = -Q3C_HALF;
}
else
{
if (ymax > Q3C_HALF)
{
points[0] = (xmin + xmax) / 2;
points[1] = ymax;
*multi_flag = 1;
*ymax0 = Q3C_HALF;
}
else
{
*multi_flag = 0;
}
}
}
}
}
/* Get the list of 4 ipix ranges
* which decsribe the neighborhood of a given point
* specified by q3c_region
* ra in degrees, dec in degrees, radius in degrees
* strictly 0<=ra<360 and -90<=dec<=90
*/
void q3c_get_nearby(struct q3c_prm *hprm, q3c_region region, void *region_data,
q3c_ipix_t *ipix)
{
q3c_coord_t xmin, xmax, ymin, ymax, xesize, yesize, points[4];
q3c_ipix_t nside = hprm->nside, *xbits = hprm->xbits, *ybits = hprm->ybits,
*ipix_cur = ipix, ipix0, xi, yi, n0, n1, ixmin,
ixmax, iymin, iymax, xistack[4], yistack[4], facestack[4],
nstack[4];
char face_num, face_num0, multi_flag;
int i, nistack = 0;
const q3c_coord_t q3c_lg2 = Q3C_LG2;
if (q3c_too_big_check(region, region_data))
{
/* the whole sky */
q3c_ipix_t maxval = 6 * (nside * nside);
*(ipix_cur++) = -1;
*(ipix_cur++) = maxval;
for(i = 1; i < 4; i++ )
{
*(ipix_cur++) = 1;
*(ipix_cur++) = -1;
}
return;
}
face_num = q3c_get_region_facenum(region, region_data);
face_num0 = face_num;
q3c_fast_get_xy_minmax(face_num, region, region_data, &xmin, &xmax, &ymin, &ymax);
/* xmin, xmax, ymin, ymax are in the coordinate system of the cube face
* where -0.5<=x<=0.5 and -0.5<=y<=0.5
*/
#ifdef Q3C_DEBUG
fprintf(stderr, "XMIN: %f XMAX: %f YMIN: %f YMAX: %f\n", xmin, xmax, ymin, ymax);
#endif
q3c_multi_face_check(&xmin, &ymin, &xmax, &ymax, points, &multi_flag);
if (multi_flag == 0)
{
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
if (iymin == iymax)
{
if (ixmin == ixmax)
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
nistack = 1;
}
else
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
xistack[1] = (q3c_ipix_t)(ixmax * n1);
yistack[1] = (q3c_ipix_t)(iymin * n1);
facestack[1] = face_num;
nstack[1] = n1;
nistack = 2;
}
}
else
{
if (ixmin == ixmax)
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
xistack[1] = (q3c_ipix_t)(ixmin * n1);
yistack[1] = (q3c_ipix_t)(iymax * n1);
facestack[1] = face_num;
nstack[1] = n1;
nistack = 2;
}
else
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
xistack[1] = (q3c_ipix_t)(ixmin * n1);
yistack[1] = (q3c_ipix_t)(iymax * n1);
facestack[1] = face_num;
nstack[1] = n1;
xistack[2] = (q3c_ipix_t)(ixmax * n1);
yistack[2] = (q3c_ipix_t)(iymin * n1);
facestack[2] = face_num;
nstack[2] = n1;
xistack[3] = (q3c_ipix_t)(ixmax * n1);
yistack[3] = (q3c_ipix_t)(iymax * n1);
facestack[3] = face_num;
nstack[3] = n1;
nistack = 4;
}
}
}
else
{
if (multi_flag == 1)
{
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
if (ixmin == ixmax)
{
if (iymin == iymax)
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
nistack = 1;
}
else
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
xistack[1] = (q3c_ipix_t)(ixmin * n1);
yistack[1] = (q3c_ipix_t)(iymax * n1);
facestack[1] = face_num;
nstack[1] = n1;
nistack = 2;
}
}
else
{
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
xistack[1] = (q3c_ipix_t)(ixmax * n1);
yistack[1] = (q3c_ipix_t)(iymin * n1);
facestack[1] = face_num;
nstack[1] = n1;
nistack = 2;
}
face_num = q3c_xy2facenum(2 * points[0], 2 * points[1], face_num0);
q3c_fast_get_xy_minmax(face_num, region, region_data, &xmin,
&xmax, &ymin, &ymax);
xmax = (xmax > Q3C_HALF ? Q3C_HALF : xmax);
xmin = (xmin < -Q3C_HALF ? -Q3C_HALF : xmin);
ymax = (ymax > Q3C_HALF ? Q3C_HALF : ymax);
ymin = (ymin < -Q3C_HALF ? -Q3C_HALF : ymin);
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
if (ixmin == ixmax)
{
if (iymin == iymax)
{
xistack[nistack] = (q3c_ipix_t)(ixmin * n1);
yistack[nistack] = (q3c_ipix_t)(iymin * n1);
facestack[nistack] = face_num;
nstack[nistack++] = n1;
}
else
{
xistack[nistack] = (q3c_ipix_t)(ixmin * n1);
yistack[nistack] = (q3c_ipix_t)(iymin * n1);
facestack[nistack] = face_num;
nstack[nistack++] = n1;
xistack[nistack] = (q3c_ipix_t)(ixmin * n1);
yistack[nistack] = (q3c_ipix_t)(iymax * n1);
facestack[nistack] = face_num;
nstack[nistack++] = n1;
}
}
else
{
xistack[nistack] = (q3c_ipix_t)(ixmin * n1);
yistack[nistack] = (q3c_ipix_t)(iymin * n1);
facestack[nistack] = face_num;
nstack[nistack++] = n1;
xistack[nistack] = (q3c_ipix_t)(ixmax * n1);
yistack[nistack] = (q3c_ipix_t)(iymin * n1);
facestack[nistack] = face_num;
nstack[nistack++] = n1;
}
}
else
{
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
xistack[0] = (q3c_ipix_t)(ixmin * n1);
yistack[0] = (q3c_ipix_t)(iymin * n1);
facestack[0] = face_num;
nstack[0] = n1;
nistack = 1;
face_num = q3c_xy2facenum(2 * points[0], 2 * points[1], face_num0);
q3c_fast_get_xy_minmax(face_num, region, region_data, &xmin,
&xmax, &ymin, &ymax);
xmax = (xmax > Q3C_HALF ? Q3C_HALF : xmax);
xmin = (xmin < -Q3C_HALF ? -Q3C_HALF : xmin);
ymax = (ymax > Q3C_HALF ? Q3C_HALF : ymax);
ymin = (ymin < -Q3C_HALF ? -Q3C_HALF : ymin);
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
xistack[1] = (q3c_ipix_t)(ixmin * n1);
yistack[1] = (q3c_ipix_t)(iymin * n1);
facestack[1] = face_num;
nstack[1] = n1;
nistack = 2;
face_num = q3c_xy2facenum(2 * points[2], 2 * points[3], face_num0);
q3c_fast_get_xy_minmax(face_num, region, region_data, &xmin,
&xmax, &ymin, &ymax);
xmax = (xmax > Q3C_HALF ? Q3C_HALF : xmax);
xmin = (xmin < -Q3C_HALF ? -Q3C_HALF : xmin);
ymax = (ymax > Q3C_HALF ? Q3C_HALF : ymax);
ymin = (ymin < -Q3C_HALF ? -Q3C_HALF : ymin);
xesize = xmax - xmin;
yesize = ymax - ymin;
xesize = xesize > yesize ? xesize : yesize;
if (xesize * nside < 1)
/* If the region is too small */
{
xesize = 1 / (q3c_coord_t)nside;
}
n0 = 1 << ((q3c_ipix_t)(-q3c_ceil(q3c_log(xesize) / q3c_lg2)));
/* n0 is now the level of quadtree for which the minimal
* element is >~ our ellipse
*/
ixmin = (Q3C_HALF + xmin) * n0;
ixmax = (Q3C_HALF + xmax) * n0;
iymin = (Q3C_HALF + ymin) * n0;
iymax = (Q3C_HALF + ymax) * n0;
ixmax = (ixmax == n0 ? n0 - 1 : ixmax);
iymax = (iymax == n0 ? n0 - 1 : iymax);
n1 = nside / n0;
xistack[2] = (q3c_ipix_t)(ixmin * n1);
yistack[2] = (q3c_ipix_t)(iymin * n1);
facestack[2] = face_num;
nstack[2] = n1;
nistack = 3;
}
}
#ifdef Q3C_DEBUG
fprintf(stderr, "MULTI_FLAG: %d\n", multi_flag);
#endif
/* Now I produce the final pixel value by converting x and y values to bitfields
and combining them by interleaving, using the predefined arrays xbits and ybits
*/
for(i = 0; i < nistack; i++)
{
face_num = facestack[i];
xi = xistack[i];
yi = yistack[i];
n1 = nstack[i];
ipix0 = q3c_xiyi2ipix(nside, xbits, ybits, face_num, xi, yi);
*(ipix_cur++) = ipix0;
*(ipix_cur++) = ipix0 + n1 * n1 - 1;
/* IMPORTANT!! I subtract 1 to make after the query with <=ipix<=
*/
}
for(; i < 4; i++)
{
*(ipix_cur++) = 1;
*(ipix_cur++) = -1;
}
}
/* Converts integer coordinates on cube face to
* ipix number by performing bit interleaving
*/
q3c_ipix_t q3c_xiyi2ipix(q3c_ipix_t nside, q3c_ipix_t *xbits,
q3c_ipix_t *ybits, char face_num,
q3c_ipix_t xi, q3c_ipix_t yi)
{
return ((q3c_ipix_t)face_num) * nside * nside +
xbits[xi % Q3C_I1] + ybits[yi % Q3C_I1] +
(xbits[(xi >> Q3C_INTERLEAVED_NBITS) % Q3C_I1] +
ybits[(yi >> Q3C_INTERLEAVED_NBITS) % Q3C_I1]) * Q3C_I1 * Q3C_I1;
/*8byte computation*/
}
/* convert ipix number ra,dec in degrees */
void q3c_ipix2ang(struct q3c_prm *hprm, q3c_ipix_t ipix,
q3c_coord_t *ra, q3c_coord_t *dec)
{
q3c_ipix_t nside = hprm->nside, ipix1, *xbits1 = hprm->xbits1,
*ybits1 = hprm->ybits1, i2, i3, x0, y0;
q3c_coord_t x, y, ra0;
char face_num = ipix / (nside * nside);
const q3c_ipix_t ii1 = 1 << (Q3C_INTERLEAVED_NBITS / 2);
ipix1 = ipix % (nside * nside);
i3 = ipix1 % Q3C_I1;
i2 = ipix1 / Q3C_I1;
x0 = xbits1[i3];
y0 = ybits1[i3];
i3 = i2 % Q3C_I1;
i2 = i2 / Q3C_I1;
x0 += xbits1[i3] * ii1;
y0 += ybits1[i3] * ii1;
i3 = i2 % Q3C_I1;
i2 = i2 / Q3C_I1;