-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUMB3D.HPP
499 lines (356 loc) · 11.7 KB
/
DUMB3D.HPP
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**************************************************************************
dumb3d.hpp - A simple linear algebra library for 3D.
**************************************************************************/
/**************************************************************************
(C) Copyright 1994 Microsoft Corp. All rights reserved.
You have a royalty-free right to use, modify, reproduce and
distribute the Sample Files (and/or any modified version) in
any way you find useful, provided that you agree that
Microsoft has no warranty obligations or liability for any
Sample Application Files which are modified.
**************************************************************************/
#if !defined(DUMB3D_HPP)
#define DUMB3D_HPP
#pragma warning (disable:4244) // int to float conversion
/*----------------------------------------------------------------------------
This header contains the declarations for the dumb3d functions.
*/
// real type
typedef float real;
#define M_PI 3.14159265358979323846
// forward declarations
class point_4;
class vector_4;
class matrix_4x4;
/*----------------------------------------------------------------------------
globally useful functions.
*/
inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 );
inline vector_4 operator+( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline vector_4 operator-( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline vector_4 operator*( vector_4 const &Multiplicand,
real const &Multiplier );
inline vector_4 operator*( real const &Multiplier,
vector_4 const &Multiplicand );
inline vector_4 operator/( vector_4 const &Quotient,
real const &Divisor );
inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 );
inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 );
inline point_4 operator+( vector_4 const &Operand2, point_4 const &Operand1 );
inline vector_4 operator-( vector_4 const &Operand1 );
inline vector_4 CrossProduct( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline vector_4 VectorFrom( point_4 const &Point );
inline point_4 PointFrom( vector_4 const &Vector );
inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 );
real AngleBetween( vector_4 const &Operand1, vector_4 const &Operand2 );
real AngleBetweenUnit( vector_4 const &Unit1, vector_4 const &Unit2 );
inline real RadiansFrom( real Degrees );
inline real DegreesFrom( real Radians );
matrix_4x4 operator*( matrix_4x4 const &Multiplicand,
matrix_4x4 const &Multiplier );
vector_4 operator*( matrix_4x4 const &Multiplicand,
vector_4 const &Multiplier );
point_4 operator*( matrix_4x4 const &Multiplicand,
point_4 const &Multiplier );
void SetViewDistance( matrix_4x4 &Transform, real Distance );
/*----------------------------------------------------------------------------
quadruple. Base class for homogeneous vectors and points.
*/
class quadruple
{
public:
inline real GetElement( int Row ) const;
inline real GetX( void ) const;
inline real GetY( void ) const;
inline real GetZ( void ) const;
inline real GetW( void ) const;
inline void SetElement( int Row, real Value );
inline void SetX( real Value );
inline void SetY( real Value );
inline void SetZ( real Value );
inline void SetW( real Value );
protected:
inline quadruple( void );
inline quadruple( real X, real Y, real Z, real W );
inline quadruple( quadruple const & );
inline quadruple &operator=( quadruple const & );
real aElements[4];
};
/*----------------------------------------------------------------------------
point_4. This class represents a homogeneous 3D point.
*/
class point_4 :
public quadruple
{
public:
inline point_4( void );
inline point_4( real X, real Y, real Z );
inline void Homogenize( void );
};
/*----------------------------------------------------------------------------
vector_4. This class represents a homogeneous 3D vector.
*/
class vector_4 :
public quadruple
{
public:
inline vector_4( void );
inline vector_4( real X, real Y, real Z );
vector_4 &Normalize( void );
real Length( void );
};
/*----------------------------------------------------------------------------
matrix_4x4. This class represents row major 4x4 homogeneous matrices.
*/
class matrix_4x4
{
public:
matrix_4x4( void );
inline matrix_4x4( vector_4 const &Axis, real AngleInRadians );
inline matrix_4x4( point_4 const &Origin, vector_4 const &XAxis,
vector_4 const &YAxis, vector_4 const &ZAxis );
matrix_4x4 &ConcatenateXRotation( real Radians );
matrix_4x4 &ConcatenateYRotation( real Radians );
matrix_4x4 &ConcatenateZRotation( real Radians );
matrix_4x4 &ConcatenateXTranslation( real Distance );
matrix_4x4 &ConcatenateYTranslation( real Distance );
matrix_4x4 &ConcatenateZTranslation( real Distance );
matrix_4x4 &ConcatenateXScale( real Distance );
matrix_4x4 &ConcatenateYScale( real Distance );
matrix_4x4 &ConcatenateZScale( real Distance );
inline real GetElement( int Row, int Column ) const;
inline matrix_4x4 &SetElement( int Row, int Column, real Value );
protected:
enum do_not_initialize { DoNotInitialize };
inline matrix_4x4( do_not_initialize );
void InitWith( vector_4 const &Axis, real AngleInRadians );
void InitWith( point_4 const &Origin, vector_4 const &XAxis,
vector_4 const &YAxis, vector_4 const &ZAxis );
real aElements[4][4];
};
/*----------------------------------------------------------------------------
view transform.
*/
class view_transform :
public matrix_4x4
{
public:
view_transform( point_4 const &Viewpoint, vector_4 const &ViewDirection,
vector_4 const &Up );
};
/*----------------------------------------------------------------------------
inline function definitions.
*/
inline real RadiansFrom( real Degrees )
{
return (Degrees / real(360)) * real(M_PI) * real(2);
}
inline real DegreesFrom( real Radians )
{
return real(360) * (Radians / (real(M_PI) * real(2)));
}
inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 )
{
return vector_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline vector_4 operator+( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
return vector_4(Operand1.GetX() + Operand2.GetX(),
Operand1.GetY() + Operand2.GetY(),
Operand1.GetZ() + Operand2.GetZ());
}
inline vector_4 operator-( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
return vector_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline vector_4 operator-( vector_4 const &Operand1 )
{
return vector_4(-Operand1.GetX(),-Operand1.GetY(),-Operand1.GetZ());
}
inline vector_4 operator*( vector_4 const &Multiplicand,
real const &Multiplier )
{
return vector_4(Multiplicand.GetX() * Multiplier,
Multiplicand.GetY() * Multiplier,
Multiplicand.GetZ() * Multiplier);
}
inline vector_4 operator*( real const &Multiplier,
vector_4 const &Multiplicand )
{
return Multiplicand * Multiplier;
}
inline vector_4 operator/( vector_4 const &Quotient,
real const &Divisor )
{
return Quotient * (real(1.0)/Divisor);
}
inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 )
{
return point_4(Operand1.GetX() + Operand2.GetX(),
Operand1.GetY() + Operand2.GetY(),
Operand1.GetZ() + Operand2.GetZ());
}
inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 )
{
return point_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline point_4 operator+( vector_4 const &Operand1, point_4 const &Operand2 )
{
return Operand2 + Operand1;
}
inline vector_4 CrossProduct( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
real X = Operand1.GetY() * Operand2.GetZ() -
Operand1.GetZ() * Operand2.GetY();
real Y = Operand1.GetZ() * Operand2.GetX() -
Operand1.GetX() * Operand2.GetZ();
real Z = Operand1.GetX() * Operand2.GetY() -
Operand1.GetY() * Operand2.GetX();
return vector_4(X,Y,Z);
}
inline vector_4 VectorFrom( point_4 const &Point )
{
return vector_4(Point.GetX(),Point.GetY(),Point.GetZ());
}
inline point_4 PointFrom( vector_4 const &Vector )
{
return point_4(Vector.GetX(),Vector.GetY(),Vector.GetZ());
}
inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 )
{
return Operand1.GetX() * Operand2.GetX() +
Operand1.GetY() * Operand2.GetY() +
Operand1.GetZ() * Operand2.GetZ();
}
inline real quadruple::GetElement( int Row ) const
{
return aElements[Row];
}
inline real quadruple::GetX( void ) const
{
return aElements[0];
}
inline real quadruple::GetY( void ) const
{
return aElements[1];
}
inline real quadruple::GetZ( void ) const
{
return aElements[2];
}
inline real quadruple::GetW( void ) const
{
return aElements[3];
}
inline void quadruple::SetElement( int Row, real Value )
{
aElements[Row] = Value;
}
inline void quadruple::SetX( real Value )
{
aElements[0] = Value;
}
inline void quadruple::SetY( real Value )
{
aElements[1] = Value;
}
inline void quadruple::SetZ( real Value )
{
aElements[2] = Value;
}
inline void quadruple::SetW( real Value )
{
aElements[3] = Value;
}
inline void point_4::Homogenize( void )
{
aElements[0] = aElements[0] / aElements[3];
aElements[1] = aElements[1] / aElements[3];
aElements[2] = aElements[2] / aElements[3];
}
inline quadruple::quadruple( void )
{
aElements[0] = aElements[1] = aElements[2] = aElements[3] = 0;
}
inline quadruple::quadruple( real X, real Y, real Z, real W )
{
aElements[0] = X;
aElements[1] = Y;
aElements[2] = Z;
aElements[3] = W;
}
inline quadruple::quadruple( quadruple const &Source )
{
aElements[0] = Source.aElements[0];
aElements[1] = Source.aElements[1];
aElements[2] = Source.aElements[2];
aElements[3] = Source.aElements[3];
}
inline quadruple &quadruple::operator=( quadruple const &Source )
{
aElements[0] = Source.aElements[0];
aElements[1] = Source.aElements[1];
aElements[2] = Source.aElements[2];
aElements[3] = Source.aElements[3];
return *this;
}
inline point_4::point_4( void ) :
quadruple(0,0,0,1)
{
}
inline point_4::point_4( real X, real Y, real Z ) :
quadruple(X,Y,Z,1)
{
#if 0
char aBuffer[100];
sprintf(aBuffer,"X: %f Y: %f Z: %f",X,Y,Z);
MessageBox(0,aBuffer,"foobar",MB_OK);
sprintf(aBuffer,"X: %f Y: %f Z: %f W:%f",aElements[0],aElements[1],
aElements[2],aElements[3]);
MessageBox(0,aBuffer,"foobar",MB_OK);
#endif
}
inline vector_4::vector_4( void ) :
quadruple(0,0,0,0)
{
}
inline vector_4::vector_4( real X, real Y, real Z ) :
quadruple(X,Y,Z,0)
{
}
inline real matrix_4x4::GetElement( int Row, int Column ) const
{
return aElements[Row][Column];
}
inline matrix_4x4 &matrix_4x4::SetElement( int Row, int Column, real Value )
{
aElements[Row][Column] = Value;
return *this;
}
inline matrix_4x4::matrix_4x4( do_not_initialize )
{
}
inline matrix_4x4::matrix_4x4( vector_4 const &Axis, real AngleInRadians )
{
InitWith(Axis,AngleInRadians);
}
inline matrix_4x4::matrix_4x4( point_4 const &Origin, vector_4 const &XAxis,
vector_4 const &YAxis, vector_4 const &ZAxis )
{
InitWith(Origin,XAxis,YAxis,ZAxis);
}
#pragma warning (disable:4514) // unreferenced inline removed
#pragma warning (default:4244) // int to float conversion
#endif