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