GRALE
grid.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_GRID_H
27 
28 #define GRALE_GRID_H
29 
30 #include "graleconfig.h"
32 #include <errut/errorbase.h>
33 #include <list>
34 
35 namespace grale
36 {
37 
38 class GridSquare
39 {
40 public:
41  GridSquare(Vector2D<double> center, double size) { m_center = center; m_size = size; }
42  ~GridSquare() { }
43  double getSize() const { return m_size; }
44  Vector2D<double> getCenter() const { return m_center; }
45  Vector2D<double> getBottomLeft() const { return Vector2D<double>(m_center.getX()-m_size/2.0,m_center.getY()-m_size/2.0); }
46  Vector2D<double> getTopRight() const { return Vector2D<double>(m_center.getX()+m_size/2.0,m_center.getY()+m_size/2.0); }
47 private:
48  Vector2D<double> m_center;
49  double m_size;
50 };
51 
52 class GRALE_IMPORTEXPORT Grid : public errut::ErrorBase
53 {
54 public:
55  Grid();
56  ~Grid();
57 
58  void getSquares(std::list<GridSquare> &gridSquares) const;
59 
60  bool buildUniform(Vector2D<double> center, double size, int subdiv);
61  bool buildDensity(Real2DFunction &f, Vector2D<double> center, double size, double subdivfraction,
62  int xdiv, int ydiv, bool abs, bool keeplarger);
63  bool buildDensity(Real2DFunction &f, Vector2D<double> center, double size, int minsquares, int maxsquares,
64  int xdiv, int ydiv, bool abs, bool keeplarger);
65  bool buildGradient(Real2DDerivableFunction &f, Vector2D<double> center, double size, double subdivfraction,
66  int xdiv, int ydiv, bool keeplarger);
67  bool buildDensityAndGradient(Real2DDerivableFunction &f, Vector2D<double> center, double size,
68  double denssubdivfraction, double gradsubdivfraction,
69  int xdiv, int ydiv, bool abs, bool denskeeplarger,
70  bool gradkeeplarger);
71  bool buildDensityAndGradient(Real2DDerivableFunction &f, Vector2D<double> center, double size, int minsquares,
72  int maxsquares, int xdiv, int ydiv, bool abs, bool keeplarger);
73 private:
74  class RationalNumber
75  {
76  public:
77  RationalNumber(int num, int denom) { a = num; b = denom; }
78  ~RationalNumber() { }
79  int GetNumerator() const { return a; }
80  int GetDenominator() const { return b; }
81  double GetRealValue() const { return ((double)a)/((double)b); }
82  bool operator==(const RationalNumber &r) const { if (a == r.a && b == r.b) return true; return false; }
83  private:
84  int a, b;
85  };
86 
87  class RationalGridSquare
88  {
89  public:
90  RationalGridSquare(RationalNumber xc, RationalNumber yc, RationalNumber s) : xcenter(xc),ycenter(yc),sz(s)
91  { m_marked = false; }
92  RationalNumber GetCenterX() const { return xcenter; }
93  RationalNumber GetCenterY() const { return ycenter; }
94  RationalNumber GetSize() const { return sz; }
95  bool operator==(const RationalGridSquare &g) const { if (xcenter == g.xcenter && ycenter == g.ycenter && sz == g.sz) return true; return false; }
96 
97  bool isMarked() const { return m_marked; }
98  void setMarker() { m_marked = true; }
99  private:
100  RationalNumber xcenter,ycenter,sz;
101  bool m_marked;
102  };
103 
104  class GradientFunction : public Real2DFunction
105  {
106  public:
107  GradientFunction(Real2DDerivableFunction &g) : f(g) { }
108  ~GradientFunction() { }
109  double operator()(Vector2D<double> v) const { return f.getGradient(v).getLength(); }
110  private:
111  Real2DDerivableFunction &f;
112  };
113 
114  std::list<RationalGridSquare> grid;
115  Vector2D<double> gridcenter;
116  double gridsize;
117 };
118 
119 } // end namespace
120 
121 #endif // GRALE_GRID_H
122