QGIS API Documentation  3.0.2-Girona (307d082)
qgsrectangle.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrectangle.h - description
3  -------------------
4  begin : Sat Jun 22 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSRECTANGLE_H
19 #define QGSRECTANGLE_H
20 
21 #include "qgis_core.h"
22 #include <iosfwd>
23 #include <QDomDocument>
24 
25 class QString;
26 class QRectF;
27 class QgsBox3d;
28 #include "qgspointxy.h"
29 
30 
39 class CORE_EXPORT QgsRectangle
40 {
41  public:
42 
44  QgsRectangle() = default; // optimised constructor for null rectangle - no need to call normalize here
45 
47  explicit QgsRectangle( double xMin, double yMin = 0, double xMax = 0, double yMax = 0 );
49  QgsRectangle( const QgsPointXY &p1, const QgsPointXY &p2 );
51  QgsRectangle( const QRectF &qRectF );
53  QgsRectangle( const QgsRectangle &other );
54 
55  // IMPORTANT - while QgsRectangle is inherited by QgsReferencedRectangle, we do NOT want a virtual destructor here
56  // because this class MUST be lightweight and we don't want the cost of the vtable here.
57  // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
58  ~QgsRectangle() = default;
59 
65  static QgsRectangle fromWkt( const QString &wkt );
66 
72  static QgsRectangle fromCenterAndSize( QgsPointXY center, double width, double height );
73 
78  void set( const QgsPointXY &p1, const QgsPointXY &p2 );
79 
84  void set( double mXmin, double mYmin, double mXmax, double mYmax );
85 
89  void setXMinimum( double x ) { mXmin = x; }
90 
94  void setXMaximum( double x ) { mXmax = x; }
95 
99  void setYMinimum( double y ) { mYmin = y; }
100 
104  void setYMaximum( double y ) { mYmax = y; }
105 
110  void setMinimal();
111 
115  double xMaximum() const { return mXmax; }
116 
120  double xMinimum() const { return mXmin; }
121 
125  double yMaximum() const { return mYmax; }
126 
130  double yMinimum() const { return mYmin; }
131 
135  void normalize();
136 
142  double width() const { return mXmax - mXmin; }
143 
149  double height() const { return mYmax - mYmin; }
150 
158  double area() const { return ( mXmax - mXmin ) * ( mYmax - mYmin ); }
159 
165  double perimeter() const { return 2 * ( mXmax - mXmin ) + 2 * ( mYmax - mYmin ); }
166 
170  QgsPointXY center() const { return QgsPointXY( mXmin + width() / 2, mYmin + height() / 2 ); }
171 
175  void scale( double scaleFactor, const QgsPointXY *c = nullptr );
176 
180  void scale( double scaleFactor, double centerX, double centerY );
181 
186  void grow( double delta );
187 
191  void include( const QgsPointXY &p );
192 
199  QgsRectangle buffered( double width ) const;
200 
204  QgsRectangle intersect( const QgsRectangle *rect ) const;
205 
209  bool intersects( const QgsRectangle &rect ) const;
210 
214  bool contains( const QgsRectangle &rect ) const;
215 
219  bool contains( const QgsPointXY &p ) const;
220 
224  void combineExtentWith( const QgsRectangle &rect );
225 
229  void combineExtentWith( double x, double y );
230 
235  QgsRectangle operator-( const QgsVector v ) const;
236 
241  QgsRectangle operator+( const QgsVector v ) const;
242 
247  QgsRectangle &operator-=( const QgsVector v );
248 
253  QgsRectangle &operator+=( const QgsVector v );
254 
259  bool isEmpty() const;
260 
266  bool isNull() const;
267 
271  QString asWktCoordinates() const;
272 
276  QString asWktPolygon() const;
277 
281  QRectF toRectF() const;
282 
288  QString toString( int precision = 16 ) const;
289 
293  QString asPolygon() const;
294 
299  bool operator==( const QgsRectangle &r1 ) const;
300 
305  bool operator!=( const QgsRectangle &r1 ) const;
306 
311  QgsRectangle &operator=( const QgsRectangle &r1 );
312 
317  bool isFinite() const;
318 
322  void invert();
323 
329  QgsBox3d toBox3d( double zMin, double zMax ) const;
330 
332  operator QVariant() const
333  {
334  return QVariant::fromValue( *this );
335  }
336 
337  private:
338 
339  double mXmin = 0.0;
340  double mYmin = 0.0;
341  double mXmax = 0.0;
342  double mYmax = 0.0;
343 
344 };
345 
347 
348 #ifndef SIP_RUN
349 
353 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle );
354 
358 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle );
359 
360 inline std::ostream &operator << ( std::ostream &os, const QgsRectangle &r )
361 {
362  return os << r.toString().toLocal8Bit().data();
363 }
364 
365 #endif
366 
367 #endif // QGSRECTANGLE_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:94
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A class to represent a 2D point.
Definition: qgspointxy.h:43
double perimeter() const
Returns the perimeter of the rectangle.
Definition: qgsrectangle.h:165
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:35
Q_DECLARE_METATYPE(QModelIndex)
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:142
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:99
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QDateTime operator+(const QDateTime &start, QgsInterval interval)
A class to represent a vector.
Definition: qgsvector.h:27
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
double area() const
Returns the area of the rectangle.
Definition: qgsrectangle.h:158
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:130
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:115
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsRectangle &rectangle)
Reads a rectangle from stream in into rectangle.
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:104
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:120
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:125
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:170
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:89
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:149