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_VEC4_H
18 #define UI_VEC4_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <ui/vec3.h>
24 
25 namespace android {
26 // -------------------------------------------------------------------------------------
27 
28 template <typename T>
29 class tvec4 :   public TVecProductOperators<tvec4, T>,
30                 public TVecAddOperators<tvec4, T>,
31                 public TVecUnaryOperators<tvec4, T>,
32                 public TVecComparisonOperators<tvec4, T>,
33                 public TVecFunctions<tvec4, T>
34 {
35 public:
36     enum no_init { NO_INIT };
37     typedef T value_type;
38     typedef T& reference;
39     typedef T const& const_reference;
40     typedef size_t size_type;
41 
42     union {
43         struct { T x, y, z, w; };
44         struct { T s, t, p, q; };
45         struct { T r, g, b, a; };
46         Impersonator< tvec2<T> > xy;
47         Impersonator< tvec2<T> > st;
48         Impersonator< tvec2<T> > rg;
49         Impersonator< tvec3<T> > xyz;
50         Impersonator< tvec3<T> > stp;
51         Impersonator< tvec3<T> > rgb;
52     };
53 
54     enum { SIZE = 4 };
size()55     inline static size_type size() { return SIZE; }
56 
57     // array access
58     inline T const& operator [] (size_t i) const { return (&x)[i]; }
59     inline T&       operator [] (size_t i)       { return (&x)[i]; }
60 
61     // -----------------------------------------------------------------------
62     // we don't provide copy-ctor and operator= on purpose
63     // because we want the compiler generated versions
64 
65     // constructors
66 
67     // leaves object uninitialized. use with caution.
tvec4(no_init)68     explicit tvec4(no_init) { }
69 
70     // default constructor
tvec4()71     tvec4() : x(0), y(0), z(0), w(0) { }
72 
73     // handles implicit conversion to a tvec4. must not be explicit.
74     template<typename A>
tvec4(A v)75     tvec4(A v) : x(v), y(v), z(v), w(v) { }
76 
77     template<typename A, typename B, typename C, typename D>
tvec4(A x,B y,C z,D w)78     tvec4(A x, B y, C z, D w) : x(x), y(y), z(z), w(w) { }
79 
80     template<typename A, typename B, typename C>
tvec4(const tvec2<A> & v,B z,C w)81     tvec4(const tvec2<A>& v, B z, C w) : x(v.x), y(v.y), z(z), w(w) { }
82 
83     template<typename A, typename B>
tvec4(const tvec3<A> & v,B w)84     tvec4(const tvec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
85 
86     template<typename A>
tvec4(const tvec4<A> & v)87     explicit tvec4(const tvec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
88 
89     template<typename A>
tvec4(const Impersonator<tvec4<A>> & v)90     tvec4(const Impersonator< tvec4<A> >& v)
91         : x(((const tvec4<A>&)v).x),
92           y(((const tvec4<A>&)v).y),
93           z(((const tvec4<A>&)v).z),
94           w(((const tvec4<A>&)v).w) { }
95 
96     template<typename A, typename B>
tvec4(const Impersonator<tvec3<A>> & v,B w)97     tvec4(const Impersonator< tvec3<A> >& v, B w)
98         : x(((const tvec3<A>&)v).x),
99           y(((const tvec3<A>&)v).y),
100           z(((const tvec3<A>&)v).z),
101           w(w) { }
102 
103     template<typename A, typename B, typename C>
tvec4(const Impersonator<tvec2<A>> & v,B z,C w)104     tvec4(const Impersonator< tvec2<A> >& v, B z, C w)
105         : x(((const tvec2<A>&)v).x),
106           y(((const tvec2<A>&)v).y),
107           z(z),
108           w(w) { }
109 };
110 
111 // ----------------------------------------------------------------------------------------
112 
113 typedef tvec4<float> vec4;
114 
115 // ----------------------------------------------------------------------------------------
116 }; // namespace android
117 
118 #endif /* UI_VEC4_H */
119