-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_matrix3x3.h
1207 lines (1037 loc) · 41.6 KB
/
tiny_matrix3x3.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
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_MATRIX3x3_H
#define TINY_MATRIX3x3_H
#include <stdio.h>
#include "tiny_quaternion.h"
#include "tiny_vector3.h"
template <typename TinyScalar, typename TinyConstants>
class TinyMatrix3x3 {
typedef ::TinyQuaternion<TinyScalar, TinyConstants> TinyQuaternion;
typedef ::TinyVector3<TinyScalar, TinyConstants> TinyVector3;
/// Data storage for the matrix, each vector is a row of the matrix
TinyVector3 m_el[3];
public:
/** @brief No initialization constructor */
TinyMatrix3x3() {
set_zero();
};
// TinyMatrix3x3() = default;
// necessary for variable-size matrix interoperability
int m_rows{3};
int m_cols{3};
// explicit TinyMatrix3x3(const TinyScalar *m) {
// setFromOpenGLSubMatrix(m); }
/**@brief Constructor from Quaternion */
explicit TinyMatrix3x3(const TinyQuaternion& q) { setRotation(q); }
/** @brief Constructor with row major formatting */
TinyMatrix3x3(const TinyScalar& xx, const TinyScalar& xy,
const TinyScalar& xz, const TinyScalar& yx,
const TinyScalar& yy, const TinyScalar& yz,
const TinyScalar& zx, const TinyScalar& zy,
const TinyScalar& zz) {
setValue(xx, xy, xz, yx, yy, yz, zx, zy, zz);
}
/** @brief Copy constructor */
inline TinyMatrix3x3(const TinyMatrix3x3& other) {
m_el[0] = other.m_el[0];
m_el[1] = other.m_el[1];
m_el[2] = other.m_el[2];
}
/** @brief Assignment Operator */
inline TinyMatrix3x3& operator=(const TinyMatrix3x3& other) {
m_el[0] = other.m_el[0];
m_el[1] = other.m_el[1];
m_el[2] = other.m_el[2];
return *this;
}
inline TinyMatrix3x3& operator*=(const TinyMatrix3x3& m) {
setValue(m.tdotx(m_el[0]), m.tdoty(m_el[0]), m.tdotz(m_el[0]),
m.tdotx(m_el[1]), m.tdoty(m_el[1]), m.tdotz(m_el[1]),
m.tdotx(m_el[2]), m.tdoty(m_el[2]), m.tdotz(m_el[2]));
return *this;
}
inline TinyMatrix3x3(const TinyVector3& v0, const TinyVector3& v1,
const TinyVector3& v2) {
m_el[0] = v0;
m_el[1] = v1;
m_el[2] = v2;
}
/** @brief Set the values of the matrix explicitly (row major)
* @param xx Top left
* @param xy Top Middle
* @param xz Top Right
* @param yx Middle Left
* @param yy Middle Middle
* @param yz Middle Right
* @param zx Bottom Left
* @param zy Bottom Middle
* @param zz Bottom Right*/
void setValue(const TinyScalar& xx, const TinyScalar& xy,
const TinyScalar& xz, const TinyScalar& yx,
const TinyScalar& yy, const TinyScalar& yz,
const TinyScalar& zx, const TinyScalar& zy,
const TinyScalar& zz) {
m_el[0].setValue(xx, xy, xz);
m_el[1].setValue(yx, yy, yz);
m_el[2].setValue(zx, zy, zz);
}
/** @brief Get a mutable reference to a row of the matrix as a vector
* @param i Row number 0 indexed */
inline TinyVector3& operator[](int i) {
TinyConstants::FullAssert(0 <= i && i < 3);
return m_el[i];
}
/** @brief Get a const reference to a row of the matrix as a vector
* @param i Row number 0 indexed */
inline const TinyVector3& operator[](int i) const {
TinyConstants::FullAssert(0 <= i && i < 3);
return m_el[i];
}
inline const TinyScalar& operator()(int row, int col) const {
TinyConstants::FullAssert(0 <= row && row < 3);
TinyConstants::FullAssert(0 <= col && col < 3);
return m_el[row][col];
}
inline TinyScalar& operator()(int row, int col) {
TinyConstants::FullAssert(0 <= row && row < 3);
TinyConstants::FullAssert(0 <= col && col < 3);
return m_el[row][col];
}
/** @brief Set the matrix from euler angles YPR around ZYX axes
* @param eulerX Roll about X axis
* @param eulerY Pitch around Y axis
* @param eulerZ Yaw about Z axis
*
* These angles are used to produce a rotation matrix. The euler
* angles are applied in ZYX order. I.e a vector is first rotated
* about X then Y and then Z
**/
void setEulerZYX(TinyScalar eulerX, TinyScalar eulerY, TinyScalar eulerZ) {
///@todo proposed to reverse this since it's labeled zyx but takes arguments
/// xyz and it will match all other parts of the code
TinyScalar ci(TinyConstants::cos1(eulerX));
TinyScalar cj(TinyConstants::cos1(eulerY));
TinyScalar ch(TinyConstants::cos1(eulerZ));
TinyScalar si(TinyConstants::sin1(eulerX));
TinyScalar sj(TinyConstants::sin1(eulerY));
TinyScalar sh(TinyConstants::sin1(eulerZ));
TinyScalar cc = ci * ch;
TinyScalar cs = ci * sh;
TinyScalar sc = si * ch;
TinyScalar ss = si * sh;
setValue(cj * ch, sj * sc - cs, sj * cc + ss, cj * sh, sj * ss + cc,
sj * cs - sc, -sj, cj * si, cj * ci);
}
void set_rotation_x(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
m_el[0].setValue(i, o, o);
m_el[1].setValue(o, c, -s);
m_el[2].setValue(o, s, c);
}
TinyScalar adj_set_rotation_x(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
return - m_el[1][1]*s - m_el[1][2]*c
+ m_el[2][1]*c - m_el[2][2]*s;
}
void set_rotation_y(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
m_el[0].setValue(c, o, s);
m_el[1].setValue(o, i, o);
m_el[2].setValue(-s, o, c);
}
TinyScalar adj_set_rotation_y(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
return -m_el[0][0]*s + m_el[0][2]*c
- m_el[2][0]*c - m_el[2][2]*s;
}
void set_rotation_z(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
m_el[0].setValue(c, -s, o);
m_el[1].setValue(s, c, o);
m_el[2].setValue(o, o, i);
}
TinyScalar adj_set_rotation_z(TinyScalar angle) {
TinyScalar c(TinyConstants::cos1(angle));
TinyScalar s(TinyConstants::sin1(angle));
TinyScalar o(TinyConstants::zero());
TinyScalar i(TinyConstants::one());
return -m_el[0][0]*s - m_el[0][1]*c
+ m_el[1][0]*c - m_el[1][1]*s ;
}
static TinyMatrix3x3 vvt(const TinyVector3& vecA, const TinyVector3& vecB) {
TinyMatrix3x3 m;
for (int i = 0; i < 3; i++) {
m[i] = vecA[i] * vecB;
}
return m;
}
/**@brief Set the matrix to the identity */
void set_identity() {
setValue(TinyConstants::convert(1), TinyConstants::convert(0),
TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(1), TinyConstants::convert(0),
TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(1));
}
void set_zero() {
setValue(TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(0), TinyConstants::convert(0),
TinyConstants::convert(0));
}
inline TinyScalar tdotx(const TinyVector3& v) const {
return m_el[0].x() * v.x() + m_el[1].x() * v.y() + m_el[2].x() * v.z();
}
inline TinyScalar tdoty(const TinyVector3& v) const {
return m_el[0].y() * v.x() + m_el[1].y() * v.y() + m_el[2].y() * v.z();
}
inline TinyScalar tdotz(const TinyVector3& v) const {
return m_el[0].z() * v.x() + m_el[1].z() * v.y() + m_el[2].z() * v.z();
}
/** @brief Set the matrix from a quaternion
* @param q The Quaternion to match */
void setRotation(const TinyQuaternion& q) {
TinyScalar d = q.length2();
// TODO reactivate assertion
if (TinyConstants::getBool(
d == TinyConstants::zero())) {
return;
}
// TinyConstants::FullAssert(d != TinyConstants::zero());
TinyScalar s = TinyConstants::two() / d;
TinyScalar xs = q.x() * s, ys = q.y() * s, zs = q.z() * s;
TinyScalar wx = q.w() * xs, wy = q.w() * ys, wz = q.w() * zs;
TinyScalar xx = q.x() * xs, xy = q.x() * ys, xz = q.x() * zs;
TinyScalar yy = q.y() * ys, yz = q.y() * zs, zz = q.z() * zs;
setValue(TinyConstants::one() - (yy + zz), xy - wz, xz + wy,
xy + wz, TinyConstants::one() - (xx + zz), yz - wx,
xz - wy, yz + wx, TinyConstants::one() - (xx + yy));
}
/** @brief Set the matrix from a quaternion
* @param q The Quaternion to match */
void adj_setRotation(const TinyQuaternion& q, TinyQuaternion& R) const {
TinyScalar d = q.length2();
// TODO reactivate assertion
if (TinyConstants::getBool(
d == TinyConstants::zero())) {
return;
}
// TinyConstants::FullAssert(d != TinyConstants::zero());
TinyScalar s = TinyConstants::two() / d;
TinyScalar xs = q.x() * s, ys = q.y() * s, zs = q.z() * s;
TinyScalar Rxs, Rys, Rzs;
Rxs = TinyConstants::zero();
Rys = TinyConstants::zero();
Rzs = TinyConstants::zero();
R.m_x += m_el[0][1]*ys + m_el[0][2]*zs + m_el[1][0]*ys - m_el[1][1]*xs + m_el[2][0]*zs - m_el[2][2]*xs;
R.m_y += -m_el[0][0]*ys + m_el[1][2]*zs + m_el[2][1]*zs - m_el[2][2]*ys;
R.m_z += -m_el[0][0]*zs - m_el[1][1]*zs;
R.m_w += -m_el[0][1]*zs + m_el[0][2]*ys + m_el[1][0]*zs - m_el[1][2]*xs - m_el[2][0]*ys + m_el[2][1]*xs;
Rxs += -m_el[1][1]*q.x() - m_el[1][2]*q.w() + m_el[2][1]*q.w() - m_el[2][2]*q.x();
Rys += -m_el[0][0]*q.y() + m_el[0][1]*q.x() + m_el[0][2]*q.w() + m_el[1][0]*q.x() - m_el[2][0]*q.w()
- m_el[2][2]*q.y();
Rzs += -m_el[0][0]*q.z() - m_el[0][1]*q.w() + m_el[0][2]*q.x() + m_el[1][0]*q.w() - m_el[1][1]*q.z()
+m_el[1][2]*q.y() + m_el[2][0]*q.x() + m_el[2][1]*q.y();
TinyScalar Rs = TinyConstants::zero();
Rs += q.x()*Rxs + q.y()*Rys + q.z()*Rzs;
TinyScalar s4 = TinyConstants::two()*TinyConstants::two();
R.m_x += s*Rxs - s4 * q.x()/d/d * Rs;
R.m_y += s*Rys - s4 * q.y()/d/d * Rs;
R.m_z += s*Rzs - s4 * q.z()/d/d * Rs;
R.m_w += - s4 * q.w()/d/d * Rs;
// setValue( TinyConstants::one() - (yy + zz), xy - wz, xz + wy,
// xy + wz, TinyConstants::one() - (xx + zz), yz - wx,
// xz - wy, yz + wx, TinyConstants::one() - (xx + yy));
}
/**@brief Get the matrix represented as a quaternion
* @param q The quaternion which will be set */
void getRotation(TinyQuaternion& q) const {
TinyScalar trace = m_el[0].x() + m_el[1].y() + m_el[2].z();
TinyScalar temp[4];
if (TinyConstants::getBool(
trace < TinyConstants::zero())) {
int i = TinyConstants::getBool(
m_el[0].x() < m_el[1].y()) ? (TinyConstants::getBool(
m_el[1].y() < m_el[2].z()) ? 2 : 1)
: (TinyConstants::getBool(
m_el[0].x() < m_el[2].z()) ? 2 : 0);
int j = (i + 1) % 3;
int k = (i + 2) % 3;
TinyScalar tmp =
((m_el[i][i] - m_el[j][j]) - m_el[k][k]) + TinyConstants::one();
TinyScalar s = TinyConstants::sqrt1(tmp);
temp[i] = s * TinyConstants::half();
s = TinyConstants::half() / s;
temp[3] = (m_el[k][j] - m_el[j][k]) * s;
temp[j] = (m_el[j][i] + m_el[i][j]) * s;
temp[k] = (m_el[k][i] + m_el[i][k]) * s;
} else {
TinyScalar s = TinyConstants::sqrt1(trace + TinyConstants::one());
temp[3] = (s * TinyConstants::half());
s = TinyConstants::half() / s;
temp[0] = ((m_el[2].y() - m_el[1].z()) * s);
temp[1] = ((m_el[0].z() - m_el[2].x()) * s);
temp[2] = ((m_el[1].x() - m_el[0].y()) * s);
}
q.setValue(temp[0], temp[1], temp[2], temp[3]);
}
void adj_getRotation(TinyQuaternion& Rq, TinyMatrix3x3& R) const {
TinyScalar trace = m_el[0].x() + m_el[1].y() + m_el[2].z();
TinyScalar Rtrace = TinyConstants::zero();
if (TinyConstants::getBool(
trace < TinyConstants::zero())) {
int i = TinyConstants::getBool(
m_el[0].x() < m_el[1].y()) ? (TinyConstants::getBool(
m_el[1].y() < m_el[2].z()) ? 2 : 1)
: (TinyConstants::getBool(
m_el[0].x() < m_el[2].z()) ? 2 : 0);
int j = (i + 1) % 3;
int k = (i + 2) % 3;
TinyScalar tmp =
((m_el[i][i] - m_el[j][j]) - m_el[k][k]) + TinyConstants::one();
TinyScalar s0 = TinyConstants::sqrt1(tmp);
TinyScalar s = TinyConstants::half() / s0;
TinyScalar Rs = TinyConstants::zero();
TinyScalar Rs0 = TinyConstants::zero();
TinyScalar Rtmp = TinyConstants::zero();
R.m_el[k][j] += s*Rq[3];
R.m_el[j][k] += -s*Rq[3];
R.m_el[j][i] += s*Rq[j];
R.m_el[i][j] += s*Rq[j];
R.m_el[k][i] += s*Rq[k];
R.m_el[i][k] += s*Rq[k];
Rs += (m_el[k][j] - m_el[j][k])*Rq[3] +
(m_el[j][i] + m_el[i][j])*Rq[j] +
(m_el[k][i] + m_el[i][k])*Rq[k];
Rs0 += -TinyConstants::half()/s0/s0*Rs;
Rs0 += TinyConstants::half()*Rq[i];
Rtmp += TinyConstants::half() / s0 *Rs0;
R.m_el[i][i] += Rtmp;
R.m_el[j][j] += -Rtmp;
R.m_el[k][k] += -Rtmp;
} else {
TinyScalar s0 = TinyConstants::sqrt1(trace + TinyConstants::one());
TinyScalar s = TinyConstants::half() / s0;
TinyScalar Rs = TinyConstants::zero();
TinyScalar Rs0 = TinyConstants::zero();
R.m_el[2][1] += s*Rq[0];
R.m_el[1][2] += -s*Rq[0];
R.m_el[0][2] += s*Rq[1];
R.m_el[2][0] += -s*Rq[1];
R.m_el[1][0] += s*Rq[2];
R.m_el[0][1] += -s*Rq[2];
Rs += (m_el[2].y() - m_el[1].z())*Rq[0] +
(m_el[0].z() - m_el[2].x())*Rq[1] +
(m_el[1].x() - m_el[0].y())*Rq[2];
Rs0 += -TinyConstants::half()/s0/s0*Rs;
Rs0 += TinyConstants::half()*Rq[3];
Rtrace += TinyConstants::half() / s0 *Rs0;
R.m_el[0][0] += Rtrace;
R.m_el[1][1] += Rtrace;
R.m_el[2][2] += Rtrace;
}
}
/**@brief Return the transpose of the matrix */
TinyMatrix3x3 transpose() const;
/** @brief Adds by the target matrix on the right
* @param m matrix to be applied
* Equivilant to this = this + m */
TinyMatrix3x3& operator+=(const TinyMatrix3x3& m) {
setValue(m_el[0][0] + m.m_el[0][0], m_el[0][1] + m.m_el[0][1],
m_el[0][2] + m.m_el[0][2], m_el[1][0] + m.m_el[1][0],
m_el[1][1] + m.m_el[1][1], m_el[1][2] + m.m_el[1][2],
m_el[2][0] + m.m_el[2][0], m_el[2][1] + m.m_el[2][1],
m_el[2][2] + m.m_el[2][2]);
return *this;
}
/** @brief Substractss by the target matrix on the right
* @param m matrix to be applied
* Equivilant to this = this - m */
TinyMatrix3x3& operator-=(const TinyMatrix3x3& m) {
setValue(m_el[0][0] - m.m_el[0][0], m_el[0][1] - m.m_el[0][1],
m_el[0][2] - m.m_el[0][2], m_el[1][0] - m.m_el[1][0],
m_el[1][1] - m.m_el[1][1], m_el[1][2] - m.m_el[1][2],
m_el[2][0] - m.m_el[2][0], m_el[2][1] - m.m_el[2][1],
m_el[2][2] - m.m_el[2][2]);
return *this;
}
/** @brief Get a row of the matrix as a vector
* @param i Row number 0 indexed */
inline const TinyVector3& getRow(int i) const {
TinyConstants::FullAssert(0 <= i && i < 3);
return m_el[i];
}
void print(const char* txt) const {
printf("%s\n", txt);
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
TinyScalar val = getRow(r)[c];
double v = TinyConstants::getDouble(val);
printf("%f, ", v);
}
printf("\n");
}
}
/**@brief Return the inverse of the matrix */
TinyMatrix3x3 inverse() const {
TinyVector3 co(cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1));
TinyScalar det = (*this)[0].dot(co);
// btFullAssert(det != TinyScalar(0.0));
TinyConstants::FullAssert(TinyConstants::getBool(
det != TinyConstants::zero()));
TinyScalar s = TinyConstants::one() / det;
return TinyMatrix3x3(
co.x() * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s, co.y() * s,
cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s, co.z() * s,
cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
}
inline TinyMatrix3x3 operator-() const {
return TinyMatrix3x3(-m_el[0], -m_el[1], -m_el[2]);
}
/**@brief Calculate the matrix cofactor
* @param r1 The first row to use for calculating the cofactor
* @param c1 The first column to use for calculating the cofactor
* @param r1 The second row to use for calculating the cofactor
* @param c1 The second column to use for calculating the cofactor
* See http://en.wikipedia.org/wiki/Cofactor_(linear_algebra) for more details
*/
TinyScalar cofac(int r1, int c1, int r2, int c2) const {
return m_el[r1][c1] * m_el[r2][c2] - m_el[r1][c2] * m_el[r2][c1];
}
static const TinyMatrix3x3& get_identity() {
static const TinyMatrix3x3 identityMatrix(
TinyConstants::one(), TinyConstants::zero(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::one(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::zero(), TinyConstants::one());
return identityMatrix;
}
static const TinyMatrix3x3& get_zero() {
static const TinyMatrix3x3 identityMatrix(
TinyConstants::zero(), TinyConstants::zero(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::zero(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::zero(), TinyConstants::zero());
return identityMatrix;
}
inline TinyVector3 dot(const TinyVector3& v) const {
return TinyVector3((*this)[0].dot(v), (*this)[1].dot(v), (*this)[2].dot(v));
}
#if 0
/** @brief Get a column of the matrix as a vector
* @param i Column number 0 indexed */
inline TinyVector3 getColumn(int i) const
{
return TinyVector3(m_el[0][i], m_el[1][i], m_el[2][i]);
}
/** @brief Set from the rotational part of a 4x4 OpenGL matrix
* @param m A pointer to the beginning of the array of scalars*/
void setFromOpenGLSubMatrix(const TinyScalar* m)
{
m_el[0].setValue(m[0], m[4], m[8]);
m_el[1].setValue(m[1], m[5], m[9]);
m_el[2].setValue(m[2], m[6], m[10]);
}
/** @brief Set the matrix from euler angles using YPR around YXZ respectively
* @param yaw Yaw about Y axis
* @param pitch Pitch about X axis
* @param roll Roll about Z axis
*/
void setEulerYPR(const TinyScalar& yaw, const TinyScalar& pitch, const TinyScalar& roll)
{
setEulerZYX(roll, pitch, yaw);
}
static const TinyMatrix3x3& getIdentity()
{
#if (defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE)) || defined(BT_USE_NEON)
static const TinyMatrix3x3
identityMatrix(v1000, v0100, v0010);
#else
static const TinyMatrix3x3
identityMatrix(
TinyScalar(1.0), TinyScalar(0.0), TinyScalar(0.0),
TinyScalar(0.0), TinyScalar(1.0), TinyScalar(0.0),
TinyScalar(0.0), TinyScalar(0.0), TinyScalar(1.0));
#endif
return identityMatrix;
}
/**@brief Fill the rotational part of an OpenGL matrix and clear the shear/perspective
* @param m The array to be filled */
void getOpenGLSubMatrix(TinyScalar * m) const
{
#if defined BT_USE_SIMD_VECTOR3 && defined(BT_USE_SSE_IN_API) && \
defined(BT_USE_SSE)
__m128 v0 = m_el[0].mVec128;
__m128 v1 = m_el[1].mVec128;
__m128 v2 = m_el[2].mVec128; // x2 y2 z2 w2
__m128* vm = (__m128*)m;
__m128 vT;
v2 = _mm_and_ps(v2, btvFFF0fMask); // x2 y2 z2 0
vT = _mm_unpackhi_ps(v0, v1); // z0 z1 * *
v0 = _mm_unpacklo_ps(v0, v1); // x0 x1 y0 y1
v1 = _mm_shuffle_ps(v0, v2, BT_SHUFFLE(2, 3, 1, 3)); // y0 y1 y2 0
v0 = _mm_shuffle_ps(v0, v2, BT_SHUFFLE(0, 1, 0, 3)); // x0 x1 x2 0
v2 = btCastdTo128f(_mm_move_sd(btCastfTo128d(v2), btCastfTo128d(vT))); // z0 z1 z2 0
vm[0] = v0;
vm[1] = v1;
vm[2] = v2;
#elif defined(BT_USE_NEON)
// note: zeros the w channel. We can preserve it at the cost of two more vtrn instructions.
static const uint32x2_t zMask = (const uint32x2_t){static_cast<uint32_t>(-1), 0};
float32x4_t* vm = (float32x4_t*)m;
float32x4x2_t top = vtrnq_f32(m_el[0].mVec128, m_el[1].mVec128); // {x0 x1 z0 z1}, {y0 y1 w0 w1}
float32x2x2_t bl = vtrn_f32(vget_low_f32(m_el[2].mVec128), vdup_n_f32(0.0f)); // {x2 0 }, {y2 0}
float32x4_t v0 = vcombine_f32(vget_low_f32(top.val[0]), bl.val[0]);
float32x4_t v1 = vcombine_f32(vget_low_f32(top.val[1]), bl.val[1]);
float32x2_t q = (float32x2_t)vand_u32((uint32x2_t)vget_high_f32(m_el[2].mVec128), zMask);
float32x4_t v2 = vcombine_f32(vget_high_f32(top.val[0]), q); // z0 z1 z2 0
vm[0] = v0;
vm[1] = v1;
vm[2] = v2;
#else
m[0] = TinyScalar(m_el[0].x());
m[1] = TinyScalar(m_el[1].x());
m[2] = TinyScalar(m_el[2].x());
m[3] = TinyScalar(0.0);
m[4] = TinyScalar(m_el[0].y());
m[5] = TinyScalar(m_el[1].y());
m[6] = TinyScalar(m_el[2].y());
m[7] = TinyScalar(0.0);
m[8] = TinyScalar(m_el[0].z());
m[9] = TinyScalar(m_el[1].z());
m[10] = TinyScalar(m_el[2].z());
m[11] = TinyScalar(0.0);
#endif
}
/**@brief Get the matrix represented as euler angles around YXZ, roundtrip with setEulerYPR
* @param yaw Yaw around Y axis
* @param pitch Pitch around X axis
* @param roll around Z axis */
void getEulerYPR(TinyScalar & yaw, TinyScalar & pitch, TinyScalar & roll) const
{
// first use the normal calculus
yaw = TinyScalar(btAtan2(m_el[1].x(), m_el[0].x()));
pitch = TinyScalar(btAsin(-m_el[2].x()));
roll = TinyScalar(btAtan2(m_el[2].y(), m_el[2].z()));
// on pitch = +/-HalfPI
if (btFabs(pitch) == SIMD_HALF_PI)
{
if (yaw > 0)
yaw -= SIMD_PI;
else
yaw += SIMD_PI;
if (roll > 0)
roll -= SIMD_PI;
else
roll += SIMD_PI;
}
}
/**@brief Get the matrix represented as euler angles around ZYX
* @param yaw Yaw around Z axis
* @param pitch Pitch around Y axis
* @param roll around X axis
* @param solution_number Which solution of two possible solutions ( 1 or 2) are possible values*/
void getEulerZYX(TinyScalar & yaw, TinyScalar & pitch, TinyScalar & roll, unsigned int solution_number = 1) const
{
struct Euler
{
TinyScalar yaw;
TinyScalar pitch;
TinyScalar roll;
};
Euler euler_out;
Euler euler_out2; //second solution
//get the pointer to the raw data
// Check that pitch is not at a singularity
if (btFabs(m_el[2].x()) >= 1)
{
euler_out.yaw = 0;
euler_out2.yaw = 0;
// From difference of angles formula
TinyScalar delta = btAtan2(m_el[0].x(), m_el[0].z());
if (m_el[2].x() > 0) //gimbal locked up
{
euler_out.pitch = SIMD_PI / TinyScalar(2.0);
euler_out2.pitch = SIMD_PI / TinyScalar(2.0);
euler_out.roll = euler_out.pitch + delta;
euler_out2.roll = euler_out.pitch + delta;
}
else // gimbal locked down
{
euler_out.pitch = -SIMD_PI / TinyScalar(2.0);
euler_out2.pitch = -SIMD_PI / TinyScalar(2.0);
euler_out.roll = -euler_out.pitch + delta;
euler_out2.roll = -euler_out.pitch + delta;
}
}
else
{
euler_out.pitch = -btAsin(m_el[2].x());
euler_out2.pitch = SIMD_PI - euler_out.pitch;
euler_out.roll = btAtan2(m_el[2].y() / btCos(euler_out.pitch),
m_el[2].z() / btCos(euler_out.pitch));
euler_out2.roll = btAtan2(m_el[2].y() / btCos(euler_out2.pitch),
m_el[2].z() / btCos(euler_out2.pitch));
euler_out.yaw = btAtan2(m_el[1].x() / btCos(euler_out.pitch),
m_el[0].x() / btCos(euler_out.pitch));
euler_out2.yaw = btAtan2(m_el[1].x() / btCos(euler_out2.pitch),
m_el[0].x() / btCos(euler_out2.pitch));
}
if (solution_number == 1)
{
yaw = euler_out.yaw;
pitch = euler_out.pitch;
roll = euler_out.roll;
}
else
{
yaw = euler_out2.yaw;
pitch = euler_out2.pitch;
roll = euler_out2.roll;
}
}
/**@brief Create a scaled copy of the matrix
* @param s Scaling vector The elements of the vector will scale each column */
TinyMatrix3x3 scaled(const TinyVector3& s) const
{
#if (defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE)) || defined(BT_USE_NEON)
return TinyMatrix3x3(m_el[0] * s, m_el[1] * s, m_el[2] * s);
#else
return TinyMatrix3x3(
m_el[0].x() * s.x(), m_el[0].y() * s.y(), m_el[0].z() * s.z(),
m_el[1].x() * s.x(), m_el[1].y() * s.y(), m_el[1].z() * s.z(),
m_el[2].x() * s.x(), m_el[2].y() * s.y(), m_el[2].z() * s.z());
#endif
}
/**@brief Return the determinant of the matrix */
TinyScalar determinant() const;
/**@brief Return the adjoint of the matrix */
TinyMatrix3x3 adjoint() const;
/**@brief Return the matrix with all values non negative */
TinyMatrix3x3 absolute() const;
/// Solve A * x = b, where b is a column vector. This is more efficient
/// than computing the inverse in one-shot cases.
///Solve33 is from Box2d, thanks to Erin Catto,
TinyVector3 solve33(const TinyVector3& b) const
{
TinyVector3 col1 = getColumn(0);
TinyVector3 col2 = getColumn(1);
TinyVector3 col3 = getColumn(2);
TinyScalar det = btDot(col1, btCross(col2, col3));
if (btFabs(det) > SIMD_EPSILON)
{
det = 1.0f / det;
}
TinyVector3 x;
x[0] = det * btDot(b, btCross(col2, col3));
x[1] = det * btDot(col1, btCross(b, col3));
x[2] = det * btDot(col1, btCross(col2, b));
return x;
}
TinyMatrix3x3 transposeTimes(const TinyMatrix3x3& m) const;
TinyMatrix3x3 timesTranspose(const TinyMatrix3x3& m) const;
///extractRotation is from "A robust method to extract the rotational part of deformations"
///See http://dl.acm.org/citation.cfm?doid=2994258.2994269
///decomposes a matrix A in a orthogonal matrix R and a
///symmetric matrix S:
///A = R*S.
///note that R can include both rotation and scaling.
inline void extractRotation(btQuaternion & q, TinyScalar tolerance = 1.0e-9, int maxIter = 100)
{
int iter = 0;
TinyScalar w;
const TinyMatrix3x3& A = *this;
for (iter = 0; iter < maxIter; iter++)
{
TinyMatrix3x3 R(q);
TinyVector3 omega = (R.getColumn(0).cross(A.getColumn(0)) + R.getColumn(1).cross(A.getColumn(1)) + R.getColumn(2).cross(A.getColumn(2))) * (TinyScalar(1.0) / btFabs(R.getColumn(0).dot(A.getColumn(0)) + R.getColumn(1).dot(A.getColumn(1)) + R.getColumn(2).dot(A.getColumn(2))) +
tolerance);
w = omega.norm();
if (w < tolerance)
break;
q = btQuaternion(TinyVector3((TinyScalar(1.0) / w) * omega), w) *
q;
q.normalize();
}
}
/**@brief diagonalizes this matrix by the Jacobi method.
* @param rot stores the rotation from the coordinate system in which the matrix is diagonal to the original
* coordinate system, i.e., old_this = rot * new_this * rot^T.
* @param threshold See iteration
* @param iteration The iteration stops when all off-diagonal elements are less than the threshold multiplied
* by the sum of the absolute values of the diagonal, or when maxSteps have been executed.
*
* Note that this matrix is assumed to be symmetric.
*/
void diagonalize(TinyMatrix3x3 & rot, TinyScalar threshold, int maxSteps)
{
rot.setIdentity();
for (int step = maxSteps; step > 0; step--)
{
// find off-diagonal element [p][q] with largest magnitude
int p = 0;
int q = 1;
int r = 2;
TinyScalar max = btFabs(m_el[0][1]);
TinyScalar v = btFabs(m_el[0][2]);
if (v > max)
{
q = 2;
r = 1;
max = v;
}
v = btFabs(m_el[1][2]);
if (v > max)
{
p = 1;
q = 2;
r = 0;
max = v;
}
TinyScalar t = threshold * (btFabs(m_el[0][0]) + btFabs(m_el[1][1]) + btFabs(m_el[2][2]));
if (max <= t)
{
if (max <= SIMD_EPSILON * t)
{
return;
}
step = 1;
}
// compute Jacobi rotation J which leads to a zero for element [p][q]
TinyScalar mpq = m_el[p][q];
TinyScalar theta = (m_el[q][q] - m_el[p][p]) / (2 * mpq);
TinyScalar theta2 = theta * theta;
TinyScalar cos;
TinyScalar sin;
if (theta2 * theta2 < TinyScalar(10 / SIMD_EPSILON))
{
t = (theta >= 0) ? 1 / (theta + btSqrt(1 + theta2))
: 1 / (theta - btSqrt(1 + theta2));
cos = 1 / btSqrt(1 + t * t);
sin = cos * t;
}
else
{
// approximation for large theta-value, i.e., a nearly diagonal matrix
t = 1 / (theta * (2 + TinyScalar(0.5) / theta2));
cos = 1 - TinyScalar(0.5) * t * t;
sin = cos * t;
}
// apply rotation to matrix (this = J^T * this * J)
m_el[p][q] = m_el[q][p] = 0;
m_el[p][p] -= t * mpq;
m_el[q][q] += t * mpq;
TinyScalar mrp = m_el[r][p];
TinyScalar mrq = m_el[r][q];
m_el[r][p] = m_el[p][r] = cos * mrp - sin * mrq;
m_el[r][q] = m_el[q][r] = cos * mrq + sin * mrp;
// apply rotation to rot (rot = rot * J)
for (int i = 0; i < 3; i++)
{
TinyVector3& row = rot[i];
mrp = row[p];
mrq = row[q];
row[p] = cos * mrp - sin * mrq;
row[q] = cos * mrq + sin * mrp;
}
}
}
void serialize(struct TinyMatrix3x3Data & dataOut) const;
void serializeFloat(struct TinyMatrix3x3FloatData & dataOut) const;
void deSerialize(const struct TinyMatrix3x3Data& dataIn);
void deSerializeFloat(const struct TinyMatrix3x3FloatData& dataIn);
void deSerializeDouble(const struct TinyMatrix3x3DoubleData& dataIn);
#endif
};
template <typename TinyScalar, typename TinyConstants>
inline TinyVector3<TinyScalar, TinyConstants> operator*(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m,
const TinyVector3<TinyScalar, TinyConstants>& v) {
return TinyVector3<TinyScalar, TinyConstants>(m[0].dot(v), m[1].dot(v),
m[2].dot(v));
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants>
TinyMatrix3x3<TinyScalar, TinyConstants>::transpose() const {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
m_el[0].x(), m_el[1].x(), m_el[2].x(), m_el[0].y(), m_el[1].y(),
m_el[2].y(), m_el[0].z(), m_el[1].z(), m_el[2].z());
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants> TinyVectorCrossMatrix(
const TinyVector3<TinyScalar, TinyConstants>& vector) {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
TinyConstants::zero(), -vector[2], vector[1], vector[2],
TinyConstants::zero(), -vector[0], -vector[1], vector[0],
TinyConstants::zero());
}
template <typename TinyScalar, typename TinyConstants>
inline TinyVector3<TinyScalar, TinyConstants> adj_TinyVectorCrossMatrix(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m) {
return TinyVector3<TinyScalar, TinyConstants>(
m[2].y()-m[1].z(), m[0].z()-m[2].x(), m[1].x()-m[0].y());
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants> operator*(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m, const TinyScalar& k) {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
m[0].x() * k, m[0].y() * k, m[0].z() * k, m[1].x() * k, m[1].y() * k,
m[1].z() * k, m[2].x() * k, m[2].y() * k, m[2].z() * k);
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants> operator+(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m1,
const TinyMatrix3x3<TinyScalar, TinyConstants>& m2) {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2],
m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2],
m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2]);
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants> operator-(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m1,
const TinyMatrix3x3<TinyScalar, TinyConstants>& m2) {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2],
m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2],
m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2]);
}
template <typename TinyScalar, typename TinyConstants>
inline TinyMatrix3x3<TinyScalar, TinyConstants> operator*(
const TinyMatrix3x3<TinyScalar, TinyConstants>& m1,
const TinyMatrix3x3<TinyScalar, TinyConstants>& m2) {
return TinyMatrix3x3<TinyScalar, TinyConstants>(
m2.tdotx(m1[0]), m2.tdoty(m1[0]), m2.tdotz(m1[0]), m2.tdotx(m1[1]),
m2.tdoty(m1[1]), m2.tdotz(m1[1]), m2.tdotx(m1[2]), m2.tdoty(m1[2]),
m2.tdotz(m1[2]));
}
#if 0
inline TinyScalar
TinyMatrix3x3::determinant() const
{
return btTriple((*this)[0], (*this)[1], (*this)[2]);
}
inline TinyMatrix3x3
TinyMatrix3x3::absolute() const
{
#if defined BT_USE_SIMD_VECTOR3 && \
(defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE))
return TinyMatrix3x3(
_mm_and_ps(m_el[0].mVec128, btvAbsfMask),
_mm_and_ps(m_el[1].mVec128, btvAbsfMask),
_mm_and_ps(m_el[2].mVec128, btvAbsfMask));
#elif defined(BT_USE_NEON)
return TinyMatrix3x3(
(float32x4_t)vandq_s32((int32x4_t)m_el[0].mVec128, btv3AbsMask),
(float32x4_t)vandq_s32((int32x4_t)m_el[1].mVec128, btv3AbsMask),
(float32x4_t)vandq_s32((int32x4_t)m_el[2].mVec128, btv3AbsMask));
#else
return TinyMatrix3x3(
btFabs(m_el[0].x()), btFabs(m_el[0].y()), btFabs(m_el[0].z()),
btFabs(m_el[1].x()), btFabs(m_el[1].y()), btFabs(m_el[1].z()),
btFabs(m_el[2].x()), btFabs(m_el[2].y()), btFabs(m_el[2].z()));
#endif