blob: a88d02649187eff750274fe107624cb21385f97e [file] [log] [blame]
Mathias Agopian595ea772013-08-21 23:10:41 -07001/*
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 Guy5d4bae72016-11-08 09:49:25 -080017#ifndef UI_VEC2_H_
18#define UI_VEC2_H_
Mathias Agopian595ea772013-08-21 23:10:41 -070019
Romain Guy5d4bae72016-11-08 09:49:25 -080020#include <ui/TVecHelpers.h>
21#include <ui/half.h>
22#include <assert.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070023#include <stdint.h>
24#include <sys/types.h>
Romain Guy5d4bae72016-11-08 09:49:25 -080025#include <type_traits>
Mathias Agopian595ea772013-08-21 23:10:41 -070026
Mathias Agopian595ea772013-08-21 23:10:41 -070027
28namespace android {
29// -------------------------------------------------------------------------------------
30
Romain Guy5d4bae72016-11-08 09:49:25 -080031namespace details {
32
Mathias Agopian595ea772013-08-21 23:10:41 -070033template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080034class 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 Agopian595ea772013-08-21 23:10:41 -070040public:
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 Guy5d4bae72016-11-08 09:49:25 -080053 static constexpr size_t SIZE = 2;
54 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070055
56 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080057 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 Agopian595ea772013-08-21 23:10:41 -070069
70 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080071 // we want the compiler generated versions for these...
72 TVec2(const TVec2&) = default;
73 ~TVec2() = default;
74 TVec2& operator = (const TVec2&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070075
76 // constructors
77
78 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080079 explicit
80 constexpr TVec2(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070081
82 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080083 constexpr TVec2() : x(0), y(0) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070084
85 // handles implicit conversion to a tvec4. must not be explicit.
Romain Guy5d4bae72016-11-08 09:49:25 -080086 template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
87 constexpr TVec2(A v) : x(v), y(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070088
89 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -080090 constexpr TVec2(A x, B y) : x(x), y(y) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070091
92 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -080093 explicit
94 constexpr TVec2(const TVec2<A>& v) : x(v.x), y(v.y) { }
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070095
Romain Guy5d4bae72016-11-08 09:49:25 -080096 // 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 Agopian595ea772013-08-21 23:10:41 -0700102};
103
Romain Guy5d4bae72016-11-08 09:49:25 -0800104} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700105
106// ----------------------------------------------------------------------------------------
Mathias Agopian595ea772013-08-21 23:10:41 -0700107
Romain Guy5d4bae72016-11-08 09:49:25 -0800108typedef details::TVec2<double> double2;
109typedef details::TVec2<float> float2;
110typedef details::TVec2<float> vec2;
111typedef details::TVec2<half> half2;
112typedef details::TVec2<int32_t> int2;
113typedef details::TVec2<uint32_t> uint2;
114typedef details::TVec2<int16_t> short2;
115typedef details::TVec2<uint16_t> ushort2;
116typedef details::TVec2<int8_t> byte2;
117typedef details::TVec2<uint8_t> ubyte2;
118
119// ----------------------------------------------------------------------------------------
120} // namespace android
121
122#endif // UI_VEC2_H_