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 #include "../common/MathUtils.h"
30 
31 namespace te
32 {
33  namespace map
34  {
35  /*!
36  \class WorldDeviceTransformer
37 
38  \brief This class implements the logic for transforming from device coordinate to world coordinate and vice-versa.
39 
40  \ingroup map
41 
42  \sa Canvas
43  */
45  {
46  public:
47 
48  /** @name Initializer Methods
49  * Methods related to instantiation and destruction.
50  */
51  //@{
52 
53  /*! \brief Empty constructor */
55 
56  /*!
57  \brief Creates a new transformation functor that maps between the
58  spatial coordinate system of a feature to device (canvas) coordinate system.
59 
60  \param wllx Lower left x-coordinate of the World (Spatial Coordinate System of the features).
61  \param wlly Lower left y-coordinate of the World (Spatial Coordinate System of the features).
62  \param wurx Upper right x-coordinate of the World (Spatial Coordinate System of the features).
63  \param wury Upper right y-coordinate of the World (Spatial Coordinate System of the features).
64  \param deviceWidth Device (canvas) width in pixels.
65  \param deviceHeight Device (canvas) height in pixels.
66  */
67  WorldDeviceTransformer(const double& wllx, const double& wlly,
68  const double& wurx, const double& wury,
69  int deviceWidth, int deviceHeight);
70 
71  /*! \brief Destructor */
73 
74  //@}
75 
76  /** @name Transformation Methods
77  * Methods used to transform coordinates from world (feature coordinate system) to device (canvas) and vice-versa.
78  */
79  //@{
80 
81  /*!
82  \brief It adjusts a new transformation function that maps between the spatial coordinate system of a feature to
83  device (canvas) coordinate system.
84 
85  \param wllx Lower left x-coordinate of the World (Spatial Coordinate System of the features).
86  \param wlly Lower left y-coordinate of the World (Spatial Coordinate System of the features).
87  \param wurx Upper right x-coordinate of the World (Spatial Coordinate System of the features).
88  \param wury Upper right y-coordinate of the World (Spatial Coordinate System of the features).
89  \param deviceWidth Device (canvas) width in pixels.
90  \param deviceHeight Device (canvas) height in pixels.
91  */
92  void setTransformationParameters(const double& wllx, const double& wlly,
93  const double& wurx, const double& wury,
94  int deviceWidth, int deviceHeight);
95 
96  /*!
97  \brief It transforms the coordinate wx and wy from world coordinates to
98  device (canvas) coordinates without using auxiliary variables. It
99  is supposed to be faster than the other version that has 4 parameters.
100 
101  \param wx X value in world coordinates.
102  \param wy Y value in world coordinates.
103  */
104  void world2Device(double& wx, double& wy) const;
105 
106  /*!
107  \brief It transforms the line coordinates from world coordinates to device (canvas) coordinates.
108 
109  \param line Must be a buffer of double, with npoints.
110  \param npoints The number of points in the line.
111  \param pts The device coordinate buffer.
112 
113  \note We must have buffer aligned as: buffer[0] -> x0, buffer[1] -> y0, buffer[2] -> x1, buffer[3] -> y1 and so on.
114  */
115  void world2Device(double* line, unsigned int npoints, double* pts);
116 
117  /*!
118  \brief It transforms the coordinate wx and wy from world coordinates to device (canvas) coordinates (dx and dy).
119 
120  \param wx X value in world coordinates.
121  \param wy Y value in world coordinates.
122  \param dx X value in device coordinates.
123  \param dy Y value in device coordinates.
124  */
125  void world2Device(const double& wx, const double& wy, double& dx, double& dy) const;
126 
127  /*!
128  \brief It transforms the coordinate dx and dy from device coordinates to
129  world coordinates without using auxiliary variables. It
130  is supposed to be faster than the other version that has 4 parameters.
131 
132  \param dx X value in device coordinates.
133  \param dy Y value in device coordinates.
134  */
135  void device2World(double& dx, double& dy) const;
136 
137  /*!
138  \brief It transforms the coordinate dx and dy from device (canvas) coordinates to world coordinates (wx and wy).
139 
140  \param dx X value in device coordinates.
141  \param dy Y value in device coordinates.
142  \param wx X value in world coordinates.
143  \param wy Y value in world coordinates.
144  */
145  void device2World(int dx, int dy, double& wx, double& wy) const;
146 
147  /*!
148  \brief It transforms a delta value (pixels) from device to world value (value in current unit).
149 
150  \param delta A value in pixel unit from devide
151  \return Double value in current unit from canvas projection.
152  */
153  double device2World(int delta);
154 
155  /*!
156  \brief It transforms a world delta value (value in current unit) to a pixel unit.
157 
158  \param delta Value in current unit from canvas projection
159  \return Integer value in pixel unit from devide
160  */
161  int world2Device(double delta);
162 
163  //@}
164 
165  public:
166 
167  double m_mapUnitsPP; //!< Map units per-pixel.
168  double m_wllx; //!< Lower left x-coordinate of the World (Spatial Coordinate System of the features).
169  double m_wlly; //!< Lower left y-coordinate of the World (Spatial Coordinate System of the features).
170  double m_wurx; //!< Upper right x-coordinate of the World (Spatial Coordinate System of the features).
171  double m_wury; //!< Upper right y-coordinate of the World (Spatial Coordinate System of the features).
172  };
173 
175  : m_mapUnitsPP(0.0),
176  m_wllx(0.0),
177  m_wlly(0.0),
178  m_wurx(0.0),
179  m_wury(0.0)
180  {
181  }
182 
183  inline WorldDeviceTransformer::WorldDeviceTransformer(const double& wllx, const double& wlly,
184  const double& wurx, const double& wury,
185  int deviceWidth, int deviceHeight)
186  {
187  setTransformationParameters(wllx, wlly, wurx, wury, deviceWidth, deviceHeight);
188  }
189 
191  {
192  }
193 
194  inline void WorldDeviceTransformer::setTransformationParameters(const double& wllx, const double& wlly,
195  const double& wurx, const double& wury,
196  int deviceWidth, int deviceHeight)
197  {
198  double worldWidth = wurx - wllx;
199  double worldHeight = wury - wlly;
200 
201  double muppX = worldWidth / static_cast<double>(deviceWidth); // map unit per pixel along x-direction
202  double muppY = worldHeight / static_cast<double>(deviceHeight); // map unit per pixel along y-direction
203 
204  if(muppY > muppX)
205  {
206  m_mapUnitsPP = muppY;
207  m_wlly = wlly;
208  m_wury = wury;
209  double whitespace = ((static_cast<double>(deviceWidth) * m_mapUnitsPP) - worldWidth) * 0.5;
210  m_wllx = wllx - whitespace;
211  m_wurx = wurx + whitespace;
212  }
213  else
214  {
215  m_mapUnitsPP = muppX;
216  m_wllx = wllx;
217  m_wurx = wurx;
218  double whitespace = ((static_cast<double>(deviceHeight) * m_mapUnitsPP) - worldHeight) * 0.5;
219  m_wlly = wlly - whitespace;
220  m_wury = wury + whitespace;
221  }
222  }
223 
224 
225  inline void WorldDeviceTransformer::world2Device(double& wx, double& wy) const
226  {
227  wx = (wx - m_wllx) / m_mapUnitsPP;
228  wy = (m_wury - wy) / m_mapUnitsPP;
229  }
230 
231  inline void WorldDeviceTransformer::world2Device(double* line, unsigned int npoints, double* pts)
232  {
233  const unsigned int nstep = 2 * npoints;
234 
235  for(unsigned int i = 0; i != nstep; i+=2)
236  {
237  pts[i] = (line[i] - m_wllx) / m_mapUnitsPP;
238  pts[i + 1] = (m_wury - line[i + 1]) / m_mapUnitsPP;
239  }
240  }
241 
242  inline void WorldDeviceTransformer::world2Device(const double& wx, const double& wy,
243  double& dx, double& dy) const
244  {
245  dx = (wx - m_wllx) / m_mapUnitsPP;
246  dy = (m_wury - wy) / m_mapUnitsPP;
247  }
248 
249  inline void WorldDeviceTransformer::device2World(double& dx, double& dy) const
250  {
251  dx = dx * m_mapUnitsPP + m_wllx;
252  dy = m_wury - dy * m_mapUnitsPP;
253  }
254 
255  inline void WorldDeviceTransformer::device2World(int dx, int dy,
256  double& wx, double& wy) const
257  {
258  wx = ((double)(dx)) * m_mapUnitsPP + m_wllx;
259  wy = m_wury - ((double)(dy)) * m_mapUnitsPP;
260  }
261 
262  inline double WorldDeviceTransformer::device2World(int delta)
263  {
264  return static_cast<double>(delta) * m_mapUnitsPP;
265  }
266 
267  inline int WorldDeviceTransformer::world2Device(double delta)
268  {
269  return te::common::Round< double, int >(delta / m_mapUnitsPP);
270  }
271 
272  } // end namespace map
273 } // end namespace te
274 
275 #endif // __TERRALIB_MAPTOOLS_INTERNAL_WORLDDEVICETRANSFORMER_H
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::map::WorldDeviceTransformer::WorldDeviceTransformer
WorldDeviceTransformer()
Empty constructor.
Definition: WorldDeviceTransformer.h:174
te::map::WorldDeviceTransformer::m_wury
double m_wury
Upper right y-coordinate of the World (Spatial Coordinate System of the features).
Definition: WorldDeviceTransformer.h:171
te::map::WorldDeviceTransformer::world2Device
void world2Device(double &wx, double &wy) const
It transforms the coordinate wx and wy from world coordinates to device (canvas) coordinates without ...
Definition: WorldDeviceTransformer.h:225
te::map::WorldDeviceTransformer::setTransformationParameters
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...
Definition: WorldDeviceTransformer.h:194
te::map::WorldDeviceTransformer::m_wllx
double m_wllx
Lower left x-coordinate of the World (Spatial Coordinate System of the features).
Definition: WorldDeviceTransformer.h:168
te::map::WorldDeviceTransformer::m_wurx
double m_wurx
Upper right x-coordinate of the World (Spatial Coordinate System of the features).
Definition: WorldDeviceTransformer.h:170
te::map::WorldDeviceTransformer::m_wlly
double m_wlly
Lower left y-coordinate of the World (Spatial Coordinate System of the features).
Definition: WorldDeviceTransformer.h:169
te::map::WorldDeviceTransformer
This class implements the logic for transforming from device coordinate to world coordinate and vice-...
Definition: WorldDeviceTransformer.h:45
te::map::WorldDeviceTransformer::~WorldDeviceTransformer
~WorldDeviceTransformer()
Destructor.
Definition: WorldDeviceTransformer.h:190
te::map::WorldDeviceTransformer::device2World
void device2World(double &dx, double &dy) const
It transforms the coordinate dx and dy from device coordinates to world coordinates without using aux...
Definition: WorldDeviceTransformer.h:249
te::map::WorldDeviceTransformer::m_mapUnitsPP
double m_mapUnitsPP
Map units per-pixel.
Definition: WorldDeviceTransformer.h:167