framework2
336 строк · 6.2 Кб
1#include "ofMatrix3x3.h"
2#include <iomanip>
3
4ofMatrix3x3::ofMatrix3x3( float _a, float _b, float _c,
5float _d, float _e, float _f,
6float _g, float _h, float _i )
7{
8a = _a;
9b = _b;
10c = _c;
11d = _d;
12e = _e;
13f = _f;
14g = _g;
15h = _h;
16i = _i;
17}
18
19
20
21
22void ofMatrix3x3::set( float _a, float _b, float _c,
23float _d, float _e, float _f,
24float _g, float _h, float _i )
25{
26a = _a;
27b = _b;
28c = _c;
29d = _d;
30e = _e;
31f = _f;
32g = _g;
33h = _h;
34i = _i;
35}
36
37
38float& ofMatrix3x3::operator[]( const int& index ) {
39switch(index) {
40case 0: return a;
41case 1: return b;
42case 2: return c;
43case 3: return d;
44case 4: return e;
45case 5: return f;
46case 6: return g;
47case 7: return h;
48case 8: return i;
49default: return a;
50}
51}
52
53
54/*
55* Transpose:
56* This changes the matrix.
57* [ a b c ]T [ a d g ]
58* [ d e f ] = [ b e h ]
59* [ g h i ] [ c f i ]
60*/
61
62void ofMatrix3x3::transpose() {
63b += d; d = b - d; b -= d; //swap b and d
64c += g; g = c - g; c -= g; //swap c and g
65f += h; h = f - h; f -= h; //swap f and h
66}
67
68/*
69* Transpose without changing the matrix.
70* Uses the "swap" method with additions and subtractions to swap the elements that aren't on the main diagonal.
71* @return transposed matrix.
72*/
73
74ofMatrix3x3 ofMatrix3x3::transpose(const ofMatrix3x3& A) {
75ofMatrix3x3 result = A;
76result.transpose();
77return result;
78}
79
80
81
82/*
83* Determinant: http://mathworld.wolfram.com/Determinant.html
84*/
85
86float ofMatrix3x3::determinant() const {
87float det = a * e * i
88+ b * f * g
89+ d * h * c
90- g * e * c
91- d * b * i
92- h * f * a;
93return det;
94}
95
96float ofMatrix3x3::determinant(const ofMatrix3x3& A) {
97return A.determinant();
98}
99
100
101
102/*
103* Inverse of a 3x3 matrix
104the inverse is the adjoint divided through the determinant
105find the matrix of minors (minor = determinant of 2x2 matrix of the 2 rows/colums current element is NOT in)
106turn them in cofactors (= change some of the signs)
107find the adjoint by transposing the matrix of cofactors
108divide this through the determinant to get the inverse
109*/
110
111void ofMatrix3x3::invert() {
112float det = determinant();
113ofMatrix3x3 B;
114
115//included in these calculations: minor, cofactor (changed signs), transpose (by the order of "="), division through determinant
116B.a = ( e * i - h * f) / det;
117B.b = (-b * i + h * c) / det;
118B.c = ( b * f - e * c) / det;
119B.d = (-d * i + g * f) / det;
120B.e = ( a * i - g * c) / det;
121B.f = (-a * f + d * c) / det;
122B.g = ( d * h - g * e) / det;
123B.h = (-a * h + g * b) / det;
124B.i = ( a * e - d * b) / det;
125
126*this = B;
127}
128
129ofMatrix3x3 ofMatrix3x3::inverse(const ofMatrix3x3& A) {
130ofMatrix3x3 result = A;
131result.invert();
132return result;
133}
134
135
136
137/*
138* Add two matrices
139*/
140ofMatrix3x3 ofMatrix3x3::operator+(const ofMatrix3x3& B) {
141ofMatrix3x3 result;
142result.a = a + B.a;
143result.b = b + B.b;
144result.c = c + B.c;
145result.d = d + B.d;
146result.e = e + B.e;
147result.f = f + B.f;
148result.g = g + B.g;
149result.h = h + B.h;
150result.i = i + B.i;
151return result;
152}
153
154void ofMatrix3x3::operator+=(const ofMatrix3x3& B) {
155a += B.a;
156b += B.b;
157c += B.c;
158d += B.d;
159e += B.e;
160f += B.f;
161g += B.g;
162h += B.h;
163i += B.i;
164}
165
166/*
167* Subtract two matrices
168*/
169ofMatrix3x3 ofMatrix3x3::operator-(const ofMatrix3x3& B) {
170ofMatrix3x3 result;
171result.a = a - B.a;
172result.b = b - B.b;
173result.c = c - B.c;
174result.d = d - B.d;
175result.e = e - B.e;
176result.f = f - B.f;
177result.g = g - B.g;
178result.h = h - B.h;
179result.i = i - B.i;
180return result;
181}
182
183void ofMatrix3x3::operator-=(const ofMatrix3x3& B) {
184a -= B.a;
185b -= B.b;
186c -= B.c;
187d -= B.d;
188e -= B.e;
189f -= B.f;
190g -= B.g;
191h -= B.h;
192i -= B.i;
193}
194
195
196/*
197* Multiply a matrix with a scalar
198*/
199ofMatrix3x3 ofMatrix3x3::operator*(float scalar) {
200ofMatrix3x3 result;
201result.a = a * scalar;
202result.b = b * scalar;
203result.c = c * scalar;
204result.d = d * scalar;
205result.e = e * scalar;
206result.f = f * scalar;
207result.g = g * scalar;
208result.h = h * scalar;
209result.i = i * scalar;
210return result;
211}
212
213
214void ofMatrix3x3::operator*=(const ofMatrix3x3& B) {
215*this = *this*B;
216}
217
218ofMatrix3x3 ofMatrix3x3::entrywiseTimes(const ofMatrix3x3& B){
219ofMatrix3x3 C = *this;
220C.a *= B.a;
221C.b *= B.b;
222C.c *= B.c;
223C.d *= B.d;
224C.e *= B.e;
225C.f *= B.f;
226C.g *= B.g;
227C.h *= B.h;
228C.i *= B.i;
229return C;
230}
231
232void ofMatrix3x3::operator*=(float scalar) {
233a *= scalar;
234b *= scalar;
235c *= scalar;
236d *= scalar;
237e *= scalar;
238f *= scalar;
239g *= scalar;
240h *= scalar;
241i *= scalar;
242}
243
244/*
245* Multiply a 3x3 matrix with a 3x3 matrix
246*/
247ofMatrix3x3 ofMatrix3x3::operator*(const ofMatrix3x3& B) {
248ofMatrix3x3 C;
249C.a = a * B.a + b * B.d + c * B.g;
250C.b = a * B.b + b * B.e + c * B.h;
251C.c = a * B.c + b * B.f + c * B.i;
252C.d = d * B.a + e * B.d + f * B.g;
253C.e = d * B.b + e * B.e + f * B.h;
254C.f = d * B.c + e * B.f + f * B.i;
255C.g = g * B.a + h * B.d + i * B.g;
256C.h = g * B.b + h * B.e + i * B.h;
257C.i = g * B.c + h * B.f + i * B.i;
258return C;
259}
260
261/*
262* Divide a matrix through a scalar
263*/
264ofMatrix3x3 ofMatrix3x3::operator/(float scalar) {
265ofMatrix3x3 result;
266result.a = a / scalar;
267result.b = b / scalar;
268result.c = c / scalar;
269result.d = d / scalar;
270result.e = e / scalar;
271result.f = f / scalar;
272result.g = g / scalar;
273result.h = h / scalar;
274result.i = i / scalar;
275return result;
276}
277
278
279void ofMatrix3x3::operator/=(const ofMatrix3x3& B) {
280a /= B.a;
281b /= B.b;
282c /= B.c;
283d /= B.d;
284e /= B.e;
285f /= B.f;
286g /= B.g;
287h /= B.h;
288i /= B.i;
289}
290
291void ofMatrix3x3::operator/=(float scalar) {
292a /= scalar;
293b /= scalar;
294c /= scalar;
295d /= scalar;
296e /= scalar;
297f /= scalar;
298g /= scalar;
299h /= scalar;
300i /= scalar;
301}
302
303
304std::ostream& operator<<(std::ostream& os, const ofMatrix3x3& M) {
305int w = 8;
306os << std::setw(w)
307<< M.a << ", " << std::setw(w)
308<< M.b << ", " << std::setw(w)
309<< M.c << std::endl;
310
311os << std::setw(w)
312<< M.d << ", " << std::setw(w)
313<< M.e << ", " << std::setw(w)
314<< M.f << std::endl;
315
316os << std::setw(w)
317<< M.g << ", " << std::setw(w)
318<< M.h << ", " << std::setw(w)
319<< M.i;
320return os;
321}
322
323std::istream& operator>>(std::istream& is, ofMatrix3x3& M) {
324is >> M.a; is.ignore(2);
325is >> M.b; is.ignore(2);
326is >> M.c; is.ignore(1);
327
328is >> M.d; is.ignore(2);
329is >> M.e; is.ignore(2);
330is >> M.f; is.ignore(1);
331
332is >> M.g; is.ignore(2);
333is >> M.h; is.ignore(2);
334is >> M.i;
335return is;
336}
337
338
339