-
Notifications
You must be signed in to change notification settings - Fork 65
/
Matrix4.hh
740 lines (650 loc) · 24.8 KB
/
Matrix4.hh
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
/*
* Copyright (C) 2012-2014 Open Source Robotics Foundation
*
* 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 _IGNITION_MATRIX4_HH_
#define _IGNITION_MATRIX4_HH_
#include <ignition/math/Helpers.hh>
#include <ignition/math/AffineException.hh>
#include <ignition/math/Matrix3.hh>
#include <ignition/math/Vector3.hh>
#include <ignition/math/Pose3.hh>
namespace ignition
{
namespace math
{
/// \class Matrix4 Matrix4.hh ignition/math/Matrix4.hh
/// \brief A 4x4 matrix class
template<typename T>
class Matrix4
{
/// \brief Identity matrix
public: static const Matrix4<T> Identity;
/// \brief Zero matrix
public: static const Matrix4<T> Zero;
/// \brief Constructor
public: Matrix4()
{
memset(this->data, 0, sizeof(this->data[0][0])*16);
}
/// \brief Copy constructor
/// \param _m Matrix to copy
public: Matrix4(const Matrix4<T> &_m)
{
memcpy(this->data, _m.data, sizeof(this->data[0][0])*16);
}
/// \brief Constructor
/// \param[in] _v00 Row 0, Col 0 value
/// \param[in] _v01 Row 0, Col 1 value
/// \param[in] _v02 Row 0, Col 2 value
/// \param[in] _v03 Row 0, Col 3 value
/// \param[in] _v10 Row 1, Col 0 value
/// \param[in] _v11 Row 1, Col 1 value
/// \param[in] _v12 Row 1, Col 2 value
/// \param[in] _v13 Row 1, Col 3 value
/// \param[in] _v20 Row 2, Col 0 value
/// \param[in] _v21 Row 2, Col 1 value
/// \param[in] _v22 Row 2, Col 2 value
/// \param[in] _v23 Row 2, Col 3 value
/// \param[in] _v30 Row 3, Col 0 value
/// \param[in] _v31 Row 3, Col 1 value
/// \param[in] _v32 Row 3, Col 2 value
/// \param[in] _v33 Row 3, Col 3 value
public: Matrix4(T _v00, T _v01, T _v02, T _v03,
T _v10, T _v11, T _v12, T _v13,
T _v20, T _v21, T _v22, T _v23,
T _v30, T _v31, T _v32, T _v33)
{
this->Set(_v00, _v01, _v02, _v03,
_v10, _v11, _v12, _v13,
_v20, _v21, _v22, _v23,
_v30, _v31, _v32, _v33);
}
/// \brief Construct Matrix4 from a quaternion.
/// \param[in] _q Quaternion.
public: Matrix4(const Quaternion<T> &_q)
{
Quaternion<T> qt = _q;
qt.Normalize();
this->Set(1 - 2*qt.Y()*qt.Y() - 2 *qt.Z()*qt.Z(),
2 * qt.X()*qt.Y() - 2*qt.Z()*qt.W(),
2 * qt.X() * qt.Z() + 2 * qt.Y() * qt.W(),
0,
2 * qt.X() * qt.Y() + 2 * qt.Z() * qt.W(),
1 - 2*qt.X()*qt.X() - 2 * qt.Z()*qt.Z(),
2 * qt.Y() * qt.Z() - 2 * qt.X() * qt.W(),
0,
2 * qt.X() * qt.Z() - 2 * qt.Y() * qt.W(),
2 * qt.Y() * qt.Z() + 2 * qt.X() * qt.W(),
1 - 2 * qt.X()*qt.X() - 2 * qt.Y()*qt.Y(),
0,
0, 0, 0, 1);
}
/// \brief Destructor
public: virtual ~Matrix4() {}
/// \brief Change the values
/// \param[in] _v00 Row 0, Col 0 value
/// \param[in] _v01 Row 0, Col 1 value
/// \param[in] _v02 Row 0, Col 2 value
/// \param[in] _v03 Row 0, Col 3 value
/// \param[in] _v10 Row 1, Col 0 value
/// \param[in] _v11 Row 1, Col 1 value
/// \param[in] _v12 Row 1, Col 2 value
/// \param[in] _v13 Row 1, Col 3 value
/// \param[in] _v20 Row 2, Col 0 value
/// \param[in] _v21 Row 2, Col 1 value
/// \param[in] _v22 Row 2, Col 2 value
/// \param[in] _v23 Row 2, Col 3 value
/// \param[in] _v30 Row 3, Col 0 value
/// \param[in] _v31 Row 3, Col 1 value
/// \param[in] _v32 Row 3, Col 2 value
/// \param[in] _v33 Row 3, Col 3 value
public: void Set(
T _v00, T _v01, T _v02, T _v03,
T _v10, T _v11, T _v12, T _v13,
T _v20, T _v21, T _v22, T _v23,
T _v30, T _v31, T _v32, T _v33)
{
this->data[0][0] = _v00;
this->data[0][1] = _v01;
this->data[0][2] = _v02;
this->data[0][3] = _v03;
this->data[1][0] = _v10;
this->data[1][1] = _v11;
this->data[1][2] = _v12;
this->data[1][3] = _v13;
this->data[2][0] = _v20;
this->data[2][1] = _v21;
this->data[2][2] = _v22;
this->data[2][3] = _v23;
this->data[3][0] = _v30;
this->data[3][1] = _v31;
this->data[3][2] = _v32;
this->data[3][3] = _v33;
}
/// \brief Set the upper-left 3x3 matrix from an axis and angle
/// \param[in] _axis the axis
/// \param[in] _angle ccw rotation around the axis in radians
public: void Axis(const Vector3<T> &_axis, T _angle)
{
T c = cos(_angle);
T s = sin(_angle);
T C = 1-c;
this->data[0][0] = _axis.X()*_axis.X()*C + c;
this->data[0][1] = _axis.X()*_axis.Y()*C - _axis.Z()*s;
this->data[0][2] = _axis.X()*_axis.Z()*C + _axis.Y()*s;
this->data[1][0] = _axis.Y()*_axis.X()*C + _axis.Z()*s;
this->data[1][1] = _axis.Y()*_axis.Y()*C + c;
this->data[1][2] = _axis.Y()*_axis.Z()*C - _axis.X()*s;
this->data[2][0] = _axis.Z()*_axis.X()*C - _axis.Y()*s;
this->data[2][1] = _axis.Z()*_axis.Y()*C + _axis.X()*s;
this->data[2][2] = _axis.Z()*_axis.Z()*C + c;
}
/// \brief Set the translational values [ (0, 3) (1, 3) (2, 3) ]
/// \param[in] _t Values to set
public: void Translate(const Vector3<T> &_t)
{
this->data[0][3] = _t.X();
this->data[1][3] = _t.Y();
this->data[2][3] = _t.Z();
}
/// \brief Set the translational values [ (0, 3) (1, 3) (2, 3) ]
/// \param[in] _x X translation value.
/// \param[in] _y Y translation value.
/// \param[in] _z Z translation value.
public: void Translate(T _x, T _y, T _z)
{
this->data[0][3] = _x;
this->data[1][3] = _y;
this->data[2][3] = _z;
}
/// \brief Get the translational values as a Vector3
/// \return x,y,z translation values
public: Vector3<T> Translation() const
{
return Vector3<T>(this->data[0][3], this->data[1][3], this->data[2][3]);
}
/// \brief Get the scale values as a Vector3<T>
/// \return x,y,z scale values
public: Vector3<T> Scale() const
{
return Vector3<T>(this->data[0][0], this->data[1][1], this->data[2][2]);
}
/// \brief Get the rotation as a quaternion
/// \return the rotation
public: Quaternion<T> Rotation() const
{
Quaternion<T> q;
/// algorithm from Ogre::Quaternion<T> source, which in turn is based on
/// Ken Shoemake's article "Quaternion<T> Calculus and Fast Animation".
T trace = this->data[0][0] + this->data[1][1] + this->data[2][2];
T root;
if (trace > 0)
{
root = sqrt(trace + 1.0);
q.W(root / 2.0);
root = 1.0 / (2.0 * root);
q.X((this->data[2][1] - this->data[1][2]) * root);
q.Y((this->data[0][2] - this->data[2][0]) * root);
q.Z((this->data[1][0] - this->data[0][1]) * root);
}
else
{
static unsigned int s_iNext[3] = {1, 2, 0};
unsigned int i = 0;
if (this->data[1][1] > this->data[0][0])
i = 1;
if (this->data[2][2] > this->data[i][i])
i = 2;
unsigned int j = s_iNext[i];
unsigned int k = s_iNext[j];
root = sqrt(this->data[i][i] - this->data[j][j] -
this->data[k][k] + 1.0);
T a, b, c;
a = root / 2.0;
root = 1.0 / (2.0 * root);
b = (this->data[j][i] + this->data[i][j]) * root;
c = (this->data[k][i] + this->data[i][k]) * root;
switch (i)
{
default:
case 0: q.X(a); break;
case 1: q.Y(a); break;
case 2: q.Z(a); break;
};
switch (j)
{
default:
case 0: q.X(b); break;
case 1: q.Y(b); break;
case 2: q.Z(b); break;
};
switch (k)
{
default:
case 0: q.X(c); break;
case 1: q.Y(c); break;
case 2: q.Z(c); break;
};
q.W((this->data[k][j] - this->data[j][k]) * root);
}
return q;
}
/// \brief Get the rotation as a Euler angles
/// \param[in] _firstSolution True to get the first Euler solution,
/// false to get the second.
/// \return the rotation
public: Vector3<T> EulerRotation(bool _firstSolution) const
{
Vector3<T> euler;
Vector3<T> euler2;
T m31 = this->data[2][0];
T m11 = this->data[0][0];
T m12 = this->data[0][1];
T m13 = this->data[0][2];
T m32 = this->data[2][1];
T m33 = this->data[2][2];
T m21 = this->data[1][0];
if (std::abs(m31) >= 1.0)
{
euler.Z(0.0);
euler2.Z(0.0);
if (m31 < 0.0)
{
euler.Y(IGN_PI / 2.0);
euler2.Y(IGN_PI / 2.0);
euler.X(atan2(m12, m13));
euler2.X(atan2(m12, m13));
}
else
{
euler.Y(-IGN_PI / 2.0);
euler2.Y(-IGN_PI / 2.0);
euler.X(atan2(-m12, -m13));
euler2.X(atan2(-m12, -m13));
}
}
else
{
euler.Y(-asin(m31));
euler2.Y(IGN_PI - euler.Y());
euler.X(atan2(m32 / cos(euler.Y()), m33 / cos(euler.Y())));
euler2.X(atan2(m32 / cos(euler2.Y()), m33 / cos(euler2.Y())));
euler.Z(atan2(m21 / cos(euler.Y()), m11 / cos(euler.Y())));
euler2.Z(atan2(m21 / cos(euler2.Y()), m11 / cos(euler2.Y())));
}
if (_firstSolution)
return euler;
else
return euler2;
}
/// \brief Get the transformation as math::Pose
/// \return the pose
public: Pose3<T> Pose() const
{
return Pose3<T>(this->Translation(), this->Rotation());
}
/// \brief Set the scale
/// \param[in] _s scale
public: void Scale(const Vector3<T> &_s)
{
this->data[0][0] = _s.X();
this->data[1][1] = _s.Y();
this->data[2][2] = _s.Z();
this->data[3][3] = 1.0;
}
/// \brief Set the scale
/// \param[in] _x X scale value.
/// \param[in] _y Y scale value.
/// \param[in] _z Z scale value.
public: void Scale(T _x, T _y, T _z)
{
this->data[0][0] = _x;
this->data[1][1] = _y;
this->data[2][2] = _z;
this->data[3][3] = 1.0;
}
/// \brief Return true if the matrix is affine
/// \return true if the matrix is affine, false otherwise
public: bool IsAffine() const
{
return equal(this->data[3][0], static_cast<T>(0)) &&
equal(this->data[3][1], static_cast<T>(0)) &&
equal(this->data[3][2], static_cast<T>(0)) &&
equal(this->data[3][3], static_cast<T>(1));
}
/// \brief Perform an affine transformation
/// \param _v Vector3 value for the transformation
/// \return The result of the transformation
/// \throws AffineException when matrix is not affine.
public: Vector3<T> TransformAffine(const Vector3<T> &_v) const
{
if (!this->IsAffine())
throw AffineException();
return Vector3<T>(this->data[0][0]*_v.X() + this->data[0][1]*_v.Y() +
this->data[0][2]*_v.Z() + this->data[0][3],
this->data[1][0]*_v.X() + this->data[1][1]*_v.Y() +
this->data[1][2]*_v.Z() + this->data[1][3],
this->data[2][0]*_v.X() + this->data[2][1]*_v.Y() +
this->data[2][2]*_v.Z() + this->data[2][3]);
}
/// \brief Return the inverse matrix.
/// This is a non-destructive operation.
/// \return Inverse of this matrix.
public: Matrix4<T> Inverse() const
{
T v0, v1, v2, v3, v4, v5, t00, t10, t20, t30;
Matrix4<T> r;
v0 = this->data[2][0]*this->data[3][1] -
this->data[2][1]*this->data[3][0];
v1 = this->data[2][0]*this->data[3][2] -
this->data[2][2]*this->data[3][0];
v2 = this->data[2][0]*this->data[3][3] -
this->data[2][3]*this->data[3][0];
v3 = this->data[2][1]*this->data[3][2] -
this->data[2][2]*this->data[3][1];
v4 = this->data[2][1]*this->data[3][3] -
this->data[2][3]*this->data[3][1];
v5 = this->data[2][2]*this->data[3][3] -
this->data[2][3]*this->data[3][2];
t00 = +(v5*this->data[1][1] -
v4*this->data[1][2] + v3*this->data[1][3]);
t10 = -(v5*this->data[1][0] -
v2*this->data[1][2] + v1*this->data[1][3]);
t20 = +(v4*this->data[1][0] -
v2*this->data[1][1] + v0*this->data[1][3]);
t30 = -(v3*this->data[1][0] -
v1*this->data[1][1] + v0*this->data[1][2]);
T invDet = 1 / (t00 * this->data[0][0] + t10 * this->data[0][1] +
t20 * this->data[0][2] + t30 * this->data[0][3]);
r(0, 0) = t00 * invDet;
r(1, 0) = t10 * invDet;
r(2, 0) = t20 * invDet;
r(3, 0) = t30 * invDet;
r(0, 1) = -(v5*this->data[0][1] -
v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
r(1, 1) = +(v5*this->data[0][0] -
v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
r(2, 1) = -(v4*this->data[0][0] -
v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
r(3, 1) = +(v3*this->data[0][0] -
v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
v0 = this->data[1][0]*this->data[3][1] -
this->data[1][1]*this->data[3][0];
v1 = this->data[1][0]*this->data[3][2] -
this->data[1][2]*this->data[3][0];
v2 = this->data[1][0]*this->data[3][3] -
this->data[1][3]*this->data[3][0];
v3 = this->data[1][1]*this->data[3][2] -
this->data[1][2]*this->data[3][1];
v4 = this->data[1][1]*this->data[3][3] -
this->data[1][3]*this->data[3][1];
v5 = this->data[1][2]*this->data[3][3] -
this->data[1][3]*this->data[3][2];
r(0, 2) = +(v5*this->data[0][1] -
v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
r(1, 2) = -(v5*this->data[0][0] -
v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
r(2, 2) = +(v4*this->data[0][0] -
v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
r(3, 2) = -(v3*this->data[0][0] -
v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
v0 = this->data[2][1]*this->data[1][0] -
this->data[2][0]*this->data[1][1];
v1 = this->data[2][2]*this->data[1][0] -
this->data[2][0]*this->data[1][2];
v2 = this->data[2][3]*this->data[1][0] -
this->data[2][0]*this->data[1][3];
v3 = this->data[2][2]*this->data[1][1] -
this->data[2][1]*this->data[1][2];
v4 = this->data[2][3]*this->data[1][1] -
this->data[2][1]*this->data[1][3];
v5 = this->data[2][3]*this->data[1][2] -
this->data[2][2]*this->data[1][3];
r(0, 3) = -(v5*this->data[0][1] -
v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
r(1, 3) = +(v5*this->data[0][0] -
v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
r(2, 3) = -(v4*this->data[0][0] -
v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
r(3, 3) = +(v3*this->data[0][0] -
v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
return r;
}
/// \brief Equal operator. this = _mat
/// \param _mat Incoming matrix
/// \return itself
public: Matrix4<T> &operator=(const Matrix4<T> &_mat)
{
memcpy(this->data, _mat.data, sizeof(this->data[0][0])*16);
return *this;
}
/// \brief Equal operator for 3x3 matrix
/// \param _mat Incoming matrix
/// \return itself
public: const Matrix4<T> &operator=(const Matrix3<T> &_mat)
{
this->data[0][0] = _mat(0, 0);
this->data[0][1] = _mat(0, 1);
this->data[0][2] = _mat(0, 2);
this->data[1][0] = _mat(1, 0);
this->data[1][1] = _mat(1, 1);
this->data[1][2] = _mat(1, 2);
this->data[2][0] = _mat(2, 0);
this->data[2][1] = _mat(2, 1);
this->data[2][2] = _mat(2, 2);
return *this;
}
/// \brief Multiplication operator
/// \param _mat Incoming matrix
/// \return This matrix * _mat
public: Matrix4<T> operator*(const Matrix4<T> &_m2) const
{
return Matrix4<T>(
this->data[0][0] * _m2(0, 0) +
this->data[0][1] * _m2(1, 0) +
this->data[0][2] * _m2(2, 0) +
this->data[0][3] * _m2(3, 0),
this->data[0][0] * _m2(0, 1) +
this->data[0][1] * _m2(1, 1) +
this->data[0][2] * _m2(2, 1) +
this->data[0][3] * _m2(3, 1),
this->data[0][0] * _m2(0, 2) +
this->data[0][1] * _m2(1, 2) +
this->data[0][2] * _m2(2, 2) +
this->data[0][3] * _m2(3, 2),
this->data[0][0] * _m2(0, 3) +
this->data[0][1] * _m2(1, 3) +
this->data[0][2] * _m2(2, 3) +
this->data[0][3] * _m2(3, 3),
this->data[1][0] * _m2(0, 0) +
this->data[1][1] * _m2(1, 0) +
this->data[1][2] * _m2(2, 0) +
this->data[1][3] * _m2(3, 0),
this->data[1][0] * _m2(0, 1) +
this->data[1][1] * _m2(1, 1) +
this->data[1][2] * _m2(2, 1) +
this->data[1][3] * _m2(3, 1),
this->data[1][0] * _m2(0, 2) +
this->data[1][1] * _m2(1, 2) +
this->data[1][2] * _m2(2, 2) +
this->data[1][3] * _m2(3, 2),
this->data[1][0] * _m2(0, 3) +
this->data[1][1] * _m2(1, 3) +
this->data[1][2] * _m2(2, 3) +
this->data[1][3] * _m2(3, 3),
this->data[2][0] * _m2(0, 0) +
this->data[2][1] * _m2(1, 0) +
this->data[2][2] * _m2(2, 0) +
this->data[2][3] * _m2(3, 0),
this->data[2][0] * _m2(0, 1) +
this->data[2][1] * _m2(1, 1) +
this->data[2][2] * _m2(2, 1) +
this->data[2][3] * _m2(3, 1),
this->data[2][0] * _m2(0, 2) +
this->data[2][1] * _m2(1, 2) +
this->data[2][2] * _m2(2, 2) +
this->data[2][3] * _m2(3, 2),
this->data[2][0] * _m2(0, 3) +
this->data[2][1] * _m2(1, 3) +
this->data[2][2] * _m2(2, 3) +
this->data[2][3] * _m2(3, 3),
this->data[3][0] * _m2(0, 0) +
this->data[3][1] * _m2(1, 0) +
this->data[3][2] * _m2(2, 0) +
this->data[3][3] * _m2(3, 0),
this->data[3][0] * _m2(0, 1) +
this->data[3][1] * _m2(1, 1) +
this->data[3][2] * _m2(2, 1) +
this->data[3][3] * _m2(3, 1),
this->data[3][0] * _m2(0, 2) +
this->data[3][1] * _m2(1, 2) +
this->data[3][2] * _m2(2, 2) +
this->data[3][3] * _m2(3, 2),
this->data[3][0] * _m2(0, 3) +
this->data[3][1] * _m2(1, 3) +
this->data[3][2] * _m2(2, 3) +
this->data[3][3] * _m2(3, 3));
}
/// \brief Multiplication operator
/// \param _vec Vector3
/// \return Resulting vector from multiplication
public: Vector3<T> operator*(const Vector3<T> &_vec) const
{
return Vector3<T>(
this->data[0][0]*_vec.X() + this->data[0][1]*_vec.Y() +
this->data[0][2]*_vec.Z() + this->data[0][3],
this->data[1][0]*_vec.X() + this->data[1][1]*_vec.Y() +
this->data[1][2]*_vec.Z() + this->data[1][3],
this->data[2][0]*_vec.X() + this->data[2][1]*_vec.Y() +
this->data[2][2]*_vec.Z() + this->data[2][3]);
}
/// \brief Get the value at the specified row, column index
/// \param[in] _col The column index
/// \param[in] _row the row index
/// \return The value at the specified index
public: inline const T &operator()(size_t _row, size_t _col) const
{
if (_row >= 4 || _col >= 4)
throw IndexException();
return this->data[_row][_col];
}
/// \brief Get a mutable version the value at the specified row,
/// column index
/// \param[in] _col The column index
/// \param[in] _row The row index
/// \return The value at the specified index
public: inline T &operator()(size_t _row, size_t _col)
{
if (_row >= 4 || _col >= 4)
throw IndexException();
return this->data[_row][_col];
}
/// \brief Equality operator
/// \param[in] _m Matrix3 to test
/// \return true if the 2 matrices are equal (using the tolerance 1e-6),
/// false otherwise
public: bool operator==(const Matrix4<T> &_m) const
{
return math::equal(this->data[0][0], _m(0, 0)) &&
math::equal(this->data[0][1], _m(0, 1)) &&
math::equal(this->data[0][2], _m(0, 2)) &&
math::equal(this->data[0][3], _m(0, 3)) &&
math::equal(this->data[1][0], _m(1, 0)) &&
math::equal(this->data[1][1], _m(1, 1)) &&
math::equal(this->data[1][2], _m(1, 2)) &&
math::equal(this->data[1][3], _m(1, 3)) &&
math::equal(this->data[2][0], _m(2, 0)) &&
math::equal(this->data[2][1], _m(2, 1)) &&
math::equal(this->data[2][2], _m(2, 2)) &&
math::equal(this->data[2][3], _m(2, 3)) &&
math::equal(this->data[3][0], _m(3, 0)) &&
math::equal(this->data[3][1], _m(3, 1)) &&
math::equal(this->data[3][2], _m(3, 2)) &&
math::equal(this->data[3][3], _m(3, 3));
}
/// \brief Inequality test operator
/// \param[in] _m Matrix4<T> to test
/// \return True if not equal (using the default tolerance of 1e-6)
public: bool operator!=(const Matrix4<T> &_m) const
{
return !(*this == _m);
}
/// \brief Stream insertion operator
/// \param _out output stream
/// \param _m Matrix to output
/// \return the stream
public: friend std::ostream &operator<<(
std::ostream &_out, const ignition::math::Matrix4<T> &_m)
{
_out << precision(_m(0, 0), 6) << " "
<< precision(_m(0, 1), 6) << " "
<< precision(_m(0, 2), 6) << " "
<< precision(_m(0, 3), 6) << " "
<< precision(_m(1, 0), 6) << " "
<< precision(_m(1, 1), 6) << " "
<< precision(_m(1, 2), 6) << " "
<< precision(_m(1, 3), 6) << " "
<< precision(_m(2, 0), 6) << " "
<< precision(_m(2, 1), 6) << " "
<< precision(_m(2, 2), 6) << " "
<< precision(_m(2, 3), 6) << " "
<< precision(_m(3, 0), 6) << " "
<< precision(_m(3, 1), 6) << " "
<< precision(_m(3, 2), 6) << " "
<< precision(_m(3, 3), 6);
return _out;
}
/// \brief Stream extraction operator
/// \param _in input stream
/// \param _pt Matrix4<T> to read values into
/// \return the stream
public: friend std::istream &operator>>(
std::istream &_in, ignition::math::Matrix4<T> &_m)
{
// Skip white spaces
_in.setf(std::ios_base::skipws);
T d[16];
_in >> d[0] >> d[1] >> d[2] >> d[3]
>> d[4] >> d[5] >> d[6] >> d[7]
>> d[8] >> d[9] >> d[10] >> d[11]
>> d[12] >> d[13] >> d[14] >> d[15];
_m.Set(d[0], d[1], d[2], d[3],
d[4], d[5], d[6], d[7],
d[8], d[9], d[10], d[11],
d[12], d[13], d[14], d[15]);
return _in;
}
/// \brief The 4x4 matrix
private: T data[4][4];
};
template<typename T>
const Matrix4<T> Matrix4<T>::Identity(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
template<typename T>
const Matrix4<T> Matrix4<T>::Zero(
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0);
typedef Matrix4<int> Matrix4i;
typedef Matrix4<double> Matrix4d;
typedef Matrix4<float> Matrix4f;
}
}
#endif