Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [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 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 17 | #ifndef UI_VEC3_H_ |
| 18 | #define UI_VEC3_H_ |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 19 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 20 | #include <ui/vec2.h> |
| 21 | #include <ui/half.h> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 25 | #pragma clang diagnostic push |
| 26 | #pragma clang diagnostic ignored "-Wgnu-anonymous-struct" |
| 27 | #pragma clang diagnostic ignored "-Wnested-anon-types" |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | // ------------------------------------------------------------------------------------- |
| 31 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 32 | namespace details { |
| 33 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 34 | template <typename T> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 35 | class TVec3 : public TVecProductOperators<TVec3, T>, |
| 36 | public TVecAddOperators<TVec3, T>, |
| 37 | public TVecUnaryOperators<TVec3, T>, |
| 38 | public TVecComparisonOperators<TVec3, T>, |
| 39 | public TVecFunctions<TVec3, T>, |
| 40 | public TVecDebug<TVec3, T> { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 41 | public: |
| 42 | enum no_init { NO_INIT }; |
| 43 | typedef T value_type; |
| 44 | typedef T& reference; |
| 45 | typedef T const& const_reference; |
| 46 | typedef size_t size_type; |
| 47 | |
| 48 | union { |
| 49 | struct { T x, y, z; }; |
| 50 | struct { T s, t, p; }; |
| 51 | struct { T r, g, b; }; |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 52 | TVec2<T> xy; |
| 53 | TVec2<T> st; |
| 54 | TVec2<T> rg; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 57 | static constexpr size_t SIZE = 3; |
| 58 | inline constexpr size_type size() const { return SIZE; } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 59 | |
| 60 | // array access |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 61 | inline constexpr T const& operator[](size_t i) const { |
| 62 | #if __cplusplus >= 201402L |
| 63 | // only possible in C++0x14 with constexpr |
| 64 | assert(i < SIZE); |
| 65 | #endif |
| 66 | return (&x)[i]; |
| 67 | } |
| 68 | |
| 69 | inline T& operator[](size_t i) { |
| 70 | assert(i < SIZE); |
| 71 | return (&x)[i]; |
| 72 | } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 73 | |
| 74 | // ----------------------------------------------------------------------- |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 75 | // we want the compiler generated versions for these... |
| 76 | TVec3(const TVec3&) = default; |
| 77 | ~TVec3() = default; |
| 78 | TVec3& operator = (const TVec3&) = default; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 79 | |
| 80 | // constructors |
| 81 | // leaves object uninitialized. use with caution. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 82 | explicit |
| 83 | constexpr TVec3(no_init) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 84 | |
| 85 | // default constructor |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 86 | constexpr TVec3() : x(0), y(0), z(0) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 87 | |
| 88 | // handles implicit conversion to a tvec4. must not be explicit. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 89 | template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type> |
| 90 | constexpr TVec3(A v) : x(v), y(v), z(v) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 91 | |
| 92 | template<typename A, typename B, typename C> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 93 | constexpr TVec3(A x, B y, C z) : x(x), y(y), z(z) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 94 | |
| 95 | template<typename A, typename B> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 96 | constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(z) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 97 | |
| 98 | template<typename A> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 99 | explicit |
| 100 | constexpr TVec3(const TVec3<A>& v) : x(v.x), y(v.y), z(v.z) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 101 | |
| 102 | // cross product works only on vectors of size 3 |
| 103 | template <typename RT> |
| 104 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 105 | constexpr TVec3 cross(const TVec3& u, const TVec3<RT>& v) { |
| 106 | return TVec3( |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 107 | u.y*v.z - u.z*v.y, |
| 108 | u.z*v.x - u.x*v.z, |
| 109 | u.x*v.y - u.y*v.x); |
| 110 | } |
| 111 | }; |
| 112 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 113 | } // namespace details |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 114 | |
| 115 | // ---------------------------------------------------------------------------------------- |
| 116 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 117 | typedef details::TVec3<double> double3; |
| 118 | typedef details::TVec3<float> float3; |
| 119 | typedef details::TVec3<float> vec3; |
| 120 | typedef details::TVec3<half> half3; |
| 121 | typedef details::TVec3<int32_t> int3; |
| 122 | typedef details::TVec3<uint32_t> uint3; |
| 123 | typedef details::TVec3<int16_t> short3; |
| 124 | typedef details::TVec3<uint16_t> ushort3; |
| 125 | typedef details::TVec3<int8_t> byte3; |
| 126 | typedef details::TVec3<uint8_t> ubyte3; |
Romain Guy | 11ecb63 | 2017-01-27 20:04:01 -0800 | [diff] [blame] | 127 | typedef details::TVec3<bool> bool3; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 128 | |
| 129 | // ---------------------------------------------------------------------------------------- |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 130 | } // namespace android |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 131 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 132 | #pragma clang diagnostic pop |
| 133 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 134 | #endif // UI_VEC3_H_ |