Mantid
Loading...
Searching...
No Matches
PyObjectToVMD.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 +
7//----------------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------------
12#include <boost/python/extract.hpp>
13
14// See
15// http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
16#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
17#define NO_IMPORT_ARRAY
18#include <numpy/arrayobject.h>
19
20using boost::python::extract;
21using boost::python::len;
22using boost::python::object;
23
24GNU_DIAG_OFF("strict-aliasing")
25
26namespace Mantid::PythonInterface::Converters {
34PyObjectToVMD::PyObjectToVMD(const object &p) : m_obj(p), m_alreadyVMD(false) {
35 // Is it an already wrapped VMD ?
36 extract<Kernel::VMD> converter(p);
37 if (converter.check()) {
38 m_alreadyVMD = true;
39 return;
40 }
41 // Is it a sequence
42 try {
43 const size_t length = len(p);
44 if (length < 3) {
45 throw std::invalid_argument("Must be > 2 for conversion to VMD");
46 }
47 // Can we index the object
48 p.attr("__getitem__")(0);
49 } catch (boost::python::error_already_set &) {
50 throw std::invalid_argument(std::string("Cannot convert object to VMD. "
51 "Expected a python sequence found: ") +
52 p.ptr()->ob_type->tp_name);
53 }
54}
55
63 if (m_alreadyVMD) {
64 return extract<Kernel::VMD>(m_obj)();
65 }
66 const size_t length = len(m_obj);
67 Kernel::VMD ret(length);
68 for (size_t i = 0; i < length; ++i) {
69 ret[i] = extract<float>(m_obj[i])();
70 }
71 return ret;
72}
73} // namespace Mantid::PythonInterface::Converters
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Helper class which provides the Collimation Length for SANS instruments.
Kernel::VMD operator()()
Produces a VMD object from the given PyObject.
bool m_alreadyVMD
Is the object a wrapped instance of VMD.
Definition: PyObjectToVMD.h:31
const boost::python::object & m_obj
A reference to the object.
Definition: PyObjectToVMD.h:29