blob: fdd2e202fa3a0e4aced530cce68233332ee1b715 [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
Romain Guycaf2ca42016-11-10 11:45:58 -080027#pragma clang diagnostic push
28#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
29#pragma clang diagnostic ignored "-Wnested-anon-types"
Mathias Agopian595ea772013-08-21 23:10:41 -070030
31namespace android {
32// -------------------------------------------------------------------------------------
33
Romain Guy5d4bae72016-11-08 09:49:25 -080034namespace details {
35
Mathias Agopian595ea772013-08-21 23:10:41 -070036template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080037class 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 Agopian595ea772013-08-21 23:10:41 -070043public:
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 Guy5d4bae72016-11-08 09:49:25 -080056 static constexpr size_t SIZE = 2;
57 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070058
59 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080060 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 Agopian595ea772013-08-21 23:10:41 -070072
73 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080074 // we want the compiler generated versions for these...
75 TVec2(const TVec2&) = default;
76 ~TVec2() = default;
77 TVec2& operator = (const TVec2&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070078
79 // constructors
80
81 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080082 explicit
83 constexpr TVec2(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070084
85 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080086 constexpr TVec2() : x(0), y(0) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070087
88 // handles implicit conversion to a tvec4. must not be explicit.
Romain Guy5d4bae72016-11-08 09:49:25 -080089 template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
90 constexpr TVec2(A v) : x(v), y(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070091
92 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -080093 constexpr TVec2(A x, B y) : x(x), y(y) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070094
95 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -080096 explicit
97 constexpr TVec2(const TVec2<A>& v) : x(v.x), y(v.y) { }
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070098
Romain Guy5d4bae72016-11-08 09:49:25 -080099 // cross product works only on vectors of size 2 or 3
Romain Guycaf2ca42016-11-10 11:45:58 -0800100 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800101 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 Agopian595ea772013-08-21 23:10:41 -0700105};
106
Romain Guy5d4bae72016-11-08 09:49:25 -0800107} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700108
109// ----------------------------------------------------------------------------------------
Mathias Agopian595ea772013-08-21 23:10:41 -0700110
Romain Guy5d4bae72016-11-08 09:49:25 -0800111typedef details::TVec2<double> double2;
112typedef details::TVec2<float> float2;
113typedef details::TVec2<float> vec2;
114typedef details::TVec2<half> half2;
115typedef details::TVec2<int32_t> int2;
116typedef details::TVec2<uint32_t> uint2;
117typedef details::TVec2<int16_t> short2;
118typedef details::TVec2<uint16_t> ushort2;
119typedef details::TVec2<int8_t> byte2;
120typedef details::TVec2<uint8_t> ubyte2;
121
122// ----------------------------------------------------------------------------------------
123} // namespace android
124
Romain Guycaf2ca42016-11-10 11:45:58 -0800125#pragma clang diagnostic pop
126
Romain Guy5d4bae72016-11-08 09:49:25 -0800127#endif // UI_VEC2_H_