Skip to content

Commit

Permalink
Export BoundingBox type to python
Browse files Browse the repository at this point in the history
Refs #9331
  • Loading branch information
martyngigg committed Apr 15, 2014
1 parent 0ff794b commit 8409af2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
Expand Up @@ -5,6 +5,7 @@
set ( MODULE_TEMPLATE src/geometry.cpp.in )

set ( EXPORT_FILES
src/Exports/BoundingBox.cpp
src/Exports/IComponent.cpp
src/Exports/ICompAssembly.cpp
src/Exports/IObjComponent.cpp
Expand Down
@@ -0,0 +1,40 @@
#include "MantidGeometry/Objects/BoundingBox.h"
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>

using Mantid::Geometry::BoundingBox;
using Mantid::Kernel::V3D;
using namespace boost::python;

void export_BoundingBox()
{
class_<BoundingBox>("BoundingBox", "Constructs a zero-sized box")
.def(init<double, double, double, double, double, double>(
(arg("xmax"), arg("ymax"), arg("zmax"), arg("xmin"), arg("ymin"), arg("zmin")),
"Constructs a box from the six given points"))

.def("minPoint", &BoundingBox::minPoint, return_value_policy<copy_const_reference>(),
"Returns a V3D containing the values of the minimum of the box. See mantid.kernel.V3D")

.def("maxPoint", &BoundingBox::maxPoint, return_value_policy<copy_const_reference>(),
"Returns a V3D containing the values of the minimum of the box. See mantid.kernel.V3D")

.def("centrePoint", &BoundingBox::centrePoint,
"Returns a V3D containing the coordinates of the centre point. See mantid.kernel.V3D")

.def("width", &BoundingBox::width,
"Returns a V3D containing the widths for each dimension. See mantid.kernel.V3D")

.def("isNull", &BoundingBox::isNull,
"Returns true if the box has no dimensions that have been set")

.def("isPointInside", &BoundingBox::isPointInside,
"Returns true if the given point is inside the object. See mantid.kernel.V3D")

.def("doesLineIntersect",
(bool (BoundingBox::*)(const V3D &, const V3D &) const)&BoundingBox::doesLineIntersect,
(arg("startPoint"), arg("lineDir")),
"Returns true if the line given by the starting point & direction vector passes through the box")
;
}

@@ -1,15 +1,21 @@
#include "MantidGeometry/Objects/Object.h"
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::Object;
using Mantid::Geometry::BoundingBox;
using namespace boost::python;

void export_Object()
{
register_ptr_to_python<boost::shared_ptr<Object>>();

class_<Object, boost::noncopyable>("Object", no_init)
.def("getBoundingBox", (const BoundingBox &(Object::*)()const)&Object::getBoundingBox,
return_value_policy<copy_const_reference>(),
"Return the axis-aligned bounding box for this shape")

.def("getShapeXML", &Object::getShapeXML,
"Returns the XML that was used to create this shape.")
;
Expand Down
@@ -0,0 +1,32 @@
import unittest
from mantid.geometry import BoundingBox
from mantid.kernel import V3D

class BoundingBoxTest(unittest.TestCase):

def test_default_construction_is_allowed(self):
box = BoundingBox()
self.assertTrue(isinstance(box, BoundingBox))
self.assertTrue(box.isNull())

def test_construction_with_min_max_values_is_allowed(self):
box = BoundingBox(1.0, 4.0, 5.0, 0.0, 2.0, 3.0)
self.assertTrue(isinstance(box, BoundingBox))

def test_properties_are_correct(self):
bbox = BoundingBox (1.0, 2.0, 3.0, -1.0, -2.0, -3.0)
self.assertEquals(bbox.minPoint(), V3D(-1.0,-2.0,-3.0))
self.assertEquals(bbox.maxPoint(), V3D(1.0,2.0,3.0))
self.assertEquals(bbox.centrePoint(), V3D(0.0,0.0,0.0))
self.assertEquals(bbox.width(), V3D(2.0,4.0,6.0))

def test_point_inside(self):
box = BoundingBox(1.0, 2.0, 3.0, -1.0, -2.0, -3.0)
self.assertTrue(box.isPointInside(V3D(0.0,0.0,0.0)))

def test_doesLineIntersect(self):
bbox = BoundingBox(4.1, 4.1, 4.1, -4.1, -4.1, -4.1)
self.assertTrue(bbox.doesLineIntersect(V3D(-6.0,0.0,0.0), V3D(1.0,0.0,0.0)))

if __name__ == '__main__':
unittest.main()
@@ -1,7 +1,8 @@
##
## mantid.geometry tests
##
set ( TEST_PY_FILES
set ( TEST_PY_FILES
BoundingBoxTest.py
IComponentTest.py
InstrumentTest.py
ObjectTest.py
Expand Down
@@ -1,5 +1,5 @@
import unittest
from mantid.geometry import Object
from mantid.geometry import BoundingBox, Object

class ObjectTest(unittest.TestCase):

Expand All @@ -19,5 +19,12 @@ def test_objects_XML_can_be_retrieved(self):
xml = pixel.shape().getShapeXML()
self.assertTrue('radius val="0.0127"' in xml)

def test_boundingBox_retrieval(self):
inst = self._testws.getInstrument()
pixel = inst.getComponentByName("pixel")
shape = pixel.shape()
box = shape.getBoundingBox()
self.assertTrue(isinstance(box, BoundingBox))

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

0 comments on commit 8409af2

Please sign in to comment.