13#ifndef pointtypes_declare
14#define pointtypes_declare
25#define ERROR_MESSAGE() std::cout << "ERROR: file: " << __FILE__ << " line: " << __LINE__ << std::endl;
30typedef enum {NO, YES, MAYBE} Boo;
35enum class CritType {ND=0,radial=2,tangential=1,pseudo=3,};
38std::ostream &operator<<(std::ostream &os, CritType
const &p);
42int sign(T val){
return (T(0) < val) - (val < T(0));}
52 Point_2d(
double x1,
double x2){
59 Point_2d(
const Point_2d &p){
63 Point_2d & operator=(
const Point_2d &p){
64 if(
this == &p)
return *
this;
69 Point_2d(
const PosType *p){
75 bool operator==(
const Point_2d &p)
const{
76 return (x[0] == p.x[0])*(x[1] == p.x[1]);
78 bool operator!=(
const Point_2d &p)
const{
79 return (x[0] != p.x[0]) || (x[1] != p.x[1]);
82 Point_2d operator+(
const Point_2d &p)
const{
84 tmp.x[0] = x[0] + p.x[0];
85 tmp.x[1] = x[1] + p.x[1];
88 Point_2d operator-(
const Point_2d &p)
const{
90 tmp.x[0] = x[0] - p.x[0];
91 tmp.x[1] = x[1] - p.x[1];
94 Point_2d & operator+=(
const Point_2d &p){
99 Point_2d & operator-=(
const Point_2d &p){
104 Point_2d & operator/=(PosType value){
109 Point_2d operator/(PosType value)
const{
115 Point_2d & operator*=(PosType value){
120 Point_2d operator*(PosType value)
const{
121 return Point_2d(x[0]*value,x[1]*value);
126 return x[0]*p.x[0] + x[1]*p.x[1];
130 return x[0]*p.x[1] - x[1]*p.x[0];
135 return sqrt(x[0]*x[0] + x[1]*x[1]);
140 return x[0]*x[0] + x[1]*x[1];
144 void rotate(PosType theta){
145 PosType c = cos(theta),s = sin(theta);
147 x[0] = c*tmp - s*x[1];
148 x[1] = c*x[1] + s*tmp;
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];
171 return Point_2d(x[0]/s,x[1]/s);
176 PosType* data(){
return x;}
181 PosType & operator[](
size_t i) {
return x[i];}
182 const PosType & operator[](
size_t i)
const {
return x[i];}
192 Matrix2x2(
const Matrix2x2<T> &F){
199 template <
typename B>
200 Matrix2x2<T> operator=(
const Matrix2x2<B> &F){
209 bool operator==(
const Matrix2x2<T> &F){
211 return (a[0] == F.a[0]) * (a[1] == F.a[1]) * (a[2] == F.a[2]) * (a[3] == F.a[3]);
215 Matrix2x2<T> operator*=(T f){
224 Matrix2x2<T> operator/=(T f){
233 Matrix2x2<T> operator*(T f)
const{
234 Matrix2x2<T> m = *
this;
239 Point_2d operator*(
const Point_2d &v)
const{
241 v2[0] = a[0]*v[0] + a[1]*v[1];
242 v2[1] = a[2]*v[0] + a[3]*v[1];
247 Matrix2x2<T> operator/(T f)
const{
248 Matrix2x2<T> m = *
this;
254 template <
typename B>
255 Matrix2x2<T> operator*(
const Matrix2x2<B> &F)
const{
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];
266 template <
typename B>
267 Matrix2x2<T> operator+=(
const Matrix2x2<B> &F){
276 template <
typename B>
277 Matrix2x2<T> operator+(
const Matrix2x2<B> &F)
const{
278 Matrix2x2<T> m = *
this;
283 template <
typename B>
284 Matrix2x2<T> operator-=(
const Matrix2x2<B> &F){
293 template <
typename B>
294 Matrix2x2<T> operator-(
const Matrix2x2<B> &F)
const{
295 Matrix2x2<T> m = *
this;
302 T & operator()(
int i,
int j){
306 T operator()(
int i,
int j)
const{
311 T & operator[](
int i){
316 return a[0]*a[3] - a[1]*a[2];
319 Matrix2x2<T> inverse()
const{
333 std::swap(a[0],a[3]);
348 static Matrix2x2<T> I(){
355 static Matrix2x2<T> sig1(){
362 static Matrix2x2<T> sig2(){
365 m.a[1] = m.a[2] = -1;
369 static Matrix2x2<T> sig3(){
377 T kappa()
const {
return 1-(a[0] + a[3])/2; }
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;}
383 void set(T kappa,T gamma[3]){
384 a[0] = 1 - kappa + gamma[0];
385 a[3] = 1 - kappa - gamma[0];
387 a[1] = gamma[1] - gamma[3];
388 a[2] = gamma[1] + gamma[3];
391 void gamma(T *g)
const{
399 std::cout << std::endl;
400 std::cout << a[0] <<
" " << a[1] << std::endl;
401 std::cout << a[2] <<
" " << a[3] << std::endl;
405 void eigenv(T lambda[]){
406 T m = (a[0] + a[3])/2;
407 T determinent = det();
409 lambda[0] = m + sqrt( abs(m*m - determinent));
410 lambda[1] = m - sqrt( abs(m*m - determinent));
424 Point(
const Point_2d &p);
425 Point(PosType x,PosType y);
428 Point *image=
nullptr;
430 unsigned long head=0;
433 PosType *ptr_y(){
return image->x;}
437 Point_2d::operator=(p);
444 Matrix2x2<KappaType> A;
446 KappaType invmag()
const{
449 KappaType gamma1()
const{
452 KappaType gamma2()
const{
455 KappaType gamma3()
const{
458 KappaType kappa()
const{
463 double flux(){
return surface_brightness * gridsize * gridsize;}
466 float surface_brightness;
475 return (p1->x[0] < p2->x[0]);
478 return (p1->x[0] > p2->x[0]);
481 return (p1->x[1] < p2->x[1]);
484 return (p1->x[1] > p2->x[1]);
492 return (eigens[0] < 0)*(eigens[1] < 0);
496std::ostream &operator<<(std::ostream &os,
Point const &p);
521 A = Matrix2x2<KappaType>::I();
526 RAY(
const Point &p,
double zs = -1){
529 y[0] = p.image->x[0];
530 y[1] = p.image->x[1];
540 y[0] = p.image->x[0];
541 y[1] = p.image->x[1];
559 RAY & operator=(
const Point &p){
560 assert(p.image !=
nullptr);
564 y[0] = p.image->x[0];
565 y[1] = p.image->x[1];
576 RAY & operator=(
const RAY &p){
594 PosType *ptr_y(){
return y.x;}
596 Matrix2x2<KappaType> A;
606 KappaType gamma1()
const{
609 KappaType gamma2()
const{
612 KappaType gamma3()
const{
615 KappaType kappa()
const{
623std::ostream &operator<<(std::ostream &os,
Point_2d const &p);
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);
635std::ostream &operator<<(std::ostream &os,
RAY const &r);
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);
644 struct Point *points;
649 unsigned long number;
650 double boundary_p1[2];
651 double boundary_p2[2];
661 PosType area(){
return (boundary_p2[0]-boundary_p1[0])*(boundary_p2[1]-boundary_p1[1]);}
663 std::list<Branch *> neighbors;
665 static unsigned long countID;
682 MemmoryBank():count(0){};
683 MemmoryBank(MemmoryBank &&membank){
684 *
this = std::move(membank);
686 void operator=(MemmoryBank &&membank){
687 bank = std::move(membank.bank);
691 T * operator()(
size_t N){
692 if(N <= 0)
return nullptr;
694 bank.emplace_back(
new T[N]);
696 return bank.back().get();
717 size_t number_of_blocks(){
return bank.size();}
721 MemmoryBank(
const MemmoryBank<T> &b);
722 MemmoryBank<T> & operator=(
const MemmoryBank<T> &b);
725 std::vector<std::unique_ptr<T[]> > bank;
777 iterator():current(NULL){ }
783 Point *operator*(){
return current;}
787 if(current->prev == NULL)
return false;
788 current=current->prev;
793 bool operator++(
int){
795 if(current->prev == NULL)
return false;
796 current=current->prev;
803 if(current->next == NULL)
return false;
804 current=current->next;
809 bool operator--(
int){
811 if(current->next == NULL)
return false;
812 current=current->next;
816 void JumpDownList(
int jump){
819 if(jump > 0)
for(i=0;i<jump;++i) --(*
this);
820 if(jump < 0)
for(i=0;i<abs(jump);++i) ++(*
this);
824 bool operator==(
const iterator my_it){
825 return (current == my_it.current);
828 bool operator!=(
const iterator my_it){
829 return (current != my_it.current);
834 unsigned long size()
const {
return Npoints;}
836 inline bool IsTop(PointList::iterator &it)
const{
839 inline bool IsBottom(PointList::iterator &it)
const{
840 return *it == bottom;
843 Point *Top()
const {
return top;}
844 Point *Bottom()
const {
return bottom;}
849 void InsertPointAfterCurrent(iterator ¤t,
Point *);
850 void InsertPointBeforeCurrent(iterator ¤t,
Point *);
852 void MoveCurrentToBottom(iterator ¤t);
855 void InsertListAfterCurrent(iterator ¤t,PointList *list2);
856 void InsertListBeforeCurrent(iterator ¤t,PointList *list2);
857 void MergeLists(PointList* list2);
858 void ShiftList(iterator ¤t);
864 void setN(
unsigned long N){Npoints = N;}
865 void setTop(
Point *p){top = p;}
866 void setBottom(
Point *p){bottom = p;}
871 unsigned long Npoints;
874 PointList &operator=(
const PointList &p);
886void SwapPointsInList(ListHndl list,
Point *p1,
Point *p2);
887Point *sortList(
long n,
double arr[],ListHndl list,
Point *firstpoint);
892template <
typename T = PosType>
897 Point_3d(T xx,T yy,T zz){
904 Point_3d(
const Point_3d &p){
910 Point_3d & operator=(
const Point_3d &p){
911 if(
this == &p)
return *
this;
917 Point_3d operator+(
const Point_3d &p)
const{
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];
924 Point_3d operator-(
const Point_3d &p)
const{
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];
931 Point_3d & operator+=(
const Point_3d &p){
937 Point_3d & operator-=(
const Point_3d &p){
943 Point_3d & operator/=(T value){
949 Point_3d operator/(T value)
const{
957 Point_3d & operator*=(T value){
963 Point_3d operator*(PosType value)
const{
974 return x[0]*p.x[0] + x[1]*p.x[1] + x[2]*p.x[2];
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];
988 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
991 T length_sqr()
const{
992 return x[0]*x[0] + x[1]*x[1] + x[2]*x[2];
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];
1003 x[0] = c*tmp - s*x[2];
1004 x[2] = c*x[2] + s*tmp;
1022 double s = sqrt(x[0]*x[0] + x[1]*x[1]);
1023 return Point_3d<T>(-x[1],x[0],0) / s;
1029 Point_3d<T> v(-x[0]*x[2],-x[1]*x[2] ,x[0]*x[0] + x[1]*x[1]);
1034 T* data(){
return x;}
1037 T & operator[](
size_t i){
return x[i];}
1038 const T & operator[](
size_t i)
const {
return x[i];}
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];
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;
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");
1060 file <<
"x,y" << std::endl;
1061 for(
int i=0 ; i<n ; ++i){
1062 file << x[i] <<
"," << y[i] << std::endl;
1066inline double pointx(
Point &p){
return p.x[0];}
1067inline double pointy(
Point &p){
return p.x[1];}
1090 bool operator++(int){
1092 if(current->prev == NULL) return false;
1093 current=current->prev;
1100 if(current->next == NULL) return false;
1101 current=current->next;
1106 bool operator--(int){
1108 if(current->next == NULL) return false;
1109 current=current->next;
1113 void JumpDownList(int jump){
1116 if(jump > 0) for(i=0;i<jump;++i) --(*this);
1117 if(jump < 0) for(i=0;i<abs(jump);++i) ++(*this);
1121 bool operator==(const iterator my_it){
1122 return (current == my_it.current);
1125 bool operator!=(const iterator my_it){
1126 return (current != my_it.current);
1131template <typename T>
1136 ~LIST(){EmptyList();}
1140 LISTiterator<T> top(){
1142 it.current_it = list.begin();
1146 LISTiterator<T> bottom(){
1148 it.current_it = list.end();
1152 unsigned long size() const {return list.size();}
1154 inline bool IsTop(LISTiterator<T> &it) const{
1155 return it.current_it == list.begin();
1157 inline bool IsBottom(LIST::iterator &it) const{
1158 return it.current_it == list.end();
1161 T *Top() const {return list.front();}
1162 T *Bottom() const {return list.back();}
1164 void EmptyList(){list.clear();}
1166 void InsertAfterCurrent(LISTiterator<T> &it,T &d){
1167 list.insert(it.current_it+1,d);
1169 void InsertBeforeCurrent(LISTiterator<T> &it,T &d){
1170 list.insert(it.current_it,d);
1173 void MoveCurrentToBottom(iterator ¤t);
1174 Point *TakeOutCurrent(iterator ¤t);
1176 void InsertListAfterCurrent(iterator ¤t,PointList *list2);
1177 void InsertListBeforeCurrent(iterator ¤t,PointList *list2);
1178 void MergeLists(PointList* list2);
1179 void ShiftList(iterator ¤t);
1183 //void setN(unsigned long N){Npoints = N;}
1184 //void setTop(Point *p){top = p;}
1185 //void setBottom(Point *p){bottom = p;}
1190 //unsigned long Npoints;
1192 // make a point uncopyable
1193 LIST &operator=(const LIST &p);
1201template <
typename T = PosType>
1203 Point_nd():dim(0){ }
1204 Point_nd(
int d):x(d,0),dim(d){ }
1213 Point_nd(
const Point_nd &p){
1214 for(
int i=0 ; i<dim ; ++i ) x[i] = p.x[i];
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];
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];
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];
1232 Point_nd<T> & operator+=(
const Point_nd<T> &p){
1233 for(
int i=0 ; i<dim ; ++i ) x[i] += p.x[i];
1236 Point_nd<T> & operator-=(
const Point_nd<T> &p){
1237 for(
int i=0 ; i<dim ; ++i ) x[i] -= p.x[i];
1240 Point_nd<T> & operator/=(T value){
1241 for(
int i=0 ; i<dim ; ++i ) x[i]/=value;
1244 Point_nd<T> operator/(T value)
const{
1246 for(
int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]/value;
1249 Point_nd<T> & operator*=(T value){
1250 for(
int i=0 ; i<dim ; ++i ) x[i] *=value;
1253 Point_nd operator*(PosType value)
const{
1255 for(
int i=0 ; i<dim ; ++i ) tmp.x[i] = x[i]*value;
1262 for(
int i=0 ; i<dim ; ++i ) ans += x[i]*p.x[i];
1269 for(
int i=0 ; i<dim ; ++i ) ans += x[i]*x[i];
1277 T* data(){
return x.data();}
1280 T & operator[](
size_t i){
return x[i];}
1281 const T & operator[](
size_t i)
const {
return x[i];}
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 ¤t)
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