1 | |
2 | // |
3 | // This source file is part of appleseed. |
4 | // Visit http://appleseedhq.net/ for additional information and resources. |
5 | // |
6 | // This software is released under the MIT license. |
7 | // |
8 | // Copyright (c) 2012-2013 Esteban Tovagliari, Jupiter Jazz Limited |
9 | // Copyright (c) 2014-2017 Esteban Tovagliari, The appleseedhq Organization |
10 | // |
11 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
12 | // of this software and associated documentation files (the "Software"), to deal |
13 | // in the Software without restriction, including without limitation the rights |
14 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
15 | // copies of the Software, and to permit persons to whom the Software is |
16 | // furnished to do so, subject to the following conditions: |
17 | // |
18 | // The above copyright notice and this permission notice shall be included in |
19 | // all copies or substantial portions of the Software. |
20 | // |
21 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
24 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
27 | // THE SOFTWARE. |
28 | // |
29 | |
30 | // appleseed.python headers. |
31 | #include "pyseed.h" // has to be first, to avoid redefinition warnings |
32 | |
33 | // appleseed.foundation headers. |
34 | #include "foundation/math/quaternion.h" |
35 | #include "foundation/utility/iostreamop.h" |
36 | |
37 | namespace bpy = boost::python; |
38 | using namespace foundation; |
39 | |
40 | namespace |
41 | { |
42 | template <typename T> |
43 | T quat_dot_prod(const Quaternion<T>& a, const Quaternion<T>& b) |
44 | { |
45 | return dot(a, b); |
46 | } |
47 | |
48 | template <typename T> |
49 | bpy::tuple (const Quaternion<T>& q) |
50 | { |
51 | Vector<T, 3> axis; |
52 | T angle; |
53 | |
54 | q.extract_axis_angle(axis, angle); |
55 | return bpy::make_tuple(axis, angle); |
56 | } |
57 | |
58 | template <typename T> |
59 | Quaternion<T> quat_conjugate(const Quaternion<T>& q) |
60 | { |
61 | return conjugate(q); |
62 | } |
63 | |
64 | template <typename T> |
65 | Quaternion<T> quat_inverse(const Quaternion<T>& q) |
66 | { |
67 | return inverse(q); |
68 | } |
69 | |
70 | template <typename T> |
71 | T quat_square_norm(const Quaternion<T>& q) |
72 | { |
73 | return square_norm(q); |
74 | } |
75 | |
76 | template <typename T> |
77 | T quat_norm(const Quaternion<T>& q) |
78 | { |
79 | return norm(q); |
80 | } |
81 | |
82 | template <typename T> |
83 | Quaternion<T> quat_normalize(const Quaternion<T>& q) |
84 | { |
85 | return normalize(q); |
86 | } |
87 | |
88 | template <typename T> |
89 | bool quat_is_normalized(const Quaternion<T>& q) |
90 | { |
91 | return is_normalized(q); |
92 | } |
93 | |
94 | template <typename T> |
95 | bool quat_is_normalized_with_eps(const Quaternion<T>& q, const T eps) |
96 | { |
97 | return is_normalized(q, eps); |
98 | } |
99 | |
100 | template <typename T> |
101 | Quaternion<T> quat_slerp(const Quaternion<T>& p, const Quaternion<T>& q, const T t) |
102 | { |
103 | return slerp(p, q, t); |
104 | } |
105 | |
106 | template <typename T> |
107 | void do_bind_quaternion(const char* class_name) |
108 | { |
109 | Quaternion<T>(*rot1)(const Vector<T, 3>&, T) = &Quaternion<T>::make_rotation; |
110 | Quaternion<T>(*rot2)(const Vector<T, 3>&, const Vector<T, 3>&) = &Quaternion<T>::make_rotation; |
111 | |
112 | bpy::class_<Quaternion<T> >(class_name) |
113 | .def("make_identity" , &Quaternion<T>::make_identity).staticmethod("make_identity" ) |
114 | .def("make_rotation" , rot1).def("make_rotation" , rot2).staticmethod("make_rotation" ) |
115 | |
116 | .def(bpy::init<>()) |
117 | .def(bpy::init<T, Vector<T, 3> >()) |
118 | |
119 | .def_readwrite("s" , &Quaternion<T>::s) |
120 | .def_readwrite("v" , &Quaternion<T>::v) |
121 | |
122 | // Operators. |
123 | .def(bpy::self + bpy::self) |
124 | .def(bpy::self - bpy::self) |
125 | .def(-bpy::self) |
126 | .def(bpy::self * T()) |
127 | .def(T() * bpy::self) |
128 | .def(bpy::self / T()) |
129 | .def(bpy::self += bpy::self) |
130 | .def(bpy::self -= bpy::self) |
131 | .def(bpy::self *= T()) |
132 | .def(bpy::self /= T()) |
133 | .def(bpy::self * bpy::self) |
134 | .def(bpy::self *= bpy::self) |
135 | |
136 | // Because of a bug in Boost.Python, this needs the extra self_ns qualification. |
137 | .def(bpy::self_ns::str(bpy::self)) |
138 | .def(bpy::self_ns::repr(bpy::self)) |
139 | |
140 | .def("dot" , &quat_dot_prod<T>) |
141 | .def("extract_axis_angle" , &quat_extract_axis_angle<T>) |
142 | |
143 | .def("conjugate" , &quat_conjugate<T>) |
144 | .def("inverse" , &quat_inverse<T>) |
145 | .def("square_norm" , &quat_square_norm<T>) |
146 | .def("norm" , &quat_norm<T>) |
147 | .def("normalize" , &quat_normalize<T>) |
148 | .def("is_normalized" , &quat_is_normalized<T>) |
149 | .def("is_normalized" , &quat_is_normalized_with_eps<T>) |
150 | .def("slerp" , &quat_slerp<T>); |
151 | } |
152 | } |
153 | |
154 | void bind_quaternion() |
155 | { |
156 | do_bind_quaternion<float>("Quaternionf" ); |
157 | do_bind_quaternion<double>("Quaterniond" ); |
158 | |
159 | #ifdef APPLESEED_ENABLE_IMATH_INTEROP |
160 | bpy::implicitly_convertible<Quaternionf, Imath::Quatf>(); |
161 | bpy::implicitly_convertible<Imath::Quatf, Quaternionf>(); |
162 | |
163 | bpy::implicitly_convertible<Quaterniond, Imath::Quatd>(); |
164 | bpy::implicitly_convertible<Imath::Quatd, Quaterniond>(); |
165 | #endif |
166 | } |
167 | |