blob: 8c89cd7eb99a79cb335e11bea865f939c89f5c1c [file] [log] [blame]
Romain Guy5d4bae72016-11-08 09:49:25 -08001/*
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#ifndef UI_QUAT_H_
18#define UI_QUAT_H_
19
20#include <ui/half.h>
21#include <ui/TQuatHelpers.h>
22#include <ui/vec3.h>
23#include <ui/vec4.h>
24
25#include <stdint.h>
26#include <sys/types.h>
27
28#ifndef PURE
29#define PURE __attribute__((pure))
30#endif
31
32namespace android {
33// -------------------------------------------------------------------------------------
34
35namespace details {
36
37template <typename T>
38class TQuaternion : public TVecAddOperators<TQuaternion, T>,
39 public TVecUnaryOperators<TQuaternion, T>,
40 public TVecComparisonOperators<TQuaternion, T>,
41 public TQuatProductOperators<TQuaternion, T>,
42 public TQuatFunctions<TQuaternion, T>,
43 public TQuatDebug<TQuaternion, T> {
44public:
45 enum no_init { NO_INIT };
46 typedef T value_type;
47 typedef T& reference;
48 typedef T const& const_reference;
49 typedef size_t size_type;
50
51 /*
52 * quaternion internals stored as:
53 *
54 * q = w + xi + yj + zk
55 *
56 * q[0] = x;
57 * q[1] = y;
58 * q[2] = z;
59 * q[3] = w;
60 *
61 */
62 union {
63 struct { T x, y, z, w; };
64 TVec4<T> xyzw;
65 TVec3<T> xyz;
66 TVec2<T> xy;
67 };
68
69 enum { SIZE = 4 };
70 inline constexpr static size_type size() { return SIZE; }
71
72 // array access
73 inline constexpr T const& operator[](size_t i) const {
74#if __cplusplus >= 201402L
75 // only possible in C++0x14 with constexpr
76 assert(i < SIZE);
77#endif
78 return (&x)[i];
79 }
80
81 inline T& operator[](size_t i) {
82 assert(i < SIZE);
83 return (&x)[i];
84 }
85
86 // -----------------------------------------------------------------------
87 // we want the compiler generated versions for these...
88 TQuaternion(const TQuaternion&) = default;
89 ~TQuaternion() = default;
90 TQuaternion& operator = (const TQuaternion&) = default;
91
92 // constructors
93
94 // leaves object uninitialized. use with caution.
95 explicit
96 constexpr TQuaternion(no_init) : xyzw(TVec4<T>::NO_INIT) {}
97
98 // default constructor. sets all values to zero.
99 constexpr TQuaternion() : x(0), y(0), z(0), w(0) { }
100
101 // handles implicit conversion to a tvec4. must not be explicit.
102 template<typename A>
103 constexpr TQuaternion(A w) : x(0), y(0), z(0), w(w) {
104 static_assert(std::is_arithmetic<A>::value, "requires arithmetic type");
105 }
106
107 // initialize from 4 values to w + xi + yj + zk
108 template<typename A, typename B, typename C, typename D>
109 constexpr TQuaternion(A w, B x, C y, D z) : x(x), y(y), z(z), w(w) { }
110
111 // initialize from a vec3 + a value to : v.xi + v.yj + v.zk + w
112 template<typename A, typename B>
113 constexpr TQuaternion(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
114
115 // initialize from a double4
116 template<typename A>
117 constexpr explicit TQuaternion(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
118
119 // initialize from a quaternion of a different type
120 template<typename A>
121 constexpr explicit TQuaternion(const TQuaternion<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
122
123 // conjugate operator
124 constexpr TQuaternion operator~() const {
125 return conj(*this);
126 }
127
128 // constructs a quaternion from an axis and angle
129 template <typename A, typename B>
130 constexpr static TQuaternion PURE fromAxisAngle(const TVec3<A>& axis, B angle) {
131 return TQuaternion(std::sin(angle*0.5) * normalize(axis), std::cos(angle*0.5));
132 }
133};
134
135} // namespace details
136
137// ----------------------------------------------------------------------------------------
138
139typedef details::TQuaternion<double> quatd;
140typedef details::TQuaternion<float> quat;
141typedef details::TQuaternion<float> quatf;
142typedef details::TQuaternion<half> quath;
143
144constexpr inline quat operator"" _i(long double v) {
145 return quat(0, v, 0, 0);
146}
147constexpr inline quat operator"" _j(long double v) {
148 return quat(0, 0, v, 0);
149}
150constexpr inline quat operator"" _k(long double v) {
151 return quat(0, 0, 0, v);
152}
153
154constexpr inline quat operator"" _i(unsigned long long v) { // NOLINT
155 return quat(0, v, 0, 0);
156}
157constexpr inline quat operator"" _j(unsigned long long v) { // NOLINT
158 return quat(0, 0, v, 0);
159}
160constexpr inline quat operator"" _k(unsigned long long v) { // NOLINT
161 return quat(0, 0, 0, v);
162}
163
164constexpr inline quatd operator"" _id(long double v) {
165 return quatd(0, v, 0, 0);
166}
167constexpr inline quatd operator"" _jd(long double v) {
168 return quatd(0, 0, v, 0);
169}
170constexpr inline quatd operator"" _kd(long double v) {
171 return quatd(0, 0, 0, v);
172}
173
174constexpr inline quatd operator"" _id(unsigned long long v) { // NOLINT
175 return quatd(0, v, 0, 0);
176}
177constexpr inline quatd operator"" _jd(unsigned long long v) { // NOLINT
178 return quatd(0, 0, v, 0);
179}
180constexpr inline quatd operator"" _kd(unsigned long long v) { // NOLINT
181 return quatd(0, 0, 0, v);
182}
183
184// ----------------------------------------------------------------------------------------
185} // namespace android
186
187#undef PURE
188
189#endif // UI_QUAT_H_