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_VEC2_H_ |
| 18 | #define UI_VEC2_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/TVecHelpers.h> |
| 21 | #include <ui/half.h> |
| 22 | #include <assert.h> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 25 | #include <type_traits> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 26 | |
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 TVec2 : public TVecProductOperators<TVec2, T>, |
| 35 | public TVecAddOperators<TVec2, T>, |
| 36 | public TVecUnaryOperators<TVec2, T>, |
| 37 | public TVecComparisonOperators<TVec2, T>, |
| 38 | public TVecFunctions<TVec2, T>, |
| 39 | public TVecDebug<TVec2, 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; }; |
| 49 | struct { T s, t; }; |
| 50 | struct { T r, g; }; |
| 51 | }; |
| 52 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 53 | static constexpr size_t SIZE = 2; |
| 54 | inline constexpr size_type size() const { return SIZE; } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 55 | |
| 56 | // array access |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 57 | inline constexpr T const& operator[](size_t i) const { |
| 58 | #if __cplusplus >= 201402L |
| 59 | // only possible in C++0x14 with constexpr |
| 60 | assert(i < SIZE); |
| 61 | #endif |
| 62 | return (&x)[i]; |
| 63 | } |
| 64 | |
| 65 | inline T& operator[](size_t i) { |
| 66 | assert(i < SIZE); |
| 67 | return (&x)[i]; |
| 68 | } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 69 | |
| 70 | // ----------------------------------------------------------------------- |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 71 | // we want the compiler generated versions for these... |
| 72 | TVec2(const TVec2&) = default; |
| 73 | ~TVec2() = default; |
| 74 | TVec2& operator = (const TVec2&) = default; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 75 | |
| 76 | // constructors |
| 77 | |
| 78 | // leaves object uninitialized. use with caution. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 79 | explicit |
| 80 | constexpr TVec2(no_init) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 81 | |
| 82 | // default constructor |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 83 | constexpr TVec2() : x(0), y(0) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 84 | |
| 85 | // handles implicit conversion to a tvec4. must not be explicit. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 86 | template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type> |
| 87 | constexpr TVec2(A v) : x(v), y(v) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 88 | |
| 89 | template<typename A, typename B> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 90 | constexpr TVec2(A x, B y) : x(x), y(y) { } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 91 | |
| 92 | template<typename A> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 93 | explicit |
| 94 | constexpr TVec2(const TVec2<A>& v) : x(v.x), y(v.y) { } |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 95 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 96 | // cross product works only on vectors of size 2 or 3 |
| 97 | template <typename RT> |
| 98 | friend inline |
| 99 | constexpr value_type cross(const TVec2& u, const TVec2<RT>& v) { |
| 100 | return value_type(u.x*v.y - u.y*v.x); |
| 101 | } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 104 | } // namespace details |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 105 | |
| 106 | // ---------------------------------------------------------------------------------------- |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 107 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame^] | 108 | typedef details::TVec2<double> double2; |
| 109 | typedef details::TVec2<float> float2; |
| 110 | typedef details::TVec2<float> vec2; |
| 111 | typedef details::TVec2<half> half2; |
| 112 | typedef details::TVec2<int32_t> int2; |
| 113 | typedef details::TVec2<uint32_t> uint2; |
| 114 | typedef details::TVec2<int16_t> short2; |
| 115 | typedef details::TVec2<uint16_t> ushort2; |
| 116 | typedef details::TVec2<int8_t> byte2; |
| 117 | typedef details::TVec2<uint8_t> ubyte2; |
| 118 | |
| 119 | // ---------------------------------------------------------------------------------------- |
| 120 | } // namespace android |
| 121 | |
| 122 | #endif // UI_VEC2_H_ |