WorldDeviceTransformer.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/maptools/WorldDeviceTransformer.h
22 
23  \brief This class implements the logic for transforming from device coordinate to world coordinate and vice-versa.
24  */
25 
26 #ifndef __TERRALIB_MAPTOOLS_INTERNAL_WORLDDEVICETRANSFORMER_H
27 #define __TERRALIB_MAPTOOLS_INTERNAL_WORLDDEVICETRANSFORMER_H
28 
29 namespace te
30 {
31  namespace map
32  {
33  /*!
34  \class WorldDeviceTransformer
35 
36  \brief This class implements the logic for transforming from device coordinate to world coordinate and vice-versa.
37 
38  \ingroup map
39 
40  \sa Canvas
41  */
43  {
44  public:
45 
46  /** @name Initializer Methods
47  * Methods related to instantiation and destruction.
48  */
49  //@{
50 
51  /*! \brief Empty constructor */
53 
54  /*!
55  \brief Creates a new transformation functor that maps between the
56  spatial coordinate system of a feature to device (canvas) coordinate system.
57 
58  \param wllx Lower left x-coordinate of the World (Spatial Coordinate System of the features).
59  \param wlly Lower left y-coordinate of the World (Spatial Coordinate System of the features).
60  \param wurx Upper right x-coordinate of the World (Spatial Coordinate System of the features).
61  \param wury Upper right y-coordinate of the World (Spatial Coordinate System of the features).
62  \param deviceWidth Device (canvas) width in pixels.
63  \param deviceHeight Device (canvas) height in pixels.
64  */
65  WorldDeviceTransformer(const double& wllx, const double& wlly,
66  const double& wurx, const double& wury,
67  int deviceWidth, int deviceHeight);
68 
69  /*! \brief Destructor */
71 
72  //@}
73 
74  /** @name Transformation Methods
75  * Methods used to transform coordinates from world (feature coordinate system) to device (canvas) and vice-versa.
76  */
77  //@{
78 
79  /*!
80  \brief It adjusts a new transformation function that maps between the spatial coordinate system of a feature to
81  device (canvas) coordinate system.
82 
83  \param wllx Lower left x-coordinate of the World (Spatial Coordinate System of the features).
84  \param wlly Lower left y-coordinate of the World (Spatial Coordinate System of the features).
85  \param wurx Upper right x-coordinate of the World (Spatial Coordinate System of the features).
86  \param wury Upper right y-coordinate of the World (Spatial Coordinate System of the features).
87  \param deviceWidth Device (canvas) width in pixels.
88  \param deviceHeight Device (canvas) height in pixels.
89  */
90  void setTransformationParameters(const double& wllx, const double& wlly,
91  const double& wurx, const double& wury,
92  int deviceWidth, int deviceHeight);
93 
94  /*!
95  \brief It transforms the coordinate wx and wy from world coordinates to
96  device (canvas) coordinates without using auxiliary variables. It
97  is supposed to be faster than the other version that has 4 parameters.
98 
99  \param wx X value in world coordinates.
100  \param wy Y value in world coordinates.
101  */
102  void world2Device(double& wx, double& wy) const;
103 
104  /*!
105  \brief It transforms the line coordinates from world coordinates to device (canvas) coordinates.
106 
107  \param line Must be a buffer of double, with npoints.
108  \param npoints The number of points in the line.
109  \param pts The device coordinate buffer.
110 
111  \note We must have buffer aligned as: buffer[0] -> x0, buffer[1] -> y0, buffer[2] -> x1, buffer[3] -> y1 and so on.
112  */
113  void world2Device(double* line, unsigned int npoints, double* pts);
114 
115  /*!
116  \brief It transforms the coordinate wx and wy from world coordinates to device (canvas) coordinates (dx and dy).
117 
118  \param wx X value in world coordinates.
119  \param wy Y value in world coordinates.
120  \param dx X value in device coordinates.
121  \param dy Y value in device coordinates.
122  */
123  void world2Device(const double& wx, const double& wy, double& dx, double& dy) const;
124 
125  /*!
126  \brief It transforms the coordinate dx and dy from device coordinates to
127  world coordinates without using auxiliary variables. It
128  is supposed to be faster than the other version that has 4 parameters.
129 
130  \param dx X value in device coordinates.
131  \param dy Y value in device coordinates.
132  */
133  void device2World(double& dx, double& dy) const;
134 
135  /*!
136  \brief It transforms the coordinate dx and dy from device (canvas) coordinates to world coordinates (wx and wy).
137 
138  \param dx X value in device coordinates.
139  \param dy Y value in device coordinates.
140  \param wx X value in world coordinates.
141  \param wy Y value in world coordinates.
142  */
143  void device2World(int dx, int dy, double& wx, double& wy) const;
144 
145  //@}
146 
147  public:
148 
149  double m_mapUnitsPP; //!< Map units per-pixel.
150  double m_wllx; //!< Lower left x-coordinate of the World (Spatial Coordinate System of the features).
151  double m_wlly; //!< Lower left y-coordinate of the World (Spatial Coordinate System of the features).
152  double m_wurx; //!< Upper right x-coordinate of the World (Spatial Coordinate System of the features).
153  double m_wury; //!< Upper right y-coordinate of the World (Spatial Coordinate System of the features).
154  };
155 
157  : m_mapUnitsPP(0.0),
158  m_wllx(0.0),
159  m_wlly(0.0),
160  m_wurx(0.0),
161  m_wury(0.0)
162  {
163  }
164 
165  inline WorldDeviceTransformer::WorldDeviceTransformer(const double& wllx, const double& wlly,
166  const double& wurx, const double& wury,
167  int deviceWidth, int deviceHeight)
168  {
169  setTransformationParameters(wllx, wlly, wurx, wury, deviceWidth, deviceHeight);
170  }
171 
173  {
174  }
175 
176  inline void WorldDeviceTransformer::setTransformationParameters(const double& wllx, const double& wlly,
177  const double& wurx, const double& wury,
178  int deviceWidth, int deviceHeight)
179  {
180  double worldWidth = wurx - wllx;
181  double worldHeight = wury - wlly;
182 
183  double muppX = worldWidth / static_cast<double>(deviceWidth); // map unit per pixel along x-direction
184  double muppY = worldHeight / static_cast<double>(deviceHeight); // map unit per pixel along y-direction
185 
186  if(muppY > muppX)
187  {
188  m_mapUnitsPP = muppY;
189  m_wlly = wlly;
190  m_wury = wury;
191  double whitespace = ((static_cast<double>(deviceWidth) * m_mapUnitsPP) - worldWidth) * 0.5;
192  m_wllx = wllx - whitespace;
193  m_wurx = wurx + whitespace;
194  }
195  else
196  {
197  m_mapUnitsPP = muppX;
198  m_wllx = wllx;
199  m_wurx = wurx;
200  double whitespace = ((static_cast<double>(deviceHeight) * m_mapUnitsPP) - worldHeight) * 0.5;
201  m_wlly = wlly - whitespace;
202  m_wury = wury + whitespace;
203  }
204  }
205 
206 
207  inline void WorldDeviceTransformer::world2Device(double& wx, double& wy) const
208  {
209  wx = (wx - m_wllx) / m_mapUnitsPP;
210  wy = (m_wury - wy) / m_mapUnitsPP;
211  }
212 
213  inline void WorldDeviceTransformer::world2Device(double* line, unsigned int npoints, double* pts)
214  {
215  const unsigned int nstep = 2 * npoints;
216 
217  for(register unsigned int i = 0; i != nstep; i+=2)
218  {
219  pts[i] = (line[i] - m_wllx) / m_mapUnitsPP;
220  pts[i + 1] = (m_wury - line[i + 1]) / m_mapUnitsPP;
221  }
222  }
223 
224  inline void WorldDeviceTransformer::world2Device(const double& wx, const double& wy,
225  double& dx, double& dy) const
226  {
227  dx = (wx - m_wllx) / m_mapUnitsPP;
228  dy = (m_wury - wy) / m_mapUnitsPP;
229  }
230 
231  inline void WorldDeviceTransformer::device2World(double& dx, double& dy) const
232  {
233  dx = dx * m_mapUnitsPP + m_wllx;
234  dy = m_wury - dy * m_mapUnitsPP;
235  }
236 
237  inline void WorldDeviceTransformer::device2World(int dx, int dy,
238  double& wx, double& wy) const
239  {
240  wx = ((double)(dx)) * m_mapUnitsPP + m_wllx;
241  wy = m_wury - ((double)(dy)) * m_mapUnitsPP;
242  }
243 
244  } // end namespace map
245 } // end namespace te
246 
247 #endif // __TERRALIB_MAPTOOLS_INTERNAL_WORLDDEVICETRANSFORMER_H
double m_mapUnitsPP
Map units per-pixel.
double m_wury
Upper right y-coordinate of the World (Spatial Coordinate System of the features).
void setTransformationParameters(const double &wllx, const double &wlly, const double &wurx, const double &wury, int deviceWidth, int deviceHeight)
It adjusts a new transformation function that maps between the spatial coordinate system of a feature...
This class implements the logic for transforming from device coordinate to world coordinate and vice-...
double m_wllx
Lower left x-coordinate of the World (Spatial Coordinate System of the features). ...
URI C++ Library.
void device2World(double &dx, double &dy) const
It transforms the coordinate dx and dy from device coordinates to world coordinates without using aux...
double m_wurx
Upper right x-coordinate of the World (Spatial Coordinate System of the features).
void world2Device(double &wx, double &wy) const
It transforms the coordinate wx and wy from world coordinates to device (canvas) coordinates without ...
double m_wlly
Lower left y-coordinate of the World (Spatial Coordinate System of the features). ...