Skip to content

Commit

Permalink
Add unit tests for QgsBox3d
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 12, 2017
1 parent 0d843ff commit e3dfe17
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/core/qgsbox3d.sip
Expand Up @@ -177,6 +177,9 @@ class QgsBox3d
bool contains( const QgsPointV2 &point ) const;
%Docstring
Returns true when box contains a point.

If the point is a 2D point (no z-coordinate), then the containment test
will be performed on the x/y extent of the box only.
:rtype: bool
%End

Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsbox3d.cpp
Expand Up @@ -105,5 +105,9 @@ bool QgsBox3d::contains( const QgsPointV2 &p ) const
{
if ( !mBounds2d.contains( QgsPoint( p.x(), p.y() ) ) )
return false;
return mZmin <= p.z() && p.z() <= mZmax;

if ( p.is3D() )
return mZmin <= p.z() && p.z() <= mZmax;
else
return true;
}
3 changes: 3 additions & 0 deletions src/core/qgsbox3d.h
Expand Up @@ -173,6 +173,9 @@ class CORE_EXPORT QgsBox3d

/**
* Returns true when box contains a \a point.
*
* If the point is a 2D point (no z-coordinate), then the containment test
* will be performed on the x/y extent of the box only.
*/
bool contains( const QgsPointV2 &point ) const;

Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -19,6 +19,7 @@ ADD_PYTHON_TEST(PyQgsAttributeTableModel test_qgsattributetablemodel.py)
#ADD_PYTHON_TEST(PyQgsAuthenticationSystem test_qgsauthsystem.py)
ADD_PYTHON_TEST(PyQgsBearingUtils test_qgsbearingutils.py)
ADD_PYTHON_TEST(PyQgsBlendModes test_qgsblendmodes.py)
ADD_PYTHON_TEST(PyQgsBox3d test_qgsbox3d.py)
ADD_PYTHON_TEST(PyQgsCategorizedSymbolRenderer test_qgscategorizedsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsCheckableComboBox test_qgscheckablecombobox.py)
ADD_PYTHON_TEST(PyQgsColorButton test_qgscolorbutton.py)
Expand Down
150 changes: 150 additions & 0 deletions tests/src/python/test_qgsbox3d.py
@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsBox3d.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '11/04/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.core import (QgsBox3d,
QgsPoint,
QgsPointV2,
QgsWkbTypes)

from qgis.testing import unittest


class TestQgsBox3d(unittest.TestCase):

def testCtor(self):
box = QgsBox3d(5.0, 6.0, 7.0, 10.0, 11.0, 12.0)

self.assertEqual(box.xMinimum(), 5.0)
self.assertEqual(box.yMinimum(), 6.0)
self.assertEqual(box.zMinimum(), 7.0)
self.assertEqual(box.xMaximum(), 10.0)
self.assertEqual(box.yMaximum(), 11.0)
self.assertEqual(box.zMaximum(), 12.0)

box = QgsBox3d(QgsPointV2(QgsWkbTypes.PointZ, 5, 6, 7), QgsPointV2(QgsWkbTypes.PointZ, 10, 11, 12))
self.assertEqual(box.xMinimum(), 5.0)
self.assertEqual(box.yMinimum(), 6.0)
self.assertEqual(box.zMinimum(), 7.0)
self.assertEqual(box.xMaximum(), 10.0)
self.assertEqual(box.yMaximum(), 11.0)
self.assertEqual(box.zMaximum(), 12.0)

# point constructor should normalize
box = QgsBox3d(QgsPointV2(QgsWkbTypes.PointZ, 10, 11, 12), QgsPointV2(QgsWkbTypes.PointZ, 5, 6, 7))
self.assertEqual(box.xMinimum(), 5.0)
self.assertEqual(box.yMinimum(), 6.0)
self.assertEqual(box.zMinimum(), 7.0)
self.assertEqual(box.xMaximum(), 10.0)
self.assertEqual(box.yMaximum(), 11.0)
self.assertEqual(box.zMaximum(), 12.0)

def testSetters(self):
box = QgsBox3d(5.0, 6.0, 7.0, 10.0, 11.0, 12.0)

box.setXMinimum(35.0)
box.setYMinimum(36.0)
box.setZMinimum(37.0)
box.setXMaximum(40.0)
box.setYMaximum(41.0)
box.setZMaximum(42.0)
self.assertEqual(box.xMinimum(), 35.0)
self.assertEqual(box.yMinimum(), 36.0)
self.assertEqual(box.zMinimum(), 37.0)
self.assertEqual(box.xMaximum(), 40.0)
self.assertEqual(box.yMaximum(), 41.0)
self.assertEqual(box.zMaximum(), 42.0)

