QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 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  const double z1 = std::min( mZmin, mZmax );
73  const double z2 = std::max( mZmin, mZmax );
74  mZmin = z1;
75  mZmax = z2;
76 }
77 
78 QgsBox3d QgsBox3d::intersect( const QgsBox3d &other ) const
79 {
80  const QgsRectangle intersect2d = mBounds2d.intersect( other.mBounds2d );
81  const double zMin = std::max( mZmin, other.mZmin );
82  const 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  const double z1 = ( mZmin > other.mZmin ? mZmin : other.mZmin );
98  const 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( 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 double QgsBox3d::distanceTo( const QVector3D &point ) const
122 {
123  const double dx = std::max( mBounds2d.xMinimum() - point.x(), std::max( 0., point.x() - mBounds2d.xMaximum() ) );
124  const double dy = std::max( mBounds2d.yMinimum() - point.y(), std::max( 0., point.y() - mBounds2d.yMaximum() ) );
125  const double dz = std::max( mZmin - point.z(), std::max( 0., point.z() - mZmax ) );
126  return sqrt( dx * dx + dy * dy + dz * dz );
127 }
128 
129 
130 bool QgsBox3d::operator==( const QgsBox3d &other ) const
131 {
132  return mBounds2d == other.mBounds2d &&
133  qgsDoubleNear( mZmin, other.mZmin ) &&
134  qgsDoubleNear( mZmax, other.mZmax );
135 }
136 
137 void QgsBox3d::scale( double scaleFactor, const QgsPoint &center )
138 {
139  // scale from the center
140  double centerX, centerY, centerZ;
141  if ( !center.isEmpty() )
142  {
143  centerX = center.x();
144  centerY = center.y();
145  centerZ = center.z();
146  }
147  else
148  {
149  centerX = ( xMinimum() + xMaximum() ) / 2;
150  centerY = ( yMinimum() + yMaximum() ) / 2;
151  centerZ = ( zMinimum() + zMaximum() ) / 2;
152  }
153  scale( scaleFactor, centerX, centerY, centerZ );
154 }
155 
156 void QgsBox3d::scale( double scaleFactor, double centerX, double centerY, double centerZ )
157 {
158  setXMinimum( centerX + ( xMinimum() - centerX ) * scaleFactor );
159  setXMaximum( centerX + ( xMaximum() - centerX ) * scaleFactor );
160 
161  setYMinimum( centerY + ( yMinimum() - centerY ) * scaleFactor );
162  setYMaximum( centerY + ( yMaximum() - centerY ) * scaleFactor );
163 
164  setZMinimum( centerZ + ( zMinimum() - centerZ ) * scaleFactor );
165  setZMaximum( centerZ + ( zMaximum() - centerZ ) * scaleFactor );
166 }
QgsRectangle::intersects
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
Definition: qgsrectangle.h:349
QgsBox3d::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the maximum x value.
Definition: qgsbox3d.h:98
QgsBox3d::setZMaximum
void setZMaximum(double z) SIP_HOLDGIL
Sets the maximum z value.
Definition: qgsbox3d.cpp:64
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsBox3d::setZMinimum
void setZMinimum(double z) SIP_HOLDGIL
Sets the minimum z value.
Definition: qgsbox3d.cpp:59
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
QgsBox3d::operator==
bool operator==(const QgsBox3d &other) const
Definition: qgsbox3d.cpp:130
QgsPoint::z
double z
Definition: qgspoint.h:71
QgsBox3d::normalize
void normalize()
Normalize the box so it has non-negative width/height/depth.
Definition: qgsbox3d.cpp:69
QgsBox3d::setYMaximum
void setYMaximum(double y) SIP_HOLDGIL
Sets the maximum y value.
Definition: qgsbox3d.cpp:54
QgsBox3d::intersects
bool intersects(const QgsBox3d &other) const
Returns true if box intersects with another box.
Definition: qgsbox3d.cpp:92
QgsBox3d::distanceTo
double distanceTo(const QVector3D &point) const
Returns the smallest distance between the box and the point point (returns 0 if the point is inside t...
Definition: qgsbox3d.cpp:121
QgsBox3d::contains
bool contains(const QgsBox3d &other) const
Returns true when box contains other box.
Definition: qgsbox3d.cpp:102
qgspoint.h
QgsRectangle::intersect
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:333
QgsBox3d::zMaximum
double zMaximum() const SIP_HOLDGIL
Returns the maximum z value.
Definition: qgsbox3d.h:154
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsBox3d::zMinimum
double zMinimum() const SIP_HOLDGIL
Returns the minimum z value.
Definition: qgsbox3d.h:147
QgsRectangle::normalize
void normalize()
Normalize the rectangle so it has non-negative width/height.
Definition: qgsrectangle.h:203
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
QgsPoint::y
double y
Definition: qgspoint.h:70
QgsBox3d::scale
void scale(double scaleFactor, const QgsPoint &center=QgsPoint())
Scale the rectangle around a center QgsPoint.
Definition: qgsbox3d.cpp:137
QgsBox3d::QgsBox3d
QgsBox3d(double xmin=0, double ymin=0, double zmin=0, double xmax=0, double ymax=0, double zmax=0) SIP_HOLDGIL
Constructor for QgsBox3D which accepts the ranges of x/y/z coordinates.
Definition: qgsbox3d.cpp:21
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsBox3d::setXMaximum
void setXMaximum(double x) SIP_HOLDGIL
Sets the maximum x value.
Definition: qgsbox3d.cpp:44
QgsRectangle::contains
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
Definition: qgsrectangle.h:363
QgsRectangle::setXMinimum
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
Definition: qgsrectangle.h:151
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsBox3d::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the minimum y value.
Definition: qgsbox3d.h:119
qgsbox3d.h
QgsRectangle::setXMaximum
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
Definition: qgsrectangle.h:156
QgsBox3d::setYMinimum
void setYMinimum(double y) SIP_HOLDGIL
Sets the minimum y value.
Definition: qgsbox3d.cpp:49
QgsAbstractGeometry::is3D
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
Definition: qgsabstractgeometry.h:219
QgsBox3d::is2d
bool is2d() const SIP_HOLDGIL
Returns true if the box can be considered a 2-dimensional box, i.e.
Definition: qgsbox3d.cpp:87
QgsRectangle::setYMaximum
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
Definition: qgsrectangle.h:166
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
QgsRectangle::setYMinimum
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
Definition: qgsrectangle.h:161
QgsBox3d::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the maximum y value.
Definition: qgsbox3d.h:126
QgsBox3d::intersect
QgsBox3d intersect(const QgsBox3d &other) const
Returns the intersection of this box and another 3D box.
Definition: qgsbox3d.cpp:78
QgsBox3d::setXMinimum
void setXMinimum(double x) SIP_HOLDGIL
Sets the minimum x value.
Definition: qgsbox3d.cpp:39
QgsBox3d
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:38
QgsPoint::isEmpty
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspoint.cpp:767
QgsPoint::x
double x
Definition: qgspoint.h:69
QgsBox3d::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the minimum x value.
Definition: qgsbox3d.h:91