blob: 5b8cd8b9fe4986e336ee23b43121d452ad6ff962 [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
Romain Guycaf2ca42016-11-10 11:45:58 -080032#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
34#pragma clang diagnostic ignored "-Wnested-anon-types"
35
Romain Guy5d4bae72016-11-08 09:49:25 -080036namespace android {
37// -------------------------------------------------------------------------------------
38
39namespace details {
40
41template <typename T>
42class TQuaternion : public TVecAddOperators<TQuaternion, T>,
43 public TVecUnaryOperators<TQuaternion, T>,
44 public TVecComparisonOperators<TQuaternion, T>,
45 public TQuatProductOperators<TQuaternion, T>,
46 public TQuatFunctions<TQuaternion, T>,
47 public TQuatDebug<TQuaternion, T> {
48public:
49 enum no_init { NO_INIT };
50 typedef T value_type;
51 typedef T& reference;
52 typedef T const& const_reference;
53 typedef size_t size_type;
54
55 /*
56 * quaternion internals stored as:
57 *
58 * q = w + xi + yj + zk
59 *
60 * q[0] = x;
61 * q[1] = y;
62 * q[2] = z;
63 * q[3] = w;
64 *
65 */
66 union {
67 struct { T x, y, z, w; };
68 TVec4<T> xyzw;
69 TVec3<T> xyz;
70 TVec2<T> xy;
71 };
72
73 enum { SIZE = 4 };
74 inline constexpr static size_type size() { return SIZE; }
75
76 // array access
77 inline constexpr T const& operator[](size_t i) const {
78#if __cplusplus >= 201402L
79 // only possible in C++0x14 with constexpr
80 assert(i < SIZE);
81#endif
82 return (&x)[i];
83 }
84
85 inline T& operator[](size_t i) {
86 assert(i < SIZE);
87 return (&x)[i];
88 }
89
90 // -----------------------------------------------------------------------
91 // we want the compiler generated versions for these...
92 TQuaternion(const TQuaternion&) = default;
93 ~TQuaternion() = default;
94 TQuaternion& operator = (const TQuaternion&) = default;
95
96 // constructors
97
98 // leaves object uninitialized. use with caution.
99 explicit
100 constexpr TQuaternion(no_init) : xyzw(TVec4<T>::NO_INIT) {}
101
102 // default constructor. sets all values to zero.
103 constexpr TQuaternion() : x(0), y(0), z(0), w(0) { }
104
105 // handles implicit conversion to a tvec4. must not be explicit.
106 template<typename A>
107 constexpr TQuaternion(A w) : x(0), y(0), z(0), w(w) {
108 static_assert(std::is_arithmetic<A>::value, "requires arithmetic type");
109 }
110
111 // initialize from 4 values to w + xi + yj + zk
112 template<typename A, typename B, typename C, typename D>
113 constexpr TQuaternion(A w, B x, C y, D z) : x(x), y(y), z(z), w(w) { }
114
115 // initialize from a vec3 + a value to : v.xi + v.yj + v.zk + w
116 template<typename A, typename B>
117 constexpr TQuaternion(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
118
119 // initialize from a double4
120 template<typename A>
121 constexpr explicit TQuaternion(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
122
123 // initialize from a quaternion of a different type
124 template<typename A>
125 constexpr explicit TQuaternion(const TQuaternion<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
126
127 // conjugate operator
128 constexpr TQuaternion operator~() const {
129 return conj(*this);
130 }
131
132 // constructs a quaternion from an axis and angle
133 template <typename A, typename B>
134 constexpr static TQuaternion PURE fromAxisAngle(const TVec3<A>& axis, B angle) {
135 return TQuaternion(std::sin(angle*0.5) * normalize(axis), std::cos(angle*0.5));
136 }
137};
138
139} // namespace details
140
141// ----------------------------------------------------------------------------------------
142
143typedef details::TQuaternion<double> quatd;
144typedef details::TQuaternion<float> quat;
145typedef details::TQuaternion<float> quatf;
146typedef details::TQuaternion<half> quath;
147
148constexpr inline quat operator"" _i(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800149 return quat(0, static_cast<float>(v), 0, 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800150}
151constexpr inline quat operator"" _j(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800152 return quat(0, 0, static_cast<float>(v), 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800153}
154constexpr inline quat operator"" _k(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800155 return quat(0, 0, 0, static_cast<float>(v));
Romain Guy5d4bae72016-11-08 09:49:25 -0800156}
157
158constexpr inline quat operator"" _i(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800159 return quat(0, static_cast<float>(v), 0, 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800160}
161constexpr inline quat operator"" _j(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800162 return quat(0, 0, static_cast<float>(v), 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800163}
164constexpr inline quat operator"" _k(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800165 return quat(0, 0, 0, static_cast<float>(v));
Romain Guy5d4bae72016-11-08 09:49:25 -0800166}
167
168constexpr inline quatd operator"" _id(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800169 return quatd(0, static_cast<double>(v), 0, 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800170}
171constexpr inline quatd operator"" _jd(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800172 return quatd(0, 0, static_cast<double>(v), 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800173}
174constexpr inline quatd operator"" _kd(long double v) {
Romain Guycaf2ca42016-11-10 11:45:58 -0800175 return quatd(0, 0, 0, static_cast<double>(v));
Romain Guy5d4bae72016-11-08 09:49:25 -0800176}
177
178constexpr inline quatd operator"" _id(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800179 return quatd(0, static_cast<double>(v), 0, 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800180}
181constexpr inline quatd operator"" _jd(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800182 return quatd(0, 0, static_cast<double>(v), 0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800183}
184constexpr inline quatd operator"" _kd(unsigned long long v) { // NOLINT
Romain Guycaf2ca42016-11-10 11:45:58 -0800185 return quatd(0, 0, 0, static_cast<double>(v));
Romain Guy5d4bae72016-11-08 09:49:25 -0800186}
187
188// ----------------------------------------------------------------------------------------
189} // namespace android
190
Romain Guycaf2ca42016-11-10 11:45:58 -0800191#pragma clang diagnostic pop
192
Romain Guy5d4bae72016-11-08 09:49:25 -0800193#undef PURE
194
195#endif // UI_QUAT_H_