Debian Codegraph @

src/python/PyImath/PyImathBox.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 _PyImathBox_h_
9 #define _PyImathBox_h_
10
11 #include <Python.h>
12 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <boost/python.hpp>
14 #include <ImathBox.h>
15 #include "PyImathVec.h"
16 #include "PyImathFixedArray.h"
17
18 namespace PyImath {
19
20 template <class T> boost::python::class_<IMATH_NAMESPACE::Box<T> > register_Box2();
21 template <class T> boost::python::class_<IMATH_NAMESPACE::Box<T> > register_Box3();
22
23 template <class T> boost::python::class_<FixedArray<IMATH_NAMESPACE::Box<T> > > register_BoxArray();
24
25 typedef FixedArray<IMATH_NAMESPACE::Box2s> Box2sArray;
26 typedef FixedArray<IMATH_NAMESPACE::Box2i> Box2iArray;
27 typedef FixedArray<IMATH_NAMESPACE::Box2i64> Box2i64Array;
28 typedef FixedArray<IMATH_NAMESPACE::Box2f> Box2fArray;
29 typedef FixedArray<IMATH_NAMESPACE::Box2d> Box2dArray;
30
31 typedef FixedArray<IMATH_NAMESPACE::Box3s> Box3sArray;
32 typedef FixedArray<IMATH_NAMESPACE::Box3i> Box3iArray;
33 typedef FixedArray<IMATH_NAMESPACE::Box3i64> Box3i64Array;
34 typedef FixedArray<IMATH_NAMESPACE::Box3f> Box3fArray;
35 typedef FixedArray<IMATH_NAMESPACE::Box3d> Box3dArray;
36
37 //
38
39 // Other code in the Zeno code base assumes the existance of a class with the
40 // same name as the Imath class, and with static functions wrap() and
41 // convert() to produce a PyImath object from an Imath object and vice-versa,
42 // respectively. The class Boost generates from the Imath class does not
43 // have these properties, so we define a companion class here.
44 // The template argument, T, is the element type for the box (e.g., int,
45 // float).
46
47 template <class T>
48 class Box2 {
49 public:
50 static PyObject * wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > &b);
51 static int convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > *b);
52 };
53
54 template <class T>
55 class Box3 {
56 public:
57 static PyObject * wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > &b);
58 static int convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > *v);
59 };
60
61 template <class T>
62 PyObject *
63 Box2<T>::wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > &b)
64 {
65 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > >::type converter;
66 PyObject *p = converter (b);
67 return p;
68 }
69
70 template <class T>
71 PyObject *
72 Box3<T>::wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > &b)
73 {
74 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > >::type converter;
75 PyObject *p = converter (b);
76 return p;
77 }
78
79 template <class T>
80 int
81 Box2<T>::convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > *v)
82 {
83 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2i> > extractorV2i (p);
84 if (extractorV2i.check())
85 {
86 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2i> b = extractorV2i();
87 v->min = b.min;
88 v->max = b.max;
89 return 1;
90 }
91
92 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2f> > extractorV2f (p);
93 if (extractorV2f.check())
94 {
95 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2f> b = extractorV2f();
96 v->min = b.min;
97 v->max = b.max;
98 return 1;
99 }
100
101 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2d> > extractorV2d (p);
102 if (extractorV2d.check())
103 {
104 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2d> b = extractorV2d();
105 v->min = b.min;
106 v->max = b.max;
107 return 1;
108 }
109
110 boost::python::extract <boost::python::tuple> extractorTuple (p);
111 if (extractorTuple.check())
112 {
113 boost::python::tuple t = extractorTuple();
114 if (t.attr ("__len__") () == 2)
115 {
116 PyObject *minObj =
117 boost::python::extract <boost::python::object> (t[0])().ptr();
118 PyObject *maxObj =
119 boost::python::extract <boost::python::object> (t[1])().ptr();
120
121 IMATH_NAMESPACE::Vec2<T> min, max;
122 if (! V2<T>::convert (minObj, &min))
123 return 0;
124 if (! V2<T>::convert (maxObj, &max))
125 return 0;
126
127 v->min = min;
128 v->max = max;
129
130 return 1;
131 }
132 }
133
134 return 0;
135 }
136
137 template <class T>
138 int
139 Box3<T>::convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > *v)
140 {
141 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3i> > extractorV3i (p);
142 if (extractorV3i.check())
143 {
144 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3i> b = extractorV3i();
145 v->min = b.min;
146 v->max = b.max;
147 return 1;
148 }
149
150 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3f> > extractorV3f (p);
151 if (extractorV3f.check())
152 {
153 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3f> b = extractorV3f();
154 v->min = b.min;
155 v->max = b.max;
156 return 1;
157 }
158
159 boost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3d> > extractorV3d (p);
160 if (extractorV3d.check())
161 {
162 IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3d> b = extractorV3d();
163 v->min = b.min;
164 v->max = b.max;
165 return 1;
166 }
167
168 boost::python::extract <boost::python::tuple> extractorTuple (p);
169 if (extractorTuple.check())
170 {
171 boost::python::tuple t = extractorTuple();
172 if (t.attr ("__len__") () == 2)
173 {
174 PyObject *minObj =
175 boost::python::extract <boost::python::object> (t[0])().ptr();
176 PyObject *maxObj =
177 boost::python::extract <boost::python::object> (t[1])().ptr();
178
179 IMATH_NAMESPACE::Vec3<T> min, max;
180 if (! V3<T>::convert (minObj, &min))
181 return 0;
182 if (! V3<T>::convert (maxObj, &max))
183 return 0;
184
185 v->min = min;
186 v->max = max;
187
188 return 1;
189 }
190 }
191
192 return 0;
193 }
194
195 typedef Box2<int> Box2i;
196 typedef Box2<int64_t> Box2i64;
197 typedef Box2<float> Box2f;
198 typedef Box2<double> Box2d;
199
200 typedef Box3<int> Box3i;
201 typedef Box3<int64_t> Box3i64;
202 typedef Box3<float> Box3f;
203 typedef Box3<double> Box3d;
204
205 }
206
207 #endif