Sensor Scol plugin
Multi platform sensors for handled devices
Matrix3x3d.cpp
1
2#include "tools/Matrix3x3d.h"
3
4Matrix3x3d::Matrix3x3d()
5{
6 setZero();
7}
8
9Matrix3x3d::Matrix3x3d(double m00, double m01, double m02,
10 double m10, double m11, double m12,
11 double m20, double m21, double m22)
12{
13 _m[0] = m00;
14 _m[1] = m01;
15 _m[2] = m02;
16 _m[3] = m10;
17 _m[4] = m11;
18 _m[5] = m12;
19 _m[6] = m20;
20 _m[7] = m21;
21 _m[8] = m22;
22}
23
24Matrix3x3d::Matrix3x3d(const Matrix3x3d &o)
25{
26 for (int i = 0; i < 9; i++)
27 {
28 _m[i] = o._m[i];
29 }
30}
31
32void Matrix3x3d::set(double m00, double m01, double m02,
33 double m10, double m11, double m12,
34 double m20, double m21, double m22)
35{
36 _m[0] = m00;
37 _m[1] = m01;
38 _m[2] = m02;
39 _m[3] = m10;
40 _m[4] = m11;
41 _m[5] = m12;
42 _m[6] = m20;
43 _m[7] = m21;
44 _m[8] = m22;
45}
46
47void Matrix3x3d::set(Matrix3x3d o)
48{
49 for (int i = 0; i < 9; i++)
50 {
51 _m[i] = o._m[i];
52 }
53}
54
55void Matrix3x3d::setZero()
56{
57 for (int i = 0; i < 9; i++)
58 {
59 _m[i] = 0;
60 }
61}
62
63void Matrix3x3d::setIdentity()
64{
65 _m[0] = 1;
66 _m[1] = 0;
67 _m[2] = 0;
68 _m[3] = 0;
69 _m[4] = 1;
70 _m[5] = 0;
71 _m[6] = 0;
72 _m[7] = 0;
73 _m[8] = 1;
74}
75
76void Matrix3x3d::setDiagonal(double d)
77{
78 _m[0] = d;
79 _m[4] = d;
80 _m[8] = d;
81}
82
83double Matrix3x3d::get(int row, int col)
84{
85 return _m[(3 * row + col)];
86}
87
88void Matrix3x3d::set(int row, int col, double value)
89{
90 _m[(3 * row + col)] = value;
91}
92
93void Matrix3x3d::getColumn(int col, Vector3d &v)
94{
95 v.x = _m[col];
96 v.y = _m[col + 3];
97 v.z = _m[col + 6];
98}
99
100void Matrix3x3d::setColumn(int col, Vector3d v)
101{
102 _m[col] = v.x;
103 _m[col + 3] = v.y;
104 _m[col + 6] = v.z;
105}
106
107void Matrix3x3d::scale(double s)
108{
109 for (int i = 0; i < 9; i++)
110 {
111 _m[i] *= s;
112 }
113}
114
115void Matrix3x3d::plusEquals(Matrix3x3d b)
116{
117 for (int i = 0; i < 9; i++)
118 {
119 _m[i] += b._m[i];
120 }
121}
122
123void Matrix3x3d::minusEquals(Matrix3x3d b)
124{
125 for (int i = 0; i < 9; i++)
126 {
127 _m[i] -= b._m[i];
128 }
129}
130
131void Matrix3x3d::transpose()
132{
133 double tmp = _m[1];
134 _m[1] = _m[3];
135 _m[3] = tmp;
136 tmp = _m[2];
137 _m[2] = _m[6];
138 _m[6] = tmp;
139 tmp = _m[5];
140 _m[5] = _m[7];
141 _m[7] = tmp;
142}
143
144void Matrix3x3d::transpose(Matrix3x3d &result)
145{
146 result._m[0] = _m[0];
147 result._m[1] = _m[3];
148 result._m[2] = _m[6];
149 result._m[3] = _m[1];
150 result._m[4] = _m[4];
151 result._m[5] = _m[7];
152 result._m[6] = _m[2];
153 result._m[7] = _m[5];
154 result._m[8] = _m[8];
155}
156
157void Matrix3x3d::add(Matrix3x3d a, Matrix3x3d b, Matrix3x3d &result)
158{
159 for (int i = 0; i < 9; i++)
160 {
161 result._m[i] = a._m[i] + b._m[i];
162 }
163}
164
165void Matrix3x3d::mult(Matrix3x3d a, Matrix3x3d b, Matrix3x3d &result)
166{
167 result.set(a._m[0] * b._m[0] + a._m[1] * b._m[3] + a._m[2] * b._m[6],
168 a._m[0] * b._m[1] + a._m[1] * b._m[4] + a._m[2] * b._m[7],
169 a._m[0] * b._m[2] + a._m[1] * b._m[5] + a._m[2] * b._m[8],
170 a._m[3] * b._m[0] + a._m[4] * b._m[3] + a._m[5] * b._m[6],
171 a._m[3] * b._m[1] + a._m[4] * b._m[4] + a._m[5] * b._m[7],
172 a._m[3] * b._m[2] + a._m[4] * b._m[5] + a._m[5] * b._m[8],
173 a._m[6] * b._m[0] + a._m[7] * b._m[3] + a._m[8] * b._m[6],
174 a._m[6] * b._m[1] + a._m[7] * b._m[4] + a._m[8] * b._m[7],
175 a._m[6] * b._m[2] + a._m[7] * b._m[5] + a._m[8] * b._m[8]);
176}
177
178void Matrix3x3d::mult(Matrix3x3d a, Vector3d v, Vector3d &result)
179{
180 result.set( a._m[0] * v.x + a._m[1] * v.y + a._m[2] * v.z,
181 a._m[3] * v.x + a._m[4] * v.y + a._m[5] * v.z,
182 a._m[6] * v.x + a._m[7] * v.y + a._m[8] * v.z);
183}
184
185double Matrix3x3d::determinant()
186{
187 return get(0, 0) * (get(1, 1) * get(2, 2) - get(2, 1) * get(1, 2))
188 - get(0, 1) * (get(1, 0) * get(2, 2) - get(1, 2) * get(2, 0))
189 + get(0, 2) * (get(1, 0) * get(2, 1) - get(1, 1) * get(2, 0));
190}
191
192bool Matrix3x3d::invert(Matrix3x3d &result)
193{
194 double d = determinant();
195 if (d == 0.0)
196 {
197 return false;
198 }
199 double invdet = 1.0 / d;
200 result.set( (_m[4] * _m[8] - _m[7] * _m[5]) * invdet,
201 -(_m[1] * _m[8] - _m[2] * _m[7]) * invdet,
202 (_m[1] * _m[5] - _m[2] * _m[4]) * invdet,
203 -(_m[3] * _m[8] - _m[5] * _m[6]) * invdet,
204 (_m[0] * _m[8] - _m[2] * _m[6]) * invdet,
205 -(_m[0] * _m[5] - _m[3] * _m[2]) * invdet,
206 (_m[3] * _m[7] - _m[6] * _m[4]) * invdet,
207 -(_m[0] * _m[7] - _m[6] * _m[1]) * invdet,
208 (_m[0] * _m[4] - _m[3] * _m[1]) * invdet);
209 return true;
210}