~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Math/Vector4.h

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef VECTOR4_H
 
2
#define VECTOR4_H
 
3
 
 
4
NAMESPACE_BEGIN
 
5
 
 
6
template <typename T>  
 
7
class Vec4
 
8
{
 
9
public:
 
10
    inline Vec4<T>();
 
11
    inline ~Vec4<T>();
 
12
    inline Vec4<T>(const T&, const T&, const T&, const T&);
 
13
 
 
14
    inline Vec4(const Vec4<T>&);
 
15
                
 
16
    inline Vec4<T>& operator = (const Vec4<T>&);
 
17
    inline t_bool operator == (const Vec4<T>&) const;
 
18
    inline t_bool operator != (const Vec4<T>&) const;
 
19
    inline Vec4<T> operator + (const Vec4<T>&) const;
 
20
    inline Vec4<T> operator * (const Vec4<T>&) const;
 
21
    inline Vec4<T> operator - (const Vec4<T>&) const;
 
22
    inline Vec4<T> operator - () const;
 
23
                
 
24
    inline Vec4<T>& operator *= (const Vec4<T>&);
 
25
    inline Vec4<T>& operator += (const Vec4<T>&);
 
26
    inline Vec4<T>& operator -= (const Vec4<T>&);
 
27
 
 
28
    inline Vec4<T> operator / (const T&) const;
 
29
    inline Vec4<T> operator * (const T&) const;
 
30
    inline Vec4<T>& operator /= (const T&);
 
31
    inline Vec4<T>& operator *= (const T&);
 
32
 
 
33
    inline T& operator [](int i);
 
34
    inline const T& operator [](int i) const;
 
35
 
 
36
    void divide_xyz_by_w();
 
37
 
 
38
    template <typename U> friend Vec4<U> operator* (const U&, const Vec4<U>&);
 
39
 
 
40
        T x, y, z, w;
 
41
};
 
42
 
 
43
 
 
44
template <typename T>  
 
45
inline Vec4<T>::Vec4()
 
46
{
 
47
    x = 0;
 
48
    y = 0;
 
49
    z = 0;
 
50
    w = 1;
 
51
}
 
52
 
 
53
template <typename T>  
 
54
inline Vec4<T>::~Vec4()
 
55
{
 
56
 
 
57
}
 
58
 
 
59
template <typename T>  
 
60
inline Vec4<T>::Vec4(const T& fx, const T& fy, const T& fz, const T& fw)
 
61
{
 
62
    x = fx;
 
63
    y = fy;
 
64
    z = fz;
 
65
    w = fw;
 
66
}
 
67
 
 
68
//Vec4::Vec4(t_double fx, t_double fy, t_double fz, t_double fw)
 
69
//{
 
70
//    x = T(fx);
 
71
//    y = T(fy);
 
72
//    z = T(fz);
 
73
//    w = T(fw);
 
74
//}
 
75
//
 
76
//Vec4::Vec4(t_int fx, t_int fy, t_int fz, t_int fw)
 
77
//{
 
78
//    x = T(fx);
 
79
//    y = T(fy);
 
80
//    z = T(fz);
 
81
//    w = T(fw);
 
82
//}
 
83
 
 
84
template <typename T>  
 
85
inline Vec4<T>::Vec4(const Vec4<T>& v)
 
86
{
 
87
    x = v.x;
 
88
    y = v.y;
 
89
    z = v.z;
 
90
    w = v.w;
 
91
}
 
92
 
 
93
template <typename T>  
 
94
inline Vec4<T>& Vec4<T>::operator= (const Vec4<T>& v)
 
95
{
 
96
    x = v.x;
 
97
    y = v.y;
 
98
    z = v.z;
 
99
    w = v.w;
 
100
    return (*this);
 
101
}
 
102
 
 
103
template <typename T>  
 
104
inline t_bool Vec4<T>::operator == (const Vec4<T>& v) const
 
105
{
 
106
    if((x == v.x) &&
 
107
        (y == v.y) &&
 
108
        (z == v.z) &&
 
109
        (w == v.w))
 
110
    {
 
111
        return true;
 
112
    }
 
113
    return false;
 
114
}
 
115
 
 
116
template <typename T>  
 
117
inline t_bool Vec4<T>::operator != (const Vec4<T>& v) const
 
118
{
 
119
    return !(*this == v);
 
120
}
 
121
 
 
122
template <typename T>  
 
123
inline Vec4<T> Vec4<T>::operator+ (const Vec4<T>& v) const
 
124
{
 
125
    return Vec4(x + v.x, y + v.y, z + v.z, w + v.w);
 
126
}
 
