Skip to content
Permalink
Browse files
Add methods to convert between QgsRectangle and QgsBox3d
Also add volume calculation to QgsBox3d
  • Loading branch information
nyalldawson committed Apr 12, 2017
1 parent 6632fd6 commit 36ca6d23808503abebce9a5f1375972b4344b4ec
Showing with 65 additions and 1 deletion.
  1. +12 −0 python/core/qgsbox3d.sip
  2. +8 −0 python/core/qgsrectangle.sip
  3. +10 −0 src/core/qgsbox3d.h
  4. +6 −0 src/core/qgsrectangle.cpp
  5. +8 −0 src/core/qgsrectangle.h
  6. +11 −1 tests/src/python/test_qgsbox3d.py
  7. +10 −0 tests/src/python/test_qgsrectangle.py
@@ -156,6 +156,12 @@ class QgsBox3d
:rtype: float
%End

double volume() const;
%Docstring
Returns the volume of the box.
:rtype: float
%End

QgsBox3d intersect( const QgsBox3d &other ) const;
%Docstring
Returns the intersection of this box and another 3D box.
@@ -183,6 +189,12 @@ class QgsBox3d
:rtype: bool
%End

QgsRectangle toRectangle() const;
%Docstring
Converts the box to a 2D rectangle.
:rtype: QgsRectangle
%End

};

/************************************************************************
@@ -290,6 +290,14 @@ Copy constructor
Swap x/y coordinates in the rectangle.
%End

QgsBox3d toBox3d( double zMin, double zMax ) const;
%Docstring
Converts the rectangle to a 3D box, with the specified
zMin and zMax z values.
.. versionadded:: 3.0
:rtype: QgsBox3d
%End

};


@@ -156,6 +156,11 @@ class CORE_EXPORT QgsBox3d
*/
double depth() const { return mZmax - mZmin; }

/**
* Returns the volume of the box.
*/
double volume() const { return mBounds2d.area() * ( mZmax - mZmin ); }

/**
* Returns the intersection of this box and another 3D box.
*/
@@ -179,6 +184,11 @@ class CORE_EXPORT QgsBox3d
*/
bool contains( const QgsPointV2 &point ) const;

/**
* Converts the box to a 2D rectangle.
*/
QgsRectangle toRectangle() const { return mBounds2d; }

private:

QgsRectangle mBounds2d;
@@ -28,6 +28,7 @@
#include "qgspoint.h"
#include "qgsrectangle.h"
#include "qgslogger.h"
#include "qgsbox3d.h"

QgsRectangle::QgsRectangle( double xMin, double yMin, double xMax, double yMax )
: mXmin( xMin )
@@ -377,6 +378,11 @@ void QgsRectangle::invert()
mYmax = tmp;
}

QgsBox3d QgsRectangle::toBox3d( double zMin, double zMax ) const
{
return QgsBox3d( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
}

QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
{
out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
@@ -24,6 +24,7 @@

class QString;
class QRectF;
class QgsBox3d;
#include "qgspoint.h"


@@ -274,6 +275,13 @@ class CORE_EXPORT QgsRectangle
*/
void invert();

/**
* Converts the rectangle to a 3D box, with the specified
* \a zMin and \a zMax z values.
* \since QGIS 3.0
*/
QgsBox3d toBox3d( double zMin, double zMax ) const;

private:

double mXmin;
@@ -17,7 +17,8 @@
from qgis.core import (QgsBox3d,
QgsPoint,
QgsPointV2,
QgsWkbTypes)
QgsWkbTypes,
QgsRectangle)

from qgis.testing import unittest

@@ -145,6 +146,15 @@ def testContainsPoint(self):
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.Point, 16, 7)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.Point, 6, 17)))

def testVolume(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
self.assertEqual(box.volume(), 336.0)

def testToRectangle(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
rect = box.toRectangle()
self.assertEqual(rect, QgsRectangle(5, 6, 11, 13))


if __name__ == '__main__':
unittest.main()
@@ -242,6 +242,16 @@ def testToString(self):
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

def testToBox3d(self):
rect = QgsRectangle(0, 0.1, 0.2, 0.3)
box = rect.toBox3d(0.4, 0.5)
self.assertEqual(box.xMinimum(), 0.0)
self.assertEqual(box.yMinimum(), 0.1)
self.assertEqual(box.zMinimum(), 0.4)
self.assertEqual(box.xMaximum(), 0.2)
self.assertEqual(box.yMaximum(), 0.3)
self.assertEqual(box.zMaximum(), 0.5)


if __name__ == '__main__':
unittest.main()

0 comments on commit 36ca6d2

Please sign in to comment.