def testNormalize(self):
box = QgsBox3d()
box.setXMinimum(10.0)
box.setYMinimum(11.0)
box.setZMinimum(12.0)
box.setXMaximum(5.0)
box.setYMaximum(6.0)
box.setZMaximum(7.0)

box.normalize()
self.assertEqual(box.xMinimum(), 5.0)
self.assertEqual(box.yMinimum(), 6.0)
self.assertEqual(box.zMinimum(), 7.0)
self.assertEqual(box.xMaximum(), 10.0)
self.assertEqual(box.yMaximum(), 11.0)
self.assertEqual(box.zMaximum(), 12.0)

def testDimensions(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
self.assertEqual(box.width(), 6.0)
self.assertEqual(box.height(), 7.0)
self.assertEqual(box.depth(), 8.0)

def testIntersect(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
box2 = box.intersect(QgsBox3d(7.0, 8.0, 9.0, 10.0, 11.0, 12.0))
self.assertEqual(box2.xMinimum(), 7.0)
self.assertEqual(box2.yMinimum(), 8.0)
self.assertEqual(box2.zMinimum(), 9.0)
self.assertEqual(box2.xMaximum(), 10.0)
self.assertEqual(box2.yMaximum(), 11.0)
self.assertEqual(box2.zMaximum(), 12.0)
box2 = box.intersect(QgsBox3d(0.0, 1.0, 2.0, 100.0, 111.0, 112.0))
self.assertEqual(box2.xMinimum(), 5.0)
self.assertEqual(box2.yMinimum(), 6.0)
self.assertEqual(box2.zMinimum(), 7.0)
self.assertEqual(box2.xMaximum(), 11.0)
self.assertEqual(box2.yMaximum(), 13.0)
self.assertEqual(box2.zMaximum(), 15.0)
box2 = box.intersect(QgsBox3d(1.0, 2.0, 3.0, 6.0, 7.0, 8.0))
self.assertEqual(box2.xMinimum(), 5.0)
self.assertEqual(box2.yMinimum(), 6.0)
self.assertEqual(box2.zMinimum(), 7.0)
self.assertEqual(box2.xMaximum(), 6.0)
self.assertEqual(box2.yMaximum(), 7.0)
self.assertEqual(box2.zMaximum(), 8.0)

def testIntersects(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
self.assertTrue(box.intersects(QgsBox3d(7.0, 8.0, 9.0, 10.0, 11.0, 12.0)))
self.assertTrue(box.intersects(QgsBox3d(0.0, 1.0, 2.0, 100.0, 111.0, 112.0)))
self.assertTrue(box.intersects(QgsBox3d(1.0, 2.0, 3.0, 6.0, 7.0, 8.0)))
self.assertFalse(box.intersects(QgsBox3d(15.0, 16.0, 17.0, 110.0, 112.0, 113.0)))
self.assertFalse(box.intersects(QgsBox3d(5.0, 6.0, 17.0, 11.0, 13.0, 113.0)))
self.assertFalse(box.intersects(QgsBox3d(5.0, 16.0, 7.0, 11.0, 23.0, 15.0)))
self.assertFalse(box.intersects(QgsBox3d(15.0, 6.0, 7.0, 21.0, 13.0, 15.0)))

def testContains(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
self.assertTrue(box.contains(QgsBox3d(7.0, 8.0, 9.0, 10.0, 11.0, 12.0)))
self.assertFalse(box.contains(QgsBox3d(0.0, 1.0, 2.0, 100.0, 111.0, 112.0)))
self.assertFalse(box.contains(QgsBox3d(1.0, 2.0, 3.0, 6.0, 7.0, 8.0)))
self.assertFalse(box.contains(QgsBox3d(15.0, 16.0, 17.0, 110.0, 112.0, 113.0)))
self.assertFalse(box.contains(QgsBox3d(5.0, 6.0, 17.0, 11.0, 13.0, 113.0)))
self.assertFalse(box.contains(QgsBox3d(5.0, 16.0, 7.0, 11.0, 23.0, 15.0)))
self.assertFalse(box.contains(QgsBox3d(15.0, 6.0, 7.0, 21.0, 13.0, 15.0)))

def testContainsPoint(self):
box = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
self.assertTrue(box.contains(QgsPointV2(QgsWkbTypes.PointZ, 6, 7, 8)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.PointZ, 16, 7, 8)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.PointZ, 6, 17, 8)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.PointZ, 6, 7, 18)))
# 2d containment
self.assertTrue(box.contains(QgsPointV2(QgsWkbTypes.Point, 6, 7)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.Point, 16, 7)))
self.assertFalse(box.contains(QgsPointV2(QgsWkbTypes.Point, 6, 17)))


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

0 comments on commit e3dfe17

Please sign in to comment.