Debian Codegraph @

src/python/PyImath/PyImathQuat.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 _PyImathQuat_h_
9 #define _PyImathQuat_h_
10
11 #include <Python.h>
12 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <boost/python.hpp>
14 #include <ImathQuat.h>
15 #include <ImathVec.h>
16 #include "PyImath.h"
17
18 namespace PyImath {
19
20 template <class T> boost::python::class_<IMATH_NAMESPACE::Quat<T> > register_Quat();
21 template <class T> boost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Quat<T> > > register_QuatArray();
22 typedef FixedArray<IMATH_NAMESPACE::Quatf> QuatfArray;
23 typedef FixedArray<IMATH_NAMESPACE::Quatd> QuatdArray;
24
25 }
26
27 template <class T> inline IMATH_NAMESPACE::Vec3<T> operator * (const IMATH_NAMESPACE::Vec3<T> &v, const IMATH_NAMESPACE::Quat<T> &q) { return v * q.toMatrix33(); }
28
29 template <class T> static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> >
30 operator *(const IMATH_NAMESPACE::Vec3<T> &va, const PyImath::FixedArray<IMATH_NAMESPACE::Quat<T> > &vb)
31 { size_t len = vb.len(); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va * vb[i]; return f; }
32
33 template <class T> static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> >
34 operator *(const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &va, const IMATH_NAMESPACE::Quat<T> &vb)
35 { 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] * vb; return f; }
36
37 template <class T>
38 static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> >
39 operator * (const PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > &va, const PyImath::FixedArray<IMATH_NAMESPACE::Quat<T> > &vb)
40 { size_t len = va.match_dimension(vb); PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len); for (size_t i = 0; i < len; ++i) f[i] = va[i] * vb[i]; return f; }
41
42 //
43
44 namespace PyImath {
45
46 // Other code in the Zeno code base assumes the existance of a class with the
47 // same name as the Imath class, and with static functions wrap() and
48 // convert() to produce a PyImath object from an Imath object and vice-versa,
49 // respectively. The class Boost generates from the Imath class does not
50 // have these properties, so we define a companion class here.
51 // The template argument, T, is the element type (e.g.,float, double).
52
53 template <class T>
54 class Q {
55 public:
56 static PyObject * wrap (const IMATH_NAMESPACE::Quat<T> &q);
57 static int convert (PyObject *p, IMATH_NAMESPACE::Quat<T> *q);
58 };
59
60 template <class T>
61 PyObject *
62 Q<T>::wrap (const IMATH_NAMESPACE::Quat<T> &q)
63 {
64 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Quat<T> >::type converter;
65 PyObject *p = converter (q);
66 return p;
67 }
68
69 template <class T>
70 int
71 Q<T>::convert (PyObject *p, IMATH_NAMESPACE::Quat<T> *q)
72 {
73 boost::python::extract <IMATH_NAMESPACE::Quatf> extractorQf (p);
74 if (extractorQf.check())
75 {
76 IMATH_NAMESPACE::Quatf qf = extractorQf();
77 q->r = T(qf.r);
78 q->v.setValue (qf.v);
79 return 1;
80 }
81
82 boost::python::extract <IMATH_NAMESPACE::Quatd> extractorQd (p);
83 if (extractorQd.check())
84 {
85 IMATH_NAMESPACE::Quatd qd = extractorQd();
86 q->r = T(qd.r);
87 q->v.setValue (qd.v);
88 return 1;
89 }
90
91 boost::python::extract <boost::python::tuple> extractorTuple (p);
92 if (extractorTuple.check())
93 {
94 boost::python::tuple t = extractorTuple();
95 if (t.attr ("__len__") () == 4)
96 {
97 // Extracting the tuple elements as doubles and casting them to
98 // Ts in setValue() works better than extracting them as Ts from
99 // the start.
100
101 double r = boost::python::extract <double> (t[0]);
102 double x = boost::python::extract <double> (t[1]);
103 double y = boost::python::extract <double> (t[2]);
104 double z = boost::python::extract <double> (t[3]);
105 q->r = T(r);
106 q->v.setValue (T(x), T(y), T(z));
107 return 1;
108 }
109 }
110
111 return 0;
112 }
113
114 typedef Q<float> Quatf;
115 typedef Q<double> Quatd;
116
117 }
118
119
120 #endif