-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathLinearR4.h
1098 lines (938 loc) · 28.7 KB
/
LinearR4.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* RayTrace Software Package, release 1.0.2, January 22, 2003.
*
* Mathematics Subpackage (VrMath)
*
* Author: Samuel R. Buss
*
* Software accompanying the book
* 3D Computer Graphics: A Mathematical Introduction with OpenGL,
* by S. Buss, Cambridge University Press, 2003.
*
* Software is "as-is" and carries no warranty. It may be used without
* restriction, but if you modify it, please change the filenames to
* prevent confusion between different versions. Please acknowledge
* all use of the software in any publications or products based on it.
*
* Bug reports: Sam Buss, [email protected].
* Web page: http://math.ucsd.edu/~sbuss/MathCG
*
*/
//
// Linear Algebra Classes over R4
//
//
// A. Vector and Position classes
//
// A.1. VectorR4: a column vector of length 4
//
// B. Matrix Classes
//
// B.1 LinearMapR4 - arbitrary linear map; 4x4 real matrix
//
// B.2 RotationMapR4 - orthonormal 4x4 matrix
//
#ifndef LINEAR_R4_H
#define LINEAR_R4_H
#include <math.h>
#include <assert.h>
#include <iostream>
#include "LinearR3.h"
using namespace std;
class VectorR4; // R4 Vector
class LinearMapR4; // 4x4 real matrix
class RotationMapR4; // 4x4 rotation map
// **************************************
// VectorR4 class *
// * * * * * * * * * * * * * * * * * * **
class VectorR4 {
public:
double x, y, z, w; // The x & y & z & w coordinates.
static const VectorR4 Zero;
static const VectorR4 UnitX;
static const VectorR4 UnitY;
static const VectorR4 UnitZ;
static const VectorR4 UnitW;
static const VectorR4 NegUnitX;
static const VectorR4 NegUnitY;
static const VectorR4 NegUnitZ;
static const VectorR4 NegUnitW;
public:
VectorR4( ) : x(0.0), y(0.0), z(0.0), w(0.0) {}
VectorR4( double xVal, double yVal, double zVal, double wVal )
: x(xVal), y(yVal), z(zVal), w(wVal) {}
VectorR4( const Quaternion& q); // Definition with Quaternion routines
VectorR4& SetZero() { x=0.0; y=0.0; z=0.0; w=0.0; return *this;}
VectorR4& Set( double xx, double yy, double zz, double ww )
{ x=xx; y=yy; z=zz; w=ww; return *this;}
VectorR4& Set ( const Quaternion& ); // Defined with Quaternion
VectorR4& Set ( const VectorHgR3& h ) {x=h.x; y=h.y; z=h.z; w=h.w; return *this; }
VectorR4& Load( const double* v );
VectorR4& Load( const float* v );
void Dump( double* v ) const;
void Dump( float* v ) const;
VectorR4& operator+= ( const VectorR4& v )
{ x+=v.x; y+=v.y; z+=v.z; w+=v.w; return(*this); }
VectorR4& operator-= ( const VectorR4& v )
{ x-=v.x; y-=v.y; z-=v.z; w-=v.w; return(*this); }
VectorR4& operator*= ( double m )
{ x*=m; y*=m; z*=m; w*=m; return(*this); }
VectorR4& operator/= ( double m )
{ register double mInv = 1.0/m;
x*=mInv; y*=mInv; z*=mInv; w*=mInv;
return(*this); }
VectorR4 operator- () const { return ( VectorR4(-x, -y, -z, -w) ); }
VectorR4& ArrayProd(const VectorR4&); // Component-wise product
VectorR4& ArrayProd3(const VectorR3&); // Component-wise product
VectorR4& AddScaled( const VectorR4& u, double s );
double Norm() const { return ( (double)sqrt( x*x + y*y + z*z +w*w) ); }
double NormSq() const { return ( x*x + y*y + z*z + w*w ); }
double Dist( const VectorR4& u ) const; // Distance from u
double DistSq( const VectorR4& u ) const; // Distance from u
double MaxAbs() const;
VectorR4& Normalize () { *this /= Norm(); return *this; } // No error checking
inline VectorR4& MakeUnit(); // Normalize() with error checking
inline VectorR4& ReNormalize();
bool IsUnit( ) const
{ register double norm = Norm();
return ( 1.000001>=norm && norm>=0.999999 ); }
bool IsUnit( double tolerance ) const
{ register double norm = Norm();
return ( 1.0+tolerance>=norm && norm>=1.0-tolerance ); }
bool IsZero() const { return ( x==0.0 && y==0.0 && z==0.0 && w==0.0); }
bool NearZero(double tolerance) const { return( MaxAbs()<=tolerance );}
// tolerance should be non-negative
VectorR4& RotateUnitInDirection ( const VectorR4& dir); // rotate in direction dir
};
inline VectorR4 operator+( const VectorR4& u, const VectorR4& v );
inline VectorR4 operator-( const VectorR4& u, const VectorR4& v );
inline VectorR4 operator*( const VectorR4& u, double m);
inline VectorR4 operator*( double m, const VectorR4& u);
inline VectorR4 operator/( const VectorR4& u, double m);
inline int operator==( const VectorR4& u, const VectorR4& v );
inline double operator^ (const VectorR4& u, const VectorR4& v ); // Dot Product
inline VectorR4 ArrayProd(const VectorR4& u, const VectorR4& v );
inline double Mag(const VectorR4& u) { return u.Norm(); }
inline double Dist(const VectorR4& u, const VectorR4& v) { return u.Dist(v); }
inline double DistSq(const VectorR4& u, const VectorR4& v) { return u.DistSq(v); }
inline double NormalizeError (const VectorR4& u);
// ********************************************************************
// Matrix4x4 - base class for 4x4 matrices *
// * * * * * * * * * * * * * * * * * * * * * **************************
class Matrix4x4 {
public:
double m11, m12, m13, m14, m21, m22, m23, m24,
m31, m32, m33, m34, m41, m42, m43, m44;
// Implements a 4x4 matrix: m_i_j - row-i and column-j entry
static const Matrix4x4 Identity;
public:
Matrix4x4();
Matrix4x4( const VectorR4&, const VectorR4&,
const VectorR4&, const VectorR4& ); // Sets by columns!
Matrix4x4( double, double, double, double,
double, double, double, double,
double, double, double, double,
double, double, double, double ); // Sets by columns
inline void SetIdentity (); // Set to the identity map
inline void SetZero (); // Set to the zero map
inline void Set ( const Matrix4x4& ); // Set to the matrix.
inline void Set( const VectorR4&, const VectorR4&,
const VectorR4&, const VectorR4& );
inline void Set( double, double, double, double,
double, double, double, double,
double, double, double, double,
double, double, double, double );
inline void SetByRows( const VectorR4&, const VectorR4&,
const VectorR4&, const VectorR4& );
inline void SetByRows( double, double, double, double,
double, double, double, double,
double, double, double, double,
double, double, double, double );
inline void SetColumn1 ( double, double, double, double );
inline void SetColumn2 ( double, double, double, double );
inline void SetColumn3 ( double, double, double, double );
inline void SetColumn4 ( double, double, double, double );
inline void SetColumn1 ( const VectorR4& );
inline void SetColumn2 ( const VectorR4& );
inline void SetColumn3 ( const VectorR4& );
inline void SetColumn4 ( const VectorR4& );
inline VectorR4 Column1() const;
inline VectorR4 Column2() const;
inline VectorR4 Column3() const;
inline VectorR4 Column4() const;
inline void SetDiagonal( double, double, double, double );
inline void SetDiagonal( const VectorR4& );
inline double Diagonal( int );
inline void MakeTranspose(); // Transposes it.
void Matrix4x4::operator*= (const Matrix4x4& B); // Matrix product
Matrix4x4& ReNormalize();
};
inline VectorR4 operator* ( const Matrix4x4&, const VectorR4& );
ostream& operator<< ( ostream& os, const Matrix4x4& A );
// *****************************************
// LinearMapR4 class *
// * * * * * * * * * * * * * * * * * * * * *
class LinearMapR4 : public Matrix4x4 {
public:
LinearMapR4();
LinearMapR4( const VectorR4&, const VectorR4&,
const VectorR4&, const VectorR4& ); // Sets by columns!
LinearMapR4( double, double, double, double,
double, double, double, double,
double, double, double, double,
double, double, double, double ); // Sets by columns
LinearMapR4 ( const Matrix4x4& );
inline LinearMapR4& operator+= (const LinearMapR4& );
inline LinearMapR4& operator-= (const LinearMapR4& );
inline LinearMapR4& operator*= (double);
inline LinearMapR4& operator/= (double);
inline LinearMapR4& operator*= (const Matrix4x4& ); // Matrix product
inline LinearMapR4 Transpose() const;
double Determinant () const; // Returns the determinant
LinearMapR4 Inverse() const; // Returns inverse
LinearMapR4& Invert(); // Converts into inverse.
VectorR4 Solve(const VectorR4&) const; // Returns solution
LinearMapR4 PseudoInverse() const; // Returns pseudo-inverse TO DO
VectorR4 PseudoSolve(const VectorR4&); // Finds least squares solution TO DO
};
inline LinearMapR4 operator+ (const LinearMapR4&, const LinearMapR4&);
inline LinearMapR4 operator- (const LinearMapR4&);
inline LinearMapR4 operator- (const LinearMapR4&, const LinearMapR4&);
inline LinearMapR4 operator* ( const LinearMapR4&, double);
inline LinearMapR4 operator* ( double, const LinearMapR4& );
inline LinearMapR4 operator/ ( const LinearMapR4&, double );
inline LinearMapR4 operator* ( const Matrix4x4&, const LinearMapR4& );
inline LinearMapR4 operator* ( const LinearMapR4&, const Matrix4x4& );
// Matrix product (composition)
// *******************************************
// RotationMapR4 class *
// * * * * * * * * * * * * * * * * * * * * * *
class RotationMapR4 : public Matrix4x4 {
public:
RotationMapR4();
RotationMapR4( const VectorR4&, const VectorR4&,
const VectorR4&, const VectorR4& ); // Sets by columns!
RotationMapR4( double, double, double, double,
double, double, double, double,
double, double, double, double,
double, double, double, double ); // Sets by columns!
RotationMapR4& SetZero(); // IT IS AN ERROR TO USE THIS FUNCTION!
inline RotationMapR4& operator*= (const RotationMapR4& ); // Matrix product
inline RotationMapR4 Transpose() const;
inline RotationMapR4 Inverse() const { return Transpose(); }; // Returns the transpose
inline RotationMapR4& Invert() { MakeTranspose(); return *this; }; // Transposes it.
inline VectorR4 Invert(const VectorR4&) const; // Returns solution
};
inline RotationMapR4 operator* ( const RotationMapR4&, const RotationMapR4& );
// Matrix product (composition)
// ***************************************************************
// * 4-space vector and matrix utilities (prototypes) *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Returns the angle between vectors u and v.
// Use SolidAngleUnit if both vectors are unit vectors
inline double SolidAngle( const VectorR4& u, const VectorR4& v);
inline double SolidAngleUnit( const VectorR4 u, const VectorR4 v );
// Returns a righthanded orthonormal basis to complement vectors u,v,w.
// The vectors u,v,w must be unit and orthonormal.
void GetOrtho( const VectorR4& u, RotationMapR4& rotmap );
void GetOrtho( const VectorR4& u, const VectorR4& v, RotationMapR4& rotmap );
void GetOrtho( const VectorR4& u, const VectorR4& v, const VectorR4& w,
RotationMapR4& rotmap );
void GetOrtho( int j, RotationMapR4& rotmap); // Mainly for internal use
// Projections
inline VectorR4 ProjectToUnit ( const VectorR4& u, const VectorR4& v);
// Project u onto v
inline VectorR4 ProjectPerpUnit ( const VectorR4& u, const VectorR4 & v);
// Project perp to v
inline VectorR4 ProjectPerpUnitDiff ( const VectorR4& u, const VectorR4& v);
// v must be a unit vector.
// Returns the projection of u onto unit v
inline VectorR4 ProjectToUnit ( const VectorR4& u, const VectorR4& v)
{
return (u^v)*v;
}
// Returns the projection of u onto the plane perpindicular to the unit vector v
inline VectorR4 ProjectPerpUnit ( const VectorR4& u, const VectorR4& v)
{
return ( u - ((u^v)*v) );
}
// Returns the projection of u onto the plane perpindicular to the unit vector v
// This one is more stable when u and v are nearly equal.
inline VectorR4 ProjectPerpUnitDiff ( const VectorR4& u, const VectorR4& v)
{
VectorR4 ans = u;
ans -= v;
ans -= ((ans^v)*v);
return ans; // ans = (u-v) - ((u-v)^v)*v
}
// Projection maps (LinearMapR4's)
// VectorProjectMap returns map projecting onto a given vector u.
// u should be a unit vector (otherwise the returned map is
// scaled according to the magnitude of u.
inline void VectorProjectMap( const VectorR4& u, LinearMapR4& M )
{
double a = u.x*u.y;
double b = u.x*u.z;
double c = u.x*u.w;
double d = u.y*u.z;
double e = u.y*u.w;
double f = u.z*u.w;
M.Set( u.x*u.x, a, b, c,
a, u.y*u.y, d, e,
b, d, u.z*u.z, f,
c, e, f, u.w*u.w );
}
inline LinearMapR4 VectorProjectMap( const VectorR4& u )
{
LinearMapR4 result;
VectorProjectMap( u, result );
return result;
}
inline LinearMapR4 PerpProjectMap ( const VectorR4& u );
// u - must be unit vector.
LinearMapR4 TimesTranspose( const VectorR4& u, const VectorR4& v); // u * v^T.
inline void TimesTranspose( const VectorR4& u, const VectorR4& v, LinearMapR4& M);
// Rotation Maps
RotationMapR4 RotateToMap( const VectorR4& fromVec, const VectorR4& toVec);
// fromVec and toVec should be unit vectors
// ***************************************************************
// * Stream Output Routines (Prototypes) *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ostream& operator<< ( ostream& os, const VectorR4& u );
// *****************************************************
// * VectorR4 class - inlined functions *
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
inline VectorR4& VectorR4::Load( const double* v )
{
x = *v;
y = *(v+1);
z = *(v+2);
w = *(v+3);
return *this;
}
inline VectorR4& VectorR4::Load( const float* v )
{
x = *v;
y = *(v+1);
z = *(v+2);
w = *(v+3);
return *this;
}
inline void VectorR4::Dump( double* v ) const
{
*v = x;
*(v+1) = y;
*(v+2) = z;
*(v+3) = w;
}
inline void VectorR4::Dump( float* v ) const
{
*v = (float)x;
*(v+1) = (float)y;
*(v+2) = (float)z;
*(v+3) = (float)w;
}
inline VectorR4& VectorR4::MakeUnit () // Convert to unit vector (or leave zero).
{
double nSq = NormSq();
if (nSq != 0.0) {
*this /= sqrt(nSq);
}
return *this;
}
inline VectorR4 operator+( const VectorR4& u, const VectorR4& v )
{
return VectorR4(u.x+v.x, u.y+v.y, u.z+v.z, u.w+v.w );
}
inline VectorR4 operator-( const VectorR4& u, const VectorR4& v )
{
return VectorR4(u.x-v.x, u.y-v.y, u.z-v.z, u.w-v.w);
}
inline VectorR4 operator*( const VectorR4& u, register double m)
{
return VectorR4( u.x*m, u.y*m, u.z*m, u.w*m );
}
inline VectorR4 operator*( register double m, const VectorR4& u)
{
return VectorR4( u.x*m, u.y*m, u.z*m, u.w*m );
}
inline VectorR4 operator/( const VectorR4& u, double m)
{
register double mInv = 1.0/m;
return VectorR4( u.x*mInv, u.y*mInv, u.z*mInv, u.w*mInv );
}
inline int operator==( const VectorR4& u, const VectorR4& v )
{
return ( u.x==v.x && u.y==v.y && u.z==v.z && u.w==v.w );
}
inline double operator^ ( const VectorR4& u, const VectorR4& v ) // Dot Product
{
return ( u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w );
}
inline VectorR4 ArrayProd ( const VectorR4& u, const VectorR4& v )
{
return ( VectorR4( u.x*v.x, u.y*v.y, u.z*v.z, u.w*v.w ) );
}
inline VectorR4& VectorR4::ArrayProd (const VectorR4& v) // Component-wise Product
{
x *= v.x;
y *= v.y;
z *= v.z;
w *= v.w;
return ( *this );
}
inline VectorR4& VectorR4::ArrayProd3 (const VectorR3& v) // Component-wise Product
{
x *= v.x;
y *= v.y;
z *= v.z;
return ( *this );
}
inline VectorR4& VectorR4::AddScaled( const VectorR4& u, double s )
{
x += s*u.x;
y += s*u.y;
z += s*u.z;
w += s*u.w;
return(*this);
}
inline VectorR4& VectorR4::ReNormalize() // Convert near unit back to unit
{
double nSq = NormSq();
register double mFact = 1.0-0.5*(nSq-1.0); // Multiplicative factor
*this *= mFact;
return *this;
}
inline double NormalizeError (const VectorR4& u)
{
register double discrepancy;
discrepancy = u.x*u.x + u.y*u.y + u.z*u.z + u.w*u.w - 1.0;
if ( discrepancy < 0.0 ) {
discrepancy = -discrepancy;
}
return discrepancy;
}
inline VectorR3& VectorR3::SetFromHg(const VectorR4& v) {
double wInv = 1.0/v.w;
x = v.x*wInv;
y = v.y*wInv;
z = v.z*wInv;
return *this;
}
inline double VectorR4::Dist( const VectorR4& u ) const // Distance from u
{
return sqrt( DistSq(u) );
}
inline double VectorR4::DistSq( const VectorR4& u ) const // Distance from u
{
return ( (x-u.x)*(x-u.x) + (y-u.y)*(y-u.y) + (z-u.z)*(z-u.z) + (w-u.w)*(w-u.w) );
}
// *********************************************************
// * Matrix4x4 class - inlined functions *
// * * * * * * * * * * * * * * * * * * * * * * * * * * *****
inline Matrix4x4::Matrix4x4() {}
inline Matrix4x4::Matrix4x4( const VectorR4& u, const VectorR4& v,
const VectorR4& s, const VectorR4& t)
{
m11 = u.x; // Column 1
m21 = u.y;
m31 = u.z;
m41 = u.w;
m12 = v.x; // Column 2
m22 = v.y;
m32 = v.z;
m42 = v.w;
m13 = s.x; // Column 3
m23 = s.y;
m33 = s.z;
m43 = s.w;
m14 = t.x; // Column 4
m24 = t.y;
m34 = t.z;
m44 = t.w;
}
inline Matrix4x4::Matrix4x4( double a11, double a21, double a31, double a41,
double a12, double a22, double a32, double a42,
double a13, double a23, double a33, double a43,
double a14, double a24, double a34, double a44)
// Values specified in column order!!!
{
m11 = a11; // Row 1
m12 = a12;
m13 = a13;
m14 = a14;
m21 = a21; // Row 2
m22 = a22;
m23 = a23;
m24 = a24;
m31 = a31; // Row 3
m32 = a32;
m33 = a33;
m34 = a34;
m41 = a41; // Row 4
m42 = a42;
m43 = a43;
m44 = a44;
}
/*
inline Matrix4x4::Matrix4x4 ( const Matrix4x4& A)
: m11(A.m11), m12(A.m12), m13(A.m13), m14(A.m14),
m21(A.m21), m22(A.m22), m23(A.m23), m24(A.m24),
m31(A.m31), m32(A.m32), m33(A.m33), m34(A.m34),
m41(A.m41), m42(A.m42), m43(A.m43), m44(A.m44) {} */
inline void Matrix4x4::SetIdentity ( )
{
m11 = m22 = m33 = m44 = 1.0;
m12 = m13 = m14 = m21 = m23 = m24 = m13 = m23 = m41= m42 = m43 = 0.0;
}
inline void Matrix4x4::Set( const VectorR4& u, const VectorR4& v,
const VectorR4& s, const VectorR4& t )
{
m11 = u.x; // Column 1
m21 = u.y;
m31 = u.z;
m41 = u.w;
m12 = v.x; // Column 2
m22 = v.y;
m32 = v.z;
m42 = v.w;
m13 = s.x; // Column 3
m23 = s.y;
m33 = s.z;
m43 = s.w;
m14 = t.x; // Column 4
m24 = t.y;
m34 = t.z;
m44 = t.w;
}
inline void Matrix4x4::Set( double a11, double a21, double a31, double a41,
double a12, double a22, double a32, double a42,
double a13, double a23, double a33, double a43,
double a14, double a24, double a34, double a44)
// Values specified in column order!!!
{
m11 = a11; // Row 1
m12 = a12;
m13 = a13;
m14 = a14;
m21 = a21; // Row 2
m22 = a22;
m23 = a23;
m24 = a24;
m31 = a31; // Row 3
m32 = a32;
m33 = a33;
m34 = a34;
m41 = a41; // Row 4
m42 = a42;
m43 = a43;
m44 = a44;
}
inline void Matrix4x4::Set ( const Matrix4x4& M ) // Set to the matrix.
{
m11 = M.m11;
m12 = M.m12;
m13 = M.m13;
m14 = M.m14;
m21 = M.m21;
m22 = M.m22;
m23 = M.m23;
m24 = M.m24;
m31 = M.m31;
m32 = M.m32;
m33 = M.m33;
m34 = M.m34;
m41 = M.m41;
m42 = M.m42;
m43 = M.m43;
m44 = M.m44;
}
inline void Matrix4x4::SetZero( )
{
m11 = m12 = m13 = m14 = m21 = m22 = m23 = m24
= m31 = m32 = m33 = m34 = m41 = m42 = m43 = m44 = 0.0;
}
inline void Matrix4x4::SetByRows( const VectorR4& u, const VectorR4& v,
const VectorR4& s, const VectorR4& t )
{
m11 = u.x; // Row 1
m12 = u.y;
m13 = u.z;
m14 = u.w;
m21 = v.x; // Row 2
m22 = v.y;
m23 = v.z;
m24 = v.w;
m31 = s.x; // Row 3
m32 = s.y;
m33 = s.z;
m34 = s.w;
m41 = t.x; // Row 4
m42 = t.y;
m43 = t.z;
m44 = t.w;
}
inline void Matrix4x4::SetByRows( double a11, double a12, double a13, double a14,
double a21, double a22, double a23, double a24,
double a31, double a32, double a33, double a34,
double a41, double a42, double a43, double a44 )
// Values specified in row order!!!
{
m11 = a11; // Row 1
m12 = a12;
m13 = a13;
m14 = a14;
m21 = a21; // Row 2
m22 = a22;
m23 = a23;
m24 = a24;
m31 = a31; // Row 3
m32 = a32;
m33 = a33;
m34 = a34;
m41 = a41; // Row 4
m42 = a42;
m43 = a43;
m44 = a44;
}
inline void Matrix4x4::SetColumn1 ( double x, double y, double z, double w)
{
m11 = x; m21 = y; m31= z; m41 = w;
}
inline void Matrix4x4::SetColumn2 ( double x, double y, double z, double w)
{
m12 = x; m22 = y; m32= z; m42 = w;
}
inline void Matrix4x4::SetColumn3 ( double x, double y, double z, double w)
{
m13 = x; m23 = y; m33= z; m43 = w;
}
inline void Matrix4x4::SetColumn4 ( double x, double y, double z, double w)
{
m14 = x; m24 = y; m34= z; m44 = w;
}
inline void Matrix4x4::SetColumn1 ( const VectorR4& u )
{
m11 = u.x; m21 = u.y; m31 = u.z; m41 = u.w;
}
inline void Matrix4x4::SetColumn2 ( const VectorR4& u )
{
m12 = u.x; m22 = u.y; m32 = u.z; m42 = u.w;
}
inline void Matrix4x4::SetColumn3 ( const VectorR4& u )
{
m13 = u.x; m23 = u.y; m33 = u.z; m43 = u.w;
}
inline void Matrix4x4::SetColumn4 ( const VectorR4& u )
{
m14 = u.x; m24 = u.y; m34 = u.z; m44 = u.w;
}
VectorR4 Matrix4x4::Column1() const
{
return ( VectorR4(m11, m21, m31, m41) );
}
VectorR4 Matrix4x4::Column2() const
{
return ( VectorR4(m12, m22, m32, m42) );
}
VectorR4 Matrix4x4::Column3() const
{
return ( VectorR4(m13, m23, m33, m43) );
}
VectorR4 Matrix4x4::Column4() const
{
return ( VectorR4(m14, m24, m34, m44) );
}
inline void Matrix4x4::SetDiagonal( double x, double y,
double z, double w)
{
m11 = x;
m22 = y;
m33 = z;
m44 = w;
}
inline void Matrix4x4::SetDiagonal( const VectorR4& u )
{
SetDiagonal ( u.x, u.y, u.z, u.w );
}
inline double Matrix4x4::Diagonal( int i )
{
switch (i) {
case 0:
return m11;
case 1:
return m22;
case 2:
return m33;
case 3:
return m44;
default:
assert(0);
return 0.0;
}
}
inline void Matrix4x4::MakeTranspose() // Transposes it.
{
register double temp;
temp = m12;
m12 = m21;
m21=temp;
temp = m13;
m13 = m31;
m31 = temp;
temp = m14;
m14 = m41;
m41 = temp;
temp = m23;
m23 = m32;
m32 = temp;
temp = m24;
m24 = m42;
m42 = temp;
temp = m34;
m34 = m43;
m43 = temp;
}
inline VectorR4 operator* ( const Matrix4x4& A, const VectorR4& u)
{
VectorR4 ret;
ret.x = A.m11*u.x + A.m12*u.y + A.m13*u.z + A.m14*u.w;
ret.y = A.m21*u.x + A.m22*u.y + A.m23*u.z + A.m24*u.w;
ret.z = A.m31*u.x + A.m32*u.y + A.m33*u.z + A.m34*u.w;
ret.w = A.m41*u.x + A.m42*u.y + A.m43*u.z + A.m44*u.w;
return ret;
}
// ******************************************************
// * LinearMapR4 class - inlined functions *
// * * * * * * * * * * * * * * * * * * * * * * * * * * **
inline LinearMapR4::LinearMapR4()
{
SetZero();
return;
}
inline LinearMapR4::LinearMapR4( const VectorR4& u, const VectorR4& v,
const VectorR4& s, const VectorR4& t)
:Matrix4x4 ( u, v, s ,t )
{ }
inline LinearMapR4::LinearMapR4(
double a11, double a21, double a31, double a41,
double a12, double a22, double a32, double a42,
double a13, double a23, double a33, double a43,
double a14, double a24, double a34, double a44)
// Values specified in column order!!!
:Matrix4x4 ( a11, a21, a31, a41, a12, a22, a32, a42,
a13, a23, a33, a43, a14, a24, a34, a44 )
{ }
inline LinearMapR4::LinearMapR4 ( const Matrix4x4& A )
: Matrix4x4 (A)
{}
inline LinearMapR4& LinearMapR4::operator+= (const LinearMapR4& B)
{
m11 += B.m11;
m12 += B.m12;
m13 += B.m13;
m14 += B.m14;
m21 += B.m21;
m22 += B.m22;
m23 += B.m23;
m24 += B.m24;
m31 += B.m31;
m32 += B.m32;
m33 += B.m33;
m34 += B.m34;
m41 += B.m41;
m42 += B.m42;
m43 += B.m43;
m44 += B.m44;
return ( *this );
}
inline LinearMapR4& LinearMapR4::operator-= (const LinearMapR4& B)
{
m11 -= B.m11;
m12 -= B.m12;
m13 -= B.m13;
m14 -= B.m14;
m21 -= B.m21;
m22 -= B.m22;
m23 -= B.m23;
m24 -= B.m24;
m31 -= B.m31;
m32 -= B.m32;
m33 -= B.m33;
m34 -= B.m34;
m41 -= B.m41;
m42 -= B.m42;
m43 -= B.m43;
m44 -= B.m44;
return( *this );
}
inline LinearMapR4 operator+ (const LinearMapR4& A, const LinearMapR4& B)
{
return( LinearMapR4( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31, A.m41+B.m41,
A.m12+B.m12, A.m22+B.m22, A.m32+B.m32, A.m42+B.m42,
A.m13+B.m13, A.m23+B.m23, A.m33+B.m33, A.m43+B.m43,
A.m14+B.m14, A.m24+B.m24, A.m34+B.m34, A.m44+B.m44) );
}
inline LinearMapR4 operator- (const LinearMapR4& A)
{
return( LinearMapR4( -A.m11, -A.m21, -A.m31, -A.m41,
-A.m12, -A.m22, -A.m32, -A.m42,
-A.m13, -A.m23, -A.m33, -A.m43,
-A.m14, -A.m24, -A.m34, -A.m44 ) );
}
inline LinearMapR4 operator- (const LinearMapR4& A, const LinearMapR4& B)
{
return( LinearMapR4( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31, A.m41-B.m41,
A.m12-B.m12, A.m22-B.m22, A.m32-B.m32, A.m42-B.m42,
A.m13-B.m13, A.m23-B.m23, A.m33-B.m33, A.m43-B.m43,
A.m14-B.m14, A.m24-B.m24, A.m34-B.m34, A.m44-B.m44 ) );
}
inline LinearMapR4& LinearMapR4::operator*= (register double b)
{
m11 *= b;
m12 *= b;
m13 *= b;
m14 *= b;
m21 *= b;
m22 *= b;
m23 *= b;
m24 *= b;
m31 *= b;
m32 *= b;
m33 *= b;
m34 *= b;
m41 *= b;
m42 *= b;
m43 *= b;
m44 *= b;
return ( *this);
}
inline LinearMapR4 operator* ( const LinearMapR4& A, register double b)
{
return( LinearMapR4( A.m11*b, A.m21*b, A.m31*b, A.m41*b,
A.m12*b, A.m22*b, A.m32*b, A.m42*b,
A.m13*b, A.m23*b, A.m33*b, A.m43*b,
A.m14*b, A.m24*b, A.m34*b, A.m44*b) );
}
inline LinearMapR4 operator* ( register double b, const LinearMapR4& A)
{
return( LinearMapR4( A.m11*b, A.m21*b, A.m31*b, A.m41*b,
A.m12*b, A.m22*b, A.m32*b, A.m42*b,
A.m13*b, A.m23*b, A.m33*b, A.m43*b,
A.m14*b, A.m24*b, A.m34*b, A.m44*b ) );
}
inline LinearMapR4 operator/ ( const LinearMapR4& A, double b)
{
register double bInv = 1.0/b;
return ( A*bInv );
}
inline LinearMapR4& LinearMapR4::operator/= (register double b)
{
register double bInv = 1.0/b;
return ( *this *= bInv );
}
inline VectorR4 operator* ( const LinearMapR4& A, const VectorR4& u)
{
return(VectorR4 ( A.m11*u.x + A.m12*u.y + A.m13*u.z + A.m14*u.w,
A.m21*u.x + A.m22*u.y + A.m23*u.z + A.m24*u.w,
A.m31*u.x + A.m32*u.y + A.m33*u.z + A.m34*u.w,
A.m41*u.x + A.m42*u.y + A.m43*u.z + A.m44*u.w ) );
}
inline LinearMapR4 LinearMapR4::Transpose() const // Returns the transpose
{
return (LinearMapR4( m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44 ) );
}
inline LinearMapR4& LinearMapR4::operator*= (const Matrix4x4& B) // Matrix product
{
(*this).Matrix4x4::operator*=(B);
return( *this );
}
inline LinearMapR4 operator* ( const LinearMapR4& A, const Matrix4x4& B)
{
LinearMapR4 AA(A);
AA.Matrix4x4::operator*=(B);
return AA;
}