QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
qgsaabb.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsaabb.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsaabb.h"
17
18QgsAABB::QgsAABB( float xMin, float yMin, float zMin, float xMax, float yMax, float zMax )
19 : xMin( xMin )
20 , yMin( yMin )
21 , zMin( zMin )
22 , xMax( xMax )
23 , yMax( yMax )
24 , zMax( zMax )
25{
26 // normalize coords
27 if ( this->xMax < this->xMin )
28 std::swap( this->xMin, this->xMax );
29 if ( this->yMax < this->yMin )
30 std::swap( this->yMin, this->yMax );
31 if ( this->zMax < this->zMin )
32 std::swap( this->zMin, this->zMax );
33}
34
35bool QgsAABB::intersects( const QgsAABB &other ) const
36{
37 return xMin < other.xMax && other.xMin < xMax &&
38 yMin < other.yMax && other.yMin < yMax &&
39 zMin < other.zMax && other.zMin < zMax;
40}
41
42bool QgsAABB::intersects( float x, float y, float z ) const
43{
44 return xMin <= x && xMax >= x &&
45 yMin <= y && yMax >= y &&
46 zMin <= z && zMax >= z;
47}
48
49
50float QgsAABB::distanceFromPoint( float x, float y, float z ) const
51{
52 const float dx = std::max( xMin - x, std::max( 0.f, x - xMax ) );
53 const float dy = std::max( yMin - y, std::max( 0.f, y - yMax ) );
54 const float dz = std::max( zMin - z, std::max( 0.f, z - zMax ) );
55 return sqrt( dx * dx + dy * dy + dz * dz );
56}
57
58float QgsAABB::distanceFromPoint( QVector3D v ) const
59{
60 return distanceFromPoint( v.x(), v.y(), v.z() );
61}
62
63QList<QVector3D> QgsAABB::verticesForLines() const
64{
65 QList<QVector3D> vertices;
66 for ( int i = 0; i < 2; ++i )
67 {
68 const float x = i ? xMax : xMin;
69 for ( int j = 0; j < 2; ++j )
70 {
71 const float y = j ? yMax : yMin;
72 for ( int k = 0; k < 2; ++k )
73 {
74 const float z = k ? zMax : zMin;
75 if ( i == 0 )
76 {
77 vertices.append( QVector3D( xMin, y, z ) );
78 vertices.append( QVector3D( xMax, y, z ) );
79 }
80 if ( j == 0 )
81 {
82 vertices.append( QVector3D( x, yMin, z ) );
83 vertices.append( QVector3D( x, yMax, z ) );
84 }
85 if ( k == 0 )
86 {
87 vertices.append( QVector3D( x, y, zMin ) );
88 vertices.append( QVector3D( x, y, zMax ) );
89 }
90 }
91 }
92 }
93 return vertices;
94}
95
96QString QgsAABB::toString() const
97{
98 return QStringLiteral( "X %1 - %2 Y %3 - %4 Z %5 - %6" ).arg( xMin ).arg( xMax ).arg( yMin ).arg( yMax ).arg( zMin ).arg( zMax );
99}
float yMax
Definition qgsaabb.h:90
QList< QVector3D > verticesForLines() const
Returns a list of pairs of vertices (useful for display of bounding boxes)
Definition qgsaabb.cpp:63
float xMax
Definition qgsaabb.h:89
QString toString() const
Returns text representation of the bounding box.
Definition qgsaabb.cpp:96
float xMin
Definition qgsaabb.h:86
float zMax
Definition qgsaabb.h:91
bool intersects(const QgsAABB &other) const
Determines whether the box intersects some other axis aligned box.
Definition qgsaabb.cpp:35
float yMin
Definition qgsaabb.h:87
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Definition qgsaabb.cpp:50
QgsAABB()=default
Constructs bounding box with null coordinates.
float zMin
Definition qgsaabb.h:88