GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
point.h
1/*
2 * point.h
3 *
4 * Created on: Nov 15, 2010
5 * Author: bmetcalf
6 *
7 * Defines Point and Branch type.
8 *
9 * The Branch type needs to be defined here so that point.leaf can be defined.
10 */
11
12
13#ifndef pointtypes_declare
14#define pointtypes_declare
15#include <complex>
16#include <standard.h>
17#include "Kist.h"
18
19#ifndef PI
20#define PI 3.141593
21#endif
22
23#ifndef error_message
24#define error_message
25#define ERROR_MESSAGE() std::cout << "ERROR: file: " << __FILE__ << " line: " << __LINE__ << std::endl;
26#endif
27
28#ifndef boo_declare
29#define boo_declare
30typedef enum {NO, YES, MAYBE} Boo;
31#endif
32
33
35enum class CritType {ND=0,radial=2,tangential=1,pseudo=3,};
36
37//std::string to_string(const CritType &p);
38std::ostream &operator<<(std::ostream &os, CritType const &p);
39
41template <typename T>
42int sign(T val){return (T(0) < val) - (val < T(0));}
43
48struct Point_2d{
49 Point_2d(){
50 x[0]=x[1]=0.0;
51 }
52 Point_2d(double x1,double x2){
53 x[0]=x1;
54 x[1]=x2;
55 }
56
57 ~Point_2d(){};
58
59 Point_2d(const Point_2d &p){
60 x[0]=p.x[0];
61 x[1]=p.x[1];
62 }
63 Point_2d & operator=(const Point_2d &p){
64 if(this == &p) return *this;
65 x[0]=p.x[0];
66 x[1]=p.x[1];
67 return *this;
68 }
69 Point_2d(const PosType *p){
70 x[0]=p[0];
71 x[1]=p[1];
72 }
73
74
75 bool operator==(const Point_2d &p) const{
76 return (x[0] == p.x[0])*(x[1] == p.x[1]);
77 }
78 bool operator!=(const Point_2d &p) const{
79 return (x[0] != p.x[0]) || (x[1] != p.x[1]);
80 }
81
82 Point_2d operator+(const Point_2d &p) const{
83 Point_2d tmp;
84 tmp.x[0] = x[0] + p.x[0];
85 tmp.x[1] = x[1] + p.x[1];
86 return tmp;
87 }
88 Point_2d operator-(const Point_2d &p) const{
89 Point_2d tmp;
90 tmp.x[0] = x[0] - p.x[0];
91 tmp.x[1] = x[1] - p.x[1];
92 return tmp;
93 }
94 Point_2d & operator+=(const Point_2d &p){
95 x[0]+=p.x[0];
96 x[1]+=p.x[1];
97 return *this;
98 }
99 Point_2d & operator-=(const Point_2d &p){
100 x[0]-=p.x[0];
101 x[1]-=p.x[1];
102 return *this;
103 }
104 Point_2d & operator/=(PosType value){
105 x[0]/=value;
106 x[1]/=value;
107 return *this;
108 }
109 Point_2d operator/(PosType value) const{
110 Point_2d tmp;
111 tmp[0] = x[0]/value;
112 tmp[1] = x[1]/value;
113 return tmp;
114 }
115 Point_2d & operator*=(PosType value){
116 x[0]*=value;
117 x[1]*=value;
118 return *this;
119 }
120 Point_2d operator*(PosType value) const{
121 return Point_2d(x[0]*value,x[1]*value);
122 }
123
125 PosType operator*(const Point_2d &p) const{
126 return x[0]*p.x[0] + x[1]*p.x[1];
127 }
129 PosType operator^(const Point_2d &p) const{
130 return x[0]*p.x[1] - x[1]*p.x[0];
131 }
132
134 PosType length() const{
135 return sqrt(x[0]*x[0] + x[1]*x[1]);
136 }
137
139 PosType length_sqr() const{
140 return x[0]*x[0] + x[1]*x[1];
141 }
142
143 // rotates the point
144 void rotate(PosType theta){
145 PosType c = cos(theta),s = sin(theta);
146 PosType tmp = x[0];
147 x[0] = c*tmp - s*x[1];
148 x[1] = c*x[1] + s*tmp;
149 }
150
152 Point_2d rotated(PosType theta) const{
153 Point_2d p;
154 PosType c = cos(theta),s = sin(theta);
155 p[0] = c*x[0] - s*x[1];
156 p[1] = c*x[1] + s*x[0];
157
158 return p;
159 }
160
162 void unitize(){
163 PosType s = length();
164 x[0] /= s;
165 x[1] /= s;
166 }
167
168 // returns a pointer to the position
169 PosType* data(){return x;}
170
171 // array of size 2 containing the position
172 PosType x[2];
173
174 PosType & operator[](size_t i) {return x[i];}
175 const PosType & operator[](size_t i) const {return x[i];}
176};
177
178
179template <typename T>
180struct Matrix2x2{
181
182 Matrix2x2(){
183 }
184
185 Matrix2x2(const Matrix2x2<T> &F){
186 a[0] = F.a[0];
187 a[1] = F.a[1];
188 a[2] = F.a[2];
189 a[3] = F.a[3];
190 }
191
192 template <typename B>
193 Matrix2x2<T> operator=(const Matrix2x2<B> &F){
194 a[0] = F.a[0];
195 a[1] = F.a[1];
196 a[2] = F.a[2];
197 a[3] = F.a[3];
198
199 return *this;
200 }
201
202 bool operator==(const Matrix2x2<T> &F){
203
204 return (a[0] == F.a[0]) * (a[1] == F.a[1]) * (a[2] == F.a[2]) * (a[3] == F.a[3]);
205 }
206
207
208 Matrix2x2<T> operator*=(T f){
209 a[0] *= f;
210 a[1] *= f;
211 a[2] *= f;
212 a[3] *= f;
213
214 return *this;
215 }
216
217 Matrix2x2<T> operator/=(T f){
218 a[0] /= f;
219 a[1] /= f;
220 a[2] /= f;
221 a[3] /= f;
222
223 return *this;
224 }
225
226 Matrix2x2<T> operator*(T f) const{
227 Matrix2x2<T> m = *this;
228 m *= f;
229 return m;
230 }
231
232 Point_2d operator*(const Point_2d &v) const{
233 Point_2d v2;
234 v2[0] = a[0]*v[0] + a[1]*v[1];
235 v2[1] = a[2]*v[0] + a[3]*v[1];
236 return v2;
237 }
238
239
240 Matrix2x2<T> operator/(T f) const{
241 Matrix2x2<T> m = *this;
242 m /= f;
243
244 return m;
245 }
246
247 template <typename B>
248 Matrix2x2<T> operator*(const Matrix2x2<B> &F) const{
249 Matrix2x2<T> m;
250
251 m.a[0] = a[0] * F.a[0] + a[1] * F.a[2];
252 m.a[1] = a[0] * F.a[1] + a[1] * F.a[3];
253 m.a[2] = a[2] * F.a[0] + a[3] * F.a[2];
254 m.a[3] = a[2] * F.a[1] + a[3] * F.a[3];
255
256 return m;
257 }
258
259 template <typename B>
260 Matrix2x2<T> operator+=(const Matrix2x2<B> &F){
261 a[0] += F.a[0];
262 a[1] += F.a[1];
263 a[2] += F.a[2];
264 a[3] += F.a[3];
265
266 return *this;
267 }
268
269 template <typename B>
270 Matrix2x2<T> operator+(const Matrix2x2<B> &F) const{
271 Matrix2x2<T> m = *this;
272 m += F;
273
274 return m;
275 }
276 template <typename B>
277 Matrix2x2<T> operator-=(const Matrix2x2<B> &F){
278 a[0] -= F.a[0];
279 a[1] -= F.a[1];
280 a[2] -= F.a[2];
281 a[3] -= F.a[3];
282
283 return *this;
284 }
285
286 template <typename B>
287 Matrix2x2<T> operator-(const Matrix2x2<B> &F)const{
288 Matrix2x2<T> m = *this;
289 m -= F;
290
291 return m;
292 }
293
295 T & operator()(int i,int j){
296 return a[ i + 2*j ];
297 }
298
299 T operator()(int i,int j) const{
300 return a[ i + 2*j ];
301 }
302
303
304 T & operator[](int i){
305 return a[i];
306 }
307
308 T det() const{
309 return a[0]*a[3] - a[1]*a[2];
310 }
311
312 Matrix2x2<T> inverse() const{
313 Matrix2x2<T> m;
314
315 m.a[0] = a[3];
316 m.a[3] = a[0];
317 m.a[1] = -a[1];
318 m.a[2] = -a[2];
319
320 m /= det();
321 return m;
322 }
323
324 void invert(){
325
326 std::swap(a[0],a[3]);
327 a[1] *= -1;
328 a[2] *= -1;
329
330 *this /= det();
331 }
332
333 T a[4];
334
335 // make the matrix the identity
336 void setToI(){
337 a[0] = a[3] = 1;
338 a[1] = a[2] = 0;
339 }
340
341 static Matrix2x2<T> I(){
342 Matrix2x2<T> m;
343 m.a[0] = m.a[3] = 1;
344 m.a[1] = m.a[2] = 0;
345 return m;
346 }
347
348 static Matrix2x2<T> sig1(){
349 Matrix2x2<T> m;
350 m.a[0] = -1;
351 m.a[3] = 1;
352 m.a[1] = m.a[2] = 0;
353 return m;
354 }
355 static Matrix2x2<T> sig2(){
356 Matrix2x2<T> m;
357 m.a[0] = m.a[3] = 0;
358 m.a[1] = m.a[2] = -1;
359 return m;
360 }
361
362 static Matrix2x2<T> sig3(){
363 Matrix2x2<T> m;
364 m.a[0] = m.a[3] = 0;
365 m.a[1] = 1;
366 m.a[2] = -1;
367 return m;
368 }
369
370 T kappa() const {return 1-(a[0] + a[3])/2; }
371 // defined according to GLAMER II convention
372 T gamma1() const{ return (a[0] - a[3])/2;}
373 T gamma2() const{ return (a[1] + a[2])/2;}
374 T gamma3() const{ return (a[2] - a[1])/2;}
375
376 void set(T kappa,T gamma[3]){
377 a[0] = 1 - kappa + gamma[0];
378 a[3] = 1 - kappa - gamma[0];
379
380 a[1] = gamma[1] - gamma[3];
381 a[2] = gamma[1] + gamma[3];
382 }
383
384 void gamma(T *g) const{
385 g[0] = gamma1();
386 g[1] = gamma2();
387 g[2] = gamma3();
388 }
389
390
391 void print() const {
392 std::cout << std::endl;
393 std::cout << a[0] << " " << a[1] << std::endl;
394 std::cout << a[2] << " " << a[3] << std::endl;
395 }
396
397 // this assumes that the eigenvalues are real
398 void eigenv(T lambda[]){
399 T m = (a[0] + a[3])/2;
400 T determinent = det();
401
402 lambda[0] = m + sqrt( abs(m*m - determinent));
403 lambda[1] = m - sqrt( abs(m*m - determinent));
404 }
405};
406
407
408//struct branchstruct;
409struct Branch;
410
411
414struct Point: public Point_2d{
415
416 Point();
417 Point(const Point_2d &p);
418 Point(PosType x,PosType y);
419 Point *next=nullptr; // pointer to next point in linked list
420 Point *prev=nullptr;
421 Point *image=nullptr; // pointer to point on image or source plane
422 unsigned long id=0;
423 unsigned long head=0; // marks beginning of allocated array of points for easy deallocation
424 Boo in_image; // marks if point is in image
425
426 PosType *ptr_y(){return image->x;}
427
430 Point_2d::operator=(p);
431
432 return *this;
433 }
434
435 double dt; // time delay : double implies permanent precision independently from DOUBLE_PRECISION
436
437 Matrix2x2<KappaType> A;
438
439 KappaType invmag() const{
440 return A.det();
441 }
442 KappaType gamma1() const{
443 return A.gamma1();
444 }
445 KappaType gamma2() const{
446 return A.gamma2();
447 }
448 KappaType gamma3() const{
449 return A.gamma3();
450 }
451 KappaType kappa() const{
452 return A.kappa();
453 }
454
456 double flux(){return surface_brightness * gridsize * gridsize;}
457
458 double gridsize; // the size of the most refined grid the point is in
459 float surface_brightness; // the surface brightness at this points
460
461
462 Branch *leaf;
463 bool flag;
464
465 void Print();
466
467 static bool orderX(Point *p1,Point *p2){
468 return (p1->x[0] < p2->x[0]);
469 }
470 static bool orderXrev(Point *p1,Point *p2){
471 return (p1->x[0] > p2->x[0]);
472 }
473 static bool orderY(Point *p1,Point *p2){
474 return (p1->x[1] < p2->x[1]);
475 }
476 static bool orderYrev(Point *p1,Point *p2){
477 return (p1->x[1] > p2->x[1]);
478 }
479
481 bool inverted(){
482 KappaType eigens[2];
483 A.eigenv(eigens);
484
485 return (eigens[0] < 0)*(eigens[1] < 0);
486 }
487};
488
489std::ostream &operator<<(std::ostream &os, Point const &p);
490
495struct LinkedPoint : public Point
496{
497 LinkedPoint(){
498 image = &im;
499 im.image = this;
500 }
501
502 Point_2d& y(){return im;}
503private:
504 Point im;
505};
506
507
510struct RAY{
511 RAY(){
512
513 dt = 0.0;
514 A = Matrix2x2<KappaType>::I();
515 z = -1;
516
517 };
518
519 RAY(const Point &p,double zs = -1){
520 x[0] = p.x[0];
521 x[1] = p.x[1];
522 y[0] = p.image->x[0];
523 y[1] = p.image->x[1];
524
525 dt = p.dt;
526
527 A = p.A;
528 z = zs;
529 };
530 RAY(const LinkedPoint &p,double zs = -1){
531 x[0] = p.x[0];
532 x[1] = p.x[1];
533 y[0] = p.image->x[0];
534 y[1] = p.image->x[1];
535
536 dt = p.dt;
537
538 A = p.A;
539 z = zs;
540 };
541
542 RAY(const RAY &p){
543 x = p.x;
544 y = p.y;
545
546 dt = p.dt;
547
548 A = p.A;
549 z = p.z;
550 };
551
552 RAY & operator=(const Point &p){
553 assert(p.image != nullptr);
554
555 x[0] = p.x[0];
556 x[1] = p.x[1];
557 y[0] = p.image->x[0];
558 y[1] = p.image->x[1];
559
560 dt = p.dt;
561
562
563 A = p.A;
564 z = -1;
565
566 return *this;
567 };
568
569 RAY & operator=(const RAY &p){
570 x = p.x;
571 y = p.y;
572
573 dt = p.dt;
574
575 A = p.A;
576 z = p.z;
577
578 return *this;
579 };
580
581 ~RAY(){};
582
587 PosType *ptr_y(){return y.x;}
588
589 Matrix2x2<KappaType> A;
590
592 KappaType dt;
593 KappaType z;
594
596 KappaType invmag() const{
597 return A.det();
598 }
599 KappaType gamma1() const{
600 return A.gamma1();
601 }
602 KappaType gamma2() const{
603 return A.gamma2();
604 }
605 KappaType gamma3() const{
606 return A.gamma3();
607 }
608 KappaType kappa() const{
609 return A.kappa();
610 }
611
613 Point_2d alpha() const {return x - y;}
614};
615
616std::ostream &operator<<(std::ostream &os, Point_2d const &p);
617void write_csv(std::string filename,const std::vector<Point_2d> &v);
618void write_csv(std::string filename,const std::vector<RAY> &v);
619
620//inline std::string to_string(RAY &r) {
621// std::string s = "[" + std::to_string(r.x[0]) + "," + r.x[1] + ",[" + r.y[0] + "," + r.y[1]
622// + "]," + r.z + "," + r.kappa() + ",[" + r.gamma1() + "," + r.gamma2() + "," + r.gamma3() + "]," << r.dt;
623// return s;
624//}
625std::ostream &operator<<(std::ostream &os, RAY const &r);
626
628struct Branch{
629 Branch(Point *my_points,unsigned long my_npoints
630 ,double my_boundary_p1[2],double my_boundary_p2[2]
631 ,double my_center[2],int my_level);
632 ~Branch();
633
634 struct Point *points;
635
636 unsigned long npoints;
637 double center[2];
638 int level;
639 unsigned long number;
640 double boundary_p1[2];
641 double boundary_p2[2];
642 Branch *child1;
643 Branch *child2;
644 Branch *brother;
645 Branch *prev;
648
649 void print();
650
651 PosType area(){return (boundary_p2[0]-boundary_p1[0])*(boundary_p2[1]-boundary_p1[1]);}
652
653 std::list<Branch *> neighbors;
654private:
655 static unsigned long countID;
656
657 // make a Branch uncopyable
658 Branch(const Branch &p);
659 Branch &operator=(Branch &p);
660 Branch &operator=(const Branch &p);
661
662} ;
663
664//typedef struct branchstruct Branch;
665
669template<typename T>
671
672 MemmoryBank():count(0){};
673 MemmoryBank(MemmoryBank &&membank){
674 *this = std::move(membank);
675 }
676 void operator=(MemmoryBank &&membank){
677 bank = std::move(membank.bank);
678 }
679
680
681 T * operator()(size_t N){
682 if(N <= 0) return nullptr;
683
684 bank.emplace_back(new T[N]);
685 count += N;
686 return bank.back().get();
687 }
688
689 // clear all data
690 void clear(){
691 bank.clear();
692 count = 0;
693 }
694
695 // clear specific array, does not decrement count
696 bool clear(T *ptr){
697 for(auto &p : bank){
698 if(p.get() == ptr){
699 p.swap(bank.back());
700 bank.pop_back();
701 return true;
702 }
703 }
704 return false;
705 }
706
707 size_t number_of_blocks(){return bank.size();}
708
709private:
710
711 MemmoryBank(const MemmoryBank<T> &b);
712 MemmoryBank<T> & operator=(const MemmoryBank<T> &b);
713
714 size_t count;
715 std::vector<std::unique_ptr<T[]> > bank;
716};
717
718// A class that onlt counts iteslf for testing
719//struct DummyClass{
720// DummyClass(){
721// N = count;
722// ++count;
723// };
724// ~DummyClass(){
725// std::cout << "delete " << N << std::endl;
726// }
727// int N;
728// static int count;
729//};
730
731//int DummyClass::count = 0;
732//{ test lines for MemmoryBank
733// MemmoryBank<DummyClass> pointbang;
734//
735// DummyClass *p = pointbang(10);
736// p[7].N = 2;
737// for(int i=0 ; i<10 ; ++i){
738// cout << p[i].N << endl;
739// }
740//
741// DummyClass *p2 = pointbang(5);
742// for(int i=0 ; i<5 ; ++i){
743// cout << p2[i].N << endl;
744// }
745//
746// pointbang.clear(p2);
747//
748// DummyClass *p3 = pointbang(10);
749//}
750
756 PointList(){
757 top=NULL;
758 Npoints=0;
759 bottom = top;
760 }
761 ~PointList(){}
762
763 struct iterator{
764
765 Point *current;
766
767 iterator():current(NULL){ }
768
769 iterator(Point *p){
770 current = p;
771 }
772
773 Point *operator*(){return current;}
774
775 bool operator++(){
776 assert(current);
777 if(current->prev == NULL) return false;
778 current=current->prev;
779 return true;
780 }
781
783 bool operator++(int){
784 assert(current);
785 if(current->prev == NULL) return false;
786 current=current->prev;
787 return true;
788 }
789
791 bool operator--(){
792 assert(current);
793 if(current->next == NULL) return false;
794 current=current->next;
795 return true;
796 }
797
799 bool operator--(int){
800 assert(current);
801 if(current->next == NULL) return false;
802 current=current->next;
803 return true;
804 }
805
806 void JumpDownList(int jump){
807 int i;
808
809 if(jump > 0) for(i=0;i<jump;++i) --(*this);
810 if(jump < 0) for(i=0;i<abs(jump);++i) ++(*this);
811 }
812
813
814 bool operator==(const iterator my_it){
815 return (current == my_it.current);
816 }
817
818 bool operator!=(const iterator my_it){
819 return (current != my_it.current);
820 }
821
822 };
823
824 unsigned long size() const {return Npoints;}
825
826 inline bool IsTop(PointList::iterator &it) const{
827 return *it == top;
828 };
829 inline bool IsBottom(PointList::iterator &it) const{
830 return *it == bottom;
831 };
832
833 Point *Top() const {return top;}
834 Point *Bottom() const {return bottom;}
835
836 void EmptyList();
837 //void InsertAfterCurrent(iterator &current,double *x,unsigned long id,Point *image);
838 //void InsertBeforeCurrent(iterator &current,double *x,unsigned long id,Point *image);
839 void InsertPointAfterCurrent(iterator &current,Point *);
840 void InsertPointBeforeCurrent(iterator &current,Point *);
841
842 void MoveCurrentToBottom(iterator &current);
843 Point *TakeOutCurrent(iterator &current);
844
845 void InsertListAfterCurrent(iterator &current,PointList *list2);
846 void InsertListBeforeCurrent(iterator &current,PointList *list2);
847 void MergeLists(PointList* list2);
848 void ShiftList(iterator &current);
849
850 //void FillList(double **x,unsigned long N
851 // ,unsigned long idmin);
852 void PrintList();
853
854 void setN(unsigned long N){Npoints = N;}
855 void setTop(Point *p){top = p;}
856 void setBottom(Point *p){bottom = p;}
857
858private:
859 Point *top;
860 Point *bottom;
861 unsigned long Npoints;
862
863 // make a point uncopyable
864 PointList &operator=(const PointList &p);
865};
866
867typedef struct PointList *ListHndl;
868
869
870// ***********************************************************
871// routines for linked list of points
872// ************************************************************
873
874//Point *NewPoint(double *x,unsigned long id);
875
876void SwapPointsInList(ListHndl list,Point *p1,Point *p2);
877Point *sortList(long n, double arr[],ListHndl list,Point *firstpoint);
878
882template <typename T = PosType>
883struct Point_3d{
884 Point_3d(){
885 x[0]=x[1]=x[2]=0.0;
886 }
887 Point_3d(T xx,T yy,T zz){
888 x[0]=xx;
889 x[1]=yy;
890 x[2]=zz;
891 }
892 ~Point_3d(){};
893
894 Point_3d(const Point_3d &p){
895 x[0]=p.x[0];
896 x[1]=p.x[1];
897 x[2]=p.x[2];
898 }
899
900 Point_3d & operator=(const Point_3d &p){
901 if(this == &p) return *this;
902 x[0]=p.x[0];
903 x[1]=p.x[1];
904 x[2]=p.x[2];
905 return *this;
906 }
907 Point_3d operator+(const Point_3d &p) const{
908 Point_3d tmp;
909 tmp.x[0] = x[0] + p.x[0];
910 tmp.x[1] = x[1] + p.x[1];
911 tmp.x[2] = x[2] + p.x[2];
912 return tmp;
913 }
914 Point_3d operator-(const Point_3d &p) const{
915 Point_3d tmp;
916 tmp.x[0] = x[0] - p.x[0];
917 tmp.x[1] = x[1] - p.x[1];
918 tmp.x[2] = x[2] - p.x[2];
919 return tmp;
920 }
921 Point_3d & operator+=(const Point_3d &p){
922 x[0]+=p.x[0];
923 x[1]+=p.x[1];
924 x[2]+=p.x[2];
925 return *this;
926 }
927 Point_3d & operator-=(const Point_3d &p){
928 x[0]-=p.x[0];
929 x[1]-=p.x[1];
930 x[2]-=p.x[2];
931 return *this;
932 }
933 Point_3d & operator/=(T value){
934 x[0]/=value;
935 x[1]/=value;
936 x[2]/=value;
937 return *this;
938 }
939 Point_3d operator/(T value) const{
940 Point_3d tmp;
941 tmp[0] = x[0]/value;
942 tmp[1] = x[1]/value;
943 tmp[2] = x[2]/value;
944
945 return tmp;
946 }
947 Point_3d & operator*=(T value){
948 x[0] *=value;
949 x[1] *=value;
950 x[2] *=value;
951 return *this;
952 }
953 Point_3d operator*(PosType value) const{
954 Point_3d tmp;
955 tmp[0] = x[0]*value;
956 tmp[1] = x[1]*value;
957 tmp[2] = x[2]*value;
958
959 return tmp;
960 }
961
963 T operator*(const Point_3d &p) const {
964 return x[0]*p.x[0] + x[1]*p.x[1] + x[2]*p.x[2];
965 }
966
969 Point_3d<T> v;
970 v[0] = x[1]*p[2] - x[2]*p[1];
971 v[1] = x[2]*p[0] - x[0]*p[2];
972 v[2] = x[0]*p[1] - x[1]*p[0];
973 return v;
974 }
975
977 T length() const{
978 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
979 }
980
981 T length_sqr() const{
982 return x[0]*x[0] + x[1]*x[1] + x[2]*x[2];
983 }
984
986 void rotate(T theta,T phi){
987 T c = cos(theta),s = sin(theta);
988 T tmp = c*x[0] - s*x[1];
989 x[1] = c*x[1] + s*x[0];
990
991 c = cos(phi);
992 s = sin(phi);;
993 x[0] = c*tmp - s*x[2];
994 x[2] = c*x[2] + s*tmp;
995 }
996
998 void unitize(){
999 T s = length();
1000 x[0] /= s;
1001 x[1] /= s;
1002 x[2] /= s;
1003 }
1004
1007 double s = sqrt(x[0]*x[0] + x[1]*x[1]);
1008 return Point_3d<T>(-x[1],x[0],0) / s;
1009 }
1010
1014 Point_3d<T> v(-x[0]*x[2],-x[1]*x[2] ,x[0]*x[0] + x[1]*x[1]);
1015 v.unitize();
1016 return v;
1017 }
1018
1019 T* data(){return x;}
1020
1021 T x[3];
1022 T & operator[](size_t i){return x[i];}
1023 const T & operator[](size_t i) const {return x[i];}
1024};
1025
1026template <typename T>
1027std::ostream &operator<<(std::ostream &os, Point_3d<T> const &p) {
1028 return os << p.x[0] << " " << p.x[1] << " " << p.x[2];
1029}
1030
1031inline double pointx(Point &p){return p.x[0];}
1032inline double pointy(Point &p){return p.x[1];}
1033
1034/*
1035template <typename T>
1036struct LISTiterator{
1037
1038 std::list<T>::iterator current_it;
1039
1040 LISTiterator(){ }
1041
1042 T& operator*(){return *current_it;}
1043
1044 bool operator++(){
1045 if(current_it == )
1046 --current_it;
1047
1048 assert(current);
1049 if(current->prev == NULL) return false;
1050 current=current->prev;
1051 return true;
1052 }
1053
1055 bool operator++(int){
1056 assert(current);
1057 if(current->prev == NULL) return false;
1058 current=current->prev;
1059 return true;
1060 }
1061
1063 bool operator--(){
1064 assert(current);
1065 if(current->next == NULL) return false;
1066 current=current->next;
1067 return true;
1068 }
1069
1071 bool operator--(int){
1072 assert(current);
1073 if(current->next == NULL) return false;
1074 current=current->next;
1075 return true;
1076 }
1077
1078 void JumpDownList(int jump){
1079 int i;
1080
1081 if(jump > 0) for(i=0;i<jump;++i) --(*this);
1082 if(jump < 0) for(i=0;i<abs(jump);++i) ++(*this);
1083 }
1084
1085
1086 bool operator==(const iterator my_it){
1087 return (current == my_it.current);
1088 }
1089
1090 bool operator!=(const iterator my_it){
1091 return (current != my_it.current);
1092 }
1093};
1094
1095
1096template <typename T>
1097struct LIST
1098{
1099 LIST(){
1100 }
1101 ~LIST(){EmptyList();}
1102
1103 std::list<T> list;
1104
1105 LISTiterator<T> top(){
1106 iterator it;
1107 it.current_it = list.begin();
1108 return it;
1109 }
1110
1111 LISTiterator<T> bottom(){
1112 iterator it;
1113 it.current_it = list.end();
1114 return it;
1115 }
1116
1117 unsigned long size() const {return list.size();}
1118
1119 inline bool IsTop(LISTiterator<T> &it) const{
1120 return it.current_it == list.begin();
1121 };
1122 inline bool IsBottom(LIST::iterator &it) const{
1123 return it.current_it == list.end();
1124 };
1125
1126 T *Top() const {return list.front();}
1127 T *Bottom() const {return list.back();}
1128
1129 void EmptyList(){list.clear();}
1130
1131 void InsertAfterCurrent(LISTiterator<T> &it,T &d){
1132 list.insert(it.current_it+1,d);
1133 }
1134 void InsertBeforeCurrent(LISTiterator<T> &it,T &d){
1135 list.insert(it.current_it,d);
1136 }
1137
1138 void MoveCurrentToBottom(iterator &current);
1139 Point *TakeOutCurrent(iterator &current);
1140
1141 void InsertListAfterCurrent(iterator &current,PointList *list2);
1142 void InsertListBeforeCurrent(iterator &current,PointList *list2);
1143 void MergeLists(PointList* list2);
1144 void ShiftList(iterator &current);
1145
1146 void PrintList();
1147
1148 //void setN(unsigned long N){Npoints = N;}
1149 //void setTop(Point *p){top = p;}
1150 //void setBottom(Point *p){bottom = p;}
1151
1152private:
1153 //Point *top;
1154 //Point *bottom;
1155 //unsigned long Npoints;
1156
1157 // make a point uncopyable
1158 LIST &operator=(const LIST &p);
1159}
1160 */
1161
1166template <typename T = PosType>
1168 Point_nd():dim(0){ }
1169 Point_nd(int d):x(d,0),dim(d){ }
1170
1171 void setD(int d){
1172 dim=d;
1173 x.resize(dim);
1174 }
1175
1176 ~Point_nd(){};
1177
1178 Point_nd(const Point_nd &p){
1179 for(int i=0 ; i<dim ; ++i ) x[i] = p.x[i];
1180 }
1181
1182 Point_nd<T> & operator=(const Point_nd<T> &p){
1183 if(this == &p) return *this;
1184 for(int i=0 ; i<dim ; ++i ) x[i] = p.x[i];
1185 return *this;
1186 }
1187 Point_nd<T> operator+(const Point_nd<T> &p) const{
1188 Point_nd<T> tmp(dim);
1189 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i] + p.x[i];
1190 return tmp;
1191 }
1192 Point_nd<T> operator-(const Point_nd<T> &p) const{
1193 Point_nd<T> tmp(dim);
1194 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i] - p.x[i];
1195 return tmp;
1196 }
1197 Point_nd<T> & operator+=(const Point_nd<T> &p){
1198 for(int i=0 ; i<dim ; ++i ) x[i] += p.x[i];
1199 return *this;
1200 }
1201 Point_nd<T> & operator-=(const Point_nd<T> &p){
1202 for(int i=0 ; i<dim ; ++i ) x[i] -= p.x[i];
1203 return *this;
1204 }
1205 Point_nd<T> & operator/=(T value){
1206 for(int i=0 ; i<dim ; ++i ) x[i]/=value;
1207 return *this;
1208 }
1209 Point_nd<T> operator/(T value) const{
1210 Point_nd<T> tmp;
1211 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]/value;
1212 return tmp;
1213 }
1214 Point_nd<T> & operator*=(T value){
1215 for(int i=0 ; i<dim ; ++i ) x[i] *=value;
1216 return *this;
1217 }
1218 Point_nd operator*(PosType value) const{
1219 Point_nd<T> tmp;
1220 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]*value;
1221 return tmp;
1222 }
1223
1225 T operator*(const Point_nd<T> &p) const {
1226 T ans = 0;
1227 for(int i=0 ; i<dim ; ++i ) ans += x[i]*p.x[i];
1228 return ans;
1229 }
1230
1232 T length_sqr() const{
1233 T ans = 0;
1234 for(int i=0 ; i<dim ; ++i ) ans += x[i]*x[i];
1235 return ans;
1236 }
1237
1238 T length() const{
1239 return sqrt(length_sqr());
1240 }
1241
1242 T* data(){return x.data();}
1243
1244 std::vector<T> x;
1245 T & operator[](size_t i){return x[i];}
1246 const T & operator[](size_t i) const {return x[i];}
1247private:
1248 int dim;
1249};
1250
1251#endif
The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organiz...
Definition point.h:628
void print()
print out all member data for testing purposes
Definition Tree.cpp:168
bool refined
Marks point as start of a level of refinement.
Definition point.h:647
unsigned long npoints
pointer to first points in Branch
Definition point.h:636
A point that automatically has an image point.
Definition point.h:496
Definition point.h:670
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition point.h:48
Point_2d rotated(PosType theta) const
returns a copy of the point that it rotated
Definition point.h:152
PosType length() const
length
Definition point.h:134
PosType operator*(const Point_2d &p) const
scalar product
Definition point.h:125
void unitize()
rescale to make a unit length vector
Definition point.h:162
PosType length_sqr() const
length^2
Definition point.h:139
PosType operator^(const Point_2d &p) const
outer product
Definition point.h:129
Class for representing points or vectors in 3 dimensions. Not that the dereferencing operator is over...
Definition point.h:883
T length() const
length
Definition point.h:977
T operator*(const Point_3d &p) const
scalar product
Definition point.h:963
void rotate(T theta, T phi)
a rotation theta around the z-axis followed by a rotation phi around the y-axis
Definition point.h:986
Point_3d< T > unitTheta()
Definition point.h:1013
Point_3d< T > unitPhi()
returns the unit vector in the direction of the right handed spherical coordinate Phi
Definition point.h:1006
void unitize()
rescale to make a unit length vector
Definition point.h:998
Point_3d< T > operator^(const Point_3d< T > &p) const
outer product
Definition point.h:968
Definition point.h:1167
T length_sqr() const
length
Definition point.h:1232
T operator*(const Point_nd< T > &p) const
scalar product
Definition point.h:1225
A point on the source or image plane that contains a position and the lensing quantities.
Definition point.h:414
double flux()
surface_brightness * gridsize * gridsize
Definition point.h:456
bool inverted()
returns true if the image is double inverted, At very low magnification or when there is a rotation t...
Definition point.h:481
void Print()
print out all member data for testing purposes
Definition Tree.cpp:103
Point operator=(const Point_2d &p)
Only copies position!!
Definition point.h:429
Point()
Definition Tree.cpp:63
link list for points, uses the linking pointers within the Point type unlike Kist
Definition point.h:755
Point * TakeOutCurrent(iterator &current)
Definition list.cpp:105
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition point.h:510
KappaType dt
time-delay
Definition point.h:592
Point_2d alpha() const
deflection angle, x-y
Definition point.h:613
Point_2d x
image position
Definition point.h:584
KappaType invmag() const
inverse of the magnification
Definition point.h:596
Point_2d y
source position
Definition point.h:586