forked from Stellarium/stellarium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeCombinedCatalogue.v2.C
1704 lines (1465 loc) · 49.7 KB
/
MakeCombinedCatalogue.v2.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
// Author and Copyright: Johannes Gajdosik, 2006
// Modifications: Alexander Wolf, 2013
//
// 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
//
// g++ -O2 MakeCombinedCatalogue.C -o MakeCombinedCatalogue
// change catalogue locations according to your needs:
const char *nomad_names[]={
"/storage/1T/nomad.sml/Nomad00.sml",
"/storage/1T/nomad.sml/Nomad01.sml",
"/storage/1T/nomad.sml/Nomad02.sml",
"/storage/1T/nomad.sml/Nomad03.sml",
"/storage/1T/nomad.sml/Nomad04.sml",
"/storage/1T/nomad.sml/Nomad05.sml",
"/storage/1T/nomad.sml/Nomad06.sml",
"/storage/1T/nomad.sml/Nomad07.sml",
"/storage/1T/nomad.sml/Nomad08.sml",
"/storage/1T/nomad.sml/Nomad09.sml",
"/storage/1T/nomad.sml/Nomad10.sml",
"/storage/1T/nomad.sml/Nomad11.sml",
"/storage/1T/nomad.sml/Nomad12.sml",
"/storage/1T/nomad.sml/Nomad13.sml",
"/storage/1T/nomad.sml/Nomad14.sml",
"/storage/1T/nomad.sml/Nomad15.sml",
"/storage/1T/nomad.sml/Nomad16.sml",
0
};
#define NR_OF_HIP 120416
#define MAX_HIP_LEVEL 2
#define MAX_TYC_LEVEL 4
#define MAX_LEVEL 9
static
const char *output_file_names[MAX_LEVEL+1] = {
"stars0.cat",
"stars1.cat",
"stars2.cat",
"stars3.cat",
"stars4.cat",
"stars5.cat",
"stars6.cat",
"stars7.cat",
"stars8.cat",
"stars9.cat"
};
static
const char *component_ids_filename = "stars_hip_component_ids.cat";
static
const char *sp_filename = "stars_hip_sp.cat";
static
const double level_mag_limit[MAX_LEVEL+1] = {
6,7.5,9,
10.5,12,
13.5,15,16.5,18,19.5
};
/*
Vector (simple 3d-Vector)
Author and Copyright: Johannes Gajdosik, 2006
License: GPL
*/
#include <math.h>
struct Vector {
double x[3];
inline double length(void) const;
inline void normalize(void);
const Vector &operator+=(const Vector &a) {
x[0] += a.x[0];
x[1] += a.x[1];
x[2] += a.x[2];
}
const Vector &operator-=(const Vector &a) {
x[0] -= a.x[0];
x[1] -= a.x[1];
x[2] -= a.x[2];
}
};
static inline
Vector operator^(const Vector &a,const Vector &b) {
Vector c;
c.x[0] = a.x[1]*b.x[2] - a.x[2]*b.x[1];
c.x[1] = a.x[2]*b.x[0] - a.x[0]*b.x[2];
c.x[2] = a.x[0]*b.x[1] - a.x[1]*b.x[0];
return c;
}
static inline
Vector operator-(const Vector &a,const Vector &b) {
Vector c;
c.x[0] = a.x[0]-b.x[0];
c.x[1] = a.x[1]-b.x[1];
c.x[2] = a.x[2]-b.x[2];
return c;
}
static inline
Vector operator+(const Vector &a,const Vector &b) {
Vector c;
c.x[0] = a.x[0]+b.x[0];
c.x[1] = a.x[1]+b.x[1];
c.x[2] = a.x[2]+b.x[2];
return c;
}
static inline
Vector operator*(double a,const Vector &b) {
Vector c;
c.x[0] = a*b.x[0];
c.x[1] = a*b.x[1];
c.x[2] = a*b.x[2];
return c;
}
static inline
Vector operator*(const Vector &b,double a) {
Vector c;
c.x[0] = a*b.x[0];
c.x[1] = a*b.x[1];
c.x[2] = a*b.x[2];
return c;
}
static inline
double operator*(const Vector &a,const Vector &b) {
return a.x[0]*b.x[0]+a.x[1]*b.x[1]+a.x[2]*b.x[2];
}
double Vector::length(void) const {
return sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
}
void Vector::normalize(void) {
const double l = 1.0/length();
x[0] *= l;
x[1] *= l;
x[2] *= l;
}
void ChangeEpoch(double delta_years,
double &ra,double &dec, // degrees
double pm_ra,double pm_dec // mas/yr
) {
ra *= (M_PI/180);
dec *= (M_PI/180);
const double cdec = cos(dec);
Vector x = {cos(ra)*cdec,sin(ra)*cdec,sin(dec)};
const Vector north = {0.0,0.0,1.0};
Vector axis0 = north ^ x;
axis0.normalize();
const Vector axis1 = x ^ axis0;
const double f = delta_years*(0.001/3600)*(M_PI/180);
x += pm_ra*f*axis1 + pm_dec*f*axis0;
ra = atan2(x.x[1],x.x[0]);
if (ra < 0.0) ra += 2*M_PI;
dec = atan2(x.x[2],sqrt(x.x[0]*x.x[0]+x.x[1]*x.x[1]));
ra *= (180/M_PI);
dec *= (180/M_PI);
}
//------------------------------------------------
/*
GeodesicGrid: a library for dividing the sphere into triangle zones
by subdividing the icosahedron
Author and Copyright: Johannes Gajdosik, 2006
This library requires a simple Vector library,
which may have different copyright and license.
In the moment I choose to distribute the library under the GPL,
later I may choose to additionally distribute it under a more
relaxed license like the LGPL. If you want to have the library
under another license, please ask me.
This library 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.
This library 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 this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using namespace std;
struct HalfSpace {
// all x with x*n-d >= 0
Vector n;
double d;
bool inside(const Vector &x) const {return (x*n>=d);}
};
class GeodesicGrid {
// Grid of triangles (zones) on the sphere with radius 1,
// generated by subdividing the icosahedron.
// level 0: just the icosahedron, 20 zones
// level 1: 80 zones, level 2: 320 zones, ...
// Each zone has a unique integer number in the range
// [0,getNrOfZones()-1].
public:
GeodesicGrid(int max_level);
~GeodesicGrid(void);
int getMaxLevel(void) const {return max_level;}
static int nrOfZones(int level)
{return (20<<(level<<1));} // 20*4^level
int getNrOfZones(void) const {return nrOfZones(max_level);}
void getTriangleCorners(int lev, int index, Vector& c0, Vector& c1, Vector& c2) const;
// Return the position of the 3 corners for the triangle at the given level and index
int getPartnerTriangle(int lev, int index) const;
// Return the index of the partner triangle with which to form a paralelogram
typedef void (VisitFunc)(int lev,int index,
const Vector &c0,
const Vector &c1,
const Vector &c2,
void *context);
void visitTriangles(int max_visit_level,
VisitFunc *func,void *context) const;
int searchZone(const Vector &v,int search_level) const;
// find the zone number in which a given point lies.
// prerequisite: v*v==1
// When the point lies on the border of two or more zones,
// one such zone is returned (always the same one,
// because the algorithm is deterministic).
void searchZones(const HalfSpace *half_spaces,int nr_of_half_spaces,
int **inside,int **border,int max_search_level) const;
// find all zones that lie fully(inside) or partly(border)
// in the intersection of the given half spaces.
// The result is accurate when (0,0,0) lies on the border of
// each half space. If this is not the case,
// the result may be inaccurate, because it is assumed, that
// a zone lies in a half space when its 3 corners lie in
// this half space.
// inside[l] points to the begin of an integer array of size nrOfZones(l),
// border[l] points to one after the end of the same integer array.
// The array will be filled from the beginning with the inside zone numbers
// and from the end with the border zone numbers of the given level l
// for 0<=l<=getMaxLevel().
// inside[l] will not contain zones that are already contained
// in inside[l1] for some l1 < l.
// In order to restrict search depth set max_search_level < max_level,
// for full search depth set max_search_level = max_level,
private:
const Vector& getTriangleCorner(int lev, int index, int cornerNumber) const;
void initTriangle(int lev,int index,
const Vector &c0,
const Vector &c1,
const Vector &c2);
void visitTriangles(int lev,int index,
const Vector &c0,
const Vector &c1,
const Vector &c2,
int max_visit_level,
VisitFunc *func,
void *context) const;
void searchZones(int lev,int index,
const HalfSpace *half_spaces,
const int nr_of_half_spaces,
const int *index_of_used_half_spaces,
const int half_spaces_used,
const bool *corner0_inside,
const bool *corner1_inside,
const bool *corner2_inside,
int **inside,int **border,int max_search_level) const;
private:
const int max_level;
struct Triangle {
Vector e0,e1,e2; // Seitenmittelpunkte
};
Triangle **triangles;
// 20*(4^0+4^1+...+4^n)=20*(4*(4^n)-1)/3 triangles total
// 2+10*4^n corners
};
class GeodesicSearchResult {
public:
GeodesicSearchResult(const GeodesicGrid &grid);
~GeodesicSearchResult(void);
void search(const HalfSpace *half_spaces,
const int nr_of_half_spaces,
int max_search_level);
void search(const Vector &e0,const Vector &e1,const Vector &e2,const Vector &e3,
int max_search_level);
void print(void) const;
private:
friend class GeodesicSearchInsideIterator;
friend class GeodesicSearchBorderIterator;
const GeodesicGrid &grid;
int **const zones;
int **const inside;
int **const border;
};
class GeodesicSearchBorderIterator {
public:
GeodesicSearchBorderIterator(const GeodesicSearchResult &r,int level)
: r(r),level((level<0)?0:(level>r.grid.getMaxLevel())
?r.grid.getMaxLevel():level),
end(r.zones[GeodesicSearchBorderIterator::level]+
GeodesicGrid::nrOfZones(GeodesicSearchBorderIterator::level))
{reset();}
void reset(void) {index = r.border[level];}
int next(void) // returns -1 when finished
{if (index < end) {return *index++;} return -1;}
private:
const GeodesicSearchResult &r;
const int level;
const int *const end;
const int *index;
};
class GeodesicSearchInsideIterator {
public:
GeodesicSearchInsideIterator(const GeodesicSearchResult &r,int level)
: r(r),max_level((level<0)?0:(level>r.grid.getMaxLevel())
?r.grid.getMaxLevel():level)
{reset();}
void reset(void);
int next(void); // returns -1 when finished
private:
const GeodesicSearchResult &r;
const int max_level;
int level;
int max_count;
int *index_p;
int *end_p;
int index;
int count;
};
#include <stdlib.h>
#include <cassert>
#include <iostream>
using namespace std;
static const double icosahedron_G = 0.5*(1.0+sqrt(5.0));
static const double icosahedron_b = 1.0/sqrt(1.0+icosahedron_G*icosahedron_G);
static const double icosahedron_a = icosahedron_b*icosahedron_G;
static const Vector icosahedron_corners[12] = {
{ icosahedron_a, -icosahedron_b, 0.0},
{ icosahedron_a, icosahedron_b, 0.0},
{-icosahedron_a, icosahedron_b, 0.0},
{-icosahedron_a, -icosahedron_b, 0.0},
{ 0.0, icosahedron_a, -icosahedron_b},
{ 0.0, icosahedron_a, icosahedron_b},
{ 0.0, -icosahedron_a, icosahedron_b},
{ 0.0, -icosahedron_a, -icosahedron_b},
{-icosahedron_b, 0.0, icosahedron_a},
{ icosahedron_b, 0.0, icosahedron_a},
{ icosahedron_b, 0.0, -icosahedron_a},
{-icosahedron_b, 0.0, -icosahedron_a}
};
struct TopLevelTriangle {
int corners[3]; // index der Ecken
};
static const TopLevelTriangle icosahedron_triangles[20] = {
{ 1, 0,10}, // 1
{ 0, 1, 9}, // 0
{ 0, 9, 6}, // 12
{ 9, 8, 6}, // 9
{ 0, 7,10}, // 16
{ 6, 7, 0}, // 6
{ 7, 6, 3}, // 7
{ 6, 8, 3}, // 14
{11,10, 7}, // 11
{ 7, 3,11}, // 18
{ 3, 2,11}, // 3
{ 2, 3, 8}, // 2
{10,11, 4}, // 10
{ 2, 4,11}, // 19
{ 5, 4, 2}, // 5
{ 2, 8, 5}, // 15
{ 4, 1,10}, // 17
{ 4, 5, 1}, // 4
{ 5, 9, 1}, // 13
{ 8, 9, 5} // 8
};
GeodesicGrid::GeodesicGrid(const int lev)
:max_level(lev<0?0:lev) {
if (max_level > 0) {
triangles = new Triangle*[max_level];
int nr_of_triangles = 20;
for (int i=0;i<max_level;i++) {
triangles[i] = new Triangle[nr_of_triangles];
nr_of_triangles *= 4;
}
for (int i=0;i<20;i++) {
const int *const corners = icosahedron_triangles[i].corners;
initTriangle(0,i,
icosahedron_corners[corners[0]],
icosahedron_corners[corners[1]],
icosahedron_corners[corners[2]]);
}
} else {
triangles = 0;
}
}
GeodesicGrid::~GeodesicGrid(void) {
if (max_level > 0) {
for (int i=max_level-1;i>=0;i--) delete[] triangles[i];
delete triangles;
}
}
void GeodesicGrid::getTriangleCorners(int lev,int index,
Vector &h0,
Vector &h1,
Vector &h2) const {
if (lev <= 0) {
const int *const corners = icosahedron_triangles[index].corners;
h0 = icosahedron_corners[corners[0]];
h1 = icosahedron_corners[corners[1]];
h2 = icosahedron_corners[corners[2]];
} else {
lev--;
const int i = index>>2;
Triangle &t(triangles[lev][i]);
switch (index&3) {
case 0: {
Vector c0,c1,c2;
getTriangleCorners(lev,i,c0,c1,c2);
h0 = c0;
h1 = t.e2;
h2 = t.e1;
} break;
case 1: {
Vector c0,c1,c2;
getTriangleCorners(lev,i,c0,c1,c2);
h0 = t.e2;
h1 = c1;
h2 = t.e0;
} break;
case 2: {
Vector c0,c1,c2;
getTriangleCorners(lev,i,c0,c1,c2);
h0 = t.e1;
h1 = t.e0;
h2 = c2;
} break;
case 3:
h0 = t.e0;
h1 = t.e1;
h2 = t.e2;
break;
}
}
}
int GeodesicGrid::getPartnerTriangle(int lev, int index) const
{
if (lev==0)
{
assert(index<20);
return (index&1) ? index+1 : index-1;
}
switch(index&7)
{
case 2:
case 6:
return index+1;
case 3:
case 7:
return index-1;
case 0:
return (lev==1) ? index+5 : (getPartnerTriangle(lev-1, index>>2)<<2)+1;
case 1:
return (lev==1) ? index+3 : (getPartnerTriangle(lev-1, index>>2)<<2)+0;
case 4:
return (lev==1) ? index-3 : (getPartnerTriangle(lev-1, index>>2)<<2)+1;
case 5:
return (lev==1) ? index-5 : (getPartnerTriangle(lev-1, index>>2)<<2)+0;
default:
assert(0);
}
}
void GeodesicGrid::initTriangle(int lev,int index,
const Vector &c0,
const Vector &c1,
const Vector &c2) {
Triangle &t(triangles[lev][index]);
t.e0 = c1+c2;
t.e0.normalize();
t.e1 = c2+c0;
t.e1.normalize();
t.e2 = c0+c1;
t.e2.normalize();
lev++;
if (lev < max_level) {
index *= 4;
initTriangle(lev,index+0,c0,t.e2,t.e1);
initTriangle(lev,index+1,t.e2,c1,t.e0);
initTriangle(lev,index+2,t.e1,t.e0,c2);
initTriangle(lev,index+3,t.e0,t.e1,t.e2);
}
}
void GeodesicGrid::visitTriangles(int max_visit_level,
VisitFunc *func,
void *context) const {
if (func && max_visit_level >= 0) {
if (max_visit_level > max_level) max_visit_level = max_level;
for (int i=0;i<20;i++) {
const int *const corners = icosahedron_triangles[i].corners;
visitTriangles(0,i,
icosahedron_corners[corners[0]],
icosahedron_corners[corners[1]],
icosahedron_corners[corners[2]],
max_visit_level,func,context);
}
}
}
void GeodesicGrid::visitTriangles(int lev,int index,
const Vector &c0,
const Vector &c1,
const Vector &c2,
int max_visit_level,
VisitFunc *func,
void *context) const {
(*func)(lev,index,c0,c1,c2,context);
Triangle &t(triangles[lev][index]);
lev++;
if (lev <= max_visit_level) {
index *= 4;
visitTriangles(lev,index+0,c0,t.e2,t.e1,max_visit_level,func,context);
visitTriangles(lev,index+1,t.e2,c1,t.e0,max_visit_level,func,context);
visitTriangles(lev,index+2,t.e1,t.e0,c2,max_visit_level,func,context);
visitTriangles(lev,index+3,t.e0,t.e1,t.e2,max_visit_level,func,context);
}
}
int GeodesicGrid::searchZone(const Vector &v,int search_level) const {
for (int i=0;i<20;i++) {
const int *const corners = icosahedron_triangles[i].corners;
const Vector &c0(icosahedron_corners[corners[0]]);
const Vector &c1(icosahedron_corners[corners[1]]);
const Vector &c2(icosahedron_corners[corners[2]]);
if (((c0^c1)*v >= 0.0) && ((c1^c2)*v >= 0.0) && ((c2^c0)*v >= 0.0)) {
// v lies inside this icosahedron triangle
for (int lev=0;lev<search_level;lev++) {
Triangle &t(triangles[lev][i]);
i <<= 2;
if ((t.e1^t.e2)*v <= 0.0) {
// i += 0;
} else if ((t.e2^t.e0)*v <= 0.0) {
i += 1;
} else if ((t.e0^t.e1)*v <= 0.0) {
i += 2;
} else {
i += 3;
}
}
return i;
}
}
cerr << "software error: not found" << endl;
exit(-1);
// shut up the compiler
return -1;
}
void GeodesicGrid::searchZones(const HalfSpace *half_spaces,
const int nr_of_half_spaces,
int **inside_list,int **border_list,
int max_search_level) const {
if (max_search_level < 0) max_search_level = 0;
else if (max_search_level > max_level) max_search_level = max_level;
int halfs_used[nr_of_half_spaces];
for (int h=0;h<nr_of_half_spaces;h++) {halfs_used[h] = h;}
bool corner_inside[12][nr_of_half_spaces];
for (int h=0;h<nr_of_half_spaces;h++) {
const HalfSpace &half_space(half_spaces[h]);
for (int i=0;i<12;i++) {
corner_inside[i][h] = half_space.inside(icosahedron_corners[i]);
}
}
for (int i=0;i<20;i++) {
searchZones(0,i,
half_spaces,nr_of_half_spaces,halfs_used,nr_of_half_spaces,
corner_inside[icosahedron_triangles[i].corners[0]],
corner_inside[icosahedron_triangles[i].corners[1]],
corner_inside[icosahedron_triangles[i].corners[2]],
inside_list,border_list,max_search_level);
}
}
void GeodesicGrid::searchZones(int lev,int index,
const HalfSpace *half_spaces,
const int nr_of_half_spaces,
const int *index_of_used_half_spaces,
const int half_spaces_used,
const bool *corner0_inside,
const bool *corner1_inside,
const bool *corner2_inside,
int **inside_list,int **border_list,
const int max_search_level) const {
int halfs_used[half_spaces_used];
int halfs_used_count = 0;
for (int h=0;h<half_spaces_used;h++) {
const int i = index_of_used_half_spaces[h];
if (!corner0_inside[i] && !corner1_inside[i] && !corner2_inside[i]) {
// totally outside this HalfSpace
return;
} else if (corner0_inside[i] && corner1_inside[i] && corner2_inside[i]) {
// totally inside this HalfSpace
} else {
// on the border of this HalfSpace
halfs_used[halfs_used_count++] = i;
}
}
if (halfs_used_count == 0) {
// this triangle(lev,index) lies inside all halfspaces
**inside_list = index;
(*inside_list)++;
} else {
(*border_list)--;
**border_list = index;
if (lev < max_search_level) {
Triangle &t(triangles[lev][index]);
lev++;
index <<= 2;
inside_list++;
border_list++;
bool edge0_inside[nr_of_half_spaces];
bool edge1_inside[nr_of_half_spaces];
bool edge2_inside[nr_of_half_spaces];
for (int h=0;h<halfs_used_count;h++) {
const int i = halfs_used[h];
const HalfSpace &half_space(half_spaces[i]);
edge0_inside[i] = half_space.inside(t.e0);
edge1_inside[i] = half_space.inside(t.e1);
edge2_inside[i] = half_space.inside(t.e2);
}
searchZones(lev,index+0,
half_spaces,nr_of_half_spaces,halfs_used,halfs_used_count,
corner0_inside,edge2_inside,edge1_inside,
inside_list,border_list,max_search_level);
searchZones(lev,index+1,
half_spaces,nr_of_half_spaces,halfs_used,halfs_used_count,
edge2_inside,corner1_inside,edge0_inside,
inside_list,border_list,max_search_level);
searchZones(lev,index+2,
half_spaces,nr_of_half_spaces,halfs_used,halfs_used_count,
edge1_inside,edge0_inside,corner2_inside,
inside_list,border_list,max_search_level);
searchZones(lev,index+3,
half_spaces,nr_of_half_spaces,halfs_used,halfs_used_count,
edge0_inside,edge1_inside,edge2_inside,
inside_list,border_list,max_search_level);
}
}
}
GeodesicSearchResult::GeodesicSearchResult(const GeodesicGrid &grid)
:grid(grid),
zones(new int*[grid.getMaxLevel()+1]),
inside(new int*[grid.getMaxLevel()+1]),
border(new int*[grid.getMaxLevel()+1]) {
for (int i=0;i<=grid.getMaxLevel();i++) {
zones[i] = new int[GeodesicGrid::nrOfZones(i)];
}
}
GeodesicSearchResult::~GeodesicSearchResult(void) {
for (int i=grid.getMaxLevel();i>=0;i--) {
delete zones[i];
}
delete border;
delete inside;
delete zones;
}
void GeodesicSearchResult::search(const HalfSpace *half_spaces,
const int nr_of_half_spaces,
int max_search_level) {
for (int i=grid.getMaxLevel();i>=0;i--) {
inside[i] = zones[i];
border[i] = zones[i]+GeodesicGrid::nrOfZones(i);
}
grid.searchZones(half_spaces,nr_of_half_spaces,inside,border,max_search_level);
}
void GeodesicSearchResult::search(const Vector &e0,const Vector &e1,
const Vector &e2,const Vector &e3,
int max_search_level) {
HalfSpace half_spaces[4];
half_spaces[0].d = e0*((e1-e0)^(e2-e0));
if (half_spaces[0].d > 0) {
half_spaces[0].n = e0^e1;
half_spaces[1].n = e1^e2;
half_spaces[2].n = e2^e3;
half_spaces[3].n = e3^e0;
half_spaces[0].d = 0.0;
half_spaces[1].d = 0.0;
half_spaces[2].d = 0.0;
half_spaces[3].d = 0.0;
search(half_spaces,4,max_search_level);
} else {
half_spaces[0].n = (e1-e0)^(e2-e0);
search(half_spaces,1,max_search_level);
}
}
void GeodesicSearchInsideIterator::reset(void) {
level = 0;
max_count = 1<<(max_level<<1); // 4^max_level
index_p = r.zones[0];
end_p = r.inside[0];
index = (*index_p) * max_count;
count = (index_p < end_p) ? 0 : max_count;
}
int GeodesicSearchInsideIterator::next(void) {
if (count < max_count) return index+(count++);
index_p++;
if (index_p < end_p) {
index = (*index_p) * max_count;
count = 1;
return index;
}
while (level < max_level) {
level++;
max_count >>= 2;
index_p = r.zones[level];
end_p = r.inside[level];
if (index_p < end_p) {
index = (*index_p) * max_count;
count = 1;
return index;
}
}
return -1;
}
//------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <map>
#include <list>
#include <string>
#include <iostream>
using namespace std;
static int restrict_output_level_min = 0;
static int restrict_output_level_max = MAX_LEVEL;
struct FaintStar { // 6 byte
int x0:18;
int x1:18;
unsigned int b_v:7;
unsigned int mag:5;
bool operator<(const FaintStar &s) const {return (mag < s.mag);}
} __attribute__ ((__packed__)) ;
struct TycStar { // 10 byte
int x0:20;
int x1:20;
int dx0:14;
int dx1:14;
unsigned int b_v:7;
unsigned int mag:5;
bool operator<(const TycStar &s) const {return (mag < s.mag);}
} __attribute__ ((__packed__));
struct HipStarCompressed {
int hip:24; // 17 bits needed
unsigned char comp_ids_int; // 5 bits needed
int x0; // 32 bits needed
int x1; // 32 bits needed
unsigned char b_v; // 8 bits needed
unsigned char mag; // 5 bits needed
unsigned short sp_int; // 14 bits needed
int dx0,dx1,plx;
} __attribute__ ((__packed__));
struct HipStar : public HipStarCompressed {
string component_ids,sp;
void setPlxSp(double plx,const string &sp) {
HipStar::plx = (int)floor(0.5+100.0*plx);
if (HipStar::plx < 0) HipStar::plx = 0;
HipStar::sp = sp;
}
bool operator<(const HipStar &s) const {return (mag < s.mag);}
};
struct ZoneData {
virtual ~ZoneData(void) {}
virtual int getArraySize(void) const = 0;
virtual HipStar *add(int level,
int tyc1,int tyc2,int tyc3,
int hip,const char *component_ids,
double x0,double x1,double dx0,double dx1,
double mag,double b_v,
double plx,const char *sp,
bool &does_not_fit) = 0;
void writeInfoToOutput(FILE *f) const;
virtual void writeStarsToOutput(FILE *f) = 0;
Vector center;
Vector axis0;
Vector axis1;
};
struct HipZoneData : public ZoneData {
list<HipStar> stars;
int getArraySize(void) const {return stars.size();}
HipStar *add(int level,
int tyc1,int tyc2,int tyc3,
int hip,const char *component_ids,
double x0,double x1,double dx0,double dx1,
double mag,double b_v,double plx,const char *sp,
bool &does_not_fit);
void writeStarsToOutput(FILE *f);
};
static list<HipStar*> hip_index[NR_OF_HIP+1];
static void SqueezeHip(void) {
// build lookup maps
map<string,int> component_ids_map,sp_map;
for (int i=0;i<=NR_OF_HIP;i++) {
for (list<HipStar*>::const_iterator it(hip_index[i].begin());
it!=hip_index[i].end();it++) {
HipStar *h = *it;
component_ids_map[h->component_ids]++;
sp_map[h->sp]++;
}
}
int component_ids_size = 0;
for (map<string,int>::iterator it(component_ids_map.begin());
it!=component_ids_map.end();it++,component_ids_size++) {
it->second = component_ids_size;
}
if (component_ids_size >= 32) {
cerr << "SqueezeHip: too many component_ids: "
<< component_ids_size << endl;
}
int sp_size = 0;
for (map<string,int>::iterator it(sp_map.begin());
it!=sp_map.end();it++,sp_size++) {
it->second = sp_size;
}
if (sp_size >= 16384) {
cerr << "SqueezeHip: too many sp: " << sp_size << endl;
}
// translate the strings for the hip stars into integers:
for (int i=0;i<=NR_OF_HIP;i++) {
for (list<HipStar*>::const_iterator it(hip_index[i].begin());
it!=hip_index[i].end();it++) {
HipStar *h = *it;
h->comp_ids_int = component_ids_map[h->component_ids];
h->sp_int = sp_map[h->sp];
}
}
// write output files for component_ids and sp
FILE *f = fopen(component_ids_filename,"wb");
if (f==0) {
cout << "fopen(" << component_ids_filename << ") failed" << endl;
exit(-1);
}
for (map<string,int>::const_iterator it(component_ids_map.begin());
it!=component_ids_map.end();it++,component_ids_size++) {
fprintf(f,"%s\n",it->first.c_str());
}
fclose(f);
f = fopen(sp_filename,"wb");
if (f==0) {
cout << "fopen(" << sp_filename << ") failed" << endl;
exit(-1);
}
for (map<string,int>::const_iterator it(sp_map.begin());
it!=sp_map.end();it++,sp_size++) {
fprintf(f,"%s\n",it->first.c_str());
}
fclose(f);
}
struct TycZoneData : public ZoneData {
list<TycStar> stars;
int getArraySize(void) const {return stars.size();}
HipStar *add(int level,
int tyc1,int tyc2,int tyc3,
int hip,const char *component_ids,
double x0,double x1,double dx0,double dx1,
double mag,double b_v,double plx,const char *sp,
bool &does_not_fit);
void writeStarsToOutput(FILE *f);
};
struct FaintZoneData : public ZoneData {
list<FaintStar> stars;
int getArraySize(void) const {return stars.size();}
HipStar *add(int level,
int tyc1,int tyc2,int tyc3,
int hip,const char *component_ids,
double x0,double x1,double dx0,double dx1,
double mag,double b_v,double plx,const char *sp,
bool &does_not_fit);
void writeStarsToOutput(FILE *f);
};
class Accumulator {
public: