Debian Codegraph @

src/python/PyImath/PyImathBoxArrayImpl.h

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #ifndef _PyImathBoxArrayImpl_h_
9 #define _PyImathBoxArrayImpl_h_
10
11 //
12 // This .C file was turned into a header file so that instantiations
13 // of the various Box* types can be spread across multiple files in
14 // order to work around MSVC limitations.
15 //
16
17 #include <Python.h>
18 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
19 #include <boost/python.hpp>
20 #include <boost/python/make_constructor.hpp>
21 #include <boost/format.hpp>
22 #include <ImathVec.h>
23 #include <ImathVecAlgo.h>
24 #include <ImathBox.h>
25 #include "PyImath.h"
26 #include "PyImathBox.h"
27 #include "PyImathDecorators.h"
28 #include "PyImathMathExc.h"
29 #include "PyImathOperators.h"
30 #include "PyImathVecOperators.h"
31
32 namespace PyImath {
33 using namespace boost::python;
34 using namespace IMATH_NAMESPACE;
35
36 template <class T,int index>
37 static FixedArray<T>
38 BoxArray_get(FixedArray<IMATH_NAMESPACE::Box<T> > &va)
39 {
40 return index == 0 ?
41 FixedArray<T>(&(va.unchecked_index(0).min),
42 va.len(),2*va.stride(),va.handle(),va.writable()) :
43 FixedArray<T>(&(va.unchecked_index(0).max),
44 va.len(),2*va.stride(),va.handle(),va.writable());
45 }
46
47 template <class T>
48 static void
49 setItemTuple(FixedArray<IMATH_NAMESPACE::Box<T> > &va, Py_ssize_t index, const tuple &t)
50 {
51 if(t.attr("__len__")() == 2)
52 {
53 Box<T> v;
54 v.min = extract<T>(t[0]);
55 v.max = extract<T>(t[1]);
56 va[(size_t)va.canonical_index(index)] = v;
57 }
58 else
59 throw std::invalid_argument ("tuple of length 2 expected");
60 }
61
62 template <class T>
63 class_<FixedArray<IMATH_NAMESPACE::Box<T> > >
64 register_BoxArray()
65 {
66 using boost::mpl::true_;
67 using boost::mpl::false_;
68
69 class_<FixedArray<IMATH_NAMESPACE::Box<T> > > boxArray_class = FixedArray<IMATH_NAMESPACE::Box<T> >::register_("Fixed length array of IMATH_NAMESPACE::Box");
70 boxArray_class
71 .add_property("min",&BoxArray_get<T,0>)
72 .add_property("max",&BoxArray_get<T,1>)
73 .def("__setitem__", &setItemTuple<T>)
74 ;
75
76 add_comparison_functions(boxArray_class);
77 decoratecopy(boxArray_class);
78
79 return boxArray_class;
80 }
81
82 } // namespace PyImath
83
84 #endif // _PyImathBoxArrayImpl_h_