GRALE
imagesdata.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_IMAGESDATA_H
27 
28 #define GRALE_IMAGESDATA_H
29 
30 #include "graleconfig.h"
31 #include "vector2d.h"
32 #include "triangle2d.h"
33 #include "triangulation.h"
34 #include <serut/serializationinterface.h>
35 #include <sys/types.h>
36 #include <stdint.h>
37 #include <string>
38 #include <vector>
39 #include <list>
40 
41 namespace grale
42 {
43 
44 class CoordinateInterface;
45 
46 template<class T> class Polygon2D;
47 
48 class GRALE_IMPORTEXPORT ImagesData : public errut::ErrorBase
49 {
50 public:
51  ImagesData();
52  ImagesData(const ImagesData &data) { copyFrom(data); }
53  virtual ~ImagesData();
54  ImagesData *createCopy() const;
55 
56  bool createGridImage(Vector2D<double> bottomleft, Vector2D<double> topright, int numx, int numy,
57  const std::list<Triangle2D<double> > &excluderegions, bool triangulate = true,
58  bool useIntens = false);
59  bool createGridImage(Vector2D<double> bottomleft, Vector2D<double> topright, int numx, int numy,
60  const std::list<ImagesData *> &excludeImages, bool useIntens = false,
61  bool storeMinimalDistances = false);
62 
63  bool create(int numImages, bool intensities, bool shearInfo);
64 
65  bool addGridImage(Vector2D<double> bottomleft, Vector2D<double> topright, int numx, int numy,
66  const std::list<Triangle2D<double> > &excluderegions, bool triangulate = true);
67 
68  int addImage();
69  int addPoint(int imageNumber, Vector2D<double> point); // returns index of the point, -1 on error
70  int addPoint(int imageNumber, Vector2D<double> point, double intensity); // returns index of the point, -1 on error
71  int addPoint(int imageNumber, Vector2D<double> point, double shearComponent1, double shearComponent2);
72  int addPoint(int imageNumber, Vector2D<double> point, double intensity, double shearComponent1, double shearComponent2);
73  bool setImagePointIntensity(int imageNumber, int pointNumber, double intensity);
74 
75  int addGroup(); // returns the index of the group, -1 on error
76  bool addGroupPoint(int groupNumber, int imageIndex, int pointIndex);
77  bool addTimeDelayInfo(int imageIndex, int pointIndex, double timeDelay); // timeDelay in days
78  bool addTriangle(int imageNumber, int index1, int index2, int index3);
79 
80  bool load(const std::string &fname);
81  bool save(const std::string &fname) const;
82  bool read(serut::SerializationInterface &si);
83  bool write(serut::SerializationInterface &si) const;
84  bool loadPNG(const std::string &fname, Vector2D<double> bottomleft, Vector2D<double> topright,
85  bool nullspace = false, const std::string &maskfile = std::string(""), bool triangulate = true);
86  bool loadFITS(const std::string &fname, bool nullspace = false,
87  const std::string &maskfile = std::string(""), bool triangulate = true);
88  bool loadFITS(const std::string &fname, double unit, bool nullspace = false,
89  const std::string &maskfile = std::string(""), bool triangulate = true);
90  bool loadFITS(const std::string &fname, Vector2D<double> bottomleft, Vector2D<double> topright,
91  bool nullspace = false, const std::string &maskfile = std::string(""), bool triangulate = true);
92 
93  bool hasIntensities() const { if (m_intensities.empty()) return false; return true; }
94  bool hasShearInfo() const { if (m_shearComponent1s.empty()) return false; return true; }
95  bool hasTimeDelays() const { if (m_timeDelayInfo.empty()) return false; return true; }
96 
97  int getNumberOfImages() const { return m_images.size(); }
98  int getNumberOfImagePoints(int i) const { return m_images[i].size(); }
99  Vector2D<double> getImagePointPosition(int image, int point) const { return m_images[image][point]; }
100  double getImagePointIntensity(int image, int point) const { return m_intensities[image][point]; }
101  double getShearComponent1(int image, int point) const { return m_shearComponent1s[image][point]; }
102  double getShearComponent2(int image, int point) const { return m_shearComponent2s[image][point]; }
103 
104  bool createTriangulation();
105  bool clearTriangulation();
106  bool hasTriangulation() const { if (m_triangulations.size() != 0) return true; return false; }
107  bool getTriangles(int image, std::list<TriangleIndices> &triangles) const;
108  bool getImageBorder(int image, Polygon2D<double> &polygon) const;
109 
110  int getNumberOfGroups() const { return m_groupPoints.size(); }
111  int getNumberOfGroupPoints(int group) const { return m_groupPoints[group].size()/2; }
112  void getGroupPointIndices(int group, int pointnr, int *img, int *point) const { *img = m_groupPoints[group][pointnr*2]; *point = m_groupPoints[group][pointnr*2+1]; }
113  void clearGroupInfo() { m_groupPoints.clear(); }
114 
115  int getNumberOfTimeDelays() const { return m_timeDelayInfo.size(); }
116  void getTimeDelay(int index, int *pImg, int *pPoint, double *pDelay) const { *pImg = m_timeDelayInfo[index].getImageIndex(); *pPoint = m_timeDelayInfo[index].getPointIndex(); *pDelay = m_timeDelayInfo[index].getTimeDelay(); }
117  void clearTimeDelayInfo() { m_timeDelayInfo.clear(); }
118 
119  Vector2D<double> getTopRightCorner() const { return m_topRight; }
120  Vector2D<double> getBottomLeftCorner() const { return m_bottomLeft; }
121 
122  void subtractPosition(Vector2D<double> pos);
123  void subtractIntensity(double v);
124 
125  const ImagesData &operator=(const ImagesData &dat) { clear(); copyFrom(dat); return *this; }
126 private:
127  void clear();
128  void findExtremes();
129  void copyFrom(const ImagesData &data);
130  bool processData(double *data, CoordinateInterface &coordcalc, int numx, int numy, bool intens, bool nullspace, const std::string &maskfile, bool triangulate);
131  bool createMap(double *values, int numx, int numy, const std::string &maskfile, std::vector<uint16_t> &map);
132  static int processTrianglePoint(Vector2D<double> p, std::vector<Vector2D<double> > &points);
133  static int processTrianglePoint(Vector2D<double> p, std::vector<Vector2D<double> > &points, std::vector<double> &intensities);
134  static void createLineSegments(const std::list<IntVector2DPlus> &points, std::list<IntLine2D> &lines);
135  static void addLineSegment(std::list<IntLine2D> &lines, IntLine2D l);
136  static void createTriangulation(const std::list<IntLine2D> &lines, std::list<TriangleIndices> &triangles, int numx);
137 
138  class TimeDelayPoint
139  {
140  public:
141  TimeDelayPoint(int imageIndex, int pointIndex, double timeDelay)
142  {
143  m_imageIndex = imageIndex;
144  m_pointIndex = pointIndex;
145  m_timeDelay = timeDelay;
146  }
147 
148  int getImageIndex() const { return m_imageIndex; }
149  int getPointIndex() const { return m_pointIndex; }
150  double getTimeDelay() const { return m_timeDelay; }
151  private:
152  int m_imageIndex;
153  int m_pointIndex;
154  double m_timeDelay;
155  };
156 
157  std::vector<std::vector<Vector2D<double> > > m_images;
158  std::vector<std::vector<double> > m_intensities;
159  std::vector<std::vector<double> > m_shearComponent1s;
160  std::vector<std::vector<double> > m_shearComponent2s;
161  std::vector<std::vector<int32_t> > m_groupPoints;
162  std::vector<std::list<TriangleIndices> > m_triangulations;
163  std::vector<TimeDelayPoint> m_timeDelayInfo;
164  Vector2D<double> m_bottomLeft;
165  Vector2D<double> m_topRight;
166 };
167 
168 class ImagesDataExtended : public ImagesData
169 {
170 public:
171  ImagesDataExtended() { D_s = 0; D_ds = 0; }
172  ImagesDataExtended(const ImagesDataExtended &dat) : ImagesData(dat) { D_s = dat.D_s; D_ds = dat.D_ds; std::list<double>::const_iterator it; for (it = dat.extraparameters.begin() ; it != dat.extraparameters.end() ; it++) extraparameters.push_back(*it); }
173  ImagesDataExtended(double D_s,double D_ds) { ImagesDataExtended::D_s = D_s; ImagesDataExtended::D_ds = D_ds; }
174  ~ImagesDataExtended() { }
175  double getD_s() const { return D_s; }
176  double getD_ds() const { return D_ds; }
177  void setD_s(double v) { D_s = v; }
178  void setD_ds(double v) { D_ds = v; }
179 
180  bool read(serut::SerializationInterface &si);
181  bool write(serut::SerializationInterface &si) const;
182 
183  const ImagesDataExtended &operator=(const ImagesDataExtended &dat) { D_s = dat.D_s; D_ds = dat.D_ds; ImagesData::operator=(dat); }
184 
185  void clearExtraParameters() { extraparameters.clear(); }
186  void addExtraParameter(double val) { extraparameters.push_back(val); }
187  int getNumberOfExtraParameters() const { return extraparameters.size(); }
188  bool getExtraParameter(int idx, double *val) const { if (idx < 0 || idx >= extraparameters.size()) { setErrorString("Index out of bounds"); return false; } std::list<double>::const_iterator it = extraparameters.begin(); while (idx > 0) { it++; idx--; } *val = *it; return true; }
189 private:
190  double D_s,D_ds;
191  std::list<double> extraparameters;
192 };
193 
194 } // end namespace
195 
196 #endif // GRALE_IMAGESDATA_H
197