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