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 17 #ifndef TNT_MATH_VEC2_H 18 #define TNT_MATH_VEC2_H 19 20 #include "TVecHelpers.h" 21 22 #include <type_traits> 23 24 #include <assert.h> 25 #include <stdint.h> 26 #include <sys/types.h> 27 28 namespace filament { 29 namespace math { 30 // ------------------------------------------------------------------------------------- 31 32 namespace details { 33 34 template<typename T> 35 class MATH_EMPTY_BASES TVec2 : 36 public TVecProductOperators<TVec2, T>, 37 public TVecAddOperators<TVec2, T>, 38 public TVecUnaryOperators<TVec2, T>, 39 public TVecComparisonOperators<TVec2, T>, 40 public TVecFunctions<TVec2, T> { 41 public: 42 typedef T value_type; 43 typedef T& reference; 44 typedef T const& const_reference; 45 typedef size_t size_type; 46 static constexpr size_t SIZE = 2; 47 48 union { 49 T v[SIZE] MATH_CONSTEXPR_INIT; 50 struct { T x, y; }; 51 struct { T s, t; }; 52 struct { T r, g; }; 53 }; 54 size()55 inline constexpr size_type size() const { return SIZE; } 56 57 // array access 58 inline constexpr T const& operator[](size_t i) const noexcept { 59 assert(i < SIZE); 60 return v[i]; 61 } 62 63 inline constexpr T& operator[](size_t i) noexcept { 64 assert(i < SIZE); 65 return v[i]; 66 } 67 68 // constructors 69 70 // default constructor TVec2()71 MATH_DEFAULT_CTOR_CONSTEXPR TVec2() MATH_DEFAULT_CTOR 72 73 // handles implicit conversion to a tvec4. must not be explicit. 74 template<typename A, typename = enable_if_arithmetic_t<A>> 75 constexpr TVec2(A v) noexcept : v{ T(v), T(v) } {} 76 77 template<typename A, typename B, typename = enable_if_arithmetic_t<A, B>> TVec2(A x,B y)78 constexpr TVec2(A x, B y) noexcept : v{ T(x), T(y) } {} 79 80 template<typename A, typename = enable_if_arithmetic_t<A>> TVec2(const TVec2<A> & v)81 constexpr TVec2(const TVec2<A>& v) noexcept : v{ T(v[0]), T(v[1]) } {} 82 83 // cross product works only on vectors of size 2 or 3 84 template<typename U> 85 friend inline constexpr cross(const TVec2 & u,const TVec2<U> & v)86 arithmetic_result_t<T, U> cross(const TVec2& u, const TVec2<U>& v) noexcept { 87 return u[0] * v[1] - u[1] * v[0]; 88 } 89 }; 90 91 } // namespace details 92 93 // ---------------------------------------------------------------------------------------- 94 95 template<typename T, typename = details::enable_if_arithmetic_t<T>> 96 using vec2 = details::TVec2<T>; 97 98 using float2 = vec2<float>; 99 100 // ---------------------------------------------------------------------------------------- 101 } // namespace math 102 } // namespace filament 103 104 #endif // TNT_MATH_VEC2_H 105