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
47
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 }
128
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
169 Point_2d unit() const {
170 PosType s = length();
171 return Point_2d(x[0]/s,x[1]/s);
172 }
173
174
175 // returns a pointer to the position
176 PosType* data(){return x;}
177
178 // array of size 2 containing the position
179 PosType x[2];
180
181 PosType & operator[](size_t i) {return x[i];}
182 const PosType & operator[](size_t i) const {return x[i];}
183};
184
185
186template <typename T>
187struct Matrix2x2{
188
189 Matrix2x2(){
190 }
191
192 Matrix2x2(const Matrix2x2<T> &F){
193 a[0] = F.a[0];
194 a[1] = F.a[1];
195 a[2] = F.a[2];
196 a[3] = F.a[3];
197 }
198
199 template <typename B>
200 Matrix2x2<T> operator=(const Matrix2x2<B> &F){
201 a[0] = F.a[0];
202 a[1] = F.a[1];
203 a[2] = F.a[2];
204 a[3] = F.a[3];
205
206 return *this;
207 }
208
209 bool operator==(const Matrix2x2<T> &F){
210
211 return (a[0] == F.a[0]) * (a[1] == F.a[1]) * (a[2] == F.a[2]) * (a[3] == F.a[3]);
212 }
213
214
215 Matrix2x2<T> operator*=(T f){
216 a[0] *= f;
217 a[1] *= f;
218 a[2] *= f;
219 a[3] *= f;
220
221 return *this;
222 }
223
224 Matrix2x2<T> operator/=(T f){
225 a[0] /= f;
226 a[1] /= f;
227 a[2] /= f;
228 a[3] /= f;
229
230 return *this;
231 }
232
233 Matrix2x2<T> operator*(T f) const{
234 Matrix2x2<T> m = *this;
235 m *= f;
236 return m;
237 }
238
239 Point_2d operator*(const Point_2d &v) const{
240 Point_2d v2;
241 v2[0] = a[0]*v[0] + a[1]*v[1];
242 v2[1] = a[2]*v[0] + a[3]*v[1];
243 return v2;
244 }
245
246
247 Matrix2x2<T> operator/(T f) const{
248 Matrix2x2<T> m = *this;
249 m /= f;
250
251 return m;
252 }
253
254 template <typename B>
255 Matrix2x2<T> operator*(const Matrix2x2<B> &F) const{
256 Matrix2x2<T> m;
257
258 m.a[0] = a[0] * F.a[0] + a[1] * F.a[2];
259 m.a[1] = a[0] * F.a[1] + a[1] * F.a[3];
260 m.a[2] = a[2] * F.a[0] + a[3] * F.a[2];
261 m.a[3] = a[2] * F.a[1] + a[3] * F.a[3];
262
263 return m;
264 }
265
266 template <typename B>
267 Matrix2x2<T> operator+=(const Matrix2x2<B> &F){
268 a[0] += F.a[0];
269 a[1] += F.a[1];
270 a[2] += F.a[2];
271 a[3] += F.a[3];
272
273 return *this;
274 }
275
276 template <typename B>
277 Matrix2x2<T> operator+(const Matrix2x2<B> &F) const{
278 Matrix2x2<T> m = *this;
279 m += F;
280
281 return m;
282 }
283 template <typename B>
284 Matrix2x2<T> operator-=(const Matrix2x2<B> &F){
285 a[0] -= F.a[0];
286 a[1] -= F.a[1];
287 a[2] -= F.a[2];
288 a[3] -= F.a[3];
289
290 return *this;
291 }
292
293 template <typename B>
294 Matrix2x2<T> operator-(const Matrix2x2<B> &F)const{
295 Matrix2x2<T> m = *this;
296 m -= F;
297
298 return m;
299 }
300
302 T & operator()(int i,int j){
303 return a[ i + 2*j ];
304 }
305
306 T operator()(int i,int j) const{
307 return a[ i + 2*j ];
308 }
309
310
311 T & operator[](int i){
312 return a[i];
313 }
314
315 T det() const{
316 return a[0]*a[3] - a[1]*a[2];
317 }
318
319 Matrix2x2<T> inverse() const{
320 Matrix2x2<T> m;
321
322 m.a[0] = a[3];
323 m.a[3] = a[0];
324 m.a[1] = -a[1];
325 m.a[2] = -a[2];
326
327 m /= det();
328 return m;
329 }
330
331 void invert(){
332
333 std::swap(a[0],a[3]);
334 a[1] *= -1;
335 a[2] *= -1;
336
337 *this /= det();
338 }
339
340 T a[4];
341
342 // make the matrix the identity
343 void setToI(){
344 a[0] = a[3] = 1;
345 a[1] = a[2] = 0;
346 }
347
348 static Matrix2x2<T> I(){
349 Matrix2x2<T> m;
350 m.a[0] = m.a[3] = 1;
351 m.a[1] = m.a[2] = 0;
352 return m;
353 }
354
355 static Matrix2x2<T> sig1(){
356 Matrix2x2<T> m;
357 m.a[0] = -1;
358 m.a[3] = 1;
359 m.a[1] = m.a[2] = 0;
360 return m;
361 }
362 static Matrix2x2<T> sig2(){
363 Matrix2x2<T> m;
364 m.a[0] = m.a[3] = 0;
365 m.a[1] = m.a[2] = -1;
366 return m;
367 }
368
369 static Matrix2x2<T> sig3(){
370 Matrix2x2<T> m;
371 m.a[0] = m.a[3] = 0;
372 m.a[1] = 1;
373 m.a[2] = -1;
374 return m;
375 }
376
377 T kappa() const {return 1-(a[0] + a[3])/2; }
378 // defined according to GLAMER II convention
379 T gamma1() const{ return (a[0] - a[3])/2;}
380 T gamma2() const{ return (a[1] + a[2])/2;}
381 T gamma3() const{ return (a[2] - a[1])/2;}
382
383 void set(T kappa,T gamma[3]){
384 a[0] = 1 - kappa + gamma[0];
385 a[3] = 1 - kappa - gamma[0];
386
387 a[1] = gamma[1] - gamma[3];
388 a[2] = gamma[1] + gamma[3];
389 }
390
391 void gamma(T *g) const{
392 g[0] = gamma1();
393 g[1] = gamma2();
394 g[2] = gamma3();
395 }
396
397
398 void print() const {
399 std::cout << std::endl;
400 std::cout << a[0] << " " << a[1] << std::endl;
401 std::cout << a[2] << " " << a[3] << std::endl;
402 }
403
404 // this assumes that the eigenvalues are real
405 void eigenv(T lambda[]){
406 T m = (a[0] + a[3])/2;
407 T determinent = det();
408
409 lambda[0] = m + sqrt( abs(m*m - determinent));
410 lambda[1] = m - sqrt( abs(m*m - determinent));
411 }
412};
413
414
415//struct branchstruct;
416struct Branch;
417
418
420
421struct Point: public Point_2d{
422
423 Point();
424 Point(const Point_2d &p);
425 Point(PosType x,PosType y);
426 Point *next=nullptr; // pointer to next point in linked list
427 Point *prev=nullptr;
428 Point *image=nullptr; // pointer to point on image or source plane
429 unsigned long id=0;
430 unsigned long head=0; // marks beginning of allocated array of points for easy deallocation
431 Boo in_image; // marks if point is in image
432
433 PosType *ptr_y(){return image->x;}
434
436 Point operator=(const Point_2d &p){
437 Point_2d::operator=(p);
438
439 return *this;
440 }
441
442 double dt; // time delay : double implies permanent precision independently from DOUBLE_PRECISION
443
444 Matrix2x2<KappaType> A;
445
446 KappaType invmag() const{
447 return A.det();
448 }
449 KappaType gamma1() const{
450 return A.gamma1();
451 }
452 KappaType gamma2() const{
453 return A.gamma2();
454 }
455 KappaType gamma3() const{
456 return A.gamma3();
457 }
458 KappaType kappa() const{
459 return A.kappa();
460 }
461
463 double flux(){return surface_brightness * gridsize * gridsize;}
464
465 double gridsize; // the size of the most refined grid the point is in
466 float surface_brightness; // the surface brightness at this points
467
468
469 Branch *leaf;
470 bool flag;
471
472 void Print();
473
474 static bool orderX(Point *p1,Point *p2){
475 return (p1->x[0] < p2->x[0]);
476 }
477 static bool orderXrev(Point *p1,Point *p2){
478 return (p1->x[0] > p2->x[0]);
479 }
480 static bool orderY(Point *p1,Point *p2){
481 return (p1->x[1] < p2->x[1]);
482 }
483 static bool orderYrev(Point *p1,Point *p2){
484 return (p1->x[1] > p2->x[1]);
485 }
486
488 bool inverted(){
489 KappaType eigens[2];
490 A.eigenv(eigens);
491
492 return (eigens[0] < 0)*(eigens[1] < 0);
493 }
494};
495
496std::ostream &operator<<(std::ostream &os, Point const &p);
497
502struct LinkedPoint : public Point
503{
504 LinkedPoint(){
505 image = &im;
506 im.image = this;
507 }
508
509 Point_2d& y(){return im;}
510private:
511 Point im;
512};
513
514
517struct RAY{
518 RAY(){
519
520 dt = 0.0;
521 A = Matrix2x2<KappaType>::I();
522 z = -1;
523
524 };
525
526 RAY(const Point &p,double zs = -1){
527 x[0] = p.x[0];
528 x[1] = p.x[1];
529 y[0] = p.image->x[0];
530 y[1] = p.image->x[1];
531
532 dt = p.dt;
533
534 A = p.A;
535 z = zs;
536 };
537 RAY(const LinkedPoint &p,double zs = -1){
538 x[0] = p.x[0];
539 x[1] = p.x[1];
540 y[0] = p.image->x[0];
541 y[1] = p.image->x[1];
542
543 dt = p.dt;
544
545 A = p.A;
546 z = zs;
547 };
548
549 RAY(const RAY &p){
550 x = p.x;
551 y = p.y;
552
553 dt = p.dt;
554
555 A = p.A;
556 z = p.z;
557 };
558
559 RAY & operator=(const Point &p){
560 assert(p.image != nullptr);
561
562 x[0] = p.x[0];
563 x[1] = p.x[1];
564 y[0] = p.image->x[0];
565 y[1] = p.image->x[1];
566
567 dt = p.dt;
568
569
570 A = p.A;
571 z = -1;
572
573 return *this;
574 };
575
576 RAY & operator=(const RAY &p){
577 x = p.x;
578 y = p.y;
579
580 dt = p.dt;
581
582 A = p.A;
583 z = p.z;
584
585 return *this;
586 };
587
588 ~RAY(){};
589
594 PosType *ptr_y(){return y.x;}
595
596 Matrix2x2<KappaType> A;
597
599 KappaType dt;
600 KappaType z;
601
603 KappaType invmag() const{
604 return A.det();
605 }
606 KappaType gamma1() const{
607 return A.gamma1();
608 }
609 KappaType gamma2() const{
610 return A.gamma2();
611 }
612 KappaType gamma3() const{
613 return A.gamma3();
614 }
615 KappaType kappa() const{
616 return A.kappa();
617 }
618
620 Point_2d alpha() const {return x - y;}
621};
622
623std::ostream &operator<<(std::ostream &os, Point_2d const &p);
624
625void write_csv(std::string filename,const std::vector<Point_2d> &v);
626void write_csv(std::string filename,const std::vector<RAY> &v);
627void write_csv(std::string filename,const std::vector<float> &v);
628void write_csv(std::string filename,const std::vector<double> &v);
629
630//inline std::string to_string(RAY &r) {
631// std::string s = "[" + std::to_string(r.x[0]) + "," + r.x[1] + ",[" + r.y[0] + "," + r.y[1]
632// + "]," + r.z + "," + r.kappa() + ",[" + r.gamma1() + "," + r.gamma2() + "," + r.gamma3() + "]," << r.dt;
633// return s;
634//}
635std::ostream &operator<<(std::ostream &os, RAY const &r);
636
638struct Branch{
639 Branch(Point *my_points,unsigned long my_npoints
640 ,double my_boundary_p1[2],double my_boundary_p2[2]
641 ,double my_center[2],int my_level);
642 ~Branch();
643
644 struct Point *points;
645
646 unsigned long npoints;
647 double center[2];
648 int level;
649 unsigned long number;
650 double boundary_p1[2];
651 double boundary_p2[2];
652 Branch *child1;
653 Branch *child2;
654 Branch *brother;
655 Branch *prev;
658
659 void print();
660
661 PosType area(){return (boundary_p2[0]-boundary_p1[0])*(boundary_p2[1]-boundary_p1[1]);}
662
663 std::list<Branch *> neighbors;
664private:
665 static unsigned long countID;
666
667 // make a Branch uncopyable
668 Branch(const Branch &p);
669 Branch &operator=(Branch &p);
670 Branch &operator=(const Branch &p);
671
672} ;
673
674//typedef struct branchstruct Branch;
675
679template<typename T>
680struct MemmoryBank{
681
682 MemmoryBank():count(0){};
683 MemmoryBank(MemmoryBank &&membank){
684 *this = std::move(membank);
685 }
686 void operator=(MemmoryBank &&membank){
687 bank = std::move(membank.bank);
688 }
689
690
691 T * operator()(size_t N){
692 if(N <= 0) return nullptr;
693
694 bank.emplace_back(new T[N]);
695 count += N;
696 return bank.back().get();
697 }
698
699 // clear all data
700 void clear(){
701 bank.clear();
702 count = 0;
703 }
704
705 // clear specific array, does not decrement count
706 bool clear(T *ptr){
707 for(auto &p : bank){
708 if(p.get() == ptr){
709 p.swap(bank.back());
710 bank.pop_back();
711 return true;
712 }
713 }
714 return false;
715 }
716
717 size_t number_of_blocks(){return bank.size();}
718
719private:
720
721 MemmoryBank(const MemmoryBank<T> &b);
722 MemmoryBank<T> & operator=(const MemmoryBank<T> &b);
723
724 size_t count;
725 std::vector<std::unique_ptr<T[]> > bank;
726};
727
728// A class that onlt counts iteslf for testing
729//struct DummyClass{
730// DummyClass(){
731// N = count;
732// ++count;
733// };
734// ~DummyClass(){
735// std::cout << "delete " << N << std::endl;
736// }
737// int N;
738// static int count;
739//};
740
741//int DummyClass::count = 0;
742//{ test lines for MemmoryBank
743// MemmoryBank<DummyClass> pointbang;
744//
745// DummyClass *p = pointbang(10);
746// p[7].N = 2;
747// for(int i=0 ; i<10 ; ++i){
748// cout << p[i].N << endl;
749// }
750//
751// DummyClass *p2 = pointbang(5);
752// for(int i=0 ; i<5 ; ++i){
753// cout << p2[i].N << endl;
754// }
755//
756// pointbang.clear(p2);
757//
758// DummyClass *p3 = pointbang(10);
759//}
760
765struct PointList{
766 PointList(){
767 top=NULL;
768 Npoints=0;
769 bottom = top;
770 }
771 ~PointList(){}
772
773 struct iterator{
774
775 Point *current;
776
777 iterator():current(NULL){ }
778
779 iterator(Point *p){
780 current = p;
781 }
782
783 Point *operator*(){return current;}
784
785 bool operator++(){
786 assert(current);
787 if(current->prev == NULL) return false;
788 current=current->prev;
789 return true;
790 }
791
793 bool operator++(int){
794 assert(current);
795 if(current->prev == NULL) return false;
796 current=current->prev;
797 return true;
798 }
799
801 bool operator--(){
802 assert(current);
803 if(current->next == NULL) return false;
804 current=current->next;
805 return true;
806 }
807
809 bool operator--(int){
810 assert(current);
811 if(current->next == NULL) return false;
812 current=current->next;
813 return true;
814 }
815
816 void JumpDownList(int jump){
817 int i;
818
819 if(jump > 0) for(i=0;i<jump;++i) --(*this);
820 if(jump < 0) for(i=0;i<abs(jump);++i) ++(*this);
821 }
822
823
824 bool operator==(const iterator my_it){
825 return (current == my_it.current);
826 }
827
828 bool operator!=(const iterator my_it){
829 return (current != my_it.current);
830 }
831
832 };
833
834 unsigned long size() const {return Npoints;}
835
836 inline bool IsTop(PointList::iterator &it) const{
837 return *it == top;
838 };
839 inline bool IsBottom(PointList::iterator &it) const{
840 return *it == bottom;
841 };
842
843 Point *Top() const {return top;}
844 Point *Bottom() const {return bottom;}
845
846 void EmptyList();
847 //void InsertAfterCurrent(iterator &current,double *x,unsigned long id,Point *image);
848 //void InsertBeforeCurrent(iterator &current,double *x,unsigned long id,Point *image);
849 void InsertPointAfterCurrent(iterator &current,Point *);
850 void InsertPointBeforeCurrent(iterator &current,Point *);
851
852 void MoveCurrentToBottom(iterator &current);
853 Point *TakeOutCurrent(iterator &current);
854
855 void InsertListAfterCurrent(iterator &current,PointList *list2);
856 void InsertListBeforeCurrent(iterator &current,PointList *list2);
857 void MergeLists(PointList* list2);
858 void ShiftList(iterator &current);
859
860 //void FillList(double **x,unsigned long N
861 // ,unsigned long idmin);
862 void PrintList();
863
864 void setN(unsigned long N){Npoints = N;}
865 void setTop(Point *p){top = p;}
866 void setBottom(Point *p){bottom = p;}
867
868private:
869 Point *top;
870 Point *bottom;
871 unsigned long Npoints;
872
873 // make a point uncopyable
874 PointList &operator=(const PointList &p);
875};
876
877typedef struct PointList *ListHndl;
878
879
880// ***********************************************************
881// routines for linked list of points
882// ************************************************************
883
884//Point *NewPoint(double *x,unsigned long id);
885
886void SwapPointsInList(ListHndl list,Point *p1,Point *p2);
887Point *sortList(long n, double arr[],ListHndl list,Point *firstpoint);
888
892template <typename T = PosType>
893struct Point_3d{
894 Point_3d(){
895 x[0]=x[1]=x[2]=0.0;
896 }
897 Point_3d(T xx,T yy,T zz){
898 x[0]=xx;
899 x[1]=yy;
900 x[2]=zz;
901 }
902 ~Point_3d(){};
903
904 Point_3d(const Point_3d &p){
905 x[0]=p.x[0];
906 x[1]=p.x[1];
907 x[2]=p.x[2];
908 }
909
910 Point_3d & operator=(const Point_3d &p){
911 if(this == &p) return *this;
912 x[0]=p.x[0];
913 x[1]=p.x[1];
914 x[2]=p.x[2];
915 return *this;
916 }
917 Point_3d operator+(const Point_3d &p) const{
918 Point_3d tmp;
919 tmp.x[0] = x[0] + p.x[0];
920 tmp.x[1] = x[1] + p.x[1];
921 tmp.x[2] = x[2] + p.x[2];
922 return tmp;
923 }
924 Point_3d operator-(const Point_3d &p) const{
925 Point_3d tmp;
926 tmp.x[0] = x[0] - p.x[0];
927 tmp.x[1] = x[1] - p.x[1];
928 tmp.x[2] = x[2] - p.x[2];
929 return tmp;
930 }
931 Point_3d & operator+=(const Point_3d &p){
932 x[0]+=p.x[0];
933 x[1]+=p.x[1];
934 x[2]+=p.x[2];
935 return *this;
936 }
937 Point_3d & operator-=(const Point_3d &p){
938 x[0]-=p.x[0];
939 x[1]-=p.x[1];
940 x[2]-=p.x[2];
941 return *this;
942 }
943 Point_3d & operator/=(T value){
944 x[0]/=value;
945 x[1]/=value;
946 x[2]/=value;
947 return *this;
948 }
949 Point_3d operator/(T value) const{
950 Point_3d tmp;
951 tmp[0] = x[0]/value;
952 tmp[1] = x[1]/value;
953 tmp[2] = x[2]/value;
954
955 return tmp;
956 }
957 Point_3d & operator*=(T value){
958 x[0] *=value;
959 x[1] *=value;
960 x[2] *=value;
961 return *this;
962 }
963 Point_3d operator*(PosType value) const{
964 Point_3d tmp;
965 tmp[0] = x[0]*value;
966 tmp[1] = x[1]*value;
967 tmp[2] = x[2]*value;
968
969 return tmp;
970 }
971
973 T operator*(const Point_3d &p) const {
974 return x[0]*p.x[0] + x[1]*p.x[1] + x[2]*p.x[2];
975 }
976
978 Point_3d<T> operator^(const Point_3d<T> &p) const{
979 Point_3d<T> v;
980 v[0] = x[1]*p[2] - x[2]*p[1];
981 v[1] = x[2]*p[0] - x[0]*p[2];
982 v[2] = x[0]*p[1] - x[1]*p[0];
983 return v;
984 }
985
987 T length() const{
988 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
989 }
990
991 T length_sqr() const{
992 return x[0]*x[0] + x[1]*x[1] + x[2]*x[2];
993 }
994
996 void rotate(T theta,T phi){
997 T c = cos(theta),s = sin(theta);
998 T tmp = c*x[0] - s*x[1];
999 x[1] = c*x[1] + s*x[0];
1000
1001 c = cos(phi);
1002 s = sin(phi);;
1003 x[0] = c*tmp - s*x[2];
1004 x[2] = c*x[2] + s*tmp;
1005 }
1006
1008 void unitize(){
1009 T s = length();
1010 x[0] /= s;
1011 x[1] /= s;
1012 x[2] /= s;
1013 }
1014
1015 Point_3d<T> unit() const {
1016 PosType s = length();
1017 return Point_3d<T>(x[0]/s,x[1]/s,x[2]/s);
1018 }
1019
1021 Point_3d<T> unitPhi(){
1022 double s = sqrt(x[0]*x[0] + x[1]*x[1]);
1023 return Point_3d<T>(-x[1],x[0],0) / s;
1024 }
1025
1028 Point_3d<T> unitTheta(){
1029 Point_3d<T> v(-x[0]*x[2],-x[1]*x[2] ,x[0]*x[0] + x[1]*x[1]);
1030 v.unitize();
1031 return v;
1032 }
1033
1034 T* data(){return x;}
1035
1036 T x[3];
1037 T & operator[](size_t i){return x[i];}
1038 const T & operator[](size_t i) const {return x[i];}
1039};
1040
1041template <typename T>
1042std::ostream &operator<<(std::ostream &os, Point_3d<T> const &p) {
1043 return os << p.x[0] << " " << p.x[1] << " " << p.x[2];
1044}
1045
1046template <typename T>
1047void write_csv(std::string filename,const std::vector<Point_3d<T> > &v){
1048 std::ofstream file(filename);
1049 file << "x,y,z" << std::endl;
1050 for(const Point_3d<T> &p : v) file << p[0] << "," << p[1] << "," << p[2] << std::endl;
1051}
1052
1053template <typename T>
1054void write_csv(std::string filename,const std::vector<T> &x,const std::vector<T> &y){
1055 std::ofstream file(filename);
1056 if(x.size() != y.size()){
1057 throw std::invalid_argument("mismatch");
1058 }
1059 int n=x.size();
1060 file << "x,y" << std::endl;
1061 for(int i=0 ; i<n ; ++i){
1062 file << x[i] << "," << y[i] << std::endl;
1063 }
1064}
1065
1066inline double pointx(Point &p){return p.x[0];}
1067inline double pointy(Point &p){return p.x[1];}
1068
1069/*
1070template <typename T>
1071struct LISTiterator{
1072
1073 std::list<T>::iterator current_it;
1074
1075 LISTiterator(){ }
1076
1077 T& operator*(){return *current_it;}
1078
1079 bool operator++(){
1080 if(current_it == )
1081 --current_it;
1082
1083 assert(current);
1084 if(current->prev == NULL) return false;
1085 current=current->prev;
1086 return true;
1087 }
1088
1090 bool operator++(int){
1091 assert(current);
1092 if(current->prev == NULL) return false;
1093 current=current->prev;
1094 return true;
1095 }
1096
1098 bool operator--(){
1099 assert(current);
1100 if(current->next == NULL) return false;
1101 current=current->next;
1102 return true;
1103 }
1104
1106 bool operator--(int){
1107 assert(current);
1108 if(current->next == NULL) return false;
1109 current=current->next;
1110 return true;
1111 }
1112
1113 void JumpDownList(int jump){
1114 int i;
1115
1116 if(jump > 0) for(i=0;i<jump;++i) --(*this);
1117 if(jump < 0) for(i=0;i<abs(jump);++i) ++(*this);
1118 }
1119
1120
1121 bool operator==(const iterator my_it){
1122 return (current == my_it.current);
1123 }
1124
1125 bool operator!=(const iterator my_it){
1126 return (current != my_it.current);
1127 }
1128};
1129
1130
1131template <typename T>
1132struct LIST
1133{
1134 LIST(){
1135 }
1136 ~LIST(){EmptyList();}
1137
1138 std::list<T> list;
1139
1140 LISTiterator<T> top(){
1141 iterator it;
1142 it.current_it = list.begin();
1143 return it;
1144 }
1145
1146 LISTiterator<T> bottom(){
1147 iterator it;
1148 it.current_it = list.end();
1149 return it;
1150 }
1151
1152 unsigned long size() const {return list.size();}
1153
1154 inline bool IsTop(LISTiterator<T> &it) const{
1155 return it.current_it == list.begin();
1156 };
1157 inline bool IsBottom(LIST::iterator &it) const{
1158 return it.current_it == list.end();
1159 };
1160
1161 T *Top() const {return list.front();}
1162 T *Bottom() const {return list.back();}
1163
1164 void EmptyList(){list.clear();}
1165
1166 void InsertAfterCurrent(LISTiterator<T> &it,T &d){
1167 list.insert(it.current_it+1,d);
1168 }
1169 void InsertBeforeCurrent(LISTiterator<T> &it,T &d){
1170 list.insert(it.current_it,d);
1171 }
1172
1173 void MoveCurrentToBottom(iterator &current);
1174 Point *TakeOutCurrent(iterator &current);
1175
1176 void InsertListAfterCurrent(iterator &current,PointList *list2);
1177 void InsertListBeforeCurrent(iterator &current,PointList *list2);
1178 void MergeLists(PointList* list2);
1179 void ShiftList(iterator &current);
1180
1181 void PrintList();
1182
1183 //void setN(unsigned long N){Npoints = N;}
1184 //void setTop(Point *p){top = p;}
1185 //void setBottom(Point *p){bottom = p;}
1186
1187private:
1188 //Point *top;
1189 //Point *bottom;
1190 //unsigned long Npoints;
1191
1192 // make a point uncopyable
1193 LIST &operator=(const LIST &p);
1194}
1195 */
1196
1201template <typename T = PosType>
1202struct Point_nd{
1203 Point_nd():dim(0){ }
1204 Point_nd(int d):x(d,0),dim(d){ }
1205
1206 void setD(int d){
1207 dim=d;
1208 x.resize(dim);
1209 }
1210
1211 ~Point_nd(){};
1212
1213 Point_nd(const Point_nd &p){
1214 for(int i=0 ; i<dim ; ++i ) x[i] = p.x[i];
1215 }
1216
1217 Point_nd<T> & operator=(const Point_nd<T> &p){
1218 if(this == &p) return *this;
1219 for(int i=0 ; i<dim ; ++i ) x[i] = p.x[i];
1220 return *this;
1221 }
1222 Point_nd<T> operator+(const Point_nd<T> &p) const{
1223 Point_nd<T> tmp(dim);
1224 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i] + p.x[i];
1225 return tmp;
1226 }
1227 Point_nd<T> operator-(const Point_nd<T> &p) const{
1228 Point_nd<T> tmp(dim);
1229 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i] - p.x[i];
1230 return tmp;
1231 }
1232 Point_nd<T> & operator+=(const Point_nd<T> &p){
1233 for(int i=0 ; i<dim ; ++i ) x[i] += p.x[i];
1234 return *this;
1235 }
1236 Point_nd<T> & operator-=(const Point_nd<T> &p){
1237 for(int i=0 ; i<dim ; ++i ) x[i] -= p.x[i];
1238 return *this;
1239 }
1240 Point_nd<T> & operator/=(T value){
1241 for(int i=0 ; i<dim ; ++i ) x[i]/=value;
1242 return *this;
1243 }
1244 Point_nd<T> operator/(T value) const{
1245 Point_nd<T> tmp;
1246 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]/value;
1247 return tmp;
1248 }
1249 Point_nd<T> & operator*=(T value){
1250 for(int i=0 ; i<dim ; ++i ) x[i] *=value;
1251 return *this;
1252 }
1253 Point_nd operator*(PosType value) const{
1254 Point_nd<T> tmp;
1255 for(int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]*value;
1256 return tmp;
1257 }
1258
1260 T operator*(const Point_nd<T> &p) const {
1261 T ans = 0;
1262 for(int i=0 ; i<dim ; ++i ) ans += x[i]*p.x[i];
1263 return ans;
1264 }
1265
1267 T length_sqr() const{
1268 T ans = 0;
1269 for(int i=0 ; i<dim ; ++i ) ans += x[i]*x[i];
1270 return ans;
1271 }
1272
1273 T length() const{
1274 return sqrt(length_sqr());
1275 }
1276
1277 T* data(){return x.data();}
1278
1279 std::vector<T> x;
1280 T & operator[](size_t i){return x[i];}
1281 const T & operator[](size_t i) const {return x[i];}
1282private:
1283 int dim;
1284};
1285
1286#endif
The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organiz...
Definition point.h:638
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:657
unsigned long npoints
pointer to first points in Branch
Definition point.h:646
A point that automatically has an image point.
Definition point.h:503
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
Point_2d unit() const
rescale to make a unit length vector
Definition point.h:169
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:893
T length() const
length
Definition point.h:987
T operator*(const Point_3d &p) const
scalar product
Definition point.h:973
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:996
Point_3d< T > unitTheta()
Definition point.h:1028
Point_3d< T > unitPhi()
returns the unit vector in the direction of the right handed spherical coordinate Phi
Definition point.h:1021
void unitize()
rescale to make a unit length vector
Definition point.h:1008
Point_3d< T > operator^(const Point_3d< T > &p) const
outer product
Definition point.h:978
T length_sqr() const
length
Definition point.h:1267
T operator*(const Point_nd< T > &p) const
scalar product
Definition point.h:1260
A point on the source or image plane that contains a position and the lensing quantities.
Definition point.h:421
double flux()
surface_brightness * gridsize * gridsize
Definition point.h:463
bool inverted()
returns true if the image is double inverted, At very low magnification or when there is a rotation t...
Definition point.h:488
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:436
Point()
Definition Tree.cpp:63
link list for points, uses the linking pointers within the Point type unlike Kist
Definition point.h:765
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:517
KappaType dt
time-delay
Definition point.h:599
Point_2d alpha() const
deflection angle, x-y
Definition point.h:620
Point_2d x
image position
Definition point.h:591
KappaType invmag() const
inverse of the magnification
Definition point.h:603
Point_2d y
source position
Definition point.h:593