Mantid
Loading...
Searching...
No Matches
MeshObject.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#define PY_ARRAY_UNIQUE_SYMBOL GEOMETRY_ARRAY_API
19#define NO_IMPORT_ARRAY
20#include <numpy/arrayobject.h>
21
26using namespace boost::python;
27
29
30boost::python::object wrapMeshWithNDArray(const MeshObject &self) {
31 // PyArray_SimpleNewFromData doesn't interact well with smart pointers so use raw pointer
32 const auto &vertices = self.getV3Ds();
33 const auto &triangles = self.getTriangles();
34 const size_t &numberTriangles = triangles.size() / 3;
35 npy_intp dims[3] = {static_cast<int>(numberTriangles), 3, 3};
36 auto *meshCoords = new double[dims[0] * dims[1] * dims[2]];
37 for (size_t iTriangle = 0; iTriangle < numberTriangles; ++iTriangle) {
38 for (size_t corner = 0; corner < 3; corner++) {
39 auto coords = std::vector<double>(vertices[triangles[(3 * iTriangle) + corner]]);
40 for (size_t xyz = 0; xyz < 3; xyz++) {
41 meshCoords[iTriangle * 3 * 3 + corner * 3 + xyz] = coords[xyz];
42 } // for each coordinate of that corner
43 } // for each corner of the triangle
44 } // for each triangle
45 PyObject *ndarray = Impl::wrapWithNDArray(meshCoords, 3, dims, NumpyWrapMode::ReadWrite, OwnershipMode::Python);
46
47 return boost::python::object(handle<>(ndarray));
48}
49
51 register_ptr_to_python<MeshObject *>();
52
53 class_<MeshObject, boost::python::bases<IObject>, boost::noncopyable>("MeshObject", no_init)
54 .def("getMesh", &wrapMeshWithNDArray, (arg("self")), "Get the vertices, grouped by triangles, from mesh")
55
56 .def("getBoundingBox", (const BoundingBox &(MeshObject::*)() const) & MeshObject::getBoundingBox, arg("self"),
57 return_value_policy<copy_const_reference>(), "Return the axis-aligned bounding box for this shape");
58}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_MeshObject()
boost::python::object wrapMeshWithNDArray(const MeshObject &self)
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition BoundingBox.h:33
IObject : Interface for geometry objects.
Definition IObject.h:42
Triangular Mesh Object.
Definition MeshObject.h:53
const BoundingBox & getBoundingBox() const override
Return cached value of axis-aligned bounding box.
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.