OSDN Git Service

0254f5a2c65432b41c1c256cee2340d3ef2b841d
[android-x86/frameworks-native.git] / include / ui / vec3.h
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 UI_VEC3_H_
18 #define UI_VEC3_H_
19
20 #include <ui/vec2.h>
21 #include <ui/half.h>
22 #include <stdint.h>
23 #include <sys/types.h>
24
25
26 namespace android {
27 // -------------------------------------------------------------------------------------
28
29 namespace details {
30
31 template <typename T>
32 class 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> {
38 public:
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; };
49         TVec2<T> xy;
50         TVec2<T> st;
51         TVec2<T> rg;
52     };
53
54     static constexpr size_t SIZE = 3;
55     inline constexpr size_type size() const { return SIZE; }
56
57     // array access
58     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     }
70
71     // -----------------------------------------------------------------------
72     // we want the compiler generated versions for these...
73     TVec3(const TVec3&) = default;
74     ~TVec3() = default;
75     TVec3& operator = (const TVec3&) = default;
76
77     // constructors
78     // leaves object uninitialized. use with caution.
79     explicit
80     constexpr TVec3(no_init) { }
81
82     // default constructor
83     constexpr TVec3() : x(0), y(0), z(0) { }
84
85     // handles implicit conversion to a tvec4. must not be explicit.
86     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) { }
88
89     template<typename A, typename B, typename C>
90     constexpr TVec3(A x, B y, C z) : x(x), y(y), z(z) { }
91
92     template<typename A, typename B>
93     constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
94
95     template<typename A>
96     explicit
97     constexpr TVec3(const TVec3<A>& v) : x(v.x), y(v.y), z(v.z) { }
98
99     // cross product works only on vectors of size 3
100     template <typename RT>
101     friend inline
102     constexpr TVec3 cross(const TVec3& u, const TVec3<RT>& v) {
103         return TVec3(
104                 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
110 }  // namespace details
111
112 // ----------------------------------------------------------------------------------------
113
114 typedef details::TVec3<double> double3;
115 typedef details::TVec3<float> float3;
116 typedef details::TVec3<float> vec3;
117 typedef details::TVec3<half> half3;
118 typedef details::TVec3<int32_t> int3;
119 typedef details::TVec3<uint32_t> uint3;
120 typedef details::TVec3<int16_t> short3;
121 typedef details::TVec3<uint16_t> ushort3;
122 typedef details::TVec3<int8_t> byte3;
123 typedef details::TVec3<uint8_t> ubyte3;
124
125 // ----------------------------------------------------------------------------------------
126 }  // namespace android
127
128 #endif  // UI_VEC3_H_