forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmatrix3_src.I
More file actions
1336 lines (1160 loc) · 36.5 KB
/
lmatrix3_src.I
File metadata and controls
1336 lines (1160 loc) · 36.5 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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file lmatrix3_src.I
* @author drose
* @date 1999-01-29
*/
/**
* Defines a row-level index accessor to the matrix.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::Row::
Row(FLOATTYPE *row) : _row(row) {
}
/**
*
*/
INLINE_LINMATH FLOATTYPE FLOATNAME(LMatrix3)::Row::
operator [](int i) const {
nassertr(i >= 0 && i < 3, 0.0);
return _row[i];
}
/**
*
*/
INLINE_LINMATH FLOATTYPE &FLOATNAME(LMatrix3)::Row::
operator [](int i) {
nassertr(i >= 0 && i < 3, _row[0]);
return _row[i];
}
/**
* Returns 3: the number of columns of a LMatrix3.
*/
INLINE_LINMATH int FLOATNAME(LMatrix3)::Row::
size() {
return 3;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::Row::
operator const FLOATNAME(LVecBase3) &() const {
return *(const FLOATNAME(LVecBase3) *)_row;
}
/**
* Defines a row-level constant accessor to the matrix.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::CRow::
CRow(const FLOATTYPE *row) : _row(row) {
}
/**
*
*/
INLINE_LINMATH FLOATTYPE FLOATNAME(LMatrix3)::CRow::
operator [](int i) const {
nassertr(i >= 0 && i < 3, 0.0);
return _row[i];
}
/**
* Returns 3: the number of columns of a LMatrix3.
*/
INLINE_LINMATH int FLOATNAME(LMatrix3)::CRow::
size() {
return 3;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::CRow::
operator const FLOATNAME(LVecBase3) &() const {
return *(const FLOATNAME(LVecBase3) *)_row;
}
/**
* Returns an identity matrix.
*
* This function definition must appear first, since some inline functions
* below take advantage of it.
*/
INLINE_LINMATH const FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
ident_mat() {
return _ident_mat;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator = (FLOATTYPE fill_value) {
fill(fill_value);
return *this;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::
FLOATNAME(LMatrix3)(FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02,
FLOATTYPE e10, FLOATTYPE e11, FLOATTYPE e12,
FLOATTYPE e20, FLOATTYPE e21, FLOATTYPE e22) {
TAU_PROFILE("LMatrix3::LMatrix3(FLOATTYPE, ...)", " ", TAU_USER);
_m(0, 0) = e00;
_m(0, 1) = e01;
_m(0, 2) = e02;
_m(1, 0) = e10;
_m(1, 1) = e11;
_m(1, 2) = e12;
_m(2, 0) = e20;
_m(2, 1) = e21;
_m(2, 2) = e22;
}
/**
* Constructs the matrix from three individual rows.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::
FLOATNAME(LMatrix3)(const FLOATNAME(LVecBase3) &row0,
const FLOATNAME(LVecBase3) &row1,
const FLOATNAME(LVecBase3) &row2) {
TAU_PROFILE("LMatrix3::LMatrix3(const LVecBase3 &, ...)", " ", TAU_USER);
_m(0, 0) = row0._v(0);
_m(0, 1) = row0._v(1);
_m(0, 2) = row0._v(2);
_m(1, 0) = row1._v(0);
_m(1, 1) = row1._v(1);
_m(1, 2) = row1._v(2);
_m(2, 0) = row2._v(0);
_m(2, 1) = row2._v(1);
_m(2, 2) = row2._v(2);
}
/**
*
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set(FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02,
FLOATTYPE e10, FLOATTYPE e11, FLOATTYPE e12,
FLOATTYPE e20, FLOATTYPE e21, FLOATTYPE e22) {
TAU_PROFILE("void LMatrix3::set(FLOATTYPE, ...)", " ", TAU_USER);
_m(0, 0) = e00;
_m(0, 1) = e01;
_m(0, 2) = e02;
_m(1, 0) = e10;
_m(1, 1) = e11;
_m(1, 2) = e12;
_m(2, 0) = e20;
_m(2, 1) = e21;
_m(2, 2) = e22;
}
/**
* Replaces the indicated row of the matrix from a three-component vector.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set_row(int row, const FLOATNAME(LVecBase3) &v) {
#ifdef HAVE_EIGEN
_m.row(row) = v._v;
#else
(*this)(row, 0) = v._v(0);
(*this)(row, 1) = v._v(1);
(*this)(row, 2) = v._v(2);
#endif // HAVE_EIGEN
}
/**
* Replaces the indicated column of the matrix from a three-component vector.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set_col(int col, const FLOATNAME(LVecBase3) &v) {
#ifdef HAVE_EIGEN
_m.col(col) = v._v;
#else
(*this)(0, col) = v._v(0);
(*this)(1, col) = v._v(1);
(*this)(2, col) = v._v(2);
#endif // HAVE_EIGEN
}
/**
* Replaces the indicated row of the matrix from a two-component vector,
* ignoring the last column.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set_row(int row, const FLOATNAME(LVecBase2) &v) {
#ifdef HAVE_EIGEN
_m.block<1, 2>(row, 0) = v._v;
#else
(*this)(row, 0) = v._v(0);
(*this)(row, 1) = v._v(1);
#endif // HAVE_EIGEN
}
/**
* Replaces the indicated column of the matrix from a two-component vector,
* ignoring the last row.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set_col(int col, const FLOATNAME(LVecBase2) &v) {
#ifdef HAVE_EIGEN
_m.block<2, 1>(0, col) = v._v;
#else
(*this)(0, col) = v._v(0);
(*this)(1, col) = v._v(1);
#endif // HAVE_EIGEN
}
/**
* Returns the indicated row of the matrix as a three-component vector.
*/
INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
get_row(int row) const {
#ifdef HAVE_EIGEN
return FLOATNAME(LVecBase3)(_m.row(row));
#else
return FLOATNAME(LVecBase3)((*this)(row, 0), (*this)(row, 1), (*this)(row, 2));
#endif // HAVE_EIGEN
}
/**
* Stores the indicated row of the matrix as a three-component vector.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
get_row(FLOATNAME(LVecBase3) &result_vec,int row) const {
#ifdef HAVE_EIGEN
result_vec._v = _m.row(row);
#else
result_vec._v(0) = (*this)(row, 0);
result_vec._v(1) = (*this)(row, 1);
result_vec._v(2) = (*this)(row, 2);
#endif // HAVE_EIGEN
}
/**
* Returns the indicated column of the matrix as a three-component vector.
*/
INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
get_col(int col) const {
#ifdef HAVE_EIGEN
return FLOATNAME(LVecBase3)(_m.col(col));
#else
return FLOATNAME(LVecBase3)((*this)(0, col), (*this)(1, col), (*this)(2, col));
#endif // HAVE_EIGEN
}
/**
* Returns the indicated row of the matrix as a two-component vector, ignoring
* the last column.
*/
INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LMatrix3)::
get_row2(int row) const {
return FLOATNAME(LVecBase2)((*this)(row, 0), (*this)(row, 1));
}
/**
* Returns the indicated column of the matrix as a two-component vector,
* ignoring the last row.
*/
INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LMatrix3)::
get_col2(int col) const {
return FLOATNAME(LVecBase2)((*this)(0, col), (*this)(1, col));
}
/**
*
*/
INLINE_LINMATH FLOATTYPE &FLOATNAME(LMatrix3)::
operator () (int row, int col) {
nassertr(row >= 0 && row < 3 && col >= 0 && col < 3, _m(0, 0));
return _m(row, col);
}
/**
*
*/
INLINE_LINMATH FLOATTYPE FLOATNAME(LMatrix3)::
operator () (int row, int col) const {
nassertr(row >= 0 && row < 3 && col >= 0 && col < 3, 0.0);
return _m(row, col);
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::CRow FLOATNAME(LMatrix3)::
operator [](int i) const {
nassertr(i >= 0 && i < 3, CRow(&_m(0, 0)));
return CRow(&_m(i, 0));
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::Row FLOATNAME(LMatrix3)::
operator [](int i) {
nassertr(i >= 0 && i < 3, Row(&_m(0, 0)));
return Row(&_m(i, 0));
}
/**
* Returns 3: the number of rows of a LMatrix3.
*/
INLINE_LINMATH int FLOATNAME(LMatrix3)::
size() {
return 3;
}
/**
* Returns true if any component of the matrix is not-a-number, false
* otherwise.
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
is_nan() const {
TAU_PROFILE("bool LMatrix3::is_nan()", " ", TAU_USER);
return
cnan(_m(0, 0)) || cnan(_m(0, 1)) || cnan(_m(0, 2)) ||
cnan(_m(1, 0)) || cnan(_m(1, 1)) || cnan(_m(1, 2)) ||
cnan(_m(2, 0)) || cnan(_m(2, 1)) || cnan(_m(2, 2));
}
/**
* Returns true if this is (close enough to) the identity matrix, false
* otherwise.
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
is_identity() const {
return almost_equal(ident_mat(), NEARLY_ZERO(FLOATTYPE));
}
/**
* Returns a particular element of the matrix.
*/
INLINE_LINMATH FLOATTYPE FLOATNAME(LMatrix3)::
get_cell(int row, int col) const {
nassertr(row >= 0 && row < 3 && col >= 0 && col < 3, 0.0);
return _m(row, col);
}
/**
* Changes a particular element of the matrix.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
set_cell(int row, int col, FLOATTYPE value) {
nassertv(row >= 0 && row < 3 && col >= 0 && col < 3);
_m(row, col) = value;
}
/**
* Returns the address of the first of the nine data elements in the matrix.
* The remaining elements occupy the next eight positions in row-major order.
*/
INLINE_LINMATH const FLOATTYPE *FLOATNAME(LMatrix3)::
get_data() const {
return &_m(0, 0);
}
/**
* Returns the number of elements in the matrix, nine.
*/
INLINE_LINMATH int FLOATNAME(LMatrix3)::
get_num_components() const {
return 9;
}
/**
* Returns an iterator that may be used to traverse the elements of the
* matrix, STL-style.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::iterator FLOATNAME(LMatrix3)::
begin() {
return &_m(0, 0);
}
/**
* Returns an iterator that may be used to traverse the elements of the
* matrix, STL-style.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::iterator FLOATNAME(LMatrix3)::
end() {
return begin() + num_components;
}
/**
* Returns an iterator that may be used to traverse the elements of the
* matrix, STL-style.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::const_iterator FLOATNAME(LMatrix3)::
begin() const {
return &_m(0, 0);
}
/**
* Returns an iterator that may be used to traverse the elements of the
* matrix, STL-style.
*/
INLINE_LINMATH FLOATNAME(LMatrix3)::const_iterator FLOATNAME(LMatrix3)::
end() const {
return begin() + num_components;
}
/**
* This performs a lexicographical comparison. It's of questionable
* mathematical meaning, but sometimes has a practical purpose for sorting
* unique vectors, especially in an STL container. Also see compare_to().
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
operator < (const FLOATNAME(LMatrix3) &other) const {
return compare_to(other) < 0;
}
/**
*
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
operator == (const FLOATNAME(LMatrix3) &other) const {
return compare_to(other) == 0;
}
/**
*
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
operator != (const FLOATNAME(LMatrix3) &other) const {
return compare_to(other) != 0;
}
/**
* This flavor of compare_to uses a default threshold value based on the
* numeric type.
*/
INLINE_LINMATH int FLOATNAME(LMatrix3)::
compare_to(const FLOATNAME(LMatrix3) &other) const {
return compare_to(other, NEARLY_ZERO(FLOATTYPE));
}
/**
* Returns a suitable hash for phash_map.
*/
INLINE_LINMATH size_t FLOATNAME(LMatrix3)::
get_hash() const {
return add_hash(0);
}
/**
* Returns a suitable hash for phash_map.
*/
INLINE_LINMATH size_t FLOATNAME(LMatrix3)::
get_hash(FLOATTYPE threshold) const {
return add_hash(0, threshold);
}
/**
* Adds the vector into the running hash.
*/
INLINE_LINMATH size_t FLOATNAME(LMatrix3)::
add_hash(size_t hash) const {
return add_hash(hash, NEARLY_ZERO(FLOATTYPE));
}
/**
* Adds the vector into the running hash.
*/
INLINE_LINMATH size_t FLOATNAME(LMatrix3)::
add_hash(size_t hash, FLOATTYPE threshold) const {
TAU_PROFILE("size_t LMatrix3::add_hash(size_t, FLOATTYPE)", " ", TAU_USER);
float_hash fhasher(threshold);
hash = fhasher.add_hash(hash, _m(0, 0));
hash = fhasher.add_hash(hash, _m(0, 1));
hash = fhasher.add_hash(hash, _m(0, 2));
hash = fhasher.add_hash(hash, _m(1, 0));
hash = fhasher.add_hash(hash, _m(1, 1));
hash = fhasher.add_hash(hash, _m(1, 2));
hash = fhasher.add_hash(hash, _m(2, 0));
hash = fhasher.add_hash(hash, _m(2, 1));
hash = fhasher.add_hash(hash, _m(2, 2));
return hash;
}
#define VECTOR3_MATRIX3_PRODUCT(v_res, v, mat) \
v_res._v(0) = v._v(0)*mat._m(0, 0) + v._v(1)*mat._m(1, 0) + v._v(2)*mat._m(2, 0); \
v_res._v(1) = v._v(0)*mat._m(0, 1) + v._v(1)*mat._m(1, 1) + v._v(2)*mat._m(2, 1); \
v_res._v(2) = v._v(0)*mat._m(0, 2) + v._v(1)*mat._m(1, 2) + v._v(2)*mat._m(2, 2);
/**
* 3-component vector or point times matrix.
*/
INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
xform(const FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("LVecBase3 LMatrix3::xform(const LVecBase3 &)", " ", TAU_USER);
FLOATNAME(LVecBase3) v_res;
#ifdef HAVE_EIGEN
v_res._v.noalias() = v._v * _m;
#else
VECTOR3_MATRIX3_PRODUCT(v_res, v,(*this));
#endif // HAVE_EIGEN
return v_res;
}
#undef VECTOR3_MATRIX3_PRODUCT
/**
* The matrix transforms a 2-component point (including translation component)
* and returns the result. This assumes the matrix is an affine transform.
*/
INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LMatrix3)::
xform_point(const FLOATNAME(LVecBase2) &v) const {
TAU_PROFILE("LVecBase3 LMatrix3::xform_point(const LVecBase3 &)", " ", TAU_USER);
FLOATNAME(LVecBase2) v_res;
// v._v(2) == 1.0f for this case
#ifdef HAVE_EIGEN
v_res._v.noalias() = v._v * _m.block<2, 2>(0, 0) + _m.block<1, 2>(2, 0);
#else
v_res._v(0) = v._v(0)*_m(0, 0) + v._v(1)*_m(1, 0) + _m(2, 0);
v_res._v(1) = v._v(0)*_m(0, 1) + v._v(1)*_m(1, 1) + _m(2, 1);
#endif // HAVE_EIGEN
return v_res;
}
/**
* The matrix transforms a 2-component vector (without translation component)
* and returns the result. This assumes the matrix is an affine transform.
*/
INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LMatrix3)::
xform_vec(const FLOATNAME(LVecBase2) &v) const {
TAU_PROFILE("LVecBase3 LMatrix3::xform_vec(const LVecBase3 &)", " ", TAU_USER);
FLOATNAME(LVecBase2) v_res;
// v._v(2) == 0.0f for this case
#ifdef HAVE_EIGEN
v_res._v.noalias() = v._v * _m.block<2, 2>(0, 0);
#else
v_res._v(0) = v._v(0)*_m(0, 0) + v._v(1)*_m(1, 0);
v_res._v(1) = v._v(0)*_m(0, 1) + v._v(1)*_m(1, 1);
#endif // HAVE_EIGEN
return v_res;
}
/**
* The matrix transforms a 3-component vector and returns the result. This
* assumes the matrix is an orthonormal transform.
*
* In practice, this is the same computation as xform().
*/
INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
xform_vec(const FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("LVecBase3 LMatrix3::xform_vec(const LVecBase3 &)", " ", TAU_USER);
return xform(v);
}
/**
* The matrix transforms a 3-component vector (without translation component)
* and returns the result, as a fully general operation.
*/
INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LMatrix3)::
xform_vec_general(const FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("LVecBase3 LMatrix3::xform_vec_general(const LVecBase3 &)", " ", TAU_USER);
#ifdef HAVE_EIGEN
return FLOATNAME(LVecBase3)(v._v * _m.inverse().transpose());
#else
FLOATNAME(LMatrix3) i;
i.invert_transpose_from(*this);
return i.xform(v);
#endif // HAVE_EIGEN
}
/**
* 3-component vector or point times matrix.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
xform_in_place(FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("void LMatrix3::xform_in_place(LVecBase3 &)", " ", TAU_USER);
#ifdef HAVE_EIGEN
v._v = v._v * _m;
#else
v = xform(v);
#endif // HAVE_EIGEN
}
/**
* The matrix transforms a 2-component point (including translation
* component). This assumes the matrix is an affine transform.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
xform_point_in_place(FLOATNAME(LVecBase2) &v) const {
TAU_PROFILE("void LMatrix3::xform_point_in_place(LVecBase3 &)", " ", TAU_USER);
// v._v(2) == 1.0f for this case
#ifdef HAVE_EIGEN
v._v = v._v * _m.block<2, 2>(0, 0) + _m.block<1, 2>(2, 0);
#else
v = xform_point(v);
#endif // HAVE_EIGEN
}
/**
* The matrix transforms a 2-component vector (without translation component).
* This assumes the matrix is an affine transform.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
xform_vec_in_place(FLOATNAME(LVecBase2) &v) const {
TAU_PROFILE("void LMatrix3::xform_vec_in_place(LVecBase3 &)", " ", TAU_USER);
// v._v(2) == 0.0f for this case
#ifdef HAVE_EIGEN
v._v = v._v * _m.block<2, 2>(0, 0);
#else
v = xform_vec(v);
#endif // HAVE_EIGEN
}
/**
* The matrix transforms a 3-component vector. This assumes the matrix is an
* orthonormal transform.
*
* In practice, this is the same computation as xform().
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
xform_vec_in_place(FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("void LMatrix3::xform_vec_in_place(LVecBase3 &)", " ", TAU_USER);
xform_in_place(v);
}
/**
* The matrix transforms a 3-component vector (without translation component),
* as a fully general operation.
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
xform_vec_general_in_place(FLOATNAME(LVecBase3) &v) const {
TAU_PROFILE("void LMatrix3::xform_vec_general_in_place(LVecBase3 &)", " ", TAU_USER);
#ifdef HAVE_EIGEN
v._v = v._v * _m.inverse().transpose();
#else
v = xform_vec(v);
#endif // HAVE_EIGEN
}
#define MATRIX3_PRODUCT(res, a, b) \
res._m(0, 0) = a._m(0, 0)*b._m(0, 0) + a._m(0, 1)*b._m(1, 0) + a._m(0, 2)*b._m(2, 0); \
res._m(0, 1) = a._m(0, 0)*b._m(0, 1) + a._m(0, 1)*b._m(1, 1) + a._m(0, 2)*b._m(2, 1); \
res._m(0, 2) = a._m(0, 0)*b._m(0, 2) + a._m(0, 1)*b._m(1, 2) + a._m(0, 2)*b._m(2, 2); \
res._m(1, 0) = a._m(1, 0)*b._m(0, 0) + a._m(1, 1)*b._m(1, 0) + a._m(1, 2)*b._m(2, 0); \
res._m(1, 1) = a._m(1, 0)*b._m(0, 1) + a._m(1, 1)*b._m(1, 1) + a._m(1, 2)*b._m(2, 1); \
res._m(1, 2) = a._m(1, 0)*b._m(0, 2) + a._m(1, 1)*b._m(1, 2) + a._m(1, 2)*b._m(2, 2); \
res._m(2, 0) = a._m(2, 0)*b._m(0, 0) + a._m(2, 1)*b._m(1, 0) + a._m(2, 2)*b._m(2, 0); \
res._m(2, 1) = a._m(2, 0)*b._m(0, 1) + a._m(2, 1)*b._m(1, 1) + a._m(2, 2)*b._m(2, 1); \
res._m(2, 2) = a._m(2, 0)*b._m(0, 2) + a._m(2, 1)*b._m(1, 2) + a._m(2, 2)*b._m(2, 2);
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3) FLOATNAME(LMatrix3)::
operator * (const FLOATNAME(LMatrix3) &other) const {
TAU_PROFILE("LMatrix3 LMatrix3::operator *(const LMatrix3 &)", " ", TAU_USER);
FLOATNAME(LMatrix3) t;
t.multiply(*this, other);
return t;
}
// this = other1 * other2
INLINE_LINMATH void FLOATNAME(LMatrix3)::
multiply(const FLOATNAME(LMatrix3) &other1, const FLOATNAME(LMatrix3) &other2) {
TAU_PROFILE("LMatrix3 multiply(const LMatrix3 &, const LMatrix3 &)", " ", TAU_USER);
// faster than operator * since it writes result in place, avoiding extra
// copying this will fail if you try to mat.multiply(mat,other_mat)
nassertv((&other1 != this) && (&other2 != this));
MATRIX3_PRODUCT((*this), other1, other2);
}
#undef MATRIX3_PRODUCT
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3) FLOATNAME(LMatrix3)::
operator * (FLOATTYPE scalar) const {
TAU_PROFILE("LMatrix3 operator *(const LMatrix3 &, FLOATTYPE)", " ", TAU_USER);
FLOATNAME(LMatrix3) t;
t._m(0, 0) = _m(0, 0) * scalar;
t._m(0, 1) = _m(0, 1) * scalar;
t._m(0, 2) = _m(0, 2) * scalar;
t._m(1, 0) = _m(1, 0) * scalar;
t._m(1, 1) = _m(1, 1) * scalar;
t._m(1, 2) = _m(1, 2) * scalar;
t._m(2, 0) = _m(2, 0) * scalar;
t._m(2, 1) = _m(2, 1) * scalar;
t._m(2, 2) = _m(2, 2) * scalar;
return t;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3) FLOATNAME(LMatrix3)::
operator / (FLOATTYPE scalar) const {
FLOATTYPE recip_scalar = 1.0f/scalar;
return (*this) * recip_scalar;
}
/**
* Performs a memberwise addition between two matrices.
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator += (const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("LMatrix3 LMatrix3::operator +=(const LMatrix3 &)", " ", TAU_USER);
_m(0, 0) += other._m(0, 0);
_m(0, 1) += other._m(0, 1);
_m(0, 2) += other._m(0, 2);
_m(1, 0) += other._m(1, 0);
_m(1, 1) += other._m(1, 1);
_m(1, 2) += other._m(1, 2);
_m(2, 0) += other._m(2, 0);
_m(2, 1) += other._m(2, 1);
_m(2, 2) += other._m(2, 2);
return *this;
}
/**
* Performs a memberwise subtraction between two matrices.
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator -= (const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("LMatrix3 LMatrix3::operator -=(const LMatrix3 &)", " ", TAU_USER);
_m(0, 0) -= other._m(0, 0);
_m(0, 1) -= other._m(0, 1);
_m(0, 2) -= other._m(0, 2);
_m(1, 0) -= other._m(1, 0);
_m(1, 1) -= other._m(1, 1);
_m(1, 2) -= other._m(1, 2);
_m(2, 0) -= other._m(2, 0);
_m(2, 1) -= other._m(2, 1);
_m(2, 2) -= other._m(2, 2);
return *this;
}
/**
*
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator *= (const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("LMatrix3 LMatrix3::operator *=(const LMatrix3 &)", " ", TAU_USER);
FLOATNAME(LMatrix3) temp = *this;
multiply(temp, other);
return *this;
}
/**
* Performs a memberwise scale.
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator *= (FLOATTYPE scalar) {
TAU_PROFILE("LMatrix3 LMatrix3::operator *=(FLOATTYPE)", " ", TAU_USER);
_m(0, 0) *= scalar;
_m(0, 1) *= scalar;
_m(0, 2) *= scalar;
_m(1, 0) *= scalar;
_m(1, 1) *= scalar;
_m(1, 2) *= scalar;
_m(2, 0) *= scalar;
_m(2, 1) *= scalar;
_m(2, 2) *= scalar;
return *this;
}
/**
* Performs a memberwise scale.
*/
INLINE_LINMATH FLOATNAME(LMatrix3) &FLOATNAME(LMatrix3)::
operator /= (FLOATTYPE scalar) {
TAU_PROFILE("LMatrix3 LMatrix3::operator /=(FLOATTYPE)", " ", TAU_USER);
FLOATTYPE recip_scalar = 1.0f/scalar;
_m(0, 0) *= recip_scalar;
_m(0, 1) *= recip_scalar;
_m(0, 2) *= recip_scalar;
_m(1, 0) *= recip_scalar;
_m(1, 1) *= recip_scalar;
_m(1, 2) *= recip_scalar;
_m(2, 0) *= recip_scalar;
_m(2, 1) *= recip_scalar;
_m(2, 2) *= recip_scalar;
return *this;
}
/**
*
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
componentwise_mult(const FLOATNAME(LMatrix3) &other) {
#ifdef HAVE_EIGEN
_m = _m.cwiseProduct(other._m);
#else
_m(0, 0) *= other._m(0, 0);
_m(0, 1) *= other._m(0, 1);
_m(0, 2) *= other._m(0, 2);
_m(1, 0) *= other._m(1, 0);
_m(1, 1) *= other._m(1, 1);
_m(1, 2) *= other._m(1, 2);
_m(2, 0) *= other._m(2, 0);
_m(2, 1) *= other._m(2, 1);
_m(2, 2) *= other._m(2, 2);
#endif // HAVE_EIGEN
}
/**
*
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
transpose_from(const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("LMatrix3 LMatrix3::transpose_from(const LMatrix3 &other)", " ", TAU_USER);
_m(0, 0) = other._m(0, 0);
_m(0, 1) = other._m(1, 0);
_m(0, 2) = other._m(2, 0);
_m(1, 0) = other._m(0, 1);
_m(1, 1) = other._m(1, 1);
_m(1, 2) = other._m(2, 1);
_m(2, 0) = other._m(0, 2);
_m(2, 1) = other._m(1, 2);
_m(2, 2) = other._m(2, 2);
}
/**
*
*/
INLINE_LINMATH void FLOATNAME(LMatrix3)::
transpose_in_place() {
TAU_PROFILE("void LMatrix3::transpose_in_place()", " ", TAU_USER);
std::swap(_m(0, 1), _m(1, 0));
std::swap(_m(0, 2), _m(2, 0));
std::swap(_m(1, 2), _m(2, 1));
}
// Matrix inversion code from Numerical Recipes in C.
// don't trust compilers to inline these
#define DET2(E00,E01,E10,E11) ((E00)*(E11) - (E10)*(E01))
#define MATRIX3_DETERMINANT(mat) \
( (mat)(0, 0) * DET2((mat)(1, 1),(mat)(1, 2),(mat)(2, 1),(mat)(2, 2)) \
-(mat)(0, 1) * DET2((mat)(1, 0),(mat)(1, 2),(mat)(2, 0),(mat)(2, 2)) \
+(mat)(0, 2) * DET2((mat)(1, 0),(mat)(1, 1),(mat)(2, 0),(mat)(2, 1)))
/**
* Returns the determinant of the matrix.
*/
INLINE_LINMATH FLOATTYPE FLOATNAME(LMatrix3)::
determinant() const {
TAU_PROFILE("FLOATTYPE LMatrix3::determinant()", " ", TAU_USER);
#ifdef HAVE_EIGEN
return _m.determinant();
#else
return MATRIX3_DETERMINANT(_m);
#endif // HAVE_EIGEN
}
/**
* Computes the inverse of the other matrix, and stores the result in this
* matrix. This is a fully general operation and makes no assumptions about
* the type of transform represented by the matrix.
*
* The other matrix must be a different object than this matrix. However, if
* you need to invert a matrix in place, see invert_in_place.
*
* The return value is true if the matrix was successfully inverted, false if
* there was a singularity.
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
invert_from(const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("bool LMatrix3::invert_from(const LMatrix3 &)", " ", TAU_USER);
// We throw the value out only if it's smaller than our "small" threshold
// squared. This helps reduce overly-sensitive rejections.
#ifdef HAVE_EIGEN
bool invertible;
other._m.computeInverseWithCheck(_m, invertible,
NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE));
if (!invertible) {
#ifdef NOTIFY_DEBUG
linmath_cat.warning() << "Tried to invert singular LMatrix3.\n";
#endif
(*this) = ident_mat();
nassertr(!no_singular_invert, false);
}
return invertible;
#else // HAVE_EIGEN
FLOATTYPE other_det = MATRIX3_DETERMINANT(other._m);
if (IS_THRESHOLD_ZERO(other_det, (NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE)))) {
#ifdef NOTIFY_DEBUG
linmath_cat.warning() << "Tried to invert singular LMatrix3.\n";
#endif
(*this) = ident_mat();
nassertr(!no_singular_invert, false);
return false;
}
other_det = 1.0f / other_det;
_m(0, 0) = other_det * DET2(other._m(1, 1), other._m(1, 2), other._m(2, 1), other._m(2, 2));
_m(1, 0) = -other_det * DET2(other._m(1, 0), other._m(1, 2), other._m(2, 0), other._m(2, 2));
_m(2, 0) = other_det * DET2(other._m(1, 0), other._m(1, 1), other._m(2, 0), other._m(2, 1));
_m(0, 1) = -other_det * DET2(other._m(0, 1), other._m(0, 2), other._m(2, 1), other._m(2, 2));
_m(1, 1) = other_det * DET2(other._m(0, 0), other._m(0, 2), other._m(2, 0), other._m(2, 2));
_m(2, 1) = -other_det * DET2(other._m(0, 0), other._m(0, 1), other._m(2, 0), other._m(2, 1));
_m(0, 2) = other_det * DET2(other._m(0, 1), other._m(0, 2), other._m(1, 1), other._m(1, 2));
_m(1, 2) = -other_det * DET2(other._m(0, 0), other._m(0, 2), other._m(1, 0), other._m(1, 2));
_m(2, 2) = other_det * DET2(other._m(0, 0), other._m(0, 1), other._m(1, 0), other._m(1, 1));
return true;
#endif // HAVE_EIGEN
}
/**
* Inverts the current matrix. Returns true if the inverse is successful,
* false if the matrix was singular.
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
invert_in_place() {
TAU_PROFILE("bool LMatrix3::invert_in_place()", " ", TAU_USER);
FLOATNAME(LMatrix3) temp = (*this);
return invert_from(temp);
}
/**
* Simultaneously computes the inverse of the indicated matrix, and then the
* transpose of that inverse.
*/
INLINE_LINMATH bool FLOATNAME(LMatrix3)::
invert_transpose_from(const FLOATNAME(LMatrix3) &other) {
TAU_PROFILE("bool LMatrix3::invert_transpose_from(const LMatrix3 &)", " ", TAU_USER);
#ifdef HAVE_EIGEN
bool invertible;
EMatrix3 temp;
other._m.computeInverseWithCheck(temp, invertible,
NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE));
if (!invertible) {
#ifdef NOTIFY_DEBUG
linmath_cat.warning() << "Tried to invert singular LMatrix3.\n";
#endif
(*this) = ident_mat();
nassertr(!no_singular_invert, false);
}
_m = temp.transpose();