~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Math/Vector3.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 VECTOR3_H
 
2
#define VECTOR3_H
 
3
 
 
4
#include "../Exception.h"
 
5
 
 
6
#include "Vector4.h"
 
7
 
 
8
NAMESPACE_BEGIN
 
9
 
 
10
template <typename T>
 
11
class Vec3
 
12
{
 
13
public:
 
14
    inline Vec3();
 
15
    inline Vec3(const T&, const T&, const T&);
 
16
    inline ~Vec3();
 
17
    inline Vec3(const Vec3&);
 
18
                
 
19
    inline Vec3<T>& operator = (const Vec3<T>&);
 
20
    inline Vec3<T>& operator = (const Vec4<T>&);
 
21
 
 
22
    inline t_bool operator == (const Vec3<T>&) const;
 
23
    inline t_bool operator != (const Vec3<T>&) const;
 
24
    inline Vec3<T> operator + (const Vec3<T>&) const;
 
25
    inline Vec3<T> operator * (const Vec3<T>&) const;
 
26
    inline Vec3<T> operator - (const Vec3<T>&) const;
 
27
    inline Vec3<T> operator - () const;
 
28
                
 
29
    inline Vec3<T>& operator *= (const Vec3<T>&);
 
30
    inline Vec3<T>& operator += (const Vec3<T>&);
 
31
    inline Vec3<T>& operator -= (const Vec3<T>&);
 
32
 
 
33
    inline Vec3<T> operator / (const T&) const;
 
34
    inline Vec3<T> operator * (const T&) const;
 
35
    inline Vec3<T>& operator /= (const T&);
 
36
    inline Vec3<T>& operator *= (const T&);
 
37
    
 
38
    inline T& operator [](int i);
 
39
    inline const T& operator [](int i) const;
 
40
 
 
41
    inline T Length() const;
 
42
    inline T LengthSquared() const;
 
43
    inline T DotProduct(const Vec3<T>&) const;
 
44
    inline Vec3 CrossProduct(const Vec3<T>&) const;
 
45
    inline void Normalize();
 
46
 
 
47
    template <typename U> friend Vec3<U> operator* (const U&, const Vec3<U>&);
 
48
    
 
49
    //friend  Vec3<T> operator * (T, Vec3&);
 
50
 
 
51
    T x, y, z;
 
52
};
 
53
 
 
54
template <typename T>
 
55
inline Vec3<T>::Vec3()
 
56
{
 
57
    x = 0;
 
58
    y = 0;
 
59
    z = 0;
 
60
}
 
61
 
 
62
template <typename T>
 
63
inline Vec3<T>::~Vec3()
 
64
{
 
65
 
 
66
}
 
67
 
 
68
template <typename T>
 
69
inline Vec3<T>::Vec3(const T& fx, const T& fy, const T& fz)
 
70
{
 
71
    x = fx;
 
72
    y = fy;
 
73
    z = fz;
 
74
}
 
75
 
 
76
//Vec3::Vec3(t_double fx, t_double fy, t_double fz)
 
77
//{
 
78
//    x = T(fx);
 
79
//    y = T(fy);
 
80
//    z = T(fz);
 
81
//}
 
82
//
 
83
//Vec3::Vec3(t_int fx, t_int fy, t_int fz)
 
84
//{
 
85
//    x = T(fx);
 
86
//    y = T(fy);
 
87
//    z = T(fz);
 
88
//}
 
89
 
 
90
template <typename T>
 
91
Vec3<T>::Vec3(const Vec3<T>& v)
 
92
{
 
93
    x = v.x;
 
94
    y = v.y;
 
95
    z = v.z;
 
96
}
 
97
 
 
98
template <typename T>
 
99
Vec3<T>& Vec3<T>::operator = (const Vec3<T>& v)
 
100
{
 
101
    x = v.x;
 
102
    y = v.y;
 
103
    z = v.z;
 
104
    return *this;
 
105
}
 
106
 
 
107
template <typename T>
 
108
Vec3<T>& Vec3<T>::operator = (const Vec4<T>& v)
 
109
{
 
110
    x = v.x;
 
111
    y = v.y;
 
112
    z = v.z;
 
113
    return *this;
 
114
}
 
115
 
 
116
template <typename T>
 
117
t_bool Vec3<T>::operator == (const Vec3<T>& v) const
 
118
{
 
119
    if((x == v.x) &&
 
120
        (y == v.y) &&
 
121
        (z == v.z))
 
122
    {
 
123
        return true;
 
124
    }
 
125
    return false;
 
126
}
 
127
 
 
128
template <typename T>
 
129
t_bool Vec3<T>::operator != (const Vec3<T>& v) const
 
130
{
 
131
    return !(*this == v);
 
132
}
 
133
 
 
134
template <typename T>
 
135
Vec3<T> Vec3<T>::operator + (const Vec3<T>& v) const
 
136
{
 
137
    return Vec3<T>(x + v.x, y + v.y, z + v.z);
 
138
}
 
139
 
 
140
template <typename T>
 
141
Vec3<T> Vec3<T>::operator * (const Vec3& v) const
 
142
{
 
143
    return Vec3<T>(x * v.x, y * v.y, z * v.z);
 
144
}
 
