All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
WorldTransformer.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/layout/WorldTransformer.h
22 
23  \brief This class implements the logic for transforming from System 1 coordinate to other type of coordinate system and vice-versa.
24  */
25 
26 #ifndef __TERRALIB_LAYOUT_INTERNAL_WORLDTRANSFORMER_H
27 #define __TERRALIB_LAYOUT_INTERNAL_WORLDTRANSFORMER_H
28 
29 // TerraLib
30 #include "../../geometry/Envelope.h"
31 
32 namespace te
33 {
34  namespace layout
35  {
36  /*!
37  \class WorldTransformer
38 
39  \brief This class implements the logic for transforming from System 1 coordinate to other type of coordinate system and vice-versa.
40 
41  \ingroup layout
42 
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 The world coordinates system will fit with 0,0 of another coordinate system (passed as width and height).
58  This method is indicated to create a World Transformer between a coordinate system and viewport (device, logical).
59 
60  \param s1llx Lower left x-coordinate of the World (Spatial Coordinate System of the features).
61  \param s1lly 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 s1ury Upper right y-coordinate of the World (Spatial Coordinate System of the features).
64  \param s2width *Can be device (canvas) width in pixels.
65  \param s2height *Can be device (canvas) height in pixels.
66  */
67  WorldTransformer(const double& s1llx, const double& s1lly,
68  const double& s1urx, const double& s1ury,
69  double s2width, double s2height);
70 
71  /*!
72  \brief The system 1 coordinates will fit with another coordinate system.
73  This method is indicated to create a World Transformer between a coordinate system and viewport (device, logical).
74 
75  \param system1Box Box in System 1 Coordinate System.
76  \param system2Box Box in System 2 Coordinate System.
77  */
78  WorldTransformer(te::gm::Envelope system1Box, te::gm::Envelope system2Box);
79 
80  /*! \brief Destructor */
82 
83  //@}
84 
85  /** @name Transformation Methods
86  * Methods used to transform coordinates from system 1 coordinate system to system 2 coordinate and vice-versa.
87  */
88  //@{
89 
90  /*!
91  \brief It adjusts a new transformation function that maps between the world coordinate system to
92  other coordinate system.
93 
94  \param system1Box Box in System 1 Coordinate System.
95  \param system2Box Box in System 2 Coordinate System.
96  */
98 
99  /*!
100  \brief It transforms the coordinate wx and wy from world coordinates to
101  other(system) coordinates without using auxiliary variables.
102 
103  \param wx X value in system 1 coordinates.
104  \param wy Y value in system 1 coordinates.
105  */
106  void system1Tosystem2(double& wx, double& wy) const;
107 
108  /*!
109  \brief It transforms the coordinate wx and wy from world coordinates to other(system) coordinates (dx and dy).
110 
111  \param wx X value in system 1 coordinates.
112  \param wy Y value in system 1 coordinates.
113  \param dx X value in system 2 coordinates.
114  \param dy Y value in system 2 coordinates.
115  */
116  void system1Tosystem2(const double& wx, const double& wy, double& dx, double& dy) const;
117 
118  /*!
119  \brief It transforms the coordinate dx and dy from system 2 coordinates to
120  syste 1 coordinates without using auxiliary variables. It
121  is supposed to be faster than the other version that has 4 parameters.
122 
123  \param dx X value in system 2 coordinates.
124  \param dy Y value in system 2 coordinates.
125  */
126  void system2Tosystem1(double& dx, double& dy) const;
127 
128  /*!
129  \brief It transforms the coordinate dx and dy from system 2 coordinates to system 1 coordinates (wx and wy).
130 
131  \param dx X value in system 2 coordinates.
132  \param dy Y value in system 2 coordinates.
133  \param wx X value in system 1 coordinates.
134  \param wy Y value in system 1 coordinates.
135  */
136  void system2Tosystem1(double dx, double dy, double& wx, double& wy) const;
137 
138  double getScaleX();
139  double getScaleY();
140 
141  double getTranslateX();
142  double getTranslateY();
143 
144  double getS1llx();
145  double getS1lly();
146  double getS1urx();
147  double getS1ury();
148 
149  double getS1Width();
150  double getS1Height();
151 
152  double getS2llx();
153  double getS2lly();
154  double getS2urx();
155  double getS2ury();
156 
157  double getS2Width();
158  double getS2Height();
159 
160  bool isValid();
161 
162  bool isMirroring();
163 
164  void setMirroring(bool mirror);
165 
166  protected:
167 
168  void initVariables(te::gm::Envelope system1Box, te::gm::Envelope system2Box);
169 
170  //@}
171 
172  protected:
173 
174  double m_scaleX; //!< Map units per unit along x-direction between Coordinate Systems.
175  double m_scaleY; //!< Map units per unit along y-direction between Coordinate Systems.
176 
177  double m_translateX; //!< The value corresponds to the X-Axis translation of system 2.
178  double m_translateY; //!< The value corresponds to the Y-Axis translation of system 2.
179 
180  double m_s1llx; //!< Lower left x-coordinate of the System 1
181  double m_s1lly; //!< Lower left y-coordinate of the System 1
182  double m_s1urx; //!< Upper right x-coordinate of the System 1
183  double m_s1ury; //!< Upper right y-coordinate of the System 1
184  double m_s1Width; //!< Width of System 1 coordinate system.
185  double m_s1Height; //!< Height of System 1 coordinate system.
186 
187  double m_s2llx; //!< Lower left x-coordinate of the System 2
188  double m_s2lly; //!< Lower left y-coordinate of the System 2
189  double m_s2urx; //!< Upper right x-coordinate of the System 2
190  double m_s2ury; //!< Upper right y-coordinate of the System 2
191  double m_s2Width; //!< Width of System 2 coordinate system.
192  double m_s2Height; //!< Height of System 2 coordinate system.
193 
194  bool m_valid;
195 
197  };
198 
200  m_scaleX(0.),
201  m_scaleY(0.),
202  m_translateX(0),
203  m_translateY(0),
204  m_s1llx(0.),
205  m_s1lly(0.),
206  m_s1urx(0.),
207  m_s1ury(0.),
208  m_s1Width(0.),
209  m_s1Height(0.),
210  m_s2llx(0.),
211  m_s2lly(0.),
212  m_s2urx(0.),
213  m_s2ury(0.),
214  m_s2Width(0.),
215  m_s2Height(0.),
216  m_valid(false),
217  m_mirroring(true)
218  {
219  }
220 
221  inline WorldTransformer::WorldTransformer(const double& s1llx, const double& s1lly,
222  const double& s1urx, const double& s1ury,
223  double s2width, double s2height)
224  {
225  te::gm::Envelope system1Box(s1llx, s1lly, s1urx, s1ury);
226  te::gm::Envelope system2Box(0, 0, s2width, s2height);
227 
228  setTransformationParameters(system1Box, system2Box);
229  }
230 
232  {
233  setTransformationParameters(system1Box, system2Box);
234  }
235 
237  {
238  }
239 
241  te::gm::Envelope system1Box, te::gm::Envelope system2Box )
242  {
243  m_valid = true;
244  if((!system1Box.isValid()) || (!system2Box.isValid()))
245  {
246  m_valid = false;
247  return;
248  }
249 
250  initVariables(system1Box, system2Box);
251 
252  m_scaleX = m_s2Width / m_s1Width; // map units per unit along x-direction
253  m_scaleY = m_s2Height / m_s1Height; // map units per unit along y-direction
254 
257  }
258 
259  inline void WorldTransformer::system1Tosystem2(double& wx, double& wy) const
260  {
261  wx = (m_scaleX * wx) + m_translateX;
262  wy = (m_scaleY * wy) + m_translateY;
263 
264  if(m_mirroring)
265  {
266  double dyCopy = wy;
267  wy = m_s2Height - 1 - dyCopy; // mirror
268  }
269  }
270 
271  inline void WorldTransformer::system1Tosystem2(const double& wx, const double& wy,
272  double& dx, double& dy) const
273  {
274  dx = (m_scaleX * wx) + m_translateX;
275  dy = (m_scaleY * wy) + m_translateY;
276 
277  if(m_mirroring)
278  {
279  double dyCopy = dy;
280  dy = m_s2Height - 1 - dyCopy; // mirror
281  }
282  }
283 
284  inline void WorldTransformer::system2Tosystem1(double& dx, double& dy) const
285  {
286  double dyCopy = dy;
287 
288  if(m_mirroring)
289  {
290  dy = m_s2Height - 1 - dyCopy; // mirror
291  }
292 
293  dx = (dx - m_translateX) / m_scaleX;
294  dy = (dyCopy - m_translateY) / m_scaleY;
295  }
296 
297  inline void WorldTransformer::system2Tosystem1(double dx, double dy,
298  double& wx, double& wy) const
299  {
300  double dyCopy = dy;
301 
302  if(m_mirroring)
303  {
304  dy = m_s2Height - 1 - dyCopy; // mirror
305  }
306 
307  wx = (dx - m_translateX) / m_scaleX;
308  wy = (dyCopy - m_translateY) / m_scaleY;
309  }
310 
312  te::gm::Envelope system1Box, te::gm::Envelope system2Box )
313  {
314  m_scaleX = 0.;
315  m_scaleY = 0.;
316 
317  m_translateX = 0.;
318  m_translateY = 0.;
319 
320  m_s1llx = system1Box.getLowerLeftX();
321  m_s1lly = system1Box.getLowerLeftY();
322  m_s1urx = system1Box.getUpperRightX();
323  m_s1ury = system1Box.getUpperRightY();
324  m_s1Width = system1Box.getWidth();
325  m_s1Height = system1Box.getHeight();
326 
327  m_s2llx = system2Box.getLowerLeftX();
328  m_s2lly = system2Box.getLowerLeftY();
329  m_s2urx = system2Box.getUpperRightX();
330  m_s2ury = system2Box.getUpperRightY();
331  m_s2Width = system2Box.getWidth();
332  m_s2Height = system2Box.getHeight();
333  }
334 
336  {
337  return m_scaleX;
338  }
339 
341  {
342  return m_scaleY;
343  }
344 
346  {
347  return m_translateX;
348  }
349 
351  {
352  return m_translateY;
353  }
354 
356  {
357  return m_s1llx;
358  }
359 
361  {
362  return m_s1lly;
363  }
364 
366  {
367  return m_s1urx;
368  }
369 
371  {
372  return m_s1ury;
373  }
374 
376  {
377  return m_s1Width;
378  }
379 
381  {
382  return m_s1Height;
383  }
384 
386  {
387  return m_s2llx;
388  }
389 
391  {
392  return m_s2lly;
393  }
394 
396  {
397  return m_s2urx;
398  }
399 
401  {
402  return m_s2ury;
403  }
404 
406  {
407  return m_s2Width;
408  }
409 
411  {
412  return m_s2Height;
413  }
414 
416  {
417  if((m_scaleX <= 0.) && (m_scaleY <= 0.))
418  return false;
419 
420  return true;
421  }
422 
424  {
425  return m_mirroring;
426  }
427 
428  inline void WorldTransformer::setMirroring( bool mirror )
429  {
430  m_mirroring = mirror;
431  }
432  } // end namespace layout
433 } // end namespace te
434 
435 #endif
double m_translateY
The value corresponds to the Y-Axis translation of system 2.
void system1Tosystem2(double &wx, double &wy) const
It transforms the coordinate wx and wy from world coordinates to other(system) coordinates without us...
double m_scaleX
Map units per unit along x-direction between Coordinate Systems.
void system2Tosystem1(double &dx, double &dy) const
It transforms the coordinate dx and dy from system 2 coordinates to syste 1 coordinates without using...
double m_s1urx
Upper right x-coordinate of the System 1.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
double m_s1Height
Height of System 1 coordinate system.
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
double m_scaleY
Map units per unit along y-direction between Coordinate Systems.
double m_s2Width
Width of System 2 coordinate system.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
double m_s1lly
Lower left y-coordinate of the System 1.
void initVariables(te::gm::Envelope system1Box, te::gm::Envelope system2Box)
double m_s1ury
Upper right y-coordinate of the System 1.
double m_s2Height
Height of System 2 coordinate system.
double m_s2urx
Upper right x-coordinate of the System 2.
double m_s1Width
Width of System 1 coordinate system.
WorldTransformer()
Empty constructor.
This class implements the logic for transforming from System 1 coordinate to other type of coordinate...
double m_translateX
The value corresponds to the X-Axis translation of system 2.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
double m_s2ury
Upper right y-coordinate of the System 2.
void setTransformationParameters(te::gm::Envelope system1Box, te::gm::Envelope system2Box)
It adjusts a new transformation function that maps between the world coordinate system to other coord...
double m_s2lly
Lower left y-coordinate of the System 2.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
double m_s2llx
Lower left x-coordinate of the System 2.
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
double m_s1llx
Lower left x-coordinate of the System 1.