GRALE
sourceimage.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_SOURCEIMAGE_H
27 
28 #define GRALE_SOURCEIMAGE_H
29 
30 #include "graleconfig.h"
31 #include "sourceplane.h"
32 #include "constants.h"
33 #include <string>
34 
35 namespace grale
36 {
37 
38 class GRALE_IMPORTEXPORT SourceImage : public errut::ErrorBase
39 {
40 public:
41  enum SourceType { Circle, Ellipse, Polygon, Discrete, Point };
42 protected:
43  // angle is specified in degrees
44  SourceImage(SourceType t, Vector2D<double> angularpos, double angle, double brightnessScale);
45 public:
46  virtual ~SourceImage();
47 
48  SourceType getSourceType() const { return stype; }
49 
50  // creates an unattached copy
51  virtual SourceImage *createCopy() const = 0;
52  virtual bool isSerializable() const { return false; }
53 
54  bool attach(SourcePlane *parentplane, uint16_t srcid);
55  bool release();
56  SourcePlane *getParentPlane() { return parentplane; }
57  uint64_t getID() const { return id; }
58 
59  double getIntensity(Vector2D<double> beta) const;
60  bool isSourceInRange(Vector2D<double> beta, double radius) const;
61 
62  double getD_s() const { return parentplane->getD_s(); }
63  double getD_ds() const { return parentplane->getD_ds(); }
64  Vector2D<double> getAngularPosition() const { return pos; }
65  void addToAngularPosition(Vector2D<double> p) { pos += p; }
66  void setAngularPosition(Vector2D<double> p) { pos = p; }
67  void addToAngle(double ang) { angle += ang; theta = (angle/180.0)*CONST_PI; }
68  void setAngle(double ang) { angle = ang; theta = (angle/180.0)*CONST_PI; }
69  double getAngle() const { return angle; }
70 
71  bool write(serut::SerializationInterface &si) const;
72  static bool read(serut::SerializationInterface &si,SourceImage **source,std::string &errstr);
73 protected:
74  double getBrightnessScale() const { return m_brightnessScale; }
75 
76  virtual bool writeInternal(serut::SerializationInterface &si) const;
77  virtual bool readInternal(serut::SerializationInterface &si);
78  virtual double getIntensityInternal(Vector2D<double> diff) const { return 0; }
79  virtual double getMaxRadius() const { return 0; }
80 private:
81  Vector2D<double> pos;
82  double angle,theta;
83  SourcePlane *parentplane;
84  SourceType stype;
85  uint16_t id;
86  double m_brightnessScale;
87 
88  friend class SourcePlane;
89 };
90 
91 inline double SourceImage::getIntensity(Vector2D<double> beta) const
92 {
93  Vector2D<double> diff = beta - pos;
94  Vector2D<double> diff2(diff.getX()*COS(theta) + diff.getY()*SIN(theta),
95  -diff.getX()*SIN(theta) + diff.getY()*COS(theta));
96 
97  return m_brightnessScale*getIntensityInternal(diff2);
98 }
99 
100 inline bool SourceImage::isSourceInRange(Vector2D<double> beta, double radius) const
101 {
102  Vector2D<double> diff = beta - pos;
103  Vector2D<double> diff2(diff.getX()*COS(theta) + diff.getY()*SIN(theta),
104  -diff.getX()*SIN(theta) + diff.getY()*COS(theta));
105 
106  double d = diff2.getLength() - radius;
107 
108  if (d < getMaxRadius())
109  return true;
110  return false;
111 }
112 
113 } // end namespace
114 
115 #endif // GRALE_SOURCEIMAGE_H
116