145
 
 
146
template <typename T>
 
147
Vec3<T> Vec3<T>::operator - (const Vec3& v) const
 
148
{
 
149
    return Vec3<T>(x - v.x, y - v.y, z - v.z);
 
150
}
 
151
 
 
152
template <typename T>
 
153
Vec3<T> Vec3<T>::operator - () const
 
154
{
 
155
    return Vec3(-x, -y, -z);
 
156
}
 
157
 
 
158
template <typename T>
 
159
Vec3<T>& Vec3<T>::operator *= (const Vec3& v)
 
160
{
 
161
    x *= v.x;
 
162
    y *= v.y;
 
163
    z *= v.z;
 
164
    return *this;
 
165
}
 
166
 
 
167
template <typename T>
 
168
Vec3<T>& Vec3<T>::operator += (const Vec3& v)
 
169
{
 
170
    x += v.x;
 
171
    y += v.y;
 
172
    z += v.z;
 
173
    return *this;
 
174
}
 
175
 
 
176
template <typename T>
 
177
Vec3<T>& Vec3<T>::operator -= (const Vec3& v)
 
178
{
 
179
    x -= v.x;
 
180
    y -= v.y;
 
181
    z -= v.z;
 
182
    return *this;
 
183
}
 
184
 
 
185
template <typename T>
 
186
Vec3<T> Vec3<T>::operator / (const T& f) const
 
187
{
 
188
    if(f == 0)
 
189
    {
 
190
        throw DivisionByZeroException();
 
191
    }
 
192
    return Vec3<T>(x / f, y / f, z / f);
 
193
}
 
194
 
 
195
template <typename T>
 
196
Vec3<T> Vec3<T>::operator * (const T& f) const
 
197
{
 
198
    return Vec3<T>(x * f, y * f, z * f);
 
199
}
 
200
 
 
201
template <typename T>
 
202
Vec3<T>& Vec3<T>::operator /= (const T& f)
 
203
{
 
204
    if(f == 0)
 
205
    {
 
206
        throw DivisionByZeroException();
 
207
    }
 
208
    x = x / f;
 
209
    y = y / f;
 
210
    z = z / f;
 
211
    return *this;
 
212
}
 
213
 
 
214
template <typename T>
 
215
Vec3<T>& Vec3<T>::operator *= (const T& f)
 
216
{
 
217
    x = x * f;
 
218
    y = y * f;
 
219
    z = z * f;
 
220
    return *this;
 
221
}
 
222
 
 
223
/// element access
 
224
template <typename T>
 
225
T& Vec3<T>::operator [](int i)
 
226
{
 
227
    assert(i>=0);
 
228
    assert(i<=2);
 
229
    return *(&x+i);
 
230
}
 
231
 
 
232
/// element access (const)
 
233
template <typename T>
 
234
const T& Vec3<T>::operator [](int i) const
 
235
{
 
236
    assert(i>=0);
 
237
    assert(i<=2);
 
238
    return *(&x+i);
 
239
}
 
240
 
 
241
template <typename T>
 
242
T Vec3<T>::Length() const
 
243
{
 
244
    return sqrt(x*x + y*y + z*z);
 
245
}
 
246
 
 
247
template <typename T>
 
248
T Vec3<T>::LengthSquared() const
 
249
{
 
250
    return (x*x + y*y + z*z);
 
251
}
 
252
 
 
253
template <typename T>
 
254
T Vec3<T>::DotProduct(const Vec3<T>& v) const
 
255
{
 
256
    return x * v.x + y * v.y + z * v.z;
 
257
}
 
258
 
 
259
template <typename T>
 
260
Vec3<T> Vec3<T>::CrossProduct(const Vec3<T>& v) const
 
261
{
 
262
    return Vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
 
263
}
 
264
 
 
265
template <typename T>
 
266
void Vec3<T>::Normalize()
 
267
{
 
268
    T l;
 
269
    l = Length();
 
270
 
 
271
    if(l == 0)
 
272
    {
 
273
        throw DivisionByZeroException();
 
274
    }
 
275
    x = x / l;
 
276
    y = y / l;
 
277
    z = z / l;
 
278
}
 
279
 
 
280
template <typename T>
 
281
T DotProduct(const Vec3<T>& lhs, const Vec3<T>& rhs)
 
282
{
 
283
    T out;
 
284
    out = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
 
285
    return out;
 
286
}
 
287
 
 
288
template <typename T>
 
289
const Vec3<T> CrossProduct(const Vec3<T>& lhs, const Vec3<T>& rhs)
 
290
{
 
291
    Vec3<T> out;
 
292
    out.x = lhs.y * rhs.z - lhs.z * rhs.y;
 
293
    out.y = lhs.z * rhs.x - lhs.x * rhs.z;
 
294
    out.z = lhs.x * rhs.y - lhs.y * rhs.x;
 
295
 
 
296
    return out;
 
297
}
 
298
 
 
299
template <typename U>
 
300
inline Vec3<U> operator * (const U& f, const Vec3<U>& v)
 
301
{
 
302
    return v * f;
 
303
}
 
304
 
 
305
typedef Vec3<float> Vector3;
 
306
 
 
307
NAMESPACE_END
 
308
 
 
309
#endif // VECTOR3_H