QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
18 QgsAABB::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 
35 bool 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 
42 bool 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 
50 float QgsAABB::distanceFromPoint( float x, float y, float z ) const
51 {
52  float dx = std::max( xMin - x, std::max( 0.f, x - xMax ) );
53  float dy = std::max( yMin - y, std::max( 0.f, y - yMax ) );
54  float dz = std::max( zMin - z, std::max( 0.f, z - zMax ) );
55  return sqrt( dx * dx + dy * dy + dz * dz );
56 }
57 
58 float QgsAABB::distanceFromPoint( QVector3D v ) const
59 {
60  return distanceFromPoint( v.x(), v.y(), v.z() );
61 }
62 
63 QList<QVector3D> QgsAABB::verticesForLines() const
64 {
65  QList<QVector3D> vertices;
66  for ( int i = 0; i < 2; ++i )
67  {
68  float x = i ? xMax : xMin;
69  for ( int j = 0; j < 2; ++j )
70  {
71  float y = j ? yMax : yMin;
72  for ( int k = 0; k < 2; ++k )
73  {
74  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 }
QgsAABB::QgsAABB
QgsAABB()=default
Constructs bounding box with null coordinates.
qgsaabb.h
QgsAABB::zMax
float zMax
Definition: qgsaabb.h:83
QgsAABB::yMin
float yMin
Definition: qgsaabb.h:79
QgsAABB::distanceFromPoint
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Definition: qgsaabb.cpp:50
QgsAABB::verticesForLines
QList< QVector3D > verticesForLines() const
Returns a list of pairs of vertices (useful for display of bounding boxes)
Definition: qgsaabb.cpp:63
QgsAABB::xMin
float xMin
Definition: qgsaabb.h:78
QgsAABB
3 Axis-aligned bounding box - in world coords.
Definition: qgsaabb.h:34
QgsAABB::yMax
float yMax
Definition: qgsaabb.h:82
QgsAABB::intersects
bool intersects(const QgsAABB &other) const
Determines whether the box intersects some other axis aligned box.
Definition: qgsaabb.cpp:35
QgsAABB::zMin
float zMin
Definition: qgsaabb.h:80
QgsAABB::xMax
float xMax
Definition: qgsaabb.h:81