blob: 0254f5a2c65432b41c1c256cee2340d3ef2b841d [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
Mathias Agopian595ea772013-08-21 23:10:41 -070025
26namespace android {
27// -------------------------------------------------------------------------------------
28
Romain Guy5d4bae72016-11-08 09:49:25 -080029namespace details {
30
Mathias Agopian595ea772013-08-21 23:10:41 -070031template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080032class TVec3 : public TVecProductOperators<TVec3, T>,
33 public TVecAddOperators<TVec3, T>,
34 public TVecUnaryOperators<TVec3, T>,
35 public TVecComparisonOperators<TVec3, T>,
36 public TVecFunctions<TVec3, T>,
37 public TVecDebug<TVec3, T> {
Mathias Agopian595ea772013-08-21 23:10:41 -070038public:
39 enum no_init { NO_INIT };
40 typedef T value_type;
41 typedef T& reference;
42 typedef T const& const_reference;
43 typedef size_t size_type;
44
45 union {
46 struct { T x, y, z; };
47 struct { T s, t, p; };
48 struct { T r, g, b; };
Romain Guy5d4bae72016-11-08 09:49:25 -080049 TVec2<T> xy;
50 TVec2<T> st;
51 TVec2<T> rg;
Mathias Agopian595ea772013-08-21 23:10:41 -070052 };
53
Romain Guy5d4bae72016-11-08 09:49:25 -080054 static constexpr size_t SIZE = 3;
55 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070056
57 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080058 inline constexpr T const& operator[](size_t i) const {
59#if __cplusplus >= 201402L
60 // only possible in C++0x14 with constexpr
61 assert(i < SIZE);
62#endif
63 return (&x)[i];
64 }
65
66 inline T& operator[](size_t i) {
67 assert(i < SIZE);
68 return (&x)[i];
69 }
Mathias Agopian595ea772013-08-21 23:10:41 -070070
71 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080072 // we want the compiler generated versions for these...
73 TVec3(const TVec3&) = default;
74 ~TVec3() = default;
75 TVec3& operator = (const TVec3&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070076
77 // constructors
78 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080079 explicit
80 constexpr TVec3(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070081
82 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080083 constexpr TVec3() : x(0), y(0), z(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 TVec3(A v) : x(v), y(v), z(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070088
89 template<typename A, typename B, typename C>
Romain Guy5d4bae72016-11-08 09:49:25 -080090 constexpr TVec3(A x, B y, C z) : x(x), y(y), z(z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070091
92 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -080093 constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070094
95 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -080096 explicit
97 constexpr TVec3(const TVec3<A>& v) : x(v.x), y(v.y), z(v.z) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070098
99 // cross product works only on vectors of size 3
100 template <typename RT>
101 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800102 constexpr TVec3 cross(const TVec3& u, const TVec3<RT>& v) {
103 return TVec3(
Mathias Agopian595ea772013-08-21 23:10:41 -0700104 u.y*v.z - u.z*v.y,
105 u.z*v.x - u.x*v.z,
106 u.x*v.y - u.y*v.x);
107 }
108};
109
Romain Guy5d4bae72016-11-08 09:49:25 -0800110} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700111
112// ----------------------------------------------------------------------------------------
113
Romain Guy5d4bae72016-11-08 09:49:25 -0800114typedef details::TVec3<double> double3;
115typedef details::TVec3<float> float3;
116typedef details::TVec3<float> vec3;
117typedef details::TVec3<half> half3;
118typedef details::TVec3<int32_t> int3;
119typedef details::TVec3<uint32_t> uint3;
120typedef details::TVec3<int16_t> short3;
121typedef details::TVec3<uint16_t> ushort3;
122typedef details::TVec3<int8_t> byte3;
123typedef details::TVec3<uint8_t> ubyte3;
Mathias Agopian595ea772013-08-21 23:10:41 -0700124
125// ----------------------------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -0800126} // namespace android
Mathias Agopian595ea772013-08-21 23:10:41 -0700127
Romain Guy5d4bae72016-11-08 09:49:25 -0800128#endif // UI_VEC3_H_