Envelope.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/geometry/Envelope.h
22 
23  \brief An Envelope defines a 2D rectangular region.
24 */
25 
26 #ifndef __TERRALIB_GEOMETRY_INTERNAL_ENVELOPE_H
27 #define __TERRALIB_GEOMETRY_INTERNAL_ENVELOPE_H
28 
29 // TerraLib
30 #include "Config.h"
31 
32 // STL
33 #include <cassert>
34 #include <limits>
35 #include <vector>
36 
37 namespace te
38 {
39  namespace gm
40  {
41 // Forward declarations
42  struct Coord2D;
43 
44  /*!
45  \class Envelope
46 
47  \brief An Envelope defines a 2D rectangular region.
48 
49  \ingroup geometry
50  */
52  {
53  public:
54 
55  /** @name Basic Envelope Methods
56  * Basic Envelope methods.
57  */
58  //@{
59 
60  /*! \brief It constructs an envelope with invalid coordinates. */
61  Envelope();
62 
63  /*!
64  \brief Constructor.
65 
66  \param llx Lower left corner x-coordinate.
67  \param lly Lower left corner y-coordinate.
68  \param urx Upper right corner x-coordinate.
69  \param ury Upper right corner y-coordinate.
70  */
71  Envelope(const double& llx, const double& lly,
72  const double& urx, const double& ury);
73 
74  /*!
75  \brief Constructor.
76 
77  \param vectd Vector of doubles representing the envelope lower left and upper right corners.
78  */
79  Envelope(const std::vector<double>& vectd);
80 
81  /*!
82  \brief Copy constructor.
83 
84  \param rhs The source object to copy from.
85  */
86  Envelope(const Envelope& rhs);
87 
88  /*!
89  \brief It initializes (sets) the envelope bounds.
90 
91  \param llx Lower left corner x-coordinate.
92  \param lly Lower left corner y-coordinate.
93  \param urx Upper right corner x-coordinate.
94  \param ury Upper right corner y-coordinate.
95  */
96  void init(const double& llx, const double& lly,
97  const double& urx, const double& ury);
98 
99  /*!
100  \brief It returns a constant reference to the x coordinate of the lower left corner.
101 
102  \return A constant reference to the x coordinate of the lower left corner.
103  */
104  const double& getLowerLeftX() const;
105 
106  /*!
107  \brief It returns a constant reference to the x coordinate of the lower left corner.
108 
109  \return A constant reference to the x coordinate of the lower left corner.
110  */
111  double& getLowerLeftX();
112 
113  /*!
114  \brief It returns a constant refernce to the y coordinate of the lower left corner.
115 
116  \return A constant refernce to the y coordinate of the lower left corner.
117  */
118  const double& getLowerLeftY() const;
119 
120  /*!
121  \brief It returns a constant refernce to the y coordinate of the lower left corner.
122 
123  \return A constant refernce to the y coordinate of the lower left corner.
124  */
125  double& getLowerLeftY();
126 
127  /*!
128  \brief It returns a constant refernce to the x coordinate of the upper right corner.
129 
130  \return A constant refernce to the x coordinate of the upper right corner.
131  */
132  const double& getUpperRightX() const;
133 
134  /*!
135  \brief It returns a constant refernce to the y coordinate of the upper right corner.
136 
137  \return A constant refernce to the y coordinate of the upper right corner.
138  */
139  double& getUpperRightX();
140 
141  /*!
142  \brief It returns a constant refernce to the x coordinate of the upper right corner.
143 
144  \return A constant refernce to the x coordinate of the upper right corner.
145  */
146  const double& getUpperRightY() const;
147 
148  /*!
149  \brief It returns a constant refernce to the y coordinate of the upper right corner.
150 
151  \return A constant refernce to the y coordinate of the upper right corner.
152  */
153  double& getUpperRightY();
154 
155  /*!
156  \brief It returns the lower left coordinate of the envelope.
157 
158  \return The lower left coordinate of the envelope.
159  */
160  Coord2D getLowerLeft() const;
161 
162  /*!
163  \brief It returns the upper right coordinate of the envelope.
164 
165  \return The upper right coordinate of the envelope.
166  */
167  Coord2D getUpperRight() const;
168 
169  /*!
170  \brief It returns the rectangle's center coordinate.
171 
172  \return The rectangle's center coordinate.
173  */
174  Coord2D getCenter() const;
175 
176  /*! \brief It will invalidated the envelope. */
177  void makeInvalid();
178 
179  /*!
180  \brief It tells if the rectangle is valid or not.
181 
182  \return False if the rectangle coordinates are not valid and true otherwise.
183  */
184  bool isValid() const;
185 
186  /*!
187  \brief It returns the envelope width.
188 
189  \return The envelope width.
190  */
191  double getWidth() const;
192 
193  /*!
194  \brief It returns the envelope height.
195 
196  \return The envelope height.
197  */
198  double getHeight() const;
199 
200  /*!
201  \brief It returns the area of this envelope as measured in the spatial reference system of it.
202 
203  \return The area of this envelope.
204  */
205  double getArea() const;
206 
207  //@}
208 
209  /** @name Envelope Operators
210  * Overloaded operators for an envelope.
211  */
212  //@{
213 
214  /*!
215  \brief Assignment operator.
216 
217  \param rhs The source object to copy from.
218 
219  \return A reference to this instance.
220  */
221  Envelope& operator=(const Envelope& rhs);
222 
223  /*!
224  \brief Equal operator.
225 
226  \param rhs The source object to be compared.
227 
228  \return True if the rectangles have the same exact coordinates.
229  */
230  bool operator==(const Envelope& rhs) const;
231 
232  //@}
233 
234  /** @name Spatial Operations
235  * Methods for testing spatial relations between Envelope objects and to perform some operations over envelope type.
236  */
237  //@{
238 
239  /*!
240  \brief It returns true if the envelopes are "spatially equal".
241 
242  \param rhs The another envelope to be compared.
243 
244  \return True if the enevlopes are "spatially equal".
245  */
246  bool equals(const Envelope& rhs) const;
247 
248  /*!
249  \brief It returns true if this envelope is "spatially disjoint" from rhs envelope.
250 
251  \param rhs The other envelope to be compared.
252 
253  \return True if this envelope is "spatially disjoint" from the other envelope.
254  */
255  bool disjoint(const Envelope& rhs) const;
256 
257  /*!
258  \brief It returns true if the envelopes "spatially intersects".
259 
260  \param rhs The other envelope to be compared.
261 
262  \return True if the envelopes "spatially intersects".
263  */
264  bool intersects(const Envelope& rhs) const;
265 
266  /*!
267  \brief It returns true if the envelopes "spatially touches".
268 
269  \param rhs The other envelope to be compared.
270 
271  \return True if the envelopes "spatially touches".
272  */
273  bool touches(const Envelope& rhs) const;
274 
275  /*!
276  \brief It returns true if this envelope is "spatially within" the rhs envelope.
277 
278  \param rhs The other envelope to be compared.
279 
280  \return True if this envelope is "spatially within" the rhs envelope.
281  */
282  bool within(const Envelope& rhs) const;
283 
284  /*!
285  \brief It returns true if this envelope "spatially contains" the rhs envelope.
286 
287  \param rhs The other envelope to be compared.
288 
289  \return True if this envelope "spatially contains" the rhs envelope.
290  */
291  bool contains(const Envelope& rhs) const;
292 
293  /*!
294  \brief It returns the shortest distance between any two points in the two envelopes.
295 
296  \param rhs The other envelope.
297 
298  \return The shortest distance between any two points in the two envelopes.
299 
300  \note It calculates the shortest distance in the spatial reference system of the envelopes.
301 
302  \note If the two envelopes intersects it will return "0.0".
303  */
304  double distance(const Envelope& rhs) const;
305 
306  /*!
307  \brief It returns an envelope that represents the point set intersection with another envelope.
308 
309  \param rhs The other envelope whose intersection with this envelope will be calculated.
310 
311  \return An envelope representing the intersection with this envelope. It can be a degenerated envelope if they only "spatially touch" each other".
312 
313  \pre The rhs envelope must intersects this envelope.
314 
315  \note The caller of this method will take the ownership of the returned envelope.
316 
317  \warning Calling this method with two envelopes that doesn't intersect may return an invalid envelope.
318  */
319  Envelope intersection(const Envelope& rhs) const;
320 
321  /*!
322  \brief It updates the envelop with coordinates of another envelope.
323 
324  \param rhs The other envelope whose coordinates will be used to update this one.
325  */
326  void Union(const Envelope& rhs);
327 
328  /*!
329  \brief It will transform the coordinates of the Envelope from the old SRS to the new one.
330 
331  After calling this method the Envelope will be associated to the new SRID.
332 
333  \param oldsrid The old Spatial Reference System.
334  \param newsrid The new Spatial Reference System used to transform the coordinates of the Envelope.
335 
336  \exception Exception It may throw an exception if it can not do the transformation.
337  */
338  void transform(int oldsrid, int newsrid);
339 
340  //@}
341 
342  public:
343 
344  double m_llx; //!< Lower left corner x-coordinate.
345  double m_lly; //!< Lower left corner y-coordinate.
346  double m_urx; //!< Upper right corner x-coordinate.
347  double m_ury; //!< Upper right corner y-coordinate.
348  };
349 
351  {
352  makeInvalid();
353  }
354 
355  inline Envelope::Envelope(const double& llx, const double& lly,
356  const double& urx, const double& ury)
357  : m_llx(llx)
358  , m_lly(lly)
359  , m_urx(urx)
360  , m_ury(ury)
361  {
362 
363  }
364 
365  inline te::gm::Envelope::Envelope(const std::vector<double>& vectd)
366  {
367  double d[4] = { 0.0, 0.0, 0.0, 0.0 };
368 
369  int i = 0;
370 
371  for(std::vector<double>::const_iterator it = vectd.begin(); it < vectd.end(); ++it)
372  {
373  d[i] = (*it);
374  i += 1;
375  }
376 
377  init(d[0], d[1], d[2], d[3]);
378  }
379 
380  inline Envelope::Envelope(const Envelope& rhs)
381  {
382  init(rhs.m_llx, rhs.m_lly, rhs.m_urx, rhs.m_ury);
383  }
384 
385  inline void Envelope::init(const double& llx, const double& lly,
386  const double& urx, const double& ury)
387  {
388  m_llx = llx;
389  m_lly = lly;
390  m_urx = urx;
391  m_ury = ury;
392  }
393 
394  inline const double& Envelope::getLowerLeftX() const
395  {
396  return m_llx;
397  }
398 
399  inline double& Envelope::getLowerLeftX()
400  {
401  return m_llx;
402  }
403 
404  inline const double& Envelope::getLowerLeftY() const
405  {
406  return m_lly;
407  }
408 
409  inline double& Envelope::getLowerLeftY()
410  {
411  return m_lly;
412  }
413 
414  inline const double& Envelope::getUpperRightX() const
415  {
416  return m_urx;
417  }
418 
419  inline double& Envelope::getUpperRightX()
420  {
421  return m_urx;
422  }
423 
424  inline const double& Envelope::getUpperRightY() const
425  {
426  return m_ury;
427  }
428 
429  inline double& Envelope::getUpperRightY()
430  {
431  return m_ury;
432  }
433 
434  inline void Envelope::makeInvalid()
435  {
436  init((std::numeric_limits<double>::max)(),
437  (std::numeric_limits<double>::max)(),
438  -((std::numeric_limits<double>::max)()),
439  -((std::numeric_limits<double>::max)()));
440  }
441 
442  inline bool Envelope::isValid() const
443  {
444  return ((m_llx <= m_urx) && (m_lly <= m_ury)) ? true : false;
445  }
446 
447  inline double Envelope::getWidth() const
448  {
449  return m_urx - m_llx;
450  }
451 
452  inline double Envelope::getHeight() const
453  {
454  return m_ury - m_lly;
455  }
456 
457  inline double Envelope::getArea() const
458  {
459  return (getWidth() * getHeight());
460  }
461 
463  {
464  if(this != &rhs)
465  init(rhs.m_llx, rhs.m_lly, rhs.m_urx, rhs.m_ury);
466 
467  return *this;
468  }
469 
470  inline bool Envelope::operator==(const Envelope& rhs) const
471  {
472  if((this->m_llx != rhs.m_llx) ||
473  (this->m_lly != rhs.m_lly) ||
474  (this->m_urx != rhs.m_urx) ||
475  (this->m_ury != rhs.m_ury))
476  return false;
477  else
478  return true;
479  }
480 
481  inline bool Envelope::equals(const Envelope& rhs) const
482  {
483  return *this == rhs;
484  }
485 
486  inline bool Envelope::disjoint(const Envelope& rhs) const
487  {
488  if((m_urx < rhs.m_llx) ||
489  (m_llx > rhs.m_urx) ||
490  (m_ury < rhs.m_lly) ||
491  (m_lly > rhs.m_ury))
492  return true;
493  else
494  return false;
495  }
496 
497  inline bool Envelope::intersects(const Envelope& rhs) const
498  {
499  if((m_urx < rhs.m_llx) ||
500  (m_llx > rhs.m_urx) ||
501  (m_ury < rhs.m_lly) ||
502  (m_lly > rhs.m_ury))
503  return false;
504  else
505  return true;
506  }
507 
508  inline bool Envelope::touches(const Envelope& rhs) const
509  {
510  if((m_urx == rhs.m_llx) || (m_llx == rhs.m_urx))
511  {
512 // below or above?
513  if((m_ury < rhs.m_lly) || (m_lly > rhs.m_ury))
514  return false;
515  else
516  return true;
517  }
518 
519  if((m_ury == rhs.m_lly) || (m_lly == rhs.m_ury))
520  {
521 // to the left or to the right?
522  if((m_urx < rhs.m_llx) || (m_llx > rhs.m_urx))
523  return false;
524  else
525  return true;
526  }
527 
528  return false;
529  }
530 
531  inline bool Envelope::within(const Envelope& rhs) const
532  {
533  return ((m_llx >= rhs.m_llx) &&
534  (m_urx <= rhs.m_urx) &&
535  (m_lly >= rhs.m_lly) &&
536  (m_ury <= rhs.m_ury));
537  }
538 
539  inline bool Envelope::contains(const Envelope& rhs) const
540  {
541  return ((rhs.m_llx >= m_llx) &&
542  (rhs.m_urx <= m_urx) &&
543  (rhs.m_lly >= m_lly) &&
544  (rhs.m_ury <= m_ury));
545  }
546 
547  inline Envelope Envelope::intersection(const Envelope& rhs) const
548  {
549  assert(intersects(rhs));
550 
551  double llx = m_llx > rhs.m_llx ? m_llx : rhs.m_llx;
552  double lly = m_lly > rhs.m_lly ? m_lly : rhs.m_lly;
553  double urx = m_urx < rhs.m_urx ? m_urx : rhs.m_urx;
554  double ury = m_ury < rhs.m_ury ? m_ury : rhs.m_ury;
555 
556  return Envelope(llx, lly, urx, ury);
557  }
558 
559  inline void Envelope::Union(const Envelope& rhs)
560  {
561  if(rhs.m_llx < m_llx)
562  m_llx = rhs.m_llx;
563 
564  if(rhs.m_lly < m_lly)
565  m_lly = rhs.m_lly;
566 
567  if(m_urx < rhs.m_urx)
568  m_urx = rhs.m_urx;
569 
570  if(m_ury < rhs.m_ury)
571  m_ury = rhs.m_ury;
572  }
573 
574  } // end namespace gm
575 } // end namespace te
576 
577 #endif // __TERRALIB_GEOMETRY_INTERNAL_ENVELOPE_H
578 
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
Definition: Envelope.h:385
void makeInvalid()
It will invalidated the envelope.
Definition: Envelope.h:434
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
Definition: Envelope.h:497
bool touches(const Envelope &rhs) const
It returns true if the envelopes "spatially touches".
Definition: Envelope.h:508
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:414
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:404
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:447
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:424
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
Definition: Envelope.h:559
#define TEGEOMEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:76
bool disjoint(const Envelope &rhs) const
It returns true if this envelope is "spatially disjoint" from rhs envelope.
Definition: Envelope.h:486
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
bool within(const Envelope &rhs) const
It returns true if this envelope is "spatially within" the rhs envelope.
Definition: Envelope.h:531
bool contains(const Envelope &rhs) const
It returns true if this envelope "spatially contains" the rhs envelope.
Definition: Envelope.h:539
URI C++ Library.
double getArea() const
It returns the area of this envelope as measured in the spatial reference system of it...
Definition: Envelope.h:457
bool equals(const Envelope &rhs) const
It returns true if the envelopes are "spatially equal".
Definition: Envelope.h:481
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
bool operator==(const Envelope &rhs) const
Equal operator.
Definition: Envelope.h:470
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
Envelope & operator=(const Envelope &rhs)
Assignment operator.
Definition: Envelope.h:462
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:394
Envelope intersection(const Envelope &rhs) const
It returns an envelope that represents the point set intersection with another envelope.
Definition: Envelope.h:547
Configuration flags for the Vector Geometry Model of TerraLib.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:452
Envelope()
It constructs an envelope with invalid coordinates.
Definition: Envelope.h:350
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:442