QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
21QgsBox3d::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
27QgsBox3d::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
39void QgsBox3d::setXMinimum( double x )
40{
41 mBounds2d.setXMinimum( x );
42}
43
44void QgsBox3d::setXMaximum( double x )
45{
46 mBounds2d.setXMaximum( x );
47}
48
49void QgsBox3d::setYMinimum( double y )
50{
51 mBounds2d.setYMinimum( y );
52}
53
54void QgsBox3d::setYMaximum( double y )
55{
56 mBounds2d.setYMaximum( y );
57}
58
59void QgsBox3d::setZMinimum( double z )
60{
61 mZmin = z;
62}
63
64void 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
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
87bool QgsBox3d::is2d() const
88{
89 return qgsDoubleNear( mZmin, mZmax ) || ( mZmin > mZmax );
90}
91
92bool 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
102bool 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
110bool 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
121double 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
130bool QgsBox3d::operator==( const QgsBox3d &other ) const
131{
132 return mBounds2d == other.mBounds2d &&
133 qgsDoubleNear( mZmin, other.mZmin ) &&
134 qgsDoubleNear( mZmax, other.mZmax );
135}
136
137void 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
156void 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}
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:39
void setXMaximum(double x) SIP_HOLDGIL
Sets the maximum x value.
Definition: qgsbox3d.cpp:44
void setYMaximum(double y) SIP_HOLDGIL
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
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
double yMaximum() const SIP_HOLDGIL
Returns the maximum y value.
Definition: qgsbox3d.h:113
bool is2d() const SIP_HOLDGIL
Returns true if the box can be considered a 2-dimensional box, i.e.
Definition: qgsbox3d.cpp:87
double zMaximum() const SIP_HOLDGIL
Returns the maximum z value.
Definition: qgsbox3d.h:141
double zMinimum() const SIP_HOLDGIL
Returns the minimum z value.
Definition: qgsbox3d.h:134
bool operator==(const QgsBox3d &other) const
Definition: qgsbox3d.cpp:130
bool intersects(const QgsBox3d &other) const
Returns true if box intersects with another box.
Definition: qgsbox3d.cpp:92
double xMinimum() const SIP_HOLDGIL
Returns the minimum x value.
Definition: qgsbox3d.h:78
double xMaximum() const SIP_HOLDGIL
Returns the maximum x value.
Definition: qgsbox3d.h:85
void setZMinimum(double z) SIP_HOLDGIL
Sets the minimum z value.
Definition: qgsbox3d.cpp:59
QgsBox3d intersect(const QgsBox3d &other) const
Returns the intersection of this box and another 3D box.
Definition: qgsbox3d.cpp:78
void normalize()
Normalize the box so it has non-negative width/height/depth.
Definition: qgsbox3d.cpp:69
void scale(double scaleFactor, const QgsPoint &center=QgsPoint())
Scale the rectangle around a center QgsPoint.
Definition: qgsbox3d.cpp:137
void setZMaximum(double z) SIP_HOLDGIL
Sets the maximum z value.
Definition: qgsbox3d.cpp:64
void setYMinimum(double y) SIP_HOLDGIL
Sets the minimum y value.
Definition: qgsbox3d.cpp:49
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
void setXMinimum(double x) SIP_HOLDGIL
Sets the minimum x value.
Definition: qgsbox3d.cpp:39
double yMinimum() const SIP_HOLDGIL
Returns the minimum y value.
Definition: qgsbox3d.h:106
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspoint.cpp:767
Q_GADGET double x
Definition: qgspoint.h:52
double z
Definition: qgspoint.h:54
double y
Definition: qgspoint.h:53
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
Definition: qgsrectangle.h:349
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
Definition: qgsrectangle.h:161
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
Definition: qgsrectangle.h:156
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
Definition: qgsrectangle.h:151
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
Definition: qgsrectangle.h:166
void normalize()
Normalize the rectangle so it has non-negative width/height.
Definition: qgsrectangle.h:203
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:333
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
Definition: qgsrectangle.h:363
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3988