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_VEC4_H_ |
| 18 | #define UI_VEC4_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/vec3.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 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | // ------------------------------------------------------------------------------------- |
| 28 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 29 | namespace details { |
| 30 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 31 | template <typename T> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 32 | class TVec4 : public TVecProductOperators<TVec4, T>, |
| 33 | public TVecAddOperators<TVec4, T>, |
| 34 | public TVecUnaryOperators<TVec4, T>, |
| 35 | public TVecComparisonOperators<TVec4, T>, |
| 36 | public TVecFunctions<TVec4, T>, |
| 37 | public TVecDebug<TVec4, T> { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 38 | public: |
| 39 | enum no_init { NO_INIT }; |
| 40 | typedef T value_type; |
| 41 | typedef T& reference; |
| 42 | typedef T const& const_reference; |
| 43 | typedef size_t size_type; |
| 44 | |
| 45 | union { |
| 46 | struct { T x, y, z, w; }; |
| 47 | struct { T s, t, p, q; }; |
| 48 | struct { T r, g, b, a; }; |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 49 | TVec2<T> xy; |
| 50 | TVec2<T> st; |
| 51 | TVec2<T> rg; |
| 52 | TVec3<T> xyz; |
| 53 | TVec3<T> stp; |
| 54 | TVec3<T> rgb; |
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 = 4; |
| 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 | TVec4(const TVec4&) = default; |
| 77 | ~TVec4() = default; |
| 78 | TVec4& operator = (const TVec4&) = default; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 79 | |
| 80 | // constructors |
| 81 | |
| 82 | // leaves object uninitialized. use with caution. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 83 | explicit |
| 84 | constexpr TVec4(no_init) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 85 | |
| 86 | // default constructor |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 87 | constexpr TVec4() : x(0), y(0), z(0), w(0) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 88 | |
| 89 | // handles implicit conversion to a tvec4. must not be explicit. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 90 | template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type> |
| 91 | constexpr TVec4(A v) : x(v), y(v), z(v), w(v) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 92 | |
| 93 | template<typename A, typename B, typename C, typename D> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 94 | constexpr TVec4(A x, B y, C z, D w) : x(x), y(y), z(z), w(w) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 95 | |
| 96 | template<typename A, typename B, typename C> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 97 | constexpr TVec4(const TVec2<A>& v, B z, C w) : x(v.x), y(v.y), z(z), w(w) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 98 | |
| 99 | template<typename A, typename B> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 100 | constexpr TVec4(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 101 | |
| 102 | template<typename A> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 103 | explicit |
| 104 | constexpr TVec4(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 107 | } // namespace details |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 108 | |
| 109 | // ---------------------------------------------------------------------------------------- |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 110 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 111 | typedef details::TVec4<double> double4; |
| 112 | typedef details::TVec4<float> float4; |
| 113 | typedef details::TVec4<float> vec4; |
| 114 | typedef details::TVec4<half> half4; |
| 115 | typedef details::TVec4<int32_t> int4; |
| 116 | typedef details::TVec4<uint32_t> uint4; |
| 117 | typedef details::TVec4<int16_t> short4; |
| 118 | typedef details::TVec4<uint16_t> ushort4; |
| 119 | typedef details::TVec4<int8_t> byte4; |
| 120 | typedef details::TVec4<uint8_t> ubyte4; |
| 121 | |
| 122 | // ---------------------------------------------------------------------------------------- |
| 123 | } // namespace android |
| 124 | |
| 125 | #endif // UI_VEC4_H_ |