-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
410 lines (330 loc) · 7.71 KB
/
Vector.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#ifndef VECTOR_H
#define VECTOR_H
#include <stdio.h>
#include <string.h>
#include <math.h>
#define STR_LEN 100
#define PI 3.141526
typedef unsigned char uint8;
// Swap two elements
inline void swap( float& a, float& b ) {
float tmp = a;
a=b;
b=tmp;
};
// Convert degrees to radians
inline float radians( float deg ) {
return deg*PI/180.0f;
}
// Map a value from one range to another
inline float mapTo( float val, float in, float out, float nIn, float nOut ) {
float in_min, in_max, out_min, out_max;
bool in_rev = (in<out), out_rev = (nIn<nOut);
if(in_rev) { in_min = in; in_max = out; } else { in_min = out; in_max = in; }
if(out_rev) { out_min = nIn; out_max = nOut; } else { out_min = nOut; out_max = nIn; }
float in_len = in_max - in_min;
float out_len = out_max - out_min;
float tmp = val;
tmp -= in_min;
if(in_rev) tmp = in_len - tmp;
tmp /= in_len;
tmp *= out_len;
if(out_rev) tmp = out_len - tmp;
tmp += out_min;
if(tmp < out_min) tmp = out_min;
if(tmp > out_max) tmp = out_max;
return tmp;
}
//////////////////////////////////////////////////
/** VEC2 */
struct vec2 {
// Constant size
static const uint8 size = 2;
// Array
float vec[size];
// Default constructor
vec2() {
memset( vec, 0, sizeof(vec) );
}
// Braced constructor
vec2( float x, float y ) {
vec[0] = x;
vec[1] = y;
}
// Uniform constructor
vec2( float a ) {
vec[0] = a;
vec[1] = a;
}
// Copy operator
inline void operator= ( const vec2& a ) {
memcpy(vec,a.vec,sizeof(vec));
}
// Square brace operator
inline float& operator[] ( uint8 index ) {
return vec[index];
}
// Const square brace operator
inline float operator[] ( uint8 index ) const {
return vec[index];
}
// Addition operator
inline vec2& operator+= ( const vec2& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]+=a.vec[i];
}
return *this;
}
// Subtraction operator
inline vec2& operator-= ( const vec2& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]-=a.vec[i];
}
return *this;
}
// Multiplication operator
inline float operator*= ( const vec2& a ) {
float dot = 0;
for( int i = 0; i < size; ++i ) {
dot += vec[i]*a.vec[i];
}
return dot;
}
// Multiplication operator
inline vec2& operator*= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i]*=a;
}
return *this;
}
// Division operator
inline vec2& operator/= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i]/=a;
}
return *this;
}
// Vector length
inline float length() {
return sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
}
// Normalized vector
inline vec2& normalize() {
*this/=length();
return *this;
}
// String formatter
inline void toString ( char in[], bool integer = false ) {
char str[STR_LEN];
memset( str, 0, sizeof(str) );
if( integer )
sprintf(str, "| %.0f %.0f |", vec[0], vec[1] );
else
sprintf(str, "| %.2f %.2f |", vec[0], vec[1] );
strcat( in, str );
}
};
// Binary addition operator
inline vec2 operator+ ( vec2 a, const vec2& b ) {
return a+=b;
}
// Binary subtraction operator
inline vec2 operator- ( vec2 a, const vec2& b ) {
return a-=b;
}
// Binary multiplication operator
inline float operator* ( vec2 a, const vec2& b ) {
return a*=b;
}
// Binary multiplication operator
inline vec2 operator* ( vec2 a, const float b ) {
return a*=b;
}
//////////////////////////////////////////////////
/** VEC3 */
struct vec3 {
static const uint8 size = 3;
float vec[size];
vec3() {
memset( vec, 0, sizeof(vec) );
}
vec3( float x, float y, float z ) {
vec[0] = x;
vec[1] = y;
vec[2] = z;
}
vec3( float a ) {
vec[0] = a;
vec[1] = a;
vec[2] = a;
}
inline void operator= ( const vec3& a ) {
memcpy(vec,a.vec,sizeof(vec));
}
inline float& operator[] ( uint8 index ) {
return vec[index];
}
inline float operator[] ( uint8 index ) const {
return vec[index];
}
inline vec3& operator+= ( const vec3& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]+=a.vec[i];
}
return *this;
}
inline vec3& operator-= ( const vec3& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]-=a.vec[i];
}
return *this;
}
inline float operator*= ( const vec3& a ) {
float dot = 0;
for( int i = 0; i < size; ++i ) {
dot += vec[i]*a.vec[i];
}
return dot;
}
inline vec3& operator*= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i] *= a;
}
return *this;
}
inline vec3& operator/= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i]/=a;
}
return *this;
}
inline vec3& operator&= ( const vec3& a ) {
vec3 v;
v[0] = vec[1]*a.vec[2] - vec[2]*a.vec[1];
v[1] = vec[2]*a.vec[0] - vec[0]*a.vec[2];
v[2] = vec[0]*a.vec[1] - vec[1]*a.vec[0];
*this = v;
return *this;
}
inline float length() {
return sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
}
inline vec3& normalize() {
*this/=length();
return *this;
}
inline void toString ( char in[], bool integer = false ) {
char str[STR_LEN];
memset( str, 0, sizeof(str) );
if( integer )
sprintf(str, "| %.0f %.0f %.0f |", vec[0], vec[1], vec[2]);
else
sprintf(str, "| %.2f %.2f %.2f |", vec[0], vec[1], vec[2]);
strcat( in, str );
}
};
inline vec3 operator+ ( vec3 a, const vec3& b ) {
return a+=b;
}
inline vec3 operator- ( vec3 a, const vec3& b ) {
return a-=b;
}
inline float operator* ( vec3 a, const vec3& b ) {
return a*=b;
}
inline vec3 operator* ( vec3 a, const float b ) {
return a*=b;
}
inline vec3 operator& ( vec3 a, const vec3& b ) {
return a&=b;
}
//////////////////////////////////////////////////
/** VEC4 */
struct vec4 {
static const uint8 size = 4;
float vec[size];
vec4() {
memset( vec, 0, sizeof(vec) );
}
vec4( float x, float y, float z, float w ) {
vec[0] = x;
vec[1] = y;
vec[2] = z;
vec[3] = w;
}
vec4( float a ) {
vec[0] = a;
vec[1] = a;
vec[2] = a;
vec[3] = a;
}
inline void operator= ( const vec4& a ) {
memcpy(vec,a.vec,sizeof(vec));
}
inline float& operator[] ( uint8 index ) {
return vec[index];
}
inline float operator[] ( uint8 index ) const {
return vec[index];
}
inline vec4& operator+= ( const vec4& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]+=a.vec[i];
}
return *this;
}
inline vec4& operator-= ( const vec4& a ) {
for( int i = 0; i < size; ++i ) {
vec[i]-=a.vec[i];
}
return *this;
}
inline float operator*= ( const vec4& a ) {
float dot = 0;
for( int i = 0; i < size; ++i ) {
dot += vec[i]*a.vec[i];
}
return dot;
}
inline vec4& operator*= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i]*=a;
}
return *this;
}
inline vec4& operator/= ( const float a ) {
for( int i = 0; i < size; ++i ) {
vec[i]/=a;
}
return *this;
}
inline float length() {
return sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2] + vec[3]*vec[3]);
}
inline vec4& normalize() {
*this/=length();
return *this;
}
inline void toString ( char in[], bool integer = false ) {
char str[STR_LEN];
memset( str, 0, sizeof(str) );
if( integer )
sprintf(str, "| %.0f %.0f %.0f %.0f |", vec[0], vec[1], vec[2], vec[3]);
else
sprintf(str, "| %.2f %.2f %.2f %.2f |", vec[0], vec[1], vec[2], vec[3]);
strcat( in, str );
}
};
inline vec4 operator+ ( vec4 a, const vec4& b ) {
return a+=b;
}
inline vec4 operator- ( vec4 a, const vec4& b ) {
return a-=b;
}
inline float operator* ( vec4 a, const vec4& b ) {
return a*=b;
}
inline vec4 operator* ( vec4 a, const float b ) {
return a*=b;
}
#endif /** VECTOR_H */