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  */
161 
162  /*!
163  \brief It returns the upper right coordinate of the envelope.
164 
165  \return The upper right coordinate of the envelope.
166  */
168 
169  /*!
170  \brief It returns the rectangle's center coordinate.
171 
172  \return The rectangle's center coordinate.
173  */
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 double Envelope::getWidth() const
443  {
444  return m_urx - m_llx;
445  }
446 
447  inline double Envelope::getHeight() const
448  {
449  return m_ury - m_lly;
450  }
451 
452  inline double Envelope::getArea() const
453  {
454  return (getWidth() * getHeight());
455  }
456 
458  {
459  if(this != &rhs)
460  init(rhs.m_llx, rhs.m_lly, rhs.m_urx, rhs.m_ury);
461 
462  return *this;
463  }
464 
465  inline bool Envelope::operator==(const Envelope& rhs) const
466  {
467  if((this->m_llx != rhs.m_llx) ||
468  (this->m_lly != rhs.m_lly) ||
469  (this->m_urx != rhs.m_urx) ||
470  (this->m_ury != rhs.m_ury))
471  return false;
472  else
473  return true;
474  }
475 
476  inline bool Envelope::equals(const Envelope& rhs) const
477  {
478  return *this == rhs;
479  }
480 
481  inline bool Envelope::disjoint(const Envelope& rhs) const
482  {
483  if((m_urx < rhs.m_llx) ||
484  (m_llx > rhs.m_urx) ||
485  (m_ury < rhs.m_lly) ||
486  (m_lly > rhs.m_ury))
487  return true;
488  else
489  return false;
490  }
491 
492  inline bool Envelope::intersects(const Envelope& rhs) const
493  {
494  if((m_urx < rhs.m_llx) ||
495  (m_llx > rhs.m_urx) ||
496  (m_ury < rhs.m_lly) ||
497  (m_lly > rhs.m_ury))
498  return false;
499  else
500  return true;
501  }
502 
503  inline bool Envelope::touches(const Envelope& rhs) const
504  {
505  if((m_urx == rhs.m_llx) || (m_llx == rhs.m_urx))
506  {
507 // below or above?
508  if((m_ury < rhs.m_lly) || (m_lly > rhs.m_ury))
509  return false;
510  else
511  return true;
512  }
513 
514  if((m_ury == rhs.m_lly) || (m_lly == rhs.m_ury))
515  {
516 // to the left or to the right?
517  if((m_urx < rhs.m_llx) || (m_llx > rhs.m_urx))
518  return false;
519  else
520  return true;
521  }
522 
523  return false;
524  }
525 
526  inline bool Envelope::within(const Envelope& rhs) const
527  {
528  return ((m_llx >= rhs.m_llx) &&
529  (m_urx <= rhs.m_urx) &&
530  (m_lly >= rhs.m_lly) &&
531  (m_ury <= rhs.m_ury));
532  }
533 
534  inline bool Envelope::contains(const Envelope& rhs) const
535  {
536  return ((rhs.m_llx >= m_llx) &&
537  (rhs.m_urx <= m_urx) &&
538  (rhs.m_lly >= m_lly) &&
539  (rhs.m_ury <= m_ury));
540  }
541 
542  inline Envelope Envelope::intersection(const Envelope& rhs) const
543  {
544  assert(intersects(rhs));
545 
546  double llx = m_llx > rhs.m_llx ? m_llx : rhs.m_llx;
547  double lly = m_lly > rhs.m_lly ? m_lly : rhs.m_lly;
548  double urx = m_urx < rhs.m_urx ? m_urx : rhs.m_urx;
549  double ury = m_ury < rhs.m_ury ? m_ury : rhs.m_ury;
550 
551  return Envelope(llx, lly, urx, ury);
552  }
553 
554  inline void Envelope::Union(const Envelope& rhs)
555  {
556  if(rhs.m_llx < m_llx)
557  m_llx = rhs.m_llx;
558 
559  if(rhs.m_lly < m_lly)
560  m_lly = rhs.m_lly;
561 
562  if(m_urx < rhs.m_urx)
563  m_urx = rhs.m_urx;
564 
565  if(m_ury < rhs.m_ury)
566  m_ury = rhs.m_ury;
567  }
568 
569  } // end namespace gm
570 } // end namespace te
571 
572 #endif // __TERRALIB_GEOMETRY_INTERNAL_ENVELOPE_H
573 
te::gm::Envelope::equals
bool equals(const Envelope &rhs) const
It returns true if the envelopes are "spatially equal".
Definition: Envelope.h:476
te::gm::Envelope
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:52
te::gm::Envelope::getLowerLeftX
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:394
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::gm::Envelope::isValid
bool isValid() const
It tells if the rectangle is valid or not.
te::gm::Envelope::Envelope
Envelope()
It constructs an envelope with invalid coordinates.
Definition: Envelope.h:350
operator==
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
te::gm::Envelope::getArea
double getArea() const
It returns the area of this envelope as measured in the spatial reference system of it.
Definition: Envelope.h:452
te::gm::Envelope::getWidth
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:442
te::gm::Envelope::distance
double distance(const Envelope &rhs) const
It returns the shortest distance between any two points in the two envelopes.
te::gm::Envelope::getCenter
Coord2D getCenter() const
It returns the rectangle's center coordinate.
te::gm::Envelope::makeInvalid
void makeInvalid()
It will invalidated the envelope.
Definition: Envelope.h:434
TEGEOMEXPORT
#define TEGEOMEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:76
te::gm::Envelope::m_llx
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
te::gm::Envelope::operator==
bool operator==(const Envelope &rhs) const
Equal operator.
Definition: Envelope.h:465
te::gm::Envelope::getUpperRight
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
te::gm::Envelope::Union
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
Definition: Envelope.h:554
te::gm::Envelope::m_urx
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
te::gm::Envelope::operator=
Envelope & operator=(const Envelope &rhs)
Assignment operator.
Definition: Envelope.h:457
te::gm::Envelope::touches
bool touches(const Envelope &rhs) const
It returns true if the envelopes "spatially touches".
Definition: Envelope.h:503
te::gm::Envelope::intersection
Envelope intersection(const Envelope &rhs) const
It returns an envelope that represents the point set intersection with another envelope.
Definition: Envelope.h:542
te::gm::Envelope::getLowerLeftY
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:404
te::gm::Envelope::m_lly
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
te::gm::Envelope::within
bool within(const Envelope &rhs) const
It returns true if this envelope is "spatially within" the rhs envelope.
Definition: Envelope.h:526
te::gm::Envelope::contains
bool contains(const Envelope &rhs) const
It returns true if this envelope "spatially contains" the rhs envelope.
Definition: Envelope.h:534
te::gm::Envelope::getLowerLeft
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
te::gm::Envelope::getUpperRightX
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:414
te::gm::Coord2D
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:41
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::gm::Envelope::init
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
Definition: Envelope.h:385
te::gm::Envelope::m_ury
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
te::gm::Envelope::intersects
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
Definition: Envelope.h:492
te::gm::Envelope::getHeight
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:447
te::gm::Envelope::transform
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
te::gm::Envelope::getUpperRightY
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:424
te::gm::Envelope::disjoint
bool disjoint(const Envelope &rhs) const
It returns true if this envelope is "spatially disjoint" from rhs envelope.
Definition: Envelope.h:481