• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.h>
21 #include <sys/types.h>
22 
23 #include <ui/vec2.h>
24 
25 namespace android {
26 // -------------------------------------------------------------------------------------
27 
28 template <typename T>
29 class tvec3 :   public TVecProductOperators<tvec3, T>,
30                 public TVecAddOperators<tvec3, T>,
31                 public TVecUnaryOperators<tvec3, T>,
32                 public TVecComparisonOperators<tvec3, T>,
33                 public TVecFunctions<tvec3, 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; };
44         struct { T s, t, p; };
45         struct { T r, g, b; };
46         Impersonator< tvec2<T> > xy;
47         Impersonator< tvec2<T> > st;
48         Impersonator< tvec2<T> > rg;
49     };
50 
51     enum { SIZE = 3 };
size()52     inline static size_type size() { return SIZE; }
53 
54     // array access
55     inline T const& operator [] (size_t i) const { return (&x)[i]; }
56     inline T&       operator [] (size_t i)       { return (&x)[i]; }
57 
58     // -----------------------------------------------------------------------
59     // we don't provide copy-ctor and operator= on purpose
60     // because we want the compiler generated versions
61 
62     // constructors
63     // leaves object uninitialized. use with caution.
tvec3(no_init)64     explicit tvec3(no_init) { }
65 
66     // default constructor
tvec3()67     tvec3() : x(0), y(0), z(0) { }
68 
69     // handles implicit conversion to a tvec4. must not be explicit.
70     template<typename A>
tvec3(A v)71     tvec3(A v) : x(v), y(v), z(v) { }
72 
73     template<typename A, typename B, typename C>
tvec3(A x,B y,C z)74     tvec3(A x, B y, C z) : x(x), y(y), z(z) { }
75 
76     template<typename A, typename B>
tvec3(const tvec2<A> & v,B z)77     tvec3(const tvec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
78 
79     template<typename A>
tvec3(const tvec3<A> & v)80     explicit tvec3(const tvec3<A>& v) : x(v.x), y(v.y), z(v.z) { }
81 
82     template<typename A>
tvec3(const Impersonator<tvec3<A>> & v)83     tvec3(const Impersonator< tvec3<A> >& v)
84         : x(((const tvec3<A>&)v).x),
85           y(((const tvec3<A>&)v).y),
86           z(((const tvec3<A>&)v).z) { }
87 
88     template<typename A, typename B>
tvec3(const Impersonator<tvec2<A>> & v,B z)89     tvec3(const Impersonator< tvec2<A> >& v, B z)
90         : x(((const tvec2<A>&)v).x),
91           y(((const tvec2<A>&)v).y),
92           z(z) { }
93 
94     // cross product works only on vectors of size 3
95     template <typename RT>
96     friend inline
cross(const tvec3 & u,const tvec3<RT> & v)97     tvec3 __attribute__((pure)) cross(const tvec3& u, const tvec3<RT>& v) {
98         return tvec3(
99                 u.y*v.z - u.z*v.y,
100                 u.z*v.x - u.x*v.z,
101                 u.x*v.y - u.y*v.x);
102     }
103 };
104 
105 
106 // ----------------------------------------------------------------------------------------
107 
108 typedef tvec3<float> vec3;
109 
110 // ----------------------------------------------------------------------------------------
111 }; // namespace android
112 
113 #endif /* UI_VEC4_H */
114