blob: e3a6d148078ad9bb5b0c1ad92c5e2140917a7210 [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_VEC3_H_
18#define UI_VEC3_H_
Mathias Agopian595ea772013-08-21 23:10:41 -070019
Romain Guy5d4bae72016-11-08 09:49:25 -080020#include <ui/vec2.h>
21#include <ui/half.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070022#include <stdint.h>
23#include <sys/types.h>
24
Romain Guycaf2ca42016-11-10 11:45:58 -080025#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
27#pragma clang diagnostic ignored "-Wnested-anon-types"
Mathias Agopian595ea772013-08-21 23:10:41 -070028
29namespace android {
30// -------------------------------------------------------------------------------------
31
Romain Guy5d4bae72016-11-08 09:49:25 -080032namespace details {
33
Mathias Agopian595ea772013-08-21 23:10:41 -070034template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080035class TVec3 : public TVecProductOperators<TVec3, T>,
36 public TVecAddOperators<TVec3, T>,
37 public TVecUnaryOperators<TVec3, T>,
38 public TVecComparisonOperators<TVec3, T>,
39 public TVecFunctions<TVec3, T>,
40 public TVecDebug<TVec3, T> {
Mathias Agopian595ea772013-08-21 23:10:41 -070041public:
42 enum no_init { NO_INIT };
43 typedef T value_type;
44 typedef T& reference;
45 typedef T const& const_reference;
46 typedef size_t size_type;
47
48 union {
49 struct { T x, y, z; };
50 struct { T s, t, p; };
51 struct { T r, g, b; };
Romain Guy5d4bae72016-11-08 09:49:25 -080052 TVec2<T> xy;
53 TVec2<T> st;
54 TVec2<T> rg;
Mathias Agopian595ea772013-08-21 23:10:41 -070055 };
56
Romain Guy5d4bae72016-11-08 09:49:25 -080057 static constexpr size_t SIZE = 3;
58 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070059
60 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080061 inline constexpr T const& operator[](size_t i) const {
62#if __cplusplus >= 201402L
63 // only possible in C++0x14 with constexpr
64 assert(i < SIZE);
65#endif
66 return (&x)[i];
67 }
68
69 inline T& operator[](size_t i) {
70 assert(i < SIZE);
71 return (&x)[i];
72 }
Mathias Agopian595ea772013-08-21 23:10:41 -070073
74 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080075 // we want the compiler generated versions for these...
76 TVec3(const TVec3&) = default;
77 ~TVec3() = default;
78 TVec3& operator = (const TVec3&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070079
80 // constructors
81 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080082 explicit
83 constexpr TVec3(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070084
85 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080086 constexpr TVec3() : x(0), y(0), z(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 TVec3(A v) : x(v), y(v), z(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070091
92 template<typename A, typename B, typename C>
Romain Guy5d4bae72016-11-08 09:49:25 -080093 constexpr TVec3(A x, B y, C z) : x(x), y(y), z(z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070094
95 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -080096 constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070097
98 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -080099 explicit
100 constexpr TVec3(const TVec3<A>& v) : x(v.x), y(v.y), z(v.z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700101
102 // cross product works only on vectors of size 3
103 template <typename RT>
104 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800105 constexpr TVec3 cross(const TVec3& u, const TVec3<RT>& v) {
106 return TVec3(
Mathias Agopian595ea772013-08-21 23:10:41 -0700107 u.y*v.z - u.z*v.y,
108 u.z*v.x - u.x*v.z,
109 u.x*v.y - u.y*v.x);
110 }
111};
112
Romain Guy5d4bae72016-11-08 09:49:25 -0800113} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700114
115// ----------------------------------------------------------------------------------------
116
Romain Guy5d4bae72016-11-08 09:49:25 -0800117typedef details::TVec3<double> double3;
118typedef details::TVec3<float> float3;
119typedef details::TVec3<float> vec3;
120typedef details::TVec3<half> half3;
121typedef details::TVec3<int32_t> int3;
122typedef details::TVec3<uint32_t> uint3;
123typedef details::TVec3<int16_t> short3;
124typedef details::TVec3<uint16_t> ushort3;
125typedef details::TVec3<int8_t> byte3;
126typedef details::TVec3<uint8_t> ubyte3;
Romain Guy11ecb632017-01-27 20:04:01 -0800127typedef details::TVec3<bool> bool3;
Mathias Agopian595ea772013-08-21 23:10:41 -0700128
129// ----------------------------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -0800130} // namespace android
Mathias Agopian595ea772013-08-21 23:10:41 -0700131
Romain Guycaf2ca42016-11-10 11:45:58 -0800132#pragma clang diagnostic pop
133
Romain Guy5d4bae72016-11-08 09:49:25 -0800134#endif // UI_VEC3_H_