Mantid
Loading...
Searching...
No Matches
CSGObject.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
12
13#include <boost/python/class.hpp>
14#include <boost/python/copy_const_reference.hpp>
15#include <boost/python/register_ptr_to_python.hpp>
16#include <boost/python/self.hpp>
17#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
18
19#include <numpy/arrayobject.h>
20
21#define PY_ARRAY_UNIQUE_SYMBOL GEOMETRY_ARRAY_API
22#define NO_IMPORT_ARRAY
23
29using namespace boost::python;
30
32
33boost::python::object wrapMeshWithNDArray(const CSGObject *self) {
34 // PyArray_SimpleNewFromData doesn't interact well with smart pointers so use raw pointer
35
36 if (self->getShapeXML().find("infinite") != std::string::npos) {
37 throw std::runtime_error("Cannot plot Shapes of infinite extent.");
38 }
39 auto localTriangulator = GeometryTriangulator(self);
40 const auto &vertices = localTriangulator.getTriangleVertices();
41 const auto &triangles = localTriangulator.getTriangleFaces();
42 const size_t &numberTriangles = localTriangulator.numTriangleFaces();
43 npy_intp dims[3] = {static_cast<int>(numberTriangles), 3, 3};
44 auto *meshCoords = new double[dims[0] * dims[1] * dims[2]];
45 for (size_t corner_index = 0; corner_index < triangles.size(); ++corner_index) {
46 for (size_t xyz = 0; xyz < 3; xyz++) {
47 meshCoords[3 * corner_index + xyz] = vertices[3 * triangles[corner_index] + xyz];
48 } // for each coordinate of that corner
49 } // for each corner of the triangle
50
51 PyObject *ndarray = Impl::wrapWithNDArray(meshCoords, 3, dims, NumpyWrapMode::ReadWrite, OwnershipMode::Python);
52 return object(handle<>(ndarray));
53}
54
56 register_ptr_to_python<std::shared_ptr<CSGObject>>();
57
58 class_<CSGObject, boost::python::bases<IObject>, boost::noncopyable>("CSGObject", no_init)
59 .def("getBoundingBox", (const BoundingBox &(CSGObject::*)() const) & CSGObject::getBoundingBox, arg("self"),
60 return_value_policy<copy_const_reference>(), "Return the axis-aligned bounding box for this shape")
61
62 .def("getShapeXML", &CSGObject::getShapeXML, arg("self"), "Returns the XML that was used to create this shape.")
63
64 .def("volume", &CSGObject::volume, arg("self"), "Returns the volume of this shape.")
65
66 .def("getMesh", &wrapMeshWithNDArray, (arg("self")), "Get the vertices, grouped by triangles, from mesh");
67}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
boost::python::object wrapMeshWithNDArray(const CSGObject *self)
Definition: CSGObject.cpp:33
void export_Object()
Definition: CSGObject.cpp:55
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition: BoundingBox.h:34
Constructive Solid Geometry object.
Definition: CSGObject.h:51
const BoundingBox & getBoundingBox() const override
Return cached value of axis-aligned bounding box.
Definition: CSGObject.cpp:1654
double volume() const override
Calculates the volume of this object.
Definition: CSGObject.cpp:1512
std::string getShapeXML() const
Getter for the shape xml.
Definition: CSGObject.cpp:2254
IObject : Interface for geometry objects.
Definition: IObject.h:41
GeometryTriangulator : Triangulates object surfaces.
PyObject * wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode, const OwnershipMode oMode=OwnershipMode::Cpp)
Defines the wrapWithNDArray specialization for C array types.
Definition: NDArray.h:49