GRALE
gridfunction.h
Go to the documentation of this file.
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 
30 #ifndef GRALE_GRIDFUNCTION_H
31 
32 #define GRALE_GRIDFUNCTION_H
33 
34 #include "graleconfig.h"
36 #include "triangle2d.h"
37 #include <list>
38 
39 namespace grale
40 {
41 
43 class GRALE_IMPORTEXPORT GridFunction : public Real2DDerivableFunction
44 {
45 public:
47  int numX = 512, int numY = 512, bool abs = true);
48  GridFunction(const double *pValues, Vector2D<double> bottomLeft, Vector2D<double> topRight,
49  int numX, int numY, bool deleteValues = true);
50  GridFunction(const std::list<Triangle2DPlus<double> > &triangles ,Vector2D<double> bottomLeft, Vector2D<double> topRight,
51  int numX = 512, int numY = 512, bool abs = true, bool fast = true, int resampleSize = 1);
52  ~GridFunction();
53 
54  const double *getValues() const { return m_pValues; }
55  int getWidth() const { return m_numX; }
56  int getHeight() const { return m_numY; }
57  Vector2D<double> getBottomLeft() const { return m_bottomLeft; }
58  Vector2D<double> getTopRight() const { return m_topRight; }
59  Vector2D<double> getPosition(int x, int y) const { return m_bottomLeft + Vector2D<double>(m_xStep*(double)x, m_yStep*(double)y); }
60  double getValue(int x, int y) const { return m_pValues[x+y*m_numX]; }
61  IntVector2D getPosition(Vector2D<double> v) const;
62 
63  void dump() const;
64 
65  double operator()(Vector2D<double> v) const;
66  Vector2D<double> getGradient(Vector2D<double> v) const;
67 
68 private:
69  class PixelValue
70  {
71  public:
72  PixelValue() { }
73  PixelValue(int x, int y, double value) { m_x = x; m_y = y; m_value = value; }
74  int getX() const { return m_x; }
75  int getY() const { return m_y; }
76  double getValue() const { return m_value; }
77  private:
78  int m_x, m_y;
79  double m_value;
80  };
81 
82  class MinMaxValue
83  {
84  public:
85  MinMaxValue() { m_set = false; }
86  void process(int x, double value)
87  {
88  if (!m_set)
89  {
90  m_set = true;
91  m_minX = x;
92  m_maxX = x;
93  m_minValue = value;
94  m_maxValue = value;
95  }
96  else
97  {
98  if (x < m_minX)
99  {
100  m_minX = x;
101  m_minValue = value;
102  }
103  else if (x > m_maxX)
104  {
105  m_maxX = x;
106  m_maxValue = value;
107  }
108  }
109  }
110  bool isSet() const { return m_set; }
111  int getMinX() const { return m_minX; }
112  int getMaxX() const { return m_maxX; }
113  double getMinValue() const { return m_minValue; }
114  double getMaxValue() const { return m_maxValue; }
115  private:
116  bool m_set;
117  int m_minX, m_maxX;
118  double m_minValue, m_maxValue;
119  };
120 
121  void drawTriangle(const Triangle2DPlus<double> &triangle, int *pPixelCount);
122  void drawIntTriangle(const int *pX, const int *pY, const double *pZ, int *pPixelCount);
123  void processLine(int x1, int y1, double z1, int x2, int y2, double z2, std::vector<PixelValue> &pixelValues);
124 
125  Vector2D<double> m_topRight, m_bottomLeft;
126  double *m_pValues;
127  double m_xStep, m_yStep;
128  int m_numX, m_numY;
129  bool m_deleteValues;
130 };
131 
132 inline IntVector2D GridFunction::getPosition(Vector2D<double> v) const
133 {
134  Vector2D<double> diff = v-m_bottomLeft;
135 
136  int xpos = (int)((diff.getX()/m_xStep)+0.5);
137  int ypos = (int)((diff.getY()/m_yStep)+0.5);
138 
139  return IntVector2D(xpos, ypos);
140 }
141 
142 } // end namespace
143 
144 #endif // GRALE_GRIDFUNCTION_H