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
25using namespace boost::python;
26
28
29boost::python::object wrapMeshWithNDArray(MeshObject &self) {
30 // PyArray_SimpleNewFromData doesn't interact well with smart pointers so use raw pointer
31 const auto &vertices = self.getV3Ds();
32 const auto &triangles = self.getTriangles();
33 const size_t &numberTriangles = triangles.size() / 3;
34 npy_intp dims[3] = {static_cast<int>(numberTriangles), 3, 3};
35 auto *meshCoords = new double[dims[0] * dims[1] * dims[2]];
36 for (size_t iTriangle = 0; iTriangle < numberTriangles; ++iTriangle) {
37 for (size_t corner = 0; corner < 3; corner++) {
38 auto coords = std::vector<double>(vertices[triangles[(3 * iTriangle) + corner]]);
39 for (size_t xyz = 0; xyz < 3; xyz++) {
40 meshCoords[iTriangle * 3 * 3 + corner * 3 + xyz] = coords[xyz];
41 } // for each coordinate of that corner
42 } // for each corner of the triangle
43 } // for each triangle
44 PyObject *ndarray = Impl::wrapWithNDArray(meshCoords, 3, dims, NumpyWrapMode::ReadWrite, OwnershipMode::Python);
45
46 return boost::python::object(handle<>(ndarray));
47}
48
50 register_ptr_to_python<MeshObject *>();
51
52 class_<MeshObject, boost::python::bases<IObject>, boost::noncopyable>("MeshObject", no_init)
53 .def("getMesh", &wrapMeshWithNDArray, (arg("self")), "Get the vertices, grouped by triangles, from mesh");
54}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
void export_MeshObject()
Definition: MeshObject.cpp:49
boost::python::object wrapMeshWithNDArray(MeshObject &self)
Definition: MeshObject.cpp:29
IObject : Interface for geometry objects.
Definition: IObject.h:41
Triangular Mesh Object.
Definition: MeshObject.h:50
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