1 /*
2  * Copyright 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <math/half.h>
20 #include <math/TQuatHelpers.h>
21 #include <math/vec3.h>
22 #include <math/vec4.h>
23 
24 #include <stdint.h>
25 #include <sys/types.h>
26 
27 #ifndef PURE
28 #define PURE __attribute__((pure))
29 #endif
30 
31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
33 #pragma clang diagnostic ignored "-Wnested-anon-types"
34 
35 namespace android {
36 // -------------------------------------------------------------------------------------
37 
38 namespace details {
39 
40 template <typename T>
41 class TQuaternion : public TVecAddOperators<TQuaternion, T>,
42                     public TVecUnaryOperators<TQuaternion, T>,
43                     public TVecComparisonOperators<TQuaternion, T>,
44                     public TQuatProductOperators<TQuaternion, T>,
45                     public TQuatFunctions<TQuaternion, T>,
46                     public TQuatDebug<TQuaternion, T> {
47 public:
48     enum no_init { NO_INIT };
49     typedef T value_type;
50     typedef T& reference;
51     typedef T const& const_reference;
52     typedef size_t size_type;
53 
54     /*
55      * quaternion internals stored as:
56      *
57      * q = w + xi + yj + zk
58      *
59      *  q[0] = x;
60      *  q[1] = y;
61      *  q[2] = z;
62      *  q[3] = w;
63      *
64      */
65     union {
66         struct { T x, y, z, w; };
67         TVec4<T> xyzw;
68         TVec3<T> xyz;
69         TVec2<T> xy;
70     };
71 
72     enum { SIZE = 4 };
size()73     inline constexpr static size_type size() { return SIZE; }
74 
75     // array access
76     inline constexpr T const& operator[](size_t i) const {
77 #if __cplusplus >= 201402L
78         // only possible in C++0x14 with constexpr
79         assert(i < SIZE);
80 #endif
81         return (&x)[i];
82     }
83 
84     inline T& operator[](size_t i) {
85         assert(i < SIZE);
86         return (&x)[i];
87     }
88 
89     // -----------------------------------------------------------------------
90     // we want the compiler generated versions for these...
91     TQuaternion(const TQuaternion&) = default;
92     ~TQuaternion() = default;
93     TQuaternion& operator = (const TQuaternion&) = default;
94 
95     // constructors
96 
97     // leaves object uninitialized. use with caution.
98     explicit
TQuaternion(no_init)99     constexpr TQuaternion(no_init) : xyzw(TVec4<T>::NO_INIT) {}
100 
101     // default constructor. sets all values to zero.
TQuaternion()102     constexpr TQuaternion() : x(0), y(0), z(0), w(0) { }
103 
104     // handles implicit conversion to a tvec4. must not be explicit.
105     template<typename A>
TQuaternion(A w)106     constexpr TQuaternion(A w) : x(0), y(0), z(0), w(w) {
107         static_assert(std::is_arithmetic<A>::value, "requires arithmetic type");
108     }
109 
110     // initialize from 4 values to w + xi + yj + zk
111     template<typename A, typename B, typename C, typename D>
TQuaternion(A w,B x,C y,D z)112     constexpr TQuaternion(A w, B x, C y, D z) : x(static_cast<T>(x)), y(static_cast<T>(y)), z(static_cast<T>(z)), w(static_cast<T>(w)) { }
113 
114     // initialize from a vec3 + a value to : v.xi + v.yj + v.zk + w
115     template<typename A, typename B>
TQuaternion(const TVec3<A> & v,B w)116     constexpr TQuaternion(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
117 
118     // initialize from a double4
119     template<typename A>
TQuaternion(const TVec4<A> & v)120     constexpr explicit TQuaternion(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
121 
122     // initialize from a quaternion of a different type
123     template<typename A>
TQuaternion(const TQuaternion<A> & v)124     constexpr explicit TQuaternion(const TQuaternion<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
125 
126     // conjugate operator
127     constexpr TQuaternion operator~() const {
128         return conj(*this);
129     }
130 
131     // constructs a quaternion from an axis and angle
132     template <typename A, typename B>
fromAxisAngle(const TVec3<A> & axis,B angle)133     constexpr static TQuaternion PURE fromAxisAngle(const TVec3<A>& axis, B angle) {
134         return TQuaternion(std::sin(angle*0.5) * normalize(axis), std::cos(angle*0.5));
135     }
136 };
137 
138 }  // namespace details
139 
140 // ----------------------------------------------------------------------------------------
141 
142 typedef details::TQuaternion<double> quatd;
143 typedef details::TQuaternion<float> quat;
144 typedef details::TQuaternion<float> quatf;
145 typedef details::TQuaternion<half> quath;
146 
147 constexpr inline quat operator"" _i(long double v) {
148     return quat(0, static_cast<float>(v), 0, 0);
149 }
150 constexpr inline quat operator"" _j(long double v) {
151     return quat(0, 0, static_cast<float>(v), 0);
152 }
153 constexpr inline quat operator"" _k(long double v) {
154     return quat(0, 0, 0, static_cast<float>(v));
155 }
156 
157 constexpr inline quat operator"" _i(unsigned long long v) {  // NOLINT
158     return quat(0, static_cast<float>(v), 0, 0);
159 }
160 constexpr inline quat operator"" _j(unsigned long long v) {  // NOLINT
161     return quat(0, 0, static_cast<float>(v), 0);
162 }
163 constexpr inline quat operator"" _k(unsigned long long v) {  // NOLINT
164     return quat(0, 0, 0, static_cast<float>(v));
165 }
166 
167 constexpr inline quatd operator"" _id(long double v) {
168     return quatd(0, static_cast<double>(v), 0, 0);
169 }
170 constexpr inline quatd operator"" _jd(long double v) {
171     return quatd(0, 0, static_cast<double>(v), 0);
172 }
173 constexpr inline quatd operator"" _kd(long double v) {
174     return quatd(0, 0, 0, static_cast<double>(v));
175 }
176 
177 constexpr inline quatd operator"" _id(unsigned long long v) {  // NOLINT
178     return quatd(0, static_cast<double>(v), 0, 0);
179 }
180 constexpr inline quatd operator"" _jd(unsigned long long v) {  // NOLINT
181     return quatd(0, 0, static_cast<double>(v), 0);
182 }
183 constexpr inline quatd operator"" _kd(unsigned long long v) {  // NOLINT
184     return quatd(0, 0, 0, static_cast<double>(v));
185 }
186 
187 // ----------------------------------------------------------------------------------------
188 }  // namespace android
189 
190 #pragma clang diagnostic pop
191 
192 #undef PURE
193