QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 && yMin < other.yMax && other.yMin < yMax && zMin < other.zMax && other.zMin < zMax;
38}
39
40bool QgsAABB::intersects( float x, float y, float z ) const
41{
42 return xMin <= x && xMax >= x && yMin <= y && yMax >= y && zMin <= z && zMax >= z;
43}
44
45
46float QgsAABB::distanceFromPoint( float x, float y, float z ) const
47{
48 const float dx = std::max( xMin - x, std::max( 0.f, x - xMax ) );
49 const float dy = std::max( yMin - y, std::max( 0.f, y - yMax ) );
50 const float dz = std::max( zMin - z, std::max( 0.f, z - zMax ) );
51 return sqrt( dx * dx + dy * dy + dz * dz );
52}
53
54float QgsAABB::distanceFromPoint( QVector3D v ) const
55{
56 return distanceFromPoint( v.x(), v.y(), v.z() );
57}
58
59QList<QVector3D> QgsAABB::verticesForLines() const
60{
61 QList<QVector3D> vertices;
62 for ( int i = 0; i < 2; ++i )
63 {
64 const float x = i ? xMax : xMin;
65 for ( int j = 0; j < 2; ++j )
66 {
67 const float y = j ? yMax : yMin;
68 for ( int k = 0; k < 2; ++k )
69 {
70 const float z = k ? zMax : zMin;
71 if ( i == 0 )
72 {
73 vertices.append( QVector3D( xMin, y, z ) );
74 vertices.append( QVector3D( xMax, y, z ) );
75 }
76 if ( j == 0 )
77 {
78 vertices.append( QVector3D( x, yMin, z ) );
79 vertices.append( QVector3D( x, yMax, z ) );
80 }
81 if ( k == 0 )
82 {
83 vertices.append( QVector3D( x, y, zMin ) );
84 vertices.append( QVector3D( x, y, zMax ) );
85 }
86 }
87 }
88 }
89 return vertices;
90}
91
92QString QgsAABB::toString() const
93{
94 return QStringLiteral( "X %1 - %2 Y %3 - %4 Z %5 - %6" ).arg( xMin ).arg( xMax ).arg( yMin ).arg( yMax ).arg( zMin ).arg( zMax );
95}
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:59
float xMax
Definition qgsaabb.h:89
QString toString() const
Returns text representation of the bounding box.
Definition qgsaabb.cpp:92
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:46
QgsAABB()=default
Constructs bounding box with null coordinates.
float zMin
Definition qgsaabb.h:88