HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyImathQuat.h
Go to the documentation of this file.
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 HBOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <hboost/python.hpp>
14 #include <ImathQuat.h>
15 #include <ImathVec.h>
16 #include "PyImath.h"
17 
18 namespace PyImath {
19 
20 template <class T> hboost::python::class_<IMATH_NAMESPACE::Quat<T> > register_Quat();
21 template <class T> hboost::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>
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 hboost::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  hboost::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  hboost::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  hboost::python::extract <hboost::python::tuple> extractorTuple (p);
92  if (extractorTuple.check())
93  {
94  hboost::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 = hboost::python::extract <double> (t[0]);
102  double x = hboost::python::extract <double> (t[1]);
103  double y = hboost::python::extract <double> (t[2]);
104  double z = hboost::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
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:561
const GLdouble * v
Definition: glcorearb.h:837
static int convert(PyObject *p, IMATH_NAMESPACE::Quat< T > *q)
Definition: PyImathQuat.h:71
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLint y
Definition: glcorearb.h:103
GLdouble GLdouble GLdouble q
Definition: glad.h:2445
GLfloat f
Definition: glcorearb.h:1926
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
FixedArray< IMATH_NAMESPACE::Quatf > QuatfArray
Definition: PyImath.h:31
hboost::python::class_< IMATH_NAMESPACE::Quat< T > > register_Quat()
Quat< double > Quatd
Quaternion of type double.
Definition: ImathQuat.h:301
Quat< float > Quatf
Quaternion of type float.
Definition: ImathQuat.h:298
Q< double > Quatd
Definition: PyImathQuat.h:115
GLint GLenum GLint x
Definition: glcorearb.h:409
GLdouble t
Definition: glad.h:2397
Q< float > Quatf
Definition: PyImathQuat.h:114
FixedArray< IMATH_NAMESPACE::Quatd > QuatdArray
Definition: PyImath.h:32
GLboolean r
Definition: glcorearb.h:1222
static PyObject * wrap(const IMATH_NAMESPACE::Quat< T > &q)
Definition: PyImathQuat.h:62
hboost::python::class_< PyImath::FixedArray< IMATH_NAMESPACE::Quat< T > > > register_QuatArray()