Repository navigation
Expand file tree
/
Copy pathlc_math.h
More file actions
2395 lines (1898 loc) · 55.9 KB
/
Copy pathlc_math.h
File metadata and controls
2395 lines (1898 loc) · 55.9 KB
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
#pragma once
#include <math.h>
#include <float.h>
#define LC_DTOR (static_cast<float>(M_PI / 180))
#define LC_RTOD (static_cast<float>(180 / M_PI))
#define LC_PI (static_cast<float>(M_PI))
#define LC_2PI (static_cast<float>(2 * M_PI))
#define LC_RGB_EPSILON (static_cast<float>(0.5f / 255.0f))
#define LC_RGB(r,g,b) LC_RGBA(r,g,b,255)
#define LC_RGBA(r,g,b,a) ((quint32)(((quint8) (r) | ((quint16) (g) << 8)) | (((quint32) (quint8) (b)) << 16) | (((quint32) (quint8) (a)) << 24)))
#define LC_RGBA_RED(rgba) ((quint8)(((rgba) >> 0) & 0xff))
#define LC_RGBA_GREEN(rgba) ((quint8)(((rgba) >> 8) & 0xff))
#define LC_RGBA_BLUE(rgba) ((quint8)(((rgba) >> 16) & 0xff))
#define LC_RGBA_ALPHA(rgba) ((quint8)(((rgba) >> 24) & 0xff))
#define LC_SRGB_TO_LINEAR(v) (powf(v, 2.2f))
#define LC_LINEAR_TO_SRGB(v) (powf(v, 1.0f / 2.2f))
inline quint32 lcRGBAFromQColor(const QColor& Color)
{
return LC_RGBA(Color.red(), Color.green(), Color.blue(), Color.alpha());
}
inline QColor lcQColorFromRGBA(quint32 RGBA)
{
return QColor::fromRgb(LC_RGBA_RED(RGBA), LC_RGBA_GREEN(RGBA), LC_RGBA_BLUE(RGBA), LC_RGBA_ALPHA(RGBA));
}
template<typename T>
inline T lcMin(const T& a, const T& b)
{
return a < b ? a : b;
}
template<typename T>
inline T lcMax(const T& a, const T& b)
{
return a > b ? a : b;
}
template<typename T>
inline T lcClamp(const T& Value, const T& Min, const T& Max)
{
if (Value > Max)
return Max;
else if (Value < Min)
return Min;
else
return Value;
}
class lcVector2i
{
public:
lcVector2i()
{
}
constexpr lcVector2i(const int _x, const int _y)
: x(_x), y(_y)
{
}
int x, y;
};
inline bool operator==(const lcVector2i& a, const lcVector2i& b)
{
return a.x == b.x && a.y == b.y;
}
inline bool operator!=(const lcVector2i& a, const lcVector2i& b)
{
return a.x != b.x || a.y != b.y;
}
class lcVector2
{
public:
lcVector2()
{
}
constexpr lcVector2(const float _x, const float _y)
: x(_x), y(_y)
{
}
operator const float*() const
{
return (const float*)this;
}
operator float*()
{
return (float*)this;
}
const float& operator[](int i) const
{
return ((float*)this)[i];
}
float& operator[](int i)
{
return ((float*)this)[i];
}
bool IsNan() const
{
return std::isnan(x) || std::isnan(y);
}
float x, y;
};
class lcVector3
{
public:
lcVector3()
{
}
constexpr lcVector3(const float _x, const float _y, const float _z)
: x(_x), y(_y), z(_z)
{
}
explicit lcVector3(const lcVector4& v);
operator const float*() const
{
return (const float*)this;
}
operator float*()
{
return (float*)this;
}
const float& operator[](int i) const
{
return ((float*)this)[i];
}
float& operator[](int i)
{
return ((float*)this)[i];
}
bool IsNan() const
{
return std::isnan(x) || std::isnan(y) || std::isnan(z);
}
void Normalize();
float Length() const;
float LengthSquared() const;
float x, y, z;
};
class lcVector4
{
public:
lcVector4()
{
}
constexpr lcVector4(const float _x, const float _y, const float _z, const float _w)
: x(_x), y(_y), z(_z), w(_w)
{
}
constexpr lcVector4(const lcVector3& _xyz, const float _w)
: x(_xyz.x), y(_xyz.y), z(_xyz.z), w(_w)
{
}
operator const float*() const
{
return (const float*)this;
}
operator float*()
{
return (float*)this;
}
const float& operator[](int i) const
{
return ((float*)this)[i];
}
float& operator[](int i)
{
return ((float*)this)[i];
}
bool IsNan() const
{
return std::isnan(x) || std::isnan(y) || std::isnan(z) || std::isnan(w);
}
float x, y, z, w;
};
class lcMatrix33
{
public:
lcMatrix33()
{
}
lcMatrix33(const lcVector3& _x, const lcVector3& _y, const lcVector3& _z)
{
r[0] = _x;
r[1] = _y;
r[2] = _z;
}
explicit lcMatrix33(const lcMatrix44& Matrix);
operator const float*() const
{
return (const float*)this;
}
operator float*()
{
return (float*)this;
}
const lcVector3& operator[](int i) const
{
return r[i];
}
lcVector3& operator[](int i)
{
return r[i];
}
void Orthonormalize();
lcVector3 r[3];
};
class lcMatrix44
{
public:
lcMatrix44()
{
}
lcMatrix44(const lcVector4& _x, const lcVector4& _y, const lcVector4& _z, const lcVector4& _w)
{
r[0] = _x;
r[1] = _y;
r[2] = _z;
r[3] = _w;
}
lcMatrix44(const lcMatrix33& Rotation, const lcVector3& Translation)
{
r[0] = lcVector4(Rotation[0][0], Rotation[0][1], Rotation[0][2], 0.0f);
r[1] = lcVector4(Rotation[1][0], Rotation[1][1], Rotation[1][2], 0.0f);
r[2] = lcVector4(Rotation[2][0], Rotation[2][1], Rotation[2][2], 0.0f);
r[3] = lcVector4(Translation, 1.0f);
}
lcVector3 GetTranslation() const
{
return lcVector3(r[3][0], r[3][1], r[3][2]);
}
void SetTranslation(const lcVector3& Translation)
{
r[3] = lcVector4(Translation[0], Translation[1], Translation[2], 1.0f);
}
operator const float*() const
{
return (const float*)this;
}
operator float*()
{
return (float*)this;
}
const lcVector4& operator[](int i) const
{
return r[i];
}
lcVector4& operator[](int i)
{
return r[i];
}
float Determinant() const;
lcVector4 r[4];
};
inline lcVector3::lcVector3(const lcVector4& v)
: x(v.x), y(v.y), z(v.z)
{
}
inline lcVector3 operator+(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
inline lcVector3 operator-(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x - b.x, a.y - b.y, a.z - b.z);
}
inline lcVector3 operator*(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x * b.x, a.y * b.y, a.z * b.z);
}
inline lcVector3 operator/(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x / b.x, a.y / b.y, a.z / b.z);
}
inline lcVector3 operator*(const lcVector3& a, float b)
{
return lcVector3(a.x * b, a.y * b, a.z * b);
}
inline lcVector3 operator/(const lcVector3& a, float b)
{
return lcVector3(a.x / b, a.y / b, a.z / b);
}
inline lcVector3 operator*(float a, const lcVector3& b)
{
return lcVector3(b.x * a, b.y * a, b.z * a);
}
inline lcVector3 operator/(float a, const lcVector3& b)
{
return lcVector3(b.x / a, b.y / a, b.z / a);
}
inline lcVector3 operator-(const lcVector3& a)
{
return lcVector3(-a.x, -a.y, -a.z);
}
inline lcVector3& operator+=(lcVector3& a, const lcVector3& b)
{
a.x += b.x;
a.y += b.y;
a.z += b.z;
return a;
}
inline lcVector3& operator-=(lcVector3& a, const lcVector3& b)
{
a.x -= b.x;
a.y -= b.y;
a.z -= b.z;
return a;
}
inline lcVector3& operator*=(lcVector3& a, const lcVector3& b)
{
a.x *= b.x;
a.y *= b.y;
a.z *= b.z;
return a;
}
inline lcVector3& operator/=(lcVector3& a, const lcVector3& b)
{
a.x /= b.x;
a.y /= b.y;
a.z /= b.z;
return a;
}
inline lcVector3& operator+=(lcVector3& a, float b)
{
a.x += b;
a.y += b;
a.z += b;
return a;
}
inline lcVector3& operator*=(lcVector3& a, float b)
{
a.x *= b;
a.y *= b;
a.z *= b;
return a;
}
inline lcVector3& operator/=(lcVector3& a, float b)
{
a.x /= b;
a.y /= b;
a.z /= b;
return a;
}
inline bool operator==(const lcVector3& a, const lcVector3& b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
inline bool operator!=(const lcVector3& a, const lcVector3& b)
{
return a.x != b.x || a.y != b.y || a.z != b.z;
}
#ifndef QT_NO_DEBUG
inline QDebug operator<<(QDebug Debug, const lcVector2& v)
{
QDebugStateSaver Saver(Debug);
Debug.nospace() << '(' << v.x << ", " << v.y << ')';
return Debug;
}
inline QDebug operator<<(QDebug Debug, const lcVector3& v)
{
QDebugStateSaver Saver(Debug);
Debug.nospace() << '(' << v.x << ", " << v.y << ", " << v.z << ')';
return Debug;
}
inline QDebug operator<<(QDebug Debug, const lcVector4& v)
{
QDebugStateSaver Saver(Debug);
Debug.nospace() << '(' << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ')';
return Debug;
}
inline QDebug operator<<(QDebug Debug, const lcMatrix33& m)
{
QDebugStateSaver Saver(Debug);
Debug.nospace() << '[' << m[0] << ", " << m[1] << ", " << m[2] << ']';
return Debug;
}
inline QDebug operator<<(QDebug Debug, const lcMatrix44& m)
{
QDebugStateSaver Saver(Debug);
Debug.nospace() << '[' << m[0] << ", " << m[1] << ", " << m[2] << ", " << m[3] << ']';
return Debug;
}
#endif
inline QDataStream& operator<<(QDataStream& Stream, const lcVector3& v)
{
Stream << v.x << v.y << v.z;
return Stream;
}
inline QDataStream& operator>>(QDataStream& Stream, lcVector3& v)
{
Stream >> v.x >> v.y >> v.z;
return Stream;
}
inline QDataStream& operator<<(QDataStream& Stream, const lcVector4& v)
{
Stream << v.x << v.y << v.z << v.w;
return Stream;
}
inline QDataStream& operator >> (QDataStream& Stream, lcVector4& v)
{
Stream >> v.x >> v.y >> v.z >> v.w;
return Stream;
}
inline void lcVector3::Normalize()
{
const float InvLength = 1.0f / Length();
x *= InvLength;
y *= InvLength;
z *= InvLength;
}
inline float lcVector3::Length() const
{
return sqrtf(x * x + y * y + z * z);
}
inline float lcVector3::LengthSquared() const
{
return x * x + y * y + z * z;
}
inline float lcLength(const lcVector3& a)
{
return a.Length();
}
inline float lcLengthSquared(const lcVector3& a)
{
return a.LengthSquared();
}
inline lcVector3 lcNormalize(const lcVector3& a)
{
lcVector3 Ret(a);
Ret.Normalize();
return Ret;
}
inline float lcDot(const lcVector3& a, const lcVector3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float lcDot3(const lcVector4& a, const lcVector3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float lcDot3(const lcVector3& a, const lcVector4& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float lcDot3(const lcVector4& a, const lcVector4& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float lcDot(const lcVector4& a, const lcVector4& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
inline lcVector3 lcCross(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
template<>
inline lcVector3 lcMin<lcVector3>(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, a.z < b.z ? a.z : b.z);
}
template<>
inline lcVector3 lcMax<lcVector3>(const lcVector3& a, const lcVector3& b)
{
return lcVector3(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z);
}
inline lcVector4 operator+(const lcVector4& a, const lcVector4& b)
{
return lcVector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
inline lcVector4 operator-(const lcVector4& a, const lcVector4& b)
{
return lcVector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
inline lcVector4 operator*(const lcVector4& a, float f)
{
return lcVector4(a.x * f, a.y * f, a.z * f, a.w * f);
}
inline lcVector4 operator*(const lcVector4& a, const lcVector4& b)
{
return lcVector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
inline lcVector4 operator/(const lcVector4& a, float f)
{
return lcVector4(a.x / f, a.y / f, a.z / f, a.w / f);
}
inline lcVector4 operator/(const lcVector4& a, const lcVector4& b)
{
return lcVector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
}
inline lcVector4& operator+=(lcVector4& a, const lcVector4& b)
{
a.x += b.x;
a.y += b.y;
a.z += b.z;
a.w += b.w;
return a;
}
inline lcVector4& operator-=(lcVector4& a, const lcVector4& b)
{
a.x -= b.x;
a.y -= b.y;
a.z -= b.z;
a.w -= b.w;
return a;
}
inline lcVector4& operator*=(lcVector4& a, float b)
{
a.x *= b;
a.y *= b;
a.z *= b;
a.w *= b;
return a;
}
inline lcVector4& operator/=(lcVector4& a, float b)
{
a.x /= b;
a.y /= b;
a.z /= b;
a.w /= b;
return a;
}
inline quint32 lcPackNormal(const lcVector3& Normal)
{
quint32 Packed = 0;
Packed |= (((qint8)(Normal.x * 127.0f)) & 0xff) << 0;
Packed |= (((qint8)(Normal.y * 127.0f)) & 0xff) << 8;
Packed |= (((qint8)(Normal.z * 127.0f)) & 0xff) << 16;
return Packed;
}
inline lcVector3 lcUnpackNormal(quint32 Packed)
{
lcVector3 Normal;
Normal.x = (float)(qint8)((Packed >> 0) & 0xff) / 127.0f;
Normal.y = (float)(qint8)((Packed >> 8) & 0xff) / 127.0f;
Normal.z = (float)(qint8)((Packed >> 16) & 0xff) / 127.0f;
return Normal;
}
inline lcVector3 lcVector3LDrawToLeoCAD(const lcVector3& Vector)
{
return lcVector3(Vector[0], Vector[2], -Vector[1]);
}
inline lcVector3 lcVector3FromColor(quint32 Color)
{
lcVector3 v(LC_RGBA_RED(Color), LC_RGBA_GREEN(Color), LC_RGBA_BLUE(Color));
v /= 255.0f;
return v;
}
inline lcVector3 lcVector3FromQColor(QColor Color)
{
return lcVector3(Color.redF(), Color.greenF(), Color.blueF());
}
inline lcVector4 lcVector4FromColor(quint32 Color)
{
lcVector4 v(LC_RGBA_RED(Color), LC_RGBA_GREEN(Color), LC_RGBA_BLUE(Color), LC_RGBA_ALPHA(Color));
v /= 255.0f;
return v;
}
inline quint32 lcColorFromVector3(const lcVector3& Color)
{
return LC_RGB(roundf(Color[0] * 255), roundf(Color[1] * 255), roundf(Color[2] * 255));
}
inline QColor lcQColorFromVector3(const lcVector3& Color)
{
return QColor::fromRgb(roundf(Color[0] * 255), roundf(Color[1] * 255), roundf(Color[2] * 255));
}
inline float lcLuminescence(const lcVector3& Color)
{
return 0.2126f * Color[0] + 0.7152f * Color[1] + 0.0722f * Color[2];
}
inline lcVector3 lcSRGBToLinear(const lcVector3& Color)
{
const float r = LC_SRGB_TO_LINEAR(Color[0]);
const float g = LC_SRGB_TO_LINEAR(Color[1]);
const float b = LC_SRGB_TO_LINEAR(Color[2]);
return lcVector3(r, g, b);
}
inline lcVector3 lcLinearToSRGB(const lcVector3& Color)
{
const float r = LC_LINEAR_TO_SRGB(Color[0]);
const float g = LC_LINEAR_TO_SRGB(Color[1]);
const float b = LC_LINEAR_TO_SRGB(Color[2]);
return lcVector3(r, g, b);
}
inline lcVector3 lcMul(const lcVector3& a, const lcMatrix33& b)
{
return b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2];
}
inline lcVector3 lcMul31(const lcVector3& a, const lcMatrix44& b)
{
lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2] + b.r[3];
return lcVector3(v[0], v[1], v[2]);
}
inline lcVector3 lcMul31(const lcVector4& a, const lcMatrix44& b)
{
lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2] + b.r[3];
return lcVector3(v[0], v[1], v[2]);
}
inline lcVector3 lcMul30(const lcVector3& a, const lcMatrix44& b)
{
lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2];
return lcVector3(v[0], v[1], v[2]);
}
inline lcVector3 lcMul30(const lcVector4& a, const lcMatrix44& b)
{
lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2];
return lcVector3(v[0], v[1], v[2]);
}
inline lcVector4 lcMul4(const lcVector4& a, const lcMatrix44& b)
{
return b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2] + b.r[3] * a[3];
}
inline lcMatrix33 lcMul(const lcMatrix33& a, const lcMatrix33& b)
{
const lcVector3 Col0(b.r[0][0], b.r[1][0], b.r[2][0]);
const lcVector3 Col1(b.r[0][1], b.r[1][1], b.r[2][1]);
const lcVector3 Col2(b.r[0][2], b.r[1][2], b.r[2][2]);
const lcVector3 Ret0(lcDot(a.r[0], Col0), lcDot(a.r[0], Col1), lcDot(a.r[0], Col2));
const lcVector3 Ret1(lcDot(a.r[1], Col0), lcDot(a.r[1], Col1), lcDot(a.r[1], Col2));
const lcVector3 Ret2(lcDot(a.r[2], Col0), lcDot(a.r[2], Col1), lcDot(a.r[2], Col2));
return lcMatrix33(Ret0, Ret1, Ret2);
}
inline lcMatrix44 lcMul(const lcMatrix44& a, const lcMatrix44& b)
{
lcMatrix44 Result;
Result.r[0] = b.r[0] * a[0].x + b.r[1] * a[0].y + b.r[2] * a[0].z + b.r[3] * a[0].w;
Result.r[1] = b.r[0] * a[1].x + b.r[1] * a[1].y + b.r[2] * a[1].z + b.r[3] * a[1].w;
Result.r[2] = b.r[0] * a[2].x + b.r[1] * a[2].y + b.r[2] * a[2].z + b.r[3] * a[2].w;
Result.r[3] = b.r[0] * a[3].x + b.r[1] * a[3].y + b.r[2] * a[3].z + b.r[3] * a[3].w;
return Result;
}
inline lcMatrix33::lcMatrix33(const lcMatrix44& Matrix)
{
r[0] = lcVector3(Matrix.r[0].x, Matrix.r[0].y, Matrix.r[0].z);
r[1] = lcVector3(Matrix.r[1].x, Matrix.r[1].y, Matrix.r[1].z);
r[2] = lcVector3(Matrix.r[2].x, Matrix.r[2].y, Matrix.r[2].z);
}
inline void lcMatrix33::Orthonormalize()
{
r[0] = lcNormalize(r[0]);
r[1] = lcNormalize(r[1] - lcDot(r[1], r[0]) * r[0]);
r[2] = r[2] - lcDot(r[2], r[0]) * r[0];
r[2] -= lcDot(r[2], r[1]) * r[1];
r[2] = lcNormalize(r[2]);
}
inline lcMatrix33 lcMatrix33Identity()
{
lcMatrix33 m;
m.r[0] = lcVector3(1.0f, 0.0f, 0.0f);
m.r[1] = lcVector3(0.0f, 1.0f, 0.0f);
m.r[2] = lcVector3(0.0f, 0.0f, 1.0f);
return m;
}
inline lcMatrix33 lcMatrix33Scale(const lcVector3& Scale)
{
lcMatrix33 m;
m.r[0] = lcVector3(Scale.x, 0.0f, 0.0f);
m.r[1] = lcVector3(0.0f, Scale.y, 0.0f);
m.r[2] = lcVector3(0.0f, 0.0f, Scale.z);
return m;
}
inline lcMatrix33 lcMatrix33RotationX(const float Radians)
{
float s, c;
s = sinf(Radians);
c = cosf(Radians);
lcMatrix33 m;
m.r[0] = lcVector3(1.0f, 0.0f, 0.0f);
m.r[1] = lcVector3(0.0f, c, s);
m.r[2] = lcVector3(0.0f, -s, c);
return m;
}
inline lcMatrix33 lcMatrix33RotationY(const float Radians)
{
float s, c;
s = sinf(Radians);
c = cosf(Radians);
lcMatrix33 m;
m.r[0] = lcVector3( c, 0.0f, -s);
m.r[1] = lcVector3(0.0f, 1.0f, 0.0f);
m.r[2] = lcVector3( s, 0.0f, c);
return m;
}
inline lcMatrix33 lcMatrix33RotationZ(const float Radians)
{
float s = sinf(Radians);
float c = cosf(Radians);
lcMatrix33 m;
m.r[0] = lcVector3( c, s, 0.0f);
m.r[1] = lcVector3( -s, c, 0.0f);
m.r[2] = lcVector3(0.0f, 0.0f, 1.0f);
return m;
}
inline lcMatrix33 lcMatrix33FromAxisAngle(const lcVector3& Axis, const float Radians)
{
float s = sinf(Radians);
float c = cosf(Radians);
float mag = Axis.Length();
if (mag == 0.0f)
return lcMatrix33Identity();
lcVector3 Normal = Axis * (1.0f / mag);
float xx = Normal[0] * Normal[0];
float yy = Normal[1] * Normal[1];
float zz = Normal[2] * Normal[2];
float xy = Normal[0] * Normal[1];
float yz = Normal[1] * Normal[2];
float zx = Normal[2] * Normal[0];
float xs = Normal[0] * s;
float ys = Normal[1] * s;
float zs = Normal[2] * s;
float one_c = 1.0f - c;
lcMatrix33 m;
m.r[0] = lcVector3((one_c * xx) + c, (one_c * xy) + zs, (one_c * zx) - ys);
m.r[1] = lcVector3((one_c * xy) - zs, (one_c * yy) + c, (one_c * yz) + xs);
m.r[2] = lcVector3((one_c * zx) + ys, (one_c * yz) - xs, (one_c * zz) + c);
return m;
}
inline lcMatrix33 lcMatrix33Transpose(const lcMatrix33& m)
{
lcMatrix33 t;
t.r[0] = lcVector3(m[0][0], m[1][0], m[2][0]);
t.r[1] = lcVector3(m[0][1], m[1][1], m[2][1]);
t.r[2] = lcVector3(m[0][2], m[1][2], m[2][2]);
return t;
}
inline lcMatrix33 lcMatrix33AffineInverse(const lcMatrix33& m)
{
lcMatrix33 Inv;
Inv.r[0] = lcVector3(m.r[0][0], m.r[1][0], m.r[2][0]);
Inv.r[1] = lcVector3(m.r[0][1], m.r[1][1], m.r[2][1]);
Inv.r[2] = lcVector3(m.r[0][2], m.r[1][2], m.r[2][2]);
return Inv;
}
inline lcMatrix33 lcMatrix33FromEulerAngles(const lcVector3& Radians)
{
float CosYaw, SinYaw, CosPitch, SinPitch, CosRoll, SinRoll;
CosRoll = cosf(Radians[0]);
SinRoll = sinf(Radians[0]);
CosPitch = cosf(Radians[1]);
SinPitch = sinf(Radians[1]);
CosYaw = cosf(Radians[2]);
SinYaw = sinf(Radians[2]);
lcMatrix33 m;
m.r[0] = lcVector3(CosYaw * CosPitch, SinYaw * CosPitch, -SinPitch);
m.r[1] = lcVector3(CosYaw * SinPitch * SinRoll - SinYaw * CosRoll, CosYaw * CosRoll + SinYaw * SinPitch * SinRoll, CosPitch * SinRoll);
m.r[2] = lcVector3(CosYaw * SinPitch * CosRoll + SinYaw * SinRoll, SinYaw * SinPitch * CosRoll - CosYaw * SinRoll, CosPitch * CosRoll);
return m;
}
inline lcVector3 lcMatrix33ToEulerAngles(const lcMatrix33& RotMat)
{
float SinPitch, CosPitch, SinRoll, CosRoll, SinYaw, CosYaw;
SinPitch = -RotMat.r[0][2];
CosPitch = sqrtf(1 - SinPitch*SinPitch);
if (fabsf(CosPitch) > 0.0005f)
{
SinRoll = RotMat.r[1][2] / CosPitch;
CosRoll = RotMat.r[2][2] / CosPitch;
SinYaw = RotMat.r[0][1] / CosPitch;
CosYaw = RotMat.r[0][0] / CosPitch;
}
else
{
SinRoll = -RotMat.r[2][1];
CosRoll = RotMat.r[1][1];
SinYaw = 0.0f;
CosYaw = 1.0f;
}
lcVector3 Rot(atan2f(SinRoll, CosRoll), atan2f(SinPitch, CosPitch), atan2f(SinYaw, CosYaw));
if (Rot[0] < 0) Rot[0] += LC_2PI;
if (Rot[1] < 0) Rot[1] += LC_2PI;
if (Rot[2] < 0) Rot[2] += LC_2PI;
return Rot;
}
inline float lcMatrix44::Determinant() const
{
return r[0][0] * r[1][1] * r[2][2] + r[0][1] * r[1][2] * r[2][0] +
r[0][2] * r[1][0] * r[2][1] - r[0][0] * r[1][2] * r[2][1] -
r[0][1] * r[1][0] * r[2][2] - r[0][2] * r[1][1] * r[2][0];
}
inline lcMatrix44 lcMatrix44Identity()
{
lcMatrix44 m;
m.r[0] = lcVector4(1.0f, 0.0f, 0.0f, 0.0f);
m.r[1] = lcVector4(0.0f, 1.0f, 0.0f, 0.0f);
m.r[2] = lcVector4(0.0f, 0.0f, 1.0f, 0.0f);
m.r[3] = lcVector4(0.0f, 0.0f, 0.0f, 1.0f);
return m;
}
inline lcMatrix44 lcMatrix44Translation(const lcVector3& Translation)
{
lcMatrix44 m;
m.r[0] = lcVector4(1.0f, 0.0f, 0.0f, 0.0f);
m.r[1] = lcVector4(0.0f, 1.0f, 0.0f, 0.0f);
m.r[2] = lcVector4(0.0f, 0.0f, 1.0f, 0.0f);
m.r[3] = lcVector4(Translation[0], Translation[1], Translation[2], 1.0f);