Debian Codegraph @

src/python/PyImath/PyImathVec.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 _PyImathVec_h_
9 #define _PyImathVec_h_
10
11 #include <Python.h>
12 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <boost/python.hpp>
14 #include <ImathVec.h>
15 #include "PyImath.h"
16 #include "PyImathFixedArray.h"
17
18 namespace PyImath {
19
20 template <class T> boost::python::class_<IMATH_NAMESPACE::Vec2<T> > register_Vec2();
21 template <class T> boost::python::class_<FixedArray<IMATH_NAMESPACE::Vec2<T> > > register_Vec2Array();
22 typedef FixedArray<IMATH_NAMESPACE::V2s> V2sArray;
23 typedef FixedArray<IMATH_NAMESPACE::V2i> V2iArray;
24 typedef FixedArray<IMATH_NAMESPACE::V2i64> V2i64Array;
25 typedef FixedArray<IMATH_NAMESPACE::V2f> V2fArray;
26 typedef FixedArray<IMATH_NAMESPACE::V2d> V2dArray;
27
28 // TODO: template <class T> class Vec2Array : public FixedArray<IMATH_NAMESPACE::Vec2<T> >
29
30 }
31
32 // define vector*float array multiplication
33 template <class T>
34 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a0, T v1) {
35 PY_IMATH_LEAVE_PYTHON;
36 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*v1; return f;
37 }
38 template <class T>
39 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator * (T v0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a1) { return a1*v0; }
40 template <class T>
41 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a0, const PyImath::FixedArray<T> &a1) {
42 PY_IMATH_LEAVE_PYTHON;
43 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*a1[i]; return f;
44 }
45 template <class T>
46 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator * (const PyImath::FixedArray<T> &a0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a1) {
47 return a1*a0;
48 }
49
50 // define vector/float array division
51 template <class T>
52 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a0, T v1) {
53 PY_IMATH_LEAVE_PYTHON;
54 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/v1; return f;
55 }
56 template <class T>
57 static PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > &a0, const PyImath::FixedArray<T> &a1) {
58 PY_IMATH_LEAVE_PYTHON;
59 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec2<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/a1[i]; return f;
60 }
61
62
63 namespace PyImath {
64
65 template <class T> boost::python::class_<IMATH_NAMESPACE::Vec3<T> > register_Vec3();
66 template <class T> boost::python::class_<FixedArray<IMATH_NAMESPACE::Vec3<T> > > register_Vec3Array();
67 typedef FixedArray<IMATH_NAMESPACE::Vec3<unsigned char> > V3cArray;
68 typedef FixedArray<IMATH_NAMESPACE::V3s> V3sArray;
69 typedef FixedArray<IMATH_NAMESPACE::V3i> V3iArray;
70 typedef FixedArray<IMATH_NAMESPACE::V3i64> V3i64Array;
71 typedef FixedArray<IMATH_NAMESPACE::V3f> V3fArray;
72 typedef FixedArray<IMATH_NAMESPACE::V3d> V3dArray;
73
74 // TODO: template <class T> class Vec3Array : public FixedArray<IMATH_NAMESPACE::Vec3<T> >
75 }
76
77 // define vector*float array multiplication
78 template <class T>
79 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a0, T v1) {
80 PY_IMATH_LEAVE_PYTHON;
81 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*v1; return f;
82 }
83 template <class T>
84 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (T v0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a1) { return a1*v0; }
85 template <class T>
86 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a0, const PyImath::FixedArray<T> &a1) {
87 PY_IMATH_LEAVE_PYTHON;
88 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*a1[i]; return f;
89 }
90 template <class T>
91 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (const PyImath::FixedArray<T> &a0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a1) {
92 return a1*a0;
93 }
94 template <class T>
95 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &va, const IMATH_NAMESPACE::M44f &m) {
96 PY_IMATH_LEAVE_PYTHON;
97 size_t len = va.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va[i] * m; return f;
98 }
99
100 template <class T>
101 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &va, const IMATH_NAMESPACE::M44d &m) {
102 PY_IMATH_LEAVE_PYTHON;
103 size_t len = va.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va[i] * m; return f;
104 }
105
106 // define vector/float array division
107 template <class T>
108 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a0, T v1) {
109 PY_IMATH_LEAVE_PYTHON;
110 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/v1; return f;
111 }
112 template <class T>
113 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &a0, const PyImath::FixedArray<T> &a1) {
114 PY_IMATH_LEAVE_PYTHON;
115 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/a1[i]; return f;
116 }
117
118 namespace PyImath {
119
120 template <class T> boost::python::class_<IMATH_NAMESPACE::Vec4<T> > register_Vec4();
121 template <class T> boost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > > register_Vec4Array();
122 typedef FixedArray<IMATH_NAMESPACE::Vec4<unsigned char> > V4cArray;
123 typedef FixedArray<IMATH_NAMESPACE::V4s> V4sArray;
124 typedef FixedArray<IMATH_NAMESPACE::V4i> V4iArray;
125 typedef FixedArray<IMATH_NAMESPACE::V4i64> V4i64Array;
126 typedef FixedArray<IMATH_NAMESPACE::V4f> V4fArray;
127 typedef FixedArray<IMATH_NAMESPACE::V4d> V4dArray;
128
129 // TODO: template <class T> class Vec3Array : public FixedArray<IMATH_NAMESPACE::Vec3<T> >
130 }
131
132 // define vector*float array multiplication
133 template <class T>
134 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a0, T v1) {
135 PY_IMATH_LEAVE_PYTHON;
136 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*v1; return f;
137 }
138 template <class T>
139 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (T v0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a1) { return a1*v0; }
140 template <class T>
141 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a0, const PyImath::FixedArray<T> &a1) {
142 PY_IMATH_LEAVE_PYTHON;
143 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]*a1[i]; return f;
144 }
145 template <class T>
146 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (const PyImath::FixedArray<T> &a0, const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a1) {
147 return a1*a0;
148 }
149 template <class T>
150 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &va, const IMATH_NAMESPACE::M44f &m) {
151 PY_IMATH_LEAVE_PYTHON;
152 size_t len = va.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va[i] * m; return f;
153 }
154
155 template <class T>
156 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &va, const IMATH_NAMESPACE::M44d &m) {
157 PY_IMATH_LEAVE_PYTHON;
158 size_t len = va.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va[i] * m; return f;
159 }
160
161 // define vector/float array division
162 template <class T>
163 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a0, T v1) {
164 PY_IMATH_LEAVE_PYTHON;
165 size_t len = a0.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/v1; return f;
166 }
167 template <class T>
168 static PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > operator / (const PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > &a0, const PyImath::FixedArray<T> &a1) {
169 PY_IMATH_LEAVE_PYTHON;
170 size_t len = a0.match_dimension(a1); PyImath::FixedArray<IMATH_NAMESPACE::Vec4<T> > f(len); for (size_t i=0;i<len;++i) f[i]=a0[i]/a1[i]; return f;
171 }
172
173
174 //
175
176 namespace PyImath {
177
178 // Other code in the Zeno code base assumes the existance of a class with the
179 // same name as the Imath class, and with static functions wrap() and
180 // convert() to produce a PyImath object from an Imath object and vice-versa,
181 // respectively. The class Boost generates from the Imath class does not
182 // have these properties, so we define a companion class here.
183 // The template argument, T, is the element type for the vector (e.g., int,
184 // float, double).
185
186 template <class T>
187 class V2 {
188 public:
189 static PyObject * wrap (const IMATH_NAMESPACE::Vec2<T> &v);
190 static int convert (PyObject *p, IMATH_NAMESPACE::Vec2<T> *v);
191 };
192
193 template <class T>
194 class V3 {
195 public:
196 static PyObject * wrap (const IMATH_NAMESPACE::Vec3<T> &v);
197 static int convert (PyObject *p, IMATH_NAMESPACE::Vec3<T> *v);
198 };
199
200 template <class T>
201 class V4 {
202 public:
203 static PyObject * wrap (const IMATH_NAMESPACE::Vec4<T> &v);
204 static int convert (PyObject *p, IMATH_NAMESPACE::Vec4<T> *v);
205 };
206
207 template <class T>
208 PyObject *
209 V2<T>::wrap (const IMATH_NAMESPACE::Vec2<T> &v)
210 {
211 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Vec2<T> >::type converter;
212 PyObject *p = converter (v);
213 return p;
214 }
215
216 template <class T>
217 PyObject *
218 V3<T>::wrap (const IMATH_NAMESPACE::Vec3<T> &v)
219 {
220 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Vec3<T> >::type converter;
221 PyObject *p = converter (v);
222 return p;
223 }
224
225 template <class T>
226 PyObject *
227 V4<T>::wrap (const IMATH_NAMESPACE::Vec4<T> &v)
228 {
229 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Vec4<T> >::type converter;
230 PyObject *p = converter (v);
231 return p;
232 }
233
234 template <class T>
235 int
236 V2<T>::convert (PyObject *p, IMATH_NAMESPACE::Vec2<T> *v)
237 {
238 boost::python::extract <IMATH_NAMESPACE::V2i> extractorV2i (p);
239 if (extractorV2i.check())
240 {
241 IMATH_NAMESPACE::V2i v2i = extractorV2i();
242 v->setValue (T(v2i[0]), T(v2i[1]));
243 return 1;
244 }
245
246 boost::python::extract <IMATH_NAMESPACE::V2i64> extractorV2i64 (p);
247 if (extractorV2i64.check())
248 {
249 IMATH_NAMESPACE::V2i64 v2i64 = extractorV2i64();
250 v->setValue (T(v2i64[0]), T(v2i64[1]));
251 return 1;
252 }
253
254 boost::python::extract <IMATH_NAMESPACE::V2f> extractorV2f (p);
255 if (extractorV2f.check())
256 {
257 IMATH_NAMESPACE::V2f v2f = extractorV2f();
258 v->setValue (T(v2f[0]), T(v2f[1]));
259 return 1;
260 }
261
262 boost::python::extract <IMATH_NAMESPACE::V2d> extractorV2d (p);
263 if (extractorV2d.check())
264 {
265 IMATH_NAMESPACE::V2d v2d = extractorV2d();
266 v->setValue (T(v2d[0]), T(v2d[1]));
267 return 1;
268 }
269
270 boost::python::extract <boost::python::tuple> extractorTuple (p);
271 if (extractorTuple.check())
272 {
273 boost::python::tuple t = extractorTuple();
274 if (t.attr ("__len__") () == 2)
275 {
276 // Extracting the tuple elements as doubles and casting them to
277 // Ts in setValue() works better than extracting them as Ts from
278 // the start. Extracting them as Ts can fail in certain
279 // circumstances if T is int and the tuples elemnts are floats.
280 // In particular, this kind of failure occurs in PyImathBox.h,
281 // when Box2<int>::convert() is passed a tuple of two tuples of
282 // floats.
283
284 double a = boost::python::extract <double> (t[0]);
285 double b = boost::python::extract <double> (t[1]);
286 v->setValue (T(a), T(b));
287 return 1;
288 }
289 }
290
291 boost::python::extract <boost::python::list> extractorList (p);
292 if (extractorList.check())
293 {
294 boost::python::list l = extractorList();
295 if (l.attr ("__len__") () == 2)
296 {
297 boost::python::extract <double> extractor0 (l[0]);
298 boost::python::extract <double> extractor1 (l[1]);
299 if (extractor0.check() && extractor1.check())
300 {
301 v->setValue (T(extractor0()), T(extractor1()));
302 return 1;
303 }
304 }
305 }
306
307 return 0;
308 }
309
310 template <class T>
311 int
312 V3<T>::convert (PyObject *p, IMATH_NAMESPACE::Vec3<T> *v)
313 {
314 boost::python::extract <IMATH_NAMESPACE::V3i> extractorV3i (p);
315 if (extractorV3i.check())
316 {
317 IMATH_NAMESPACE::V3i v3i = extractorV3i();
318 v->setValue (T(v3i[0]), T(v3i[1]), T(v3i[2]));
319 return 1;
320 }
321
322 boost::python::extract <IMATH_NAMESPACE::V3i64> extractorV3i64 (p);
323 if (extractorV3i64.check())
324 {
325 IMATH_NAMESPACE::V3i64 v3i64 = extractorV3i64();
326 v->setValue (T(v3i64[0]), T(v3i64[1]), T(v3i64[2]));
327 return 1;
328 }
329
330 boost::python::extract <IMATH_NAMESPACE::V3f> extractorV3f (p);
331 if (extractorV3f.check())
332 {
333 IMATH_NAMESPACE::V3f v3f = extractorV3f();
334 v->setValue (T(v3f[0]), T(v3f[1]), T(v3f[2]));
335 return 1;
336 }
337
338 boost::python::extract <IMATH_NAMESPACE::V3d> extractorV3d (p);
339 if (extractorV3d.check())
340 {
341 IMATH_NAMESPACE::V3d v3d = extractorV3d();
342 v->setValue (T(v3d[0]), T(v3d[1]), T(v3d[2]));
343 return 1;
344 }
345
346 boost::python::extract <boost::python::tuple> extractorTuple (p);
347 if (extractorTuple.check())
348 {
349 boost::python::tuple t = extractorTuple();
350 if (t.attr ("__len__") () == 3)
351 {
352 // See the comment in V2<T>::convert().
353
354 double a = boost::python::extract <double> (t[0]);
355 double b = boost::python::extract <double> (t[1]);
356 double c = boost::python::extract <double> (t[2]);
357 v->setValue (T(a), T(b), T(c));
358 return 1;
359 }
360 }
361
362 boost::python::extract <boost::python::list> extractorList (p);
363 if (extractorList.check())
364 {
365 boost::python::list l = extractorList();
366 if (l.attr ("__len__") () == 3)
367 {
368 boost::python::extract <double> extractor0 (l[0]);
369 boost::python::extract <double> extractor1 (l[1]);
370 boost::python::extract <double> extractor2 (l[2]);
371 if (extractor0.check() && extractor1.check() &&
372 extractor2.check())
373 {
374 v->setValue (T(extractor0()), T(extractor1()),
375 T(extractor2()));
376 return 1;
377 }
378 }
379 }
380
381 return 0;
382 }
383
384 template <class T>
385 int
386 V4<T>::convert (PyObject *p, IMATH_NAMESPACE::Vec4<T> *v)
387 {
388 boost::python::extract <IMATH_NAMESPACE::V4i> extractorV4i (p);
389 if (extractorV4i.check())
390 {
391 IMATH_NAMESPACE::V4i v4i = extractorV4i();
392 *v = IMATH_NAMESPACE::Vec4<T>(v4i);
393 return 1;
394 }
395
396 boost::python::extract <IMATH_NAMESPACE::V4f> extractorV4f (p);
397 if (extractorV4f.check())
398 {
399 IMATH_NAMESPACE::V4f v4f = extractorV4f();
400 *v = IMATH_NAMESPACE::Vec4<T>(v4f);
401 return 1;
402 }
403
404 boost::python::extract <IMATH_NAMESPACE::V4d> extractorV4d (p);
405 if (extractorV4d.check())
406 {
407 IMATH_NAMESPACE::V4d v4d = extractorV4d();
408 *v = IMATH_NAMESPACE::Vec4<T>(v4d);
409 return 1;
410 }
411
412 boost::python::extract <boost::python::tuple> extractorTuple (p);
413 if (extractorTuple.check())
414 {
415 boost::python::tuple t = extractorTuple();
416 if (t.attr ("__len__") () == 4)
417 {
418 // See the comment in V2<T>::convert().
419
420 double a = boost::python::extract <double> (t[0]);
421 double b = boost::python::extract <double> (t[1]);
422 double c = boost::python::extract <double> (t[2]);
423 double d = boost::python::extract <double> (t[3]);
424 *v = IMATH_NAMESPACE::Vec4<T>(T(a), T(b), T(c), T(d));
425 return 1;
426 }
427 }
428
429 boost::python::extract <boost::python::list> extractorList (p);
430 if (extractorList.check())
431 {
432 boost::python::list l = extractorList();
433 if (l.attr ("__len__") () == 4)
434 {
435 boost::python::extract <double> extractor0 (l[0]);
436 boost::python::extract <double> extractor1 (l[1]);
437 boost::python::extract <double> extractor2 (l[2]);
438 boost::python::extract <double> extractor3 (l[3]);
439 if (extractor0.check() && extractor1.check() &&
440 extractor2.check() && extractor3.check())
441 {
442 *v = IMATH_NAMESPACE::Vec4<T>(T(extractor0()), T(extractor1()),
443 T(extractor2()), T(extractor3()));
444 return 1;
445 }
446 }
447 }
448
449 return 0;
450 }
451
452
453 typedef V2<int> V2i;
454 typedef V2<float> V2f;
455 typedef V2<double> V2d;
456 typedef V3<int> V3i;
457 typedef V3<float> V3f;
458 typedef V3<double> V3d;
459 typedef V4<int> V4i;
460 typedef V4<float> V4f;
461 typedef V4<double> V4d;
462
463 }
464
465 #endif