| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 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 | #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 Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 32 | #pragma clang diagnostic push | 
|  | 33 | #pragma clang diagnostic ignored "-Wgnu-anonymous-struct" | 
|  | 34 | #pragma clang diagnostic ignored "-Wnested-anon-types" | 
|  | 35 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 36 | namespace android { | 
|  | 37 | // ------------------------------------------------------------------------------------- | 
|  | 38 |  | 
|  | 39 | namespace details { | 
|  | 40 |  | 
|  | 41 | template <typename T> | 
|  | 42 | class 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> { | 
|  | 48 | public: | 
|  | 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 |  | 
|  | 143 | typedef details::TQuaternion<double> quatd; | 
|  | 144 | typedef details::TQuaternion<float> quat; | 
|  | 145 | typedef details::TQuaternion<float> quatf; | 
|  | 146 | typedef details::TQuaternion<half> quath; | 
|  | 147 |  | 
|  | 148 | constexpr inline quat operator"" _i(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 149 | return quat(0, static_cast<float>(v), 0, 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 150 | } | 
|  | 151 | constexpr inline quat operator"" _j(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 152 | return quat(0, 0, static_cast<float>(v), 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 153 | } | 
|  | 154 | constexpr inline quat operator"" _k(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 155 | return quat(0, 0, 0, static_cast<float>(v)); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | constexpr inline quat operator"" _i(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 159 | return quat(0, static_cast<float>(v), 0, 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 160 | } | 
|  | 161 | constexpr inline quat operator"" _j(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 162 | return quat(0, 0, static_cast<float>(v), 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 163 | } | 
|  | 164 | constexpr inline quat operator"" _k(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 165 | return quat(0, 0, 0, static_cast<float>(v)); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | constexpr inline quatd operator"" _id(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 169 | return quatd(0, static_cast<double>(v), 0, 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 170 | } | 
|  | 171 | constexpr inline quatd operator"" _jd(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 172 | return quatd(0, 0, static_cast<double>(v), 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 173 | } | 
|  | 174 | constexpr inline quatd operator"" _kd(long double v) { | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 175 | return quatd(0, 0, 0, static_cast<double>(v)); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | constexpr inline quatd operator"" _id(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 179 | return quatd(0, static_cast<double>(v), 0, 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 180 | } | 
|  | 181 | constexpr inline quatd operator"" _jd(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 182 | return quatd(0, 0, static_cast<double>(v), 0); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 183 | } | 
|  | 184 | constexpr inline quatd operator"" _kd(unsigned long long v) {  // NOLINT | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 185 | return quatd(0, 0, 0, static_cast<double>(v)); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | // ---------------------------------------------------------------------------------------- | 
|  | 189 | }  // namespace android | 
|  | 190 |  | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 191 | #pragma clang diagnostic pop | 
|  | 192 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 193 | #undef PURE | 
|  | 194 |  | 
|  | 195 | #endif  // UI_QUAT_H_ |