-
Notifications
You must be signed in to change notification settings - Fork 1
/
glh_linear.h
1617 lines (1242 loc) · 36.7 KB
/
glh_linear.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
/*
glh - is a platform-indepenedent C++ OpenGL helper library
Copyright (c) 2000 Cass Everitt
Copyright (c) 2000 NVIDIA Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* The names of contributors to this software may not be used
to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Cass Everitt - [email protected]
*/
/*
glh_linear.h
*/
// Author: Cass W. Everitt
#ifndef GLH_LINEAR_H
#define GLH_LINEAR_H
#include <memory.h>
#include <math.h>
#include <assert.h>
// only supports float for now...
#define GLH_REAL_IS_FLOAT
#ifdef GLH_REAL_IS_FLOAT
# define GLH_REAL float
# define GLH_REAL_NAMESPACE ns_float
#endif
#ifdef _WIN32
# define TEMPLATE_FUNCTION
#else
# define TEMPLATE_FUNCTION <>
#endif
#define GLH_QUATERNION_NORMALIZATION_THRESHOLD 64
#define GLH_RAD_TO_DEG GLH_REAL(57.2957795130823208767981548141052)
#define GLH_DEG_TO_RAD GLH_REAL(0.0174532925199432957692369076848861)
#define GLH_ZERO GLH_REAL(0.0)
#define GLH_ONE GLH_REAL(1.0)
#define GLH_TWO GLH_REAL(2.0)
#define GLH_EPSILON GLH_REAL(10e-6)
#define GLH_PI GLH_REAL(3.1415926535897932384626433832795)
#define equivalent(a,b) (((a < b + GLH_EPSILON) && (a > b - GLH_EPSILON)) ? true : false)
namespace glh
{
inline GLH_REAL to_degrees(GLH_REAL radians) { return radians*GLH_RAD_TO_DEG; }
inline GLH_REAL to_radians(GLH_REAL degrees) { return degrees*GLH_DEG_TO_RAD; }
template <int N, class T>
class vec
{
public:
int size() const { return N; }
vec(const T & t = T())
{ for(int i = 0; i < N; i++) v[i] = t; }
vec(const T * tp)
{ for(int i = 0; i < N; i++) v[i] = tp[i]; }
const T * get_value() const
{ return v; }
T dot( const vec<N,T> & rhs ) const
{
T r = 0;
for(int i = 0; i < N; i++) r += v[i]*rhs.v[i];
return r;
}
T length() const
{
T r = 0;
for(int i = 0; i < N; i++) r += v[i]*v[i];
return T(sqrt(r));
}
T square_norm() const
{
T r = 0;
for(int i = 0; i < N; i++) r += v[i]*v[i];
return r;
}
void negate()
{ for(int i = 0; i < N; i++) v[i] = -v[i]; }
T normalize()
{
T sum(0);
for(int i = 0; i < N; i++)
sum += v[i]*v[i];
sum = T(sqrt(sum));
if (sum > GLH_EPSILON)
for(int i = 0; i < N; i++)
v[i] /= sum;
return sum;
}
vec<N,T> & set_value( const T * rhs )
{ for(int i = 0; i < N; i++) v[i] = rhs[i]; return *this; }
T & operator [] ( int i )
{ return v[i]; }
const T & operator [] ( int i ) const
{ return v[i]; }
vec<N,T> & operator *= ( T d )
{ for(int i = 0; i < N; i++) v[i] *= d; return *this;}
vec<N,T> & operator *= ( const vec<N,T> & u )
{ for(int i = 0; i < N; i++) v[i] *= u[i]; return *this;}
vec<N,T> & operator /= ( T d )
{ if(d == 0) return *this; for(int i = 0; i < N; i++) v[i] /= d; return *this;}
vec<N,T> & operator += ( const vec<N,T> & u )
{ for(int i = 0; i < N; i++) v[i] += u.v[i]; return *this;}
vec<N,T> & operator -= ( const vec<N,T> & u )
{ for(int i = 0; i < N; i++) v[i] -= u.v[i]; return *this;}
vec<N,T> operator - () const
{ vec<N,T> rv = v; rv.negate(); return rv; }
vec<N,T> operator + ( const vec<N,T> &v) const
{ vec<N,T> rt(*this); return rt += v; }
vec<N,T> operator - ( const vec<N,T> &v) const
{ vec<N,T> rt(*this); return rt -= v; }
vec<N,T> operator * ( T d) const
{ vec<N,T> rt(*this); return rt *= d; }
//friend bool operator == TEMPLATE_FUNCTION ( const vec<N,T> &v1, const vec<N,T> &v2 );
//friend bool operator != TEMPLATE_FUNCTION ( const vec<N,T> &v1, const vec<N,T> &v2 );
//protected:
T v[N];
};
// vector friend operators
template <int N, class T> inline
vec<N,T> operator * ( const vec<N,T> & b, T d )
{
vec<N,T> rt(b);
return rt *= d;
}
template <int N, class T> inline
vec<N,T> operator * ( T d, const vec<N,T> & b )
{ return b*d; }
template <int N, class T> inline
vec<N,T> operator * ( const vec<N,T> & b, const vec<N,T> & d )
{
vec<N,T> rt(b);
return rt *= d;
}
template <int N, class T> inline
vec<N,T> operator / ( const vec<N,T> & b, T d )
{ vec<N,T> rt(b); return rt /= d; }
template <int N, class T> inline
vec<N,T> operator + ( const vec<N,T> & v1, const vec<N,T> & v2 )
{ vec<N,T> rt(v1); return rt += v2; }
template <int N, class T> inline
vec<N,T> operator - ( const vec<N,T> & v1, const vec<N,T> & v2 )
{ vec<N,T> rt(v1); return rt -= v2; }
template <int N, class T> inline
bool operator == ( const vec<N,T> & v1, const vec<N,T> & v2 )
{
for(int i = 0; i < N; i++)
if(v1.v[i] != v2.v[i])
return false;
return true;
}
template <int N, class T> inline
bool operator != ( const vec<N,T> & v1, const vec<N,T> & v2 )
{ return !(v1 == v2); }
typedef vec<3,unsigned char> vec3ub;
typedef vec<4,unsigned char> vec4ub;
namespace GLH_REAL_NAMESPACE
{
typedef GLH_REAL real;
class line;
class plane;
class matrix4;
class quaternion;
typedef quaternion rotation;
class vec2 : public vec<2,real>
{
public:
vec2(const real & t = real()) : vec<2,real>(t)
{}
vec2(const vec<2,real> & t) : vec<2,real>(t)
{}
vec2(const real * tp) : vec<2,real>(tp)
{}
vec2(real x, real y )
{ v[0] = x; v[1] = y; }
void get_value(real & x, real & y) const
{ x = v[0]; y = v[1]; }
vec2 & set_value( const real & x, const real & y)
{ v[0] = x; v[1] = y; return *this; }
};
class vec3 : public vec<3,real>
{
public:
vec3(const real & t = real()) : vec<3,real>(t)
{}
vec3(const vec<3,real> & t) : vec<3,real>(t)
{}
vec3(const real * tp) : vec<3,real>(tp)
{}
vec3(real x, real y, real z)
{ v[0] = x; v[1] = y; v[2] = z; }
void get_value(real & x, real & y, real & z) const
{ x = v[0]; y = v[1]; z = v[2]; }
vec3 cross( const vec3 &rhs ) const
{
vec3 rt;
rt.v[0] = v[1]*rhs.v[2]-v[2]*rhs.v[1];
rt.v[1] = v[2]*rhs.v[0]-v[0]*rhs.v[2];
rt.v[2] = v[0]*rhs.v[1]-v[1]*rhs.v[0];
return rt;
}
vec3 & set_value( const real & x, const real & y, const real & z)
{ v[0] = x; v[1] = y; v[2] = z; return *this; }
};
class vec4 : public vec<4,real>
{
public:
vec4(const real & t = real()) : vec<4,real>(t)
{}
vec4(const vec<4,real> & t) : vec<4,real>(t)
{}
vec4(const vec<3,real> & t, real fourth)
{ v[0] = t.v[0]; v[1] = t.v[1]; v[2] = t.v[2]; v[3] = fourth; }
vec4(const real * tp) : vec<4,real>(tp)
{}
vec4(real x, real y, real z, real w)
{ v[0] = x; v[1] = y; v[2] = z; v[3] = w; }
void get_value(real & x, real & y, real & z, real & w) const
{ x = v[0]; y = v[1]; z = v[2]; w = v[3]; }
vec4 & set_value( const real & x, const real & y, const real & z, const real & w)
{ v[0] = x; v[1] = y; v[2] = z; v[3] = w; return *this; }
};
inline
vec3 homogenize(const vec4 & v)
{
vec3 rt;
assert(v.v[3] != GLH_ZERO);
rt.v[0] = v.v[0]/v.v[3];
rt.v[1] = v.v[1]/v.v[3];
rt.v[2] = v.v[2]/v.v[3];
return rt;
}
class line
{
public:
line()
{ set_value(vec3(0,0,0),vec3(0,0,1)); }
line( const vec3 & p0, const vec3 &p1)
{ set_value(p0,p1); }
void set_value( const vec3 &p0, const vec3 &p1)
{
position = p0;
direction = p1-p0;
direction.normalize();
}
bool get_closest_points(const line &line2,
vec3 &pointOnThis,
vec3 &pointOnThat)
{
// quick check to see if parallel -- if so, quit.
if(fabs(direction.dot(line2.direction)) == 1.0)
return 0;
line l2 = line2;
// Algorithm: Brian Jean
//
register real u;
register real v;
vec3 Vr = direction;
vec3 Vs = l2.direction;
register real Vr_Dot_Vs = Vr.dot(Vs);
register real detA = real(1.0 - (Vr_Dot_Vs * Vr_Dot_Vs));
vec3 C = l2.position - position;
register real C_Dot_Vr = C.dot(Vr);
register real C_Dot_Vs = C.dot(Vs);
u = (C_Dot_Vr - Vr_Dot_Vs * C_Dot_Vs)/detA;
v = (C_Dot_Vr * Vr_Dot_Vs - C_Dot_Vs)/detA;
pointOnThis = position;
pointOnThis += direction * u;
pointOnThat = l2.position;
pointOnThat += l2.direction * v;
return 1;
}
vec3 get_closest_point(const vec3 &point)
{
vec3 np = point - position;
vec3 rp = direction*direction.dot(np)+position;
return rp;
}
const vec3 & get_position() const {return position;}
const vec3 & get_direction() const {return direction;}
//protected:
vec3 position;
vec3 direction;
};
// matrix
class matrix4
{
public:
matrix4() { make_identity(); }
matrix4( real r )
{ set_value(r); }
matrix4( real * m )
{ set_value(m); }
matrix4( real a00, real a01, real a02, real a03,
real a10, real a11, real a12, real a13,
real a20, real a21, real a22, real a23,
real a30, real a31, real a32, real a33 )
{
element(0,0) = a00;
element(0,1) = a01;
element(0,2) = a02;
element(0,3) = a03;
element(1,0) = a10;
element(1,1) = a11;
element(1,2) = a12;
element(1,3) = a13;
element(2,0) = a20;
element(2,1) = a21;
element(2,2) = a22;
element(2,3) = a23;
element(3,0) = a30;
element(3,1) = a31;
element(3,2) = a32;
element(3,3) = a33;
}
void get_value( real * mp ) const
{
int c = 0;
for(int j=0; j < 4; j++)
for(int i=0; i < 4; i++)
mp[c++] = element(i,j);
}
const real * get_value() const
{ return m; }
void set_value( real * mp)
{
int c = 0;
for(int j=0; j < 4; j++)
for(int i=0; i < 4; i++)
element(i,j) = mp[c++];
}
void set_value( real r )
{
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
element(i,j) = r;
}
void make_identity()
{
element(0,0) = 1.0;
element(0,1) = 0.0;
element(0,2) = 0.0;
element(0,3) = 0.0;
element(1,0) = 0.0;
element(1,1) = 1.0;
element(1,2) = 0.0;
element(1,3) = 0.0;
element(2,0) = 0.0;
element(2,1) = 0.0;
element(2,2) = 1.0;
element(2,3) = 0.0;
element(3,0) = 0.0;
element(3,1) = 0.0;
element(3,2) = 0.0;
element(3,3) = 1.0;
}
static matrix4 identity()
{
static matrix4 mident (
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 );
return mident;
}
void set_scale( real s )
{
element(0,0) = s;
element(1,1) = s;
element(2,2) = s;
}
void set_scale( const vec3 & s )
{
element(0,0) = s.v[0];
element(1,1) = s.v[1];
element(2,2) = s.v[2];
}
void set_translate( const vec3 & t )
{
element(0,3) = t.v[0];
element(1,3) = t.v[1];
element(2,3) = t.v[2];
}
void set_row(int r, const vec4 & t)
{
element(r,0) = t.v[0];
element(r,1) = t.v[1];
element(r,2) = t.v[2];
element(r,3) = t.v[3];
}
void set_column(int c, const vec4 & t)
{
element(0,c) = t.v[0];
element(1,c) = t.v[1];
element(2,c) = t.v[2];
element(3,c) = t.v[3];
}
void get_row(int r, vec4 & t) const
{
t.v[0] = element(r,0);
t.v[1] = element(r,1);
t.v[2] = element(r,2);
t.v[3] = element(r,3);
}
vec4 get_row(int r) const
{
vec4 v; get_row(r, v);
return v;
}
void get_column(int c, vec4 & t) const
{
t.v[0] = element(0,c);
t.v[1] = element(1,c);
t.v[2] = element(2,c);
t.v[3] = element(3,c);
}
vec4 get_column(int c) const
{
vec4 v; get_column(c, v);
return v;
}
matrix4 inverse() const
{
matrix4 minv;
real r1[8], r2[8], r3[8], r4[8];
real *s[4], *tmprow;
s[0] = &r1[0];
s[1] = &r2[0];
s[2] = &r3[0];
s[3] = &r4[0];
register int i,j,p,jj;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
s[i][j] = element(i,j);
if(i==j) s[i][j+4] = 1.0;
else s[i][j+4] = 0.0;
}
}
real scp[4];
for(i=0;i<4;i++)
{
scp[i] = real(fabs(s[i][0]));
for(j=1;j<4;j++)
if(real(fabs(s[i][j])) > scp[i]) scp[i] = real(fabs(s[i][j]));
if(scp[i] == 0.0) return minv; // singular matrix!
}
int pivot_to;
real scp_max;
for(i=0;i<4;i++)
{
// select pivot row
pivot_to = i;
scp_max = real(fabs(s[i][i]/scp[i]));
// find out which row should be on top
for(p=i+1;p<4;p++)
if(real(fabs(s[p][i]/scp[p])) > scp_max)
{ scp_max = real(fabs(s[p][i]/scp[p])); pivot_to = p; }
// Pivot if necessary
if(pivot_to != i)
{
tmprow = s[i];
s[i] = s[pivot_to];
s[pivot_to] = tmprow;
real tmpscp;
tmpscp = scp[i];
scp[i] = scp[pivot_to];
scp[pivot_to] = tmpscp;
}
real mji;
// perform gaussian elimination
for(j=i+1;j<4;j++)
{
mji = s[j][i]/s[i][i];
s[j][i] = 0.0;
for(jj=i+1;jj<8;jj++)
s[j][jj] -= mji*s[i][jj];
}
}
if(s[3][3] == 0.0) return minv; // singular matrix!
//
// Now we have an upper triangular matrix.
//
// x x x x | y y y y
// 0 x x x | y y y y
// 0 0 x x | y y y y
// 0 0 0 x | y y y y
//
// we'll back substitute to get the inverse
//
// 1 0 0 0 | z z z z
// 0 1 0 0 | z z z z
// 0 0 1 0 | z z z z
// 0 0 0 1 | z z z z
//
real mij;
for(i=3;i>0;i--)
{
for(j=i-1;j > -1; j--)
{
mij = s[j][i]/s[i][i];
for(jj=j+1;jj<8;jj++)
s[j][jj] -= mij*s[i][jj];
}
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
minv(i,j) = s[i][j+4] / s[i][i];
return minv;
}
matrix4 transpose() const
{
matrix4 mtrans;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
mtrans(i,j) = element(j,i);
return mtrans;
}
matrix4 & mult_right( const matrix4 & b )
{
matrix4 mt(*this);
set_value(real(0));
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
for(int c=0; c < 4; c++)
element(i,j) += mt(i,c) * b(c,j);
return *this;
}
matrix4 & mult_left( const matrix4 & b )
{
matrix4 mt(*this);
set_value(real(0));
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
for(int c=0; c < 4; c++)
element(i,j) += b(i,c) * mt(c,j);
return *this;
}
// dst = M * src
void mult_matrix_vec( const vec3 &src, vec3 &dst ) const
{
real w = (
src.v[0] * element(3,0) +
src.v[1] * element(3,1) +
src.v[2] * element(3,2) +
element(3,3) );
assert(w != GLH_ZERO);
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(0,1) +
src.v[2] * element(0,2) +
element(0,3) ) / w;
dst.v[1] = (
src.v[0] * element(1,0) +
src.v[1] * element(1,1) +
src.v[2] * element(1,2) +
element(1,3) ) / w;
dst.v[2] = (
src.v[0] * element(2,0) +
src.v[1] * element(2,1) +
src.v[2] * element(2,2) +
element(2,3) ) / w;
}
void mult_matrix_vec( vec3 & src_and_dst) const
{ mult_matrix_vec(vec3(src_and_dst), src_and_dst); }
// dst = src * M
void mult_vec_matrix( const vec3 &src, vec3 &dst ) const
{
real w = (
src.v[0] * element(0,3) +
src.v[1] * element(1,3) +
src.v[2] * element(2,3) +
element(3,3) );
assert(w != GLH_ZERO);
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(1,0) +
src.v[2] * element(2,0) +
element(3,0) ) / w;
dst.v[1] = (
src.v[0] * element(0,1) +
src.v[1] * element(1,1) +
src.v[2] * element(2,1) +
element(3,1) ) / w;
dst.v[2] = (
src.v[0] * element(0,2) +
src.v[1] * element(1,2) +
src.v[2] * element(2,2) +
element(3,2) ) / w;
}
void mult_vec_matrix( vec3 & src_and_dst) const
{ mult_vec_matrix(vec3(src_and_dst), src_and_dst); }
// dst = M * src
void mult_matrix_vec( const vec4 &src, vec4 &dst ) const
{
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(0,1) +
src.v[2] * element(0,2) +
src.v[3] * element(0,3));
dst.v[1] = (
src.v[0] * element(1,0) +
src.v[1] * element(1,1) +
src.v[2] * element(1,2) +
src.v[3] * element(1,3));
dst.v[2] = (
src.v[0] * element(2,0) +
src.v[1] * element(2,1) +
src.v[2] * element(2,2) +
src.v[3] * element(2,3));
dst.v[3] = (
src.v[0] * element(3,0) +
src.v[1] * element(3,1) +
src.v[2] * element(3,2) +
src.v[3] * element(3,3));
}
void mult_matrix_vec( vec4 & src_and_dst) const
{ mult_matrix_vec(vec4(src_and_dst), src_and_dst); }
// dst = src * M
void mult_vec_matrix( const vec4 &src, vec4 &dst ) const
{
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(1,0) +
src.v[2] * element(2,0) +
src.v[3] * element(3,0));
dst.v[1] = (
src.v[0] * element(0,1) +
src.v[1] * element(1,1) +
src.v[2] * element(2,1) +
src.v[3] * element(3,1));
dst.v[2] = (
src.v[0] * element(0,2) +
src.v[1] * element(1,2) +
src.v[2] * element(2,2) +
src.v[3] * element(3,2));
dst.v[3] = (
src.v[0] * element(0,3) +
src.v[1] * element(1,3) +
src.v[2] * element(2,3) +
src.v[3] * element(3,3));
}
void mult_vec_matrix( vec4 & src_and_dst) const
{ mult_vec_matrix(vec4(src_and_dst), src_and_dst); }
// dst = M * src
void mult_matrix_dir( const vec3 &src, vec3 &dst ) const
{
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(0,1) +
src.v[2] * element(0,2) ) ;
dst.v[1] = (
src.v[0] * element(1,0) +
src.v[1] * element(1,1) +
src.v[2] * element(1,2) ) ;
dst.v[2] = (
src.v[0] * element(2,0) +
src.v[1] * element(2,1) +
src.v[2] * element(2,2) ) ;
}
void mult_matrix_dir( vec3 & src_and_dst) const
{ mult_matrix_dir(vec3(src_and_dst), src_and_dst); }
// dst = src * M
void mult_dir_matrix( const vec3 &src, vec3 &dst ) const
{
dst.v[0] = (
src.v[0] * element(0,0) +
src.v[1] * element(1,0) +
src.v[2] * element(2,0) ) ;
dst.v[1] = (
src.v[0] * element(0,1) +
src.v[1] * element(1,1) +
src.v[2] * element(2,1) ) ;
dst.v[2] = (
src.v[0] * element(0,2) +
src.v[1] * element(1,2) +
src.v[2] * element(2,2) ) ;
}
void mult_dir_matrix( vec3 & src_and_dst) const
{ mult_dir_matrix(vec3(src_and_dst), src_and_dst); }
real & operator () (int row, int col)
{ return element(row,col); }
const real & operator () (int row, int col) const
{ return element(row,col); }
real & element (int row, int col)
{ return m[row | (col<<2)]; }
const real & element (int row, int col) const
{ return m[row | (col<<2)]; }
matrix4 & operator *= ( const matrix4 & mat )
{
mult_right( mat );
return *this;
}
matrix4 & operator *= ( const real & r )
{
for (int i = 0; i < 4; ++i)
{
element(0,i) *= r;
element(1,i) *= r;
element(2,i) *= r;
element(3,i) *= r;
}
return *this;
}
matrix4 & operator += ( const matrix4 & mat )
{
for (int i = 0; i < 4; ++i)
{
element(0,i) += mat.element(0,i);
element(1,i) += mat.element(1,i);
element(2,i) += mat.element(2,i);
element(3,i) += mat.element(3,i);
}
return *this;
}
friend matrix4 operator * ( const matrix4 & m1, const matrix4 & m2 );
friend bool operator == ( const matrix4 & m1, const matrix4 & m2 );
friend bool operator != ( const matrix4 & m1, const matrix4 & m2 );
//protected:
real m[16];
};
inline
matrix4 operator * ( const matrix4 & m1, const matrix4 & m2 )
{
matrix4 product;
product = m1;
product.mult_right(m2);
return product;
}
inline
bool operator ==( const matrix4 &m1, const matrix4 &m2 )
{
return (
m1(0,0) == m2(0,0) &&
m1(0,1) == m2(0,1) &&
m1(0,2) == m2(0,2) &&
m1(0,3) == m2(0,3) &&
m1(1,0) == m2(1,0) &&
m1(1,1) == m2(1,1) &&
m1(1,2) == m2(1,2) &&
m1(1,3) == m2(1,3) &&
m1(2,0) == m2(2,0) &&
m1(2,1) == m2(2,1) &&
m1(2,2) == m2(2,2) &&
m1(2,3) == m2(2,3) &&
m1(3,0) == m2(3,0) &&
m1(3,1) == m2(3,1) &&
m1(3,2) == m2(3,2) &&
m1(3,3) == m2(3,3) );
}
inline
bool operator != ( const matrix4 & m1, const matrix4 & m2 )
{ return !( m1 == m2 ); }