QGIS API Documentation  3.0.2-Girona (307d082)
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 ), yMin( yMin ), zMin( zMin ), xMax( xMax ), yMax( yMax ), zMax( zMax )
20 {
21  // normalize coords
22  if ( this->xMax < this->xMin )
23  qSwap( this->xMin, this->xMax );
24  if ( this->yMax < this->yMin )
25  qSwap( this->yMin, this->yMax );
26  if ( this->zMax < this->zMin )
27  qSwap( this->zMin, this->zMax );
28 }
29 
30 bool QgsAABB::intersects( const QgsAABB &other ) const
31 {
32  return xMin < other.xMax && other.xMin < xMax &&
33  yMin < other.yMax && other.yMin < yMax &&
34  zMin < other.zMax && other.zMin < zMax;
35 }
36 
37 bool QgsAABB::intersects( float x, float y, float z ) const
38 {
39  return xMin <= x && xMax >= x &&
40  yMin <= y && yMax >= y &&
41  zMin <= z && zMax >= z;
42 }
43 
44 
45 float QgsAABB::distanceFromPoint( float x, float y, float z ) const
46 {
47  float dx = qMax( xMin - x, qMax( 0.f, x - xMax ) );
48  float dy = qMax( yMin - y, qMax( 0.f, y - yMax ) );
49  float dz = qMax( zMin - z, qMax( 0.f, z - zMax ) );
50  return sqrt( dx * dx + dy * dy + dz * dz );
51 }
52 
53 float QgsAABB::distanceFromPoint( const QVector3D &v ) const
54 {
55  return distanceFromPoint( v.x(), v.y(), v.z() );
56 }
57 
58 QList<QVector3D> QgsAABB::verticesForLines() const
59 {
60  QList<QVector3D> vertices;
61  for ( int i = 0; i < 2; ++i )
62  {
63  float x = i ? xMax : xMin;
64  for ( int j = 0; j < 2; ++j )
65  {
66  float y = j ? yMax : yMin;
67  for ( int k = 0; k < 2; ++k )
68  {
69  float z = k ? zMax : zMin;
70  if ( i == 0 )
71  {
72  vertices.append( QVector3D( xMin, y, z ) );
73  vertices.append( QVector3D( xMax, y, z ) );
74  }
75  if ( j == 0 )
76  {
77  vertices.append( QVector3D( x, yMin, z ) );
78  vertices.append( QVector3D( x, yMax, z ) );
79  }
80  if ( k == 0 )
81  {
82  vertices.append( QVector3D( x, y, zMin ) );
83  vertices.append( QVector3D( x, y, zMax ) );
84  }
85  }
86  }
87  }
88  return vertices;
89 }
3 Axis-aligned bounding box - in world coords.
Definition: qgsaabb.h:30
QgsAABB()=default
Constructs bounding box with null coordinates.
QList< QVector3D > verticesForLines() const
Returns a list of pairs of vertices (useful for display of bounding boxes)
Definition: qgsaabb.cpp:58
float zMax
Definition: qgsaabb.h:80
bool intersects(const QgsAABB &other) const
Determines whether the box intersects some other axis aligned box.
Definition: qgsaabb.cpp:30
float zMin
Definition: qgsaabb.h:77
float yMax
Definition: qgsaabb.h:79
float xMin
Definition: qgsaabb.h:75
float xMax
Definition: qgsaabb.h:78
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Definition: qgsaabb.cpp:45
float yMin
Definition: qgsaabb.h:76