127
 
 
128
template <typename T>  
 
129
inline Vec4<T> Vec4<T>::operator* (const Vec4<T>& v) const
 
130
{
 
131
    return Vec4<T>(x * v.x, y * v.y, z * v.z, w * v.w);
 
132
}
 
133
 
 
134
template <typename T>  
 
135
inline Vec4<T> Vec4<T>::operator- (const Vec4<T>& v) const
 
136
{
 
137
    return Vec4<T>(x - v.x, y - v.y, z - v.z, w - v.w);
 
138
}
 
139
 
 
140
template <typename T>  
 
141
inline Vec4<T> Vec4<T>::operator- () const
 
142
{
 
143
    //Do that for Matices too
 
144
    return Vec4<T>(-x, -y, -z, -w);
 
145
}
 
146
 
 
147
template <typename T>  
 
148
inline Vec4<T>& Vec4<T>::operator*= (const Vec4<T>& v)
 
149
{
 
150
    x *= v.x;
 
151
    y *= v.y;
 
152
    z *= v.z;
 
153
    w *= v.w;
 
154
    return *this;
 
155
}
 
156
 
 
157
template <typename T>  
 
158
inline Vec4<T>& Vec4<T>::operator+= (const Vec4<T>& v)
 
159
{
 
160
    x += v.x;
 
161
    y += v.y;
 
162
    z += v.z;
 
163
    w += v.w;
 
164
    return *this;
 
165
}
 
166
 
 
167
template <typename T>  
 
168
inline Vec4<T>& Vec4<T>::operator-= (const Vec4<T>& v)
 
169
{
 
170
    x -= v.x;
 
171
    y -= v.y;
 
172
    z -= v.z;
 
173
    w -= v.w;
 
174
    return *this;
 
175
}
 
176
 
 
177
template <typename T>  
 
178
inline Vec4<T> Vec4<T>::operator / (const T& f) const
 
179
{
 
180
    if(f == 0)
 
181
    {
 
182
        throw DivisionByZeroException();
 
183
    }
 
184
    return Vec4(x / f, y / f, z / f, w / f);
 
185
}
 
186
 
 
187
template <typename T>  
 
188
inline Vec4<T> Vec4<T>::operator * (const T& f) const
 
189
{
 
190
    return Vec4<T>(x * f, y * f, z * f, w * f);
 
191
}
 
192
 
 
193
template <typename T>  
 
194
inline Vec4<T>& Vec4<T>::operator /= (const T& f)
 
195
{
 
196
    if(f == 0)
 
197
    {
 
198
        throw DivisionByZeroException();
 
199
    }
 
200
    x = x / f;
 
201
    y = y / f;
 
202
    z = z / f;
 
203
    w = w / f;
 
204
    return *this;
 
205
}
 
206
 
 
207
template <typename T>  
 
208
inline Vec4<T>& Vec4<T>::operator *= (const T& f)
 
209
{
 
210
    x = x * f;
 
211
    y = y * f;
 
212
    z = z * f;
 
213
    w = w * f;
 
214
    return *this;
 
215
}
 
216
 
 
217
template <typename T>  
 
218
inline Vec4<T> operator* (T f , const Vec4<T>& v)
 
219
{
 
220
    return Vec4<T>(f * v.x, f * v.y, f * v.z, f * v.w);
 
221
}
 
222
/// element access
 
223
template <typename T>  
 
224
inline T& Vec4<T>::operator [](int i)
 
225
{
 
226
    assert(i>=0);
 
227
    assert(i<=3);
 
228
    return *(&x+i);
 
229
}
 
230
 
 
231
/// element access (const)
 
232
template <typename T>  
 
233
inline const T& Vec4<T>::operator [](int i) const
 
234
{
 
235
    assert(i>=0);
 
236
    assert(i<=3);
 
237
    return *(&x+i);
 
238
}
 
239
 
 
240
template <typename T>  
 
241
void Vec4<T>::divide_xyz_by_w()
 
242
{
 
243
    if(w == 0)
 
244
    {
 
245
        throw DivisionByZeroException();
 
246
    }
 
247
    x = x / w;
 
248
    y = y / w;
 
249
    z = z / w;
 
250
}
 
251
 
 
252
template <typename U>  
 
253
Vec4<U> operator* (const U& f, const Vec4<U>& v)
 
254
{
 
255
    return v * f;
 
256
}
 
257
 
 
258
typedef Vec4<float> Vector4;
 
259
 
 
260
NAMESPACE_END
 
261
 
 
262
#endif // VECTOR4_H
 
263