QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsbox3d.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbox3d.cpp
3  ------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot 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 #include "qgsbox3d.h"
19 #include "qgspoint.h"
20 
21 
22 QgsBox3d::QgsBox3d( double xmin, double ymin, double zmin, double xmax, double ymax, double zmax )
23  : mBounds2d( xmin, ymin, xmax, ymax )
24  , mZmin( zmin )
25  , mZmax( zmax )
26 {}
27 
28 QgsBox3d::QgsBox3d( const QgsPoint &p1, const QgsPoint &p2 )
29  : mBounds2d( p1.x(), p1.y(), p2.x(), p2.y() )
30  , mZmin( std::min( p1.z(), p2.z() ) )
31  , mZmax( std::max( p1.z(), p2.z() ) )
32 {
33  mBounds2d.normalize();
34 }
35 
37  : mBounds2d( rect )
38 {}
39 
40 void QgsBox3d::setXMinimum( double x )
41 {
42  mBounds2d.setXMinimum( x );
43 }
44 
45 void QgsBox3d::setXMaximum( double x )
46 {
47  mBounds2d.setXMaximum( x );
48 }
49 
50 void QgsBox3d::setYMinimum( double y )
51 {
52  mBounds2d.setYMinimum( y );
53 }
54 
55 void QgsBox3d::setYMaximum( double y )
56 {
57  mBounds2d.setYMaximum( y );
58 }
59 
60 void QgsBox3d::setZMinimum( double z )
61 {
62  mZmin = z;
63 }
64 
65 void QgsBox3d::setZMaximum( double z )
66 {
67  mZmax = z;
68 }
69 
71 {
72  mBounds2d.normalize();
73  double z1 = std::min( mZmin, mZmax );
74  double z2 = std::max( mZmin, mZmax );
75  mZmin = z1;
76  mZmax = z2;
77 }
78 
79 QgsBox3d QgsBox3d::intersect( const QgsBox3d &other ) const
80 {
81  QgsRectangle intersect2d = mBounds2d.intersect( other.mBounds2d );
82  double zMin = std::max( mZmin, other.mZmin );
83  double zMax = std::min( mZmax, other.mZmax );
84  return QgsBox3d( intersect2d.xMinimum(), intersect2d.yMinimum(), zMin,
85  intersect2d.xMaximum(), intersect2d.yMaximum(), zMax );
86 }
87 
88 bool QgsBox3d::is2d() const
89 {
90  return qgsDoubleNear( mZmin, mZmax ) || ( mZmin > mZmax );
91 }
92 
93 bool QgsBox3d::intersects( const QgsBox3d &other ) const
94 {
95  if ( !mBounds2d.intersects( other.mBounds2d ) )
96  return false;
97 
98  double z1 = ( mZmin > other.mZmin ? mZmin : other.mZmin );
99  double z2 = ( mZmax < other.mZmax ? mZmax : other.mZmax );
100  return z1 <= z2;
101 }
102 
103 bool QgsBox3d::contains( const QgsBox3d &other ) const
104 {
105  if ( !mBounds2d.contains( other.mBounds2d ) )
106  return false;
107 
108  return ( other.mZmin >= mZmin && other.mZmax <= mZmax );
109 }
110 
111 bool QgsBox3d::contains( const QgsPoint &p ) const
112 {
113  if ( !mBounds2d.contains( QgsPointXY( p.x(), p.y() ) ) )
114  return false;
115 
116  if ( p.is3D() )
117  return mZmin <= p.z() && p.z() <= mZmax;
118  else
119  return true;
120 }
121 
122 bool QgsBox3d::operator==( const QgsBox3d &other ) const
123 {
124  return mBounds2d == other.mBounds2d &&
125  qgsDoubleNear( mZmin, other.mZmin ) &&
126  qgsDoubleNear( mZmax, other.mZmax );
127 }
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
Definition: qgsrectangle.h:327
A rectangle specified with double values.
Definition: qgsrectangle.h:40
double y
Definition: qgspoint.h:42
void setXMinimum(double x)
Sets the minimum x value.
Definition: qgsbox3d.cpp:40
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:134
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
Definition: qgsrectangle.h:341
bool operator==(const QgsBox3d &other) const
Definition: qgsbox3d.cpp:122
void setXMaximum(double x)
Sets the maximum x value.
Definition: qgsbox3d.cpp:45
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:36
bool intersects(const QgsBox3d &other) const
Returns true if box intersects with another box.
Definition: qgsbox3d.cpp:93
void setYMaximum(double y)
Sets the maximum y value.
Definition: qgsbox3d.cpp:55
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
bool contains(const QgsBox3d &other) const
Returns true when box contains other box.
Definition: qgsbox3d.cpp:103
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:139
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:311
void setZMinimum(double z)
Sets the minimum z value.
Definition: qgsbox3d.cpp:60
void setYMinimum(double y)
Sets the minimum y value.
Definition: qgsbox3d.cpp:50
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:144
bool is2d() const
Returns true if the box can be considered a 2-dimensional box, i.e.
Definition: qgsbox3d.cpp:88
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
QgsBox3d(double xmin=0, double ymin=0, double zmin=0, double xmax=0, double ymax=0, double zmax=0)
Constructor for QgsBox3D which accepts the ranges of x/y/z coordinates.
Definition: qgsbox3d.cpp:22
double z
Definition: qgspoint.h:43
void normalize()
Normalize the rectangle so it has non-negative width/height.
Definition: qgsrectangle.h:181
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:129
QgsBox3d intersect(const QgsBox3d &other) const
Returns the intersection of this box and another 3D box.
Definition: qgsbox3d.cpp:79
void setZMaximum(double z)
Sets the maximum z value.
Definition: qgsbox3d.cpp:65
double x
Definition: qgspoint.h:41
void normalize()
Normalize the box so it has non-negative width/height/depth.
Definition: qgsbox3d.cpp:70