1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "pxr/pxr.h"
26 #include "pxr/base/gf/quaternion.h"
27 #include "pxr/base/tf/pyUtils.h"
28 #include "pxr/base/tf/wrapTypeHelpers.h"
29 #include "pxr/base/tf/pyContainerConversions.h"
30 
31 #include <boost/python/class.hpp>
32 #include <boost/python/def.hpp>
33 #include <boost/python/copy_const_reference.hpp>
34 #include <boost/python/operators.hpp>
35 #include <boost/python/overloads.hpp>
36 #include <boost/python/return_arg.hpp>
37 
38 #include <string>
39 
40 using namespace boost::python;
41 
42 using std::string;
43 
44 PXR_NAMESPACE_USING_DIRECTIVE
45 
46 namespace {
47 
48 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( GetNormalized_overloads,
49                                         GetNormalized, 0, 1 );
50 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( Normalize_overloads,
51                                         Normalize, 0, 1 );
52 
53 #if PY_MAJOR_VERSION == 2
__truediv__(const GfQuaternion & self,double value)54 static GfQuaternion __truediv__(const GfQuaternion &self, double value)
55 {
56     return self / value;
57 }
58 
__itruediv__(GfQuaternion & self,double value)59 static GfQuaternion __itruediv__(GfQuaternion &self, double value)
60 {
61     return self /= value;
62 }
63 #endif
64 
_Repr(GfQuaternion const & self)65 static string _Repr(GfQuaternion const &self) {
66     return TF_PY_REPR_PREFIX + "Quaternion(" + TfPyRepr(self.GetReal()) + ", " +
67         TfPyRepr(self.GetImaginary()) + ")";
68 }
69 
__hash__(GfQuaternion const & self)70 static size_t __hash__(GfQuaternion const &self) { return hash_value(self); }
71 
72 } // anonymous namespace
73 
wrapQuaternion()74 void wrapQuaternion()
75 {
76     typedef GfQuaternion This;
77 
78     object getImaginary =
79         make_function(&This::GetImaginary,return_value_policy<return_by_value>());
80 
81     def( "Slerp", (GfQuaternion (*)(double, const GfQuaternion&, const GfQuaternion&))GfSlerp);
82 
83     class_<This> ( "Quaternion", "Quaternion class", init<>())
84 
85         .def(init<int>())
86 
87         .def(init<double, const GfVec3d &>())
88 
89         .def( TfTypePythonClass() )
90 
91         .def("GetIdentity", &This::GetIdentity)
92         .staticmethod("GetIdentity")
93 
94         .add_property("real", &This::GetReal, &This::SetReal)
95         .add_property("imaginary", getImaginary, &This::SetImaginary)
96 
97         .def("GetImaginary", getImaginary)
98         .def("GetInverse", &This::GetInverse)
99         .def("GetLength", &This::GetLength)
100         .def("GetReal", &This::GetReal)
101 
102         .def("GetNormalized", &This::GetNormalized, GetNormalized_overloads())
103         .def("Normalize", &This::Normalize,
104              Normalize_overloads()[return_self<>()])
105 
106         .def( str(self) )
107         .def( self == self )
108         .def( self != self )
109         .def( self *= self )
110         .def( self *= double() )
111         .def( self /= double() )
112         .def( self += self )
113         .def( self -= self )
114         .def( self + self )
115         .def( self - self )
116         .def( self * self )
117         .def( self * double() )
118         .def( double() * self )
119         .def( self / double() )
120 
121 #if PY_MAJOR_VERSION == 2
122         // Needed only to support "from __future__ import division" in
123         // python 2. In python 3 builds boost::python adds this for us.
124         .def("__truediv__", __truediv__ )
125         .def("__itruediv__", __itruediv__ )
126 #endif
127 
128         .def("__repr__", _Repr)
129         .def("__hash__", __hash__)
130 
131         ;
132     to_python_converter<std::vector<This>,
133         TfPySequenceToPython<std::vector<This> > >();
134 
135 }
136