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  {
358  init(llx, lly, urx, ury);
359  }
360 
361  inline te::gm::Envelope::Envelope(const std::vector<double>& vectd)
362  {
363  double d[4] = { 0.0, 0.0, 0.0, 0.0 };
364 
365  int i = 0;
366 
367  for(std::vector<double>::const_iterator it = vectd.begin(); it < vectd.end(); ++it)
368  {
369  d[i] = (*it);
370  i += 1;
371  }
372 
373  init(d[0], d[1], d[2], d[3]);
374  }
375 
376  inline Envelope::Envelope(const Envelope& rhs)
377  {
378  init(rhs.m_llx, rhs.m_lly, rhs.m_urx, rhs.m_ury);
379  }
380 
381  inline void Envelope::init(const double& llx, const double& lly,
382  const double& urx, const double& ury)
383  {
384  m_llx = llx;
385  m_lly = lly;
386  m_urx = urx;
387  m_ury = ury;
388  }
389 
390  inline const double& Envelope::getLowerLeftX() const
391  {
392  return m_llx;
393  }
394 
395  inline double& Envelope::getLowerLeftX()
396  {
397  return m_llx;
398  }
399 
400  inline const double& Envelope::getLowerLeftY() const
401  {
402  return m_lly;
403  }
404 
405  inline double& Envelope::getLowerLeftY()
406  {
407  return m_lly;
408  }
409 
410  inline const double& Envelope::getUpperRightX() const
411  {
412  return m_urx;
413  }
414 
415  inline double& Envelope::getUpperRightX()
416  {
417  return m_urx;
418  }
419 
420  inline const double& Envelope::getUpperRightY() const
421  {
422  return m_ury;
423  }
424 
425  inline double& Envelope::getUpperRightY()
426  {
427  return m_ury;
428  }
429 
430  inline void Envelope::makeInvalid()
431  {
432  init((std::numeric_limits<double>::max)(),
433  (std::numeric_limits<double>::max)(),
434  -((std::numeric_limits<double>::max)()),
435  -((std::numeric_limits<double>::max)()));
436  }
437 
438  inline bool Envelope::isValid() const
439  {
440  return ((m_llx <= m_urx) && (m_lly <= m_ury)) ? true : false;
441  }
442 
443  inline double Envelope::getWidth() const
444  {
445  return m_urx - m_llx;
446  }
447 
448  inline double Envelope::getHeight() const
449  {
450  return m_ury - m_lly;
451  }
452 
453  inline double Envelope::getArea() const
454  {
455  return (getWidth() * getHeight());
456  }
457 
459  {
460  if(this != &rhs)
461  init(rhs.m_llx, rhs.m_lly, rhs.m_urx, rhs.m_ury);
462 
463  return *this;
464  }
465 
466  inline bool Envelope::operator==(const Envelope& rhs) const
467  {
468  if((this->m_llx != rhs.m_llx) ||
469  (this->m_lly != rhs.m_lly) ||
470  (this->m_urx != rhs.m_urx) ||
471  (this->m_ury != rhs.m_ury))
472  return false;
473  else
474  return true;
475  }
476 
477  inline bool Envelope::equals(const Envelope& rhs) const
478  {
479  return *this == rhs;
480  }
481 
482  inline bool Envelope::disjoint(const Envelope& rhs) const
483  {
484  if((m_urx < rhs.m_llx) ||
485  (m_llx > rhs.m_urx) ||
486  (m_ury < rhs.m_lly) ||
487  (m_lly > rhs.m_ury))
488  return true;
489  else
490  return false;
491  }
492 
493  inline bool Envelope::intersects(const Envelope& rhs) const
494  {
495  if((m_urx < rhs.m_llx) ||
496  (m_llx > rhs.m_urx) ||
497  (m_ury < rhs.m_lly) ||
498  (m_lly > rhs.m_ury))
499  return false;
500  else
501  return true;
502  }
503 
504  inline bool Envelope::touches(const Envelope& rhs) const
505  {
506  if((m_urx == rhs.m_llx) || (m_llx == rhs.m_urx))
507  {
508 // below or above?
509  if((m_ury < rhs.m_lly) || (m_lly > rhs.m_ury))
510  return false;
511  else
512  return true;
513  }
514 
515  if((m_ury == rhs.m_lly) || (m_lly == rhs.m_ury))
516  {
517 // to the left or to the right?
518  if((m_urx < rhs.m_llx) || (m_llx > rhs.m_urx))
519  return false;
520  else
521  return true;
522  }
523 
524  return false;
525  }
526 
527  inline bool Envelope::within(const Envelope& rhs) const
528  {
529  return ((m_llx >= rhs.m_llx) &&
530  (m_urx <= rhs.m_urx) &&
531  (m_lly >= rhs.m_lly) &&
532  (m_ury <= rhs.m_ury));
533  }
534 
535  inline bool Envelope::contains(const Envelope& rhs) const
536  {
537  return ((rhs.m_llx >= m_llx) &&
538  (rhs.m_urx <= m_urx) &&
539  (rhs.m_lly >= m_lly) &&
540  (rhs.m_ury <= m_ury));
541  }
542 
543  inline Envelope Envelope::intersection(const Envelope& rhs) const
544  {
545  assert(intersects(rhs));
546 
547  double llx = m_llx > rhs.m_llx ? m_llx : rhs.m_llx;
548  double lly = m_lly > rhs.m_lly ? m_lly : rhs.m_lly;
549  double urx = m_urx < rhs.m_urx ? m_urx : rhs.m_urx;
550  double ury = m_ury < rhs.m_ury ? m_ury : rhs.m_ury;
551 
552  return Envelope(llx, lly, urx, ury);
553  }
554 
555  inline void Envelope::Union(const Envelope& rhs)
556  {
557  if(rhs.m_llx < m_llx)
558  m_llx = rhs.m_llx;
559 
560  if(rhs.m_lly < m_lly)
561  m_lly = rhs.m_lly;
562 
563  if(m_urx < rhs.m_urx)
564  m_urx = rhs.m_urx;
565 
566  if(m_ury < rhs.m_ury)
567  m_ury = rhs.m_ury;
568  }
569 
570  } // end namespace gm
571 } // end namespace te
572 
573 #endif // __TERRALIB_GEOMETRY_INTERNAL_ENVELOPE_H
574 
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
Definition: Envelope.h:381
void makeInvalid()
It will invalidated the envelope.
Definition: Envelope.h:430
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
Definition: Envelope.h:493
bool touches(const Envelope &rhs) const
It returns true if the envelopes "spatially touches".
Definition: Envelope.h:504
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
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:400
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
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:420
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
Definition: Envelope.h:555
#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:482
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:527
bool contains(const Envelope &rhs) const
It returns true if this envelope "spatially contains" the rhs envelope.
Definition: Envelope.h:535
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:453
bool equals(const Envelope &rhs) const
It returns true if the envelopes are "spatially equal".
Definition: Envelope.h:477
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
bool operator==(const Envelope &rhs) const
Equal operator.
Definition: Envelope.h:466
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
Envelope & operator=(const Envelope &rhs)
Assignment operator.
Definition: Envelope.h:458
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
Envelope intersection(const Envelope &rhs) const
It returns an envelope that represents the point set intersection with another envelope.
Definition: Envelope.h:543
Configuration flags for the Vector Geometry Model of TerraLib.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
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:438