GRALE
lensplane.h
1 /*
2 
3  This file is a part of GRALE, a library to facilitate the simulation
4  and inversion of gravitational lenses.
5 
6  Copyright (C) 2008-2012 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24 */
25 
26 #ifndef GRALE_LENSPLANE_H
27 
28 #define GRALE_LENSPLANE_H
29 
30 #include "graleconfig.h"
31 #include "vector2d.h"
32 #include <enut/networklayeraddress.h>
33 #include <serut/serializationinterface.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <list>
37 #include <string>
38 #include <vector>
39 
40 namespace grale
41 {
42 
43 class GravitationalLens;
44 class OpenCLKernel;
45 
46 class GRALE_IMPORTEXPORT LensPlane : public errut::ErrorBase
47 {
48 public:
49  LensPlane(const GravitationalLens *pLens);
50  virtual ~LensPlane();
51  bool init(Vector2D<double> bottomleft,Vector2D<double> topright,int xpoints,int ypoints, OpenCLKernel *pCLKernel = 0);
52  bool init(Vector2D<double> bottomleft,Vector2D<double> topright,int xpoints,int ypoints,
53  const nut::NetworkLayerAddress &serverAddress, uint16_t serverport);
54  bool isInit() const { return m_init; }
55  const GravitationalLens *getLens() const { return m_pLens; }
56 
57  GravitationalLens *createDeflectionGridLens() const;
58 
59  bool scaleDeflections(double factor);
60  int getNumXPoints() const { return numx; }
61  int getNumYPoints() const { return numy; }
62  Vector2D<double> getBottomLeft() const { return bottomleft; }
63  Vector2D<double> getTopRight() const { return topright; }
64  double getXStep() const { return xstep; }
65  double getYStep() const { return ystep; }
66 
67  Vector2D<double> getAlpha(int xpos, int ypos) const { return alphas[xpos+ypos*numx]; }
68  void getAlphaDerivatives(int xpos, int ypos, double &axx, double &ayy, double &axy) const { axx = alphaxx[xpos+ypos*numx]; ayy = alphayy[xpos+ypos*numx]; axy = alphaxy[xpos+ypos*numx]; }
69 
70  Vector2D<double> getIndexCoordinate(int xpos,int ypos) const;
71  bool getIndices(Vector2D<double> v,int *xpos,int *ypos) const;
72 
73  bool write(serut::SerializationInterface &si) const;
74  static bool read(serut::SerializationInterface &si,LensPlane **ip,std::string &errstr);
75  static bool load(const std::string &fname,LensPlane **ip,std::string &errstr);
76  bool save(const std::string &fname) const;
77 
78  const std::vector<Vector2D<double> > &getAlphas() const { return alphas; }
79  const std::vector<double> &getAlphaXXs() const { return alphaxx; }
80  const std::vector<double> &getAlphaXYs() const { return alphaxy; }
81  const std::vector<double> &getAlphaYYs() const { return alphayy; }
82 protected:
83  virtual void setFeedbackStatus(const std::string &msg) { }
84  virtual void setFeedbackPercentage(int pct) { }
85 private:
86  LensPlane() { }
87  static double getTime();
88 
89  bool initUsingGPU(OpenCLKernel *pCLKernel, bool lilePerLine, int numSubLenses, bool &renderError);
90  bool renderRemote(const nut::NetworkLayerAddress &serverAddress, uint16_t serverport);
91 
92  GravitationalLens *m_pLens;
93 
94  std::vector<Vector2D<double> > alphas;
95  std::vector<double> alphaxx, alphayy, alphaxy;
96 
97  Vector2D<double> bottomleft;
98  Vector2D<double> topright;
99  double xstep,ystep;
100  int numx,numy;
101  bool m_init;
102 };
103 
104 inline Vector2D<double> LensPlane::getIndexCoordinate(int xpos,int ypos) const
105 {
106  return Vector2D<double>((((double)xpos))*xstep+bottomleft.getX(),(((double)ypos))*ystep+bottomleft.getY());
107 }
108 
109 inline bool LensPlane::getIndices(Vector2D<double> v,int *xpos,int *ypos) const
110 {
111  int x,y;
112 
113  x = (int)(((v.getX()-bottomleft.getX())/xstep)+0.5);
114  y = (int)(((v.getY()-bottomleft.getY())/ystep)+0.5);
115 
116  if (x < 0 || y < 0 || x >= numx || y >= numy)
117  return false;
118  *xpos = x;
119  *ypos = y;
120  return true;
121 }
122 
123 } // end namespace
124 
125 #endif // GRALE_LENSPLANE_H
126