QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
20 
21 QgsBox3d::QgsBox3d( double xmin, double ymin, double zmin, double xmax, double ymax, double zmax )
22  : mBounds2d( xmin, ymin, xmax, ymax )
23  , mZmin( zmin )
24  , mZmax( zmax )
25 {}
26 
27 QgsBox3d::QgsBox3d( const QgsPoint &p1, const QgsPoint &p2 )
28  : mBounds2d( p1.x(), p1.y(), p2.x(), p2.y() )
29  , mZmin( std::min( p1.z(), p2.z() ) )
30  , mZmax( std::max( p1.z(), p2.z() ) )
31 {
32  mBounds2d.normalize();
33 }
34 
36  : mBounds2d( rect )
37 {}
38 
39 void QgsBox3d::setXMinimum( double x )
40 {
41  mBounds2d.setXMinimum( x );
42 }
43 
44 void QgsBox3d::setXMaximum( double x )
45 {
46  mBounds2d.setXMaximum( x );
47 }
48 
49 void QgsBox3d::setYMinimum( double y )
50 {
51  mBounds2d.setYMinimum( y );
52 }
53 
54 void QgsBox3d::setYMaximum( double y )
55 {
56  mBounds2d.setYMaximum( y );
57 }
58 
59 void QgsBox3d::setZMinimum( double z )
60 {
61  mZmin = z;
62 }
63 
64 void QgsBox3d::setZMaximum( double z )
65 {
66  mZmax = z;
67 }
68 
70 {
71  mBounds2d.normalize();
72  double z1 = std::min( mZmin, mZmax );
73  double z2 = std::max( mZmin, mZmax );
74  mZmin = z1;
75  mZmax = z2;
76 }
77 
78 QgsBox3d QgsBox3d::intersect( const QgsBox3d &other ) const
79 {
80  QgsRectangle intersect2d = mBounds2d.intersect( other.mBounds2d );
81  double zMin = std::max( mZmin, other.mZmin );
82  double zMax = std::min( mZmax, other.mZmax );
83  return QgsBox3d( intersect2d.xMinimum(), intersect2d.yMinimum(), zMin,
84  intersect2d.xMaximum(), intersect2d.yMaximum(), zMax );
85 }
86 
87 bool QgsBox3d::is2d() const
88 {
89  return qgsDoubleNear( mZmin, mZmax ) || ( mZmin > mZmax );
90 }
91 
92 bool QgsBox3d::intersects( const QgsBox3d &other ) const
93 {
94  if ( !mBounds2d.intersects( other.mBounds2d ) )
95  return false;
96 
97  double z1 = ( mZmin > other.mZmin ? mZmin : other.mZmin );
98  double z2 = ( mZmax < other.mZmax ? mZmax : other.mZmax );
99  return z1 <= z2;
100 }
101 
102 bool QgsBox3d::contains( const QgsBox3d &other ) const
103 {
104  if ( !mBounds2d.contains( other.mBounds2d ) )
105  return false;
106 
107  return ( other.mZmin >= mZmin && other.mZmax <= mZmax );
108 }
109 
110 bool QgsBox3d::contains( const QgsPoint &p ) const
111 {
112  if ( !mBounds2d.contains( QgsPointXY( p.x(), p.y() ) ) )
113  return false;
114 
115  if ( p.is3D() )
116  return mZmin <= p.z() && p.z() <= mZmax;
117  else
118  return true;
119 }
120 
121 bool QgsBox3d::operator==( const QgsBox3d &other ) const
122 {
123  return mBounds2d == other.mBounds2d &&
124  qgsDoubleNear( mZmin, other.mZmin ) &&
125  qgsDoubleNear( mZmax, other.mZmax );
126 }
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
Definition: qgsrectangle.h:335
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:39
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:134
bool intersects(const QgsBox3d &other) const
Returns true if box intersects with another box.
Definition: qgsbox3d.cpp:92
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:251
void setXMaximum(double x)
Sets the maximum x value.
Definition: qgsbox3d.cpp:44
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:35
void setYMaximum(double y)
Sets the maximum y value.
Definition: qgsbox3d.cpp:54
bool contains(const QgsBox3d &other) const
Returns true when box contains other box.
Definition: qgsbox3d.cpp:102
QgsBox3d intersect(const QgsBox3d &other) const
Returns the intersection of this box and another 3D box.
Definition: qgsbox3d.cpp:78
bool operator==(const QgsBox3d &other) const
Definition: qgsbox3d.cpp:121
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:139
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:305
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setZMinimum(double z)
Sets the minimum z value.
Definition: qgsbox3d.cpp:59
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
void setYMinimum(double y)
Sets the minimum y value.
Definition: qgsbox3d.cpp:49
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:144
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:21
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
double z
Definition: qgspoint.h:43
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
void normalize()
Normalize the rectangle so it has non-negative width/height.
Definition: qgsrectangle.h:181
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
Definition: qgsrectangle.h:321
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:129
bool is2d() const
Returns true if the box can be considered a 2-dimensional box, i.e.
Definition: qgsbox3d.cpp:87
void setZMaximum(double z)
Sets the maximum z value.
Definition: qgsbox3d.cpp:64
double x
Definition: qgspoint.h:41
void normalize()
Normalize the box so it has non-negative width/height/depth.
Definition: qgsbox3d.cpp:69