-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector4D.h
366 lines (354 loc) · 7.79 KB
/
Vector4D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Vector4D.h: interface for the Vector4D class.
//
//////////////////////////////////////////////////////////////////////
#ifndef __VECTOR4D_H__
#define __VECTOR4D_H__
#include <cmath>
#include <cassert>
#include <climits>
#ifdef STLPORT
#ifndef __GNUC__
#define SSE
#endif
#endif
#ifdef SSE
#ifdef __GNUC__
#define __SSE__
#define __MMX__
#ifndef __inline
#ifdef __MINGW32__
#define __inline inline
#else
//#define __inline __attribute__((always_inline))
#endif
#endif
#endif
#include <xmmintrin.h>
#ifdef _MSC_VER
#define ALIGN16 _MM_ALIGN16
#elif __GNUC__
#define ALIGN16 __attribute__(aligned(16))
#else
#endif
#else
#define ALIGN16
#endif
#undef INLINE
#ifdef _WIN32
#ifdef _MSC_VER
#define INLINE __forceinline
#endif
#else
#ifdef __GNUC__
//#define INLINE __attribute__((always_inline))
#endif
#endif
#ifndef INLINE
#define INLINE inline
#endif
class Vector3D;
class Vector4D {
private:
typedef unsigned char uchar;
typedef unsigned int uint;
public:
#ifdef SSE
union ALIGN16 {
__m128 v;
struct {
float x;
float y;
float z;
float w;
};
struct
{
float r;
float g;
float b;
float a;
};
};
#else
union {
struct {
float x;
float y;
float z;
float w;
};
struct {
float r;
float g;
float b;
float a;
};
float v[4];
};
#endif
INLINE static const Vector4D Zero()
{
return Vector4D(0.0f, 0.0f, 0.0f, 0.0f);
}
const float& xyz() const
{
return x;
}
const float& xyz()
{
return x;
}
void Set(float X, float Y, float Z, float W)
{
x = X;
y = Y;
z = Z;
w = W;
}
INLINE Vector4D& operator = (const Vector4D& vector4D)
{
assert(this != &vector4D && "Apropriation to itself");
x = vector4D.x;
y = vector4D.y;
z = vector4D.z;
w = vector4D.w;
return *this;
}
uint RGBA() const
{
uint dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : static_cast<uint>(r * 255.0f + 0.5f);
uint dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : static_cast<uint>(g * 255.0f + 0.5f);
uint dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : static_cast<uint>(b * 255.0f + 0.5f);
uint dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : static_cast<uint>(a * 255.0f + 0.5f);
return (dwA << 24) | (dwB << 16) | (dwG << 8) | dwR;
}
operator uint () const
{
uint dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : static_cast<uint>(r * 255.0f + 0.5f);
uint dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : static_cast<uint>(g * 255.0f + 0.5f);
uint dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : static_cast<uint>(b * 255.0f + 0.5f);
uint dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : static_cast<uint>(a * 255.0f + 0.5f);
return (dwA << 24) | (dwR << 16) | (dwG << 8) | dwB;
}
INLINE operator float * ()
{
return &x;
}
INLINE operator const float * () const
{
return &x;
}
INLINE Vector4D operator - () const
{
return Vector4D(- x, - y, - z, - w);
}
INLINE Vector4D& operator += (const Vector4D& vector)
{
x += vector.x;
y += vector.y;
z += vector.z;
w += vector.w;
return *this;
}
INLINE Vector4D& operator -= (const Vector4D& vector)
{
x -= vector.x;
y -= vector.y;
z -= vector.z;
w -= vector.w;
return *this;
}
INLINE Vector4D& operator *= (float Value)
{
x *= Value;
y *= Value;
z *= Value;
w *= Value;
return *this;
}
INLINE Vector4D& operator /= (float Value)
{
x /= Value;
y /= Value;
z /= Value;
w /= Value;
return *this;
}
INLINE Vector4D& operator /= (const Vector4D& vector)
{
x /= vector.x;
y /= vector.y;
z /= vector.z;
w /= vector.w;
return *this;
}
INLINE Vector4D& operator *= (const Vector4D& vector)
{
x *= vector.x;
y *= vector.y;
z *= vector.z;
w *= vector.w;
return *this;
}
INLINE Vector4D& operator += (float Value)
{
x += Value;
y += Value;
z += Value;
return *this;
}
INLINE Vector4D& operator -= (float Value)
{
x -= Value;
y -= Value;
z -= Value;
return *this;
}
INLINE bool operator == (const Vector4D& v)
{
return (x == v.x && y == v.y && z == v.z && w == v.w);
}
INLINE bool operator != (const Vector4D& v) const
{
return (x != v.x || y != v.y || z != v.z || w != v.w);
}
//
INLINE float& operator [](int index)
{
assert(index < 4 && "Out of Range");
return *(index + &x);
}
INLINE float operator [] (int index) const
{
assert(index < 4 && "Out of Range");
return * (index + &x);
}
INLINE bool operator > (const Vector4D& v) const
{
return x > v.x && y > v.y && z > v.z && w > v.w;
}
INLINE bool operator < (const Vector4D& v) const
{
return x < v.x && y < v.y && z < v.z && w < v.w;
}
INLINE friend Vector4D operator + ( const Vector4D& vector1, const Vector4D& vector2)
{
#ifdef SSE
return Vector4D(_mm_add_ps(vector1.v, vector2.v));
#else
return Vector4D (vector1.x+vector2.x, vector1.y+vector2.y, vector1.z+vector2.z, vector1.w+vector2.w );
#endif
}
INLINE friend Vector4D operator + ( const Vector4D& vector, float value)
{
return Vector4D(vector.x + value, vector.y + value, vector.z + value, vector.w);
}
INLINE friend Vector4D operator - ( const Vector4D& vector1, const Vector4D& vector2)
{
#ifdef SSE
return Vector4D(_mm_sub_ps(vector1.v, vector2.v));
#else
return Vector4D(vector1.x-vector2.x, vector1.y-vector2.y, vector1.z-vector2.z, vector1.w-vector2.w );
#endif
}
INLINE friend Vector4D operator - ( const Vector4D& vector, float value)
{
return Vector4D(vector.x - value, vector.y - value, vector.z - value, vector.w);
}
INLINE friend Vector4D operator * (float Value, const Vector4D& vector)
{
return Vector4D (vector.x * Value, vector.y * Value, vector.z * Value, vector.w * Value);
}
INLINE friend Vector4D operator * (const Vector4D& vector, float Value)
{
return Vector4D(vector.x * Value, vector.y * Value, vector.z * Value, vector.w * Value);
}
INLINE friend Vector4D operator * (const Vector4D& vector1, const Vector4D& vector2)
{
#ifdef SSE
return Vector4D(_mm_mul_ps(vector1.v, vector2.v));
#else
return Vector4D(vector1.x *vector2.x, vector1.y * vector2.y, vector1.z * vector2.z, vector1.w * vector2.w );
#endif
}
INLINE friend Vector4D operator / (const Vector4D& vector, float Value)
{
return Vector4D(vector.x / Value, vector.y / Value, vector.z / Value, vector.w / Value);
}
INLINE friend Vector4D operator / (float Value, const Vector4D& vector)
{
return Vector4D ( Value / vector.x, Value / vector.y, Value / vector.z, Value / vector.w);
}
//
bool IsZero() const
{
return !x && !y && !z && !w;
}
INLINE float Length() const
{
return sqrtf(LengthSq());
}
INLINE float LengthSq() const
{
return x * x + y * y + z * z;
}
//
INLINE Vector4D& Normalize()
{
float l = Length();
assert(l && "Invalid Value");
l = 1.0f / l;
x *= l;
y *= l;
z *= l;
w *= l;
return *this;
};
Vector4D& FromARGB(uint color)
{
const float f = 1.0f / 255.0f;
r = f * static_cast<uchar>((color >> 16) & 0xff);
g = f * static_cast<uchar>((color >> 8) & 0xff);
b = f * static_cast<uchar>(color & 0xff);
a = f * static_cast<uchar>((color >> 24) & 0xff);
return *this;
}
Vector4D& FromRGBA(uint color)
{
const float f = 1.0f / 255.0f;
r = f * static_cast<uchar>((color >> 24) & 0xff);
g = f * static_cast<uchar>((color >> 16) & 0xff);
b = f * static_cast<uchar>((color >> 8) & 0xff);
a = f * static_cast<uchar>(color & 0xff);
return *this;
}
void FromColor(const unsigned char* color)
{
assert(color && "NULL Pointer");
const float ColorFloatToUchar = 1.0f / UCHAR_MAX;
x = color[0] * ColorFloatToUchar;
y = color[1] * ColorFloatToUchar;
z = color[2] * ColorFloatToUchar;
w = color[3] * ColorFloatToUchar;
}
INLINE Vector4D()
{
}
#ifdef SSE
INLINE Vector4D (const __m128& V) : v(V)
{
}
#endif
INLINE Vector4D (float X, float Y, float Z, float W ): x(X), y(Y), z(Z), w(W)
{
}
INLINE Vector4D (const Vector4D& vector) : x(vector.x), y(vector.y), z(vector.z),
w(vector.w)
{
}
INLINE Vector4D (const uchar* color)
{
FromColor(color);
}
};
#endif // __VECTOR4D_H__