-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.h
459 lines (339 loc) · 10.3 KB
/
vec.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//////////////////////////////////////////////////////////////////////////////
//
// --- vec.h ---
//
//////////////////////////////////////////////////////////////////////////////
#ifndef __ANGEL_VEC_H__
#define __ANGEL_VEC_H__
#include "Angel-yjc.h"
namespace Angel {
//////////////////////////////////////////////////////////////////////////////
//
// vec2.h - 2D vector
//
struct vec2 {
GLfloat x;
GLfloat y;
//
// --- Constructors and Destructors ---
//
vec2( GLfloat s = GLfloat(0.0) ) :
x(s), y(s) {}
vec2( GLfloat x, GLfloat y ) :
x(x), y(y) {}
vec2( const vec2& v )
{ x = v.x; y = v.y; }
//
// --- Indexing Operator ---
//
GLfloat& operator [] ( int i ) { return *(&x + i); }
const GLfloat operator [] ( int i ) const { return *(&x + i); }
//
// --- (non-modifying) Arithematic Operators ---
//
vec2 operator - () const // unary minus operator
{ return vec2( -x, -y ); }
vec2 operator + ( const vec2& v ) const
{ return vec2( x + v.x, y + v.y ); }
vec2 operator - ( const vec2& v ) const
{ return vec2( x - v.x, y - v.y ); }
vec2 operator * ( const GLfloat s ) const
{ return vec2( s*x, s*y ); }
vec2 operator * ( const vec2& v ) const
{ return vec2( x*v.x, y*v.y ); }
friend vec2 operator * ( const GLfloat s, const vec2& v )
{ return v * s; }
vec2 operator / ( const GLfloat s ) const {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
return vec2();
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
return *this * r;
}
//
// --- (modifying) Arithematic Operators ---
//
vec2& operator += ( const vec2& v )
{ x += v.x; y += v.y; return *this; }
vec2& operator -= ( const vec2& v )
{ x -= v.x; y -= v.y; return *this; }
vec2& operator *= ( const GLfloat s )
{ x *= s; y *= s; return *this; }
vec2& operator *= ( const vec2& v )
{ x *= v.x; y *= v.y; return *this; }
vec2& operator /= ( const GLfloat s ) {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
*this *= r;
return *this;
}
//
// --- Insertion and Extraction Operators ---
//
friend std::ostream& operator << ( std::ostream& os, const vec2& v ) {
return os << "( " << v.x << ", " << v.y << " )";
}
friend std::istream& operator >> ( std::istream& is, vec2& v )
{ return is >> v.x >> v.y ; }
//
// --- Conversion Operators ---
//
operator const GLfloat* () const
{ return static_cast<const GLfloat*>( &x ); }
operator GLfloat* ()
{ return static_cast<GLfloat*>( &x ); }
};
//----------------------------------------------------------------------------
//
// Non-class vec2 Methods
//
inline
GLfloat dot( const vec2& u, const vec2& v ) {
return u.x * v.x + u.y * v.y;
}
inline
GLfloat length( const vec2& v ) {
return std::sqrt( dot(v,v) );
}
inline
vec2 normalize( const vec2& v ) {
return v / length(v);
}
//////////////////////////////////////////////////////////////////////////////
//
// vec3.h - 3D vector
//
//////////////////////////////////////////////////////////////////////////////
struct vec3 {
GLfloat x;
GLfloat y;
GLfloat z;
//
// --- Constructors and Destructors ---
//
vec3( GLfloat s = GLfloat(0.0) ) :
x(s), y(s), z(s) {}
vec3( GLfloat x, GLfloat y, GLfloat z ) :
x(x), y(y), z(z) {}
vec3( const vec3& v ) { x = v.x; y = v.y; z = v.z; }
vec3( const vec2& v, const float f ) { x = v.x; y = v.y; z = f; }
//
// --- Indexing Operator ---
//
GLfloat& operator [] ( int i ) { return *(&x + i); }
const GLfloat operator [] ( int i ) const { return *(&x + i); }
//
// --- (non-modifying) Arithematic Operators ---
//
vec3 operator - () const // unary minus operator
{ return vec3( -x, -y, -z ); }
vec3 operator + ( const vec3& v ) const
{ return vec3( x + v.x, y + v.y, z + v.z ); }
vec3 operator - ( const vec3& v ) const
{ return vec3( x - v.x, y - v.y, z - v.z ); }
vec3 operator * ( const GLfloat s ) const
{ return vec3( s*x, s*y, s*z ); }
vec3 operator * ( const vec3& v ) const
{ return vec3( x*v.x, y*v.y, z*v.z ); }
friend vec3 operator * ( const GLfloat s, const vec3& v )
{ return v * s; }
vec3 operator / ( const GLfloat s ) const {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
return vec3();
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
return *this * r;
}
//
// --- (modifying) Arithematic Operators ---
//
vec3& operator += ( const vec3& v )
{ x += v.x; y += v.y; z += v.z; return *this; }
vec3& operator -= ( const vec3& v )
{ x -= v.x; y -= v.y; z -= v.z; return *this; }
vec3& operator *= ( const GLfloat s )
{ x *= s; y *= s; z *= s; return *this; }
vec3& operator *= ( const vec3& v )
{ x *= v.x; y *= v.y; z *= v.z; return *this; }
vec3& operator /= ( const GLfloat s ) {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
*this *= r;
return *this;
}
//
// --- Insertion and Extraction Operators ---
//
friend std::ostream& operator << ( std::ostream& os, const vec3& v ) {
return os << "( " << v.x << ", " << v.y << ", " << v.z << " )";
}
friend std::istream& operator >> ( std::istream& is, vec3& v )
{ return is >> v.x >> v.y >> v.z ; }
//
// --- Conversion Operators ---
//
operator const GLfloat* () const
{ return static_cast<const GLfloat*>( &x ); }
operator GLfloat* ()
{ return static_cast<GLfloat*>( &x ); }
};
//----------------------------------------------------------------------------
//
// Non-class vec3 Methods
//
inline
GLfloat dot( const vec3& u, const vec3& v ) {
return u.x*v.x + u.y*v.y + u.z*v.z ;
}
inline
GLfloat length( const vec3& v ) {
return std::sqrt( dot(v,v) );
}
inline
vec3 normalize( const vec3& v ) {
return v / length(v);
}
inline
vec3 cross(const vec3& a, const vec3& b )
{
return vec3( a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x );
}
//////////////////////////////////////////////////////////////////////////////
//
// vec4 - 4D vector
//
//////////////////////////////////////////////////////////////////////////////
struct vec4 {
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat w;
//
// --- Constructors and Destructors ---
//
vec4( GLfloat s = GLfloat(0.0) ) :
x(s), y(s), z(s), w(s) {}
vec4( GLfloat x, GLfloat y, GLfloat z, GLfloat w ) :
x(x), y(y), z(z), w(w) {}
vec4( const vec4& v ) { x = v.x; y = v.y; z = v.z; w = v.w; }
vec4( const vec3& v, const float w = 1.0 ) : w(w)
{ x = v.x; y = v.y; z = v.z; }
vec4( const vec2& v, const float z, const float w ) : z(z), w(w)
{ x = v.x; y = v.y; }
//
// --- Indexing Operator ---
//
GLfloat& operator [] ( int i ) { return *(&x + i); }
const GLfloat operator [] ( int i ) const { return *(&x + i); }
//
// --- (non-modifying) Arithematic Operators ---
//
vec4 operator - () const // unary minus operator
{ return vec4( -x, -y, -z, -w ); }
vec4 operator + ( const vec4& v ) const
{ return vec4( x + v.x, y + v.y, z + v.z, w + v.w ); }
vec4 operator - ( const vec4& v ) const
{ return vec4( x - v.x, y - v.y, z - v.z, w - v.w ); }
vec4 operator * ( const GLfloat s ) const
{ return vec4( s*x, s*y, s*z, s*w ); }
vec4 operator * ( const vec4& v ) const
{ return vec4( x*v.x, y*v.y, z*v.z, w*v.w ); } // YJC: Correct version
// { return vec4( x*v.x, y*v.y, z*v.z, w*v.z ); } // Wrong! Fixed as above
friend vec4 operator * ( const GLfloat s, const vec4& v )
{ return v * s; }
vec4 operator / ( const GLfloat s ) const {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
return vec4();
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
return *this * r;
}
//
// --- (modifying) Arithematic Operators ---
//
vec4& operator += ( const vec4& v )
{ x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
vec4& operator -= ( const vec4& v )
{ x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
vec4& operator *= ( const GLfloat s )
{ x *= s; y *= s; z *= s; w *= s; return *this; }
vec4& operator *= ( const vec4& v )
{ x *= v.x, y *= v.y, z *= v.z, w *= v.w; return *this; }
vec4& operator /= ( const GLfloat s ) {
#ifdef DEBUG
if ( std::fabs(s) < DivideByZeroTolerance ) {
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Division by zero" << std::endl;
}
#endif // DEBUG
GLfloat r = GLfloat(1.0) / s;
*this *= r;
return *this;
}
//
// --- Insertion and Extraction Operators ---
//
friend std::ostream& operator << ( std::ostream& os, const vec4& v ) {
return os << "( " << v.x << ", " << v.y
<< ", " << v.z << ", " << v.w << " )";
}
friend std::istream& operator >> ( std::istream& is, vec4& v )
{ return is >> v.x >> v.y >> v.z >> v.w; }
//
// --- Conversion Operators ---
//
operator const GLfloat* () const
{ return static_cast<const GLfloat*>( &x ); }
operator GLfloat* ()
{ return static_cast<GLfloat*>( &x ); }
};
//----------------------------------------------------------------------------
//
// Non-class vec4 Methods
//
inline
GLfloat dot( const vec4& u, const vec4& v ) {
return u.x*v.x + u.y*v.y + u.z*v.z + u.w+v.w;
}
inline
GLfloat length( const vec4& v ) {
return std::sqrt( dot(v,v) );
}
inline
vec4 normalize( const vec4& v ) {
return v / length(v);
}
inline
vec3 cross(const vec4& a, const vec4& b )
{
return vec3( a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x );
}
//----------------------------------------------------------------------------
} // namespace Angel
#endif // __ANGEL_VEC_H__