| 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 |  | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 27 | #pragma clang diagnostic push | 
|  | 28 | #pragma clang diagnostic ignored "-Wgnu-anonymous-struct" | 
|  | 29 | #pragma clang diagnostic ignored "-Wnested-anon-types" | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 | // ------------------------------------------------------------------------------------- | 
|  | 33 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 34 | namespace details { | 
|  | 35 |  | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 36 | template <typename T> | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 37 | class TVec2 :   public TVecProductOperators<TVec2, T>, | 
|  | 38 | public TVecAddOperators<TVec2, T>, | 
|  | 39 | public TVecUnaryOperators<TVec2, T>, | 
|  | 40 | public TVecComparisonOperators<TVec2, T>, | 
|  | 41 | public TVecFunctions<TVec2, T>, | 
|  | 42 | public TVecDebug<TVec2, T> { | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 43 | public: | 
|  | 44 | enum no_init { NO_INIT }; | 
|  | 45 | typedef T value_type; | 
|  | 46 | typedef T& reference; | 
|  | 47 | typedef T const& const_reference; | 
|  | 48 | typedef size_t size_type; | 
|  | 49 |  | 
|  | 50 | union { | 
|  | 51 | struct { T x, y; }; | 
|  | 52 | struct { T s, t; }; | 
|  | 53 | struct { T r, g; }; | 
|  | 54 | }; | 
|  | 55 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 56 | static constexpr size_t SIZE = 2; | 
|  | 57 | inline constexpr size_type size() const { return SIZE; } | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | // array access | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 60 | inline constexpr T const& operator[](size_t i) const { | 
|  | 61 | #if __cplusplus >= 201402L | 
|  | 62 | // only possible in C++0x14 with constexpr | 
|  | 63 | assert(i < SIZE); | 
|  | 64 | #endif | 
|  | 65 | return (&x)[i]; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | inline T& operator[](size_t i) { | 
|  | 69 | assert(i < SIZE); | 
|  | 70 | return (&x)[i]; | 
|  | 71 | } | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | // ----------------------------------------------------------------------- | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 74 | // we want the compiler generated versions for these... | 
|  | 75 | TVec2(const TVec2&) = default; | 
|  | 76 | ~TVec2() = default; | 
|  | 77 | TVec2& operator = (const TVec2&) = default; | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | // constructors | 
|  | 80 |  | 
|  | 81 | // leaves object uninitialized. use with caution. | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 82 | explicit | 
|  | 83 | constexpr TVec2(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 TVec2() : x(0), y(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 TVec2(A v) : x(v), y(v) { } | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | template<typename A, typename B> | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 93 | constexpr TVec2(A x, B y) : x(x), y(y) { } | 
| Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | template<typename A> | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 96 | explicit | 
|  | 97 | constexpr TVec2(const TVec2<A>& v) : x(v.x), y(v.y) { } | 
| Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 98 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 99 | // cross product works only on vectors of size 2 or 3 | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 100 | template<typename RT> | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 101 | friend inline | 
|  | 102 | constexpr value_type cross(const TVec2& u, const TVec2<RT>& v) { | 
|  | 103 | return value_type(u.x*v.y - u.y*v.x); | 
|  | 104 | } | 
| 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::TVec2<double> double2; | 
|  | 112 | typedef details::TVec2<float> float2; | 
|  | 113 | typedef details::TVec2<float> vec2; | 
|  | 114 | typedef details::TVec2<half> half2; | 
|  | 115 | typedef details::TVec2<int32_t> int2; | 
|  | 116 | typedef details::TVec2<uint32_t> uint2; | 
|  | 117 | typedef details::TVec2<int16_t> short2; | 
|  | 118 | typedef details::TVec2<uint16_t> ushort2; | 
|  | 119 | typedef details::TVec2<int8_t> byte2; | 
|  | 120 | typedef details::TVec2<uint8_t> ubyte2; | 
| Romain Guy | 11ecb63 | 2017-01-27 20:04:01 -0800 | [diff] [blame] | 121 | typedef details::TVec2<bool> bool2; | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 122 |  | 
|  | 123 | // ---------------------------------------------------------------------------------------- | 
|  | 124 | }  // namespace android | 
|  | 125 |  | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 126 | #pragma clang diagnostic pop | 
|  | 127 |  | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 128 | #endif  // UI_VEC2_H_ |