framework2

Форк
0
336 строк · 6.2 Кб
1
#include "ofMatrix3x3.h"
2
#include <iomanip>
3

4
ofMatrix3x3::ofMatrix3x3( float _a, float _b, float _c,
5
			  float _d, float _e, float _f,
6
			  float _g, float _h, float _i )
7
{
8
	a = _a;
9
	b = _b;
10
	c = _c;
11
	d = _d;
12
	e = _e;
13
	f = _f;
14
	g = _g;
15
	h = _h;
16
	i = _i;
17
}
18

19

20

21

22
void ofMatrix3x3::set( float _a, float _b, float _c,
23
		  float _d, float _e, float _f,
24
		  float _g, float _h, float _i )
25
{
26
	a = _a;
27
	b = _b;
28
	c = _c;
29
	d = _d;
30
	e = _e;
31
	f = _f;
32
	g = _g;
33
	h = _h;
34
	i = _i;
35
}
36

37

38
float& ofMatrix3x3::operator[]( const int& index ) {
39
	switch(index) {
40
		case 0:  return a;
41
		case 1:  return b;
42
		case 2:  return c;
43
		case 3:  return d;
44
		case 4:  return e;
45
		case 5:  return f;
46
		case 6:  return g;
47
		case 7:  return h;
48
		case 8:  return i;
49
		default: 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

62
void ofMatrix3x3::transpose() {
63
	b += d; d = b - d; b -= d; //swap b and d
64
	c += g; g = c - g; c -= g; //swap c and g
65
	f += 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

74
ofMatrix3x3 ofMatrix3x3::transpose(const ofMatrix3x3& A) {
75
	ofMatrix3x3 result = A;
76
	result.transpose();
77
	return result;
78
}
79

80

81

82
/*
83
* Determinant: http://mathworld.wolfram.com/Determinant.html
84
*/
85

86
float ofMatrix3x3::determinant() const {
87
	float det = a * e * i
88
			   + b * f * g
89
			   + d * h * c
90
			   - g * e * c
91
			   - d * b * i
92
			   - h * f * a;
93
	return det;
94
}
95

96
float ofMatrix3x3::determinant(const ofMatrix3x3& A) {
97
	return A.determinant();
98
}
99

100

101

102
/*
103
* Inverse of a 3x3 matrix
104
  the inverse is the adjoint divided through the determinant
105
  find the matrix of minors (minor = determinant of 2x2 matrix of the 2 rows/colums current element is NOT in)
106
  turn them in cofactors (= change some of the signs)
107
  find the adjoint by transposing the matrix of cofactors
108
  divide this through the determinant to get the inverse
109
*/
110

111
void ofMatrix3x3::invert() {
112
	 float det = determinant();
113
	 ofMatrix3x3 B;
114

115
	 //included in these calculations: minor, cofactor (changed signs), transpose (by the order of "="), division through determinant
116
	 B.a = ( e * i - h * f) / det;
117
	 B.b = (-b * i + h * c) / det;
118
	 B.c = ( b * f - e * c) / det;
119
	 B.d = (-d * i + g * f) / det;
120
	 B.e = ( a * i - g * c) / det;
121
	 B.f = (-a * f + d * c) / det;
122
	 B.g = ( d * h - g * e) / det;
123
	 B.h = (-a * h + g * b) / det;
124
	 B.i = ( a * e - d * b) / det;
125

126
	 *this = B;
127
}
128

129
ofMatrix3x3 ofMatrix3x3::inverse(const ofMatrix3x3& A) {
130
	ofMatrix3x3 result = A;
131
	result.invert();
132
	return result;
133
}
134

135

136

137
/*
138
* Add two matrices
139
*/
140
ofMatrix3x3 ofMatrix3x3::operator+(const ofMatrix3x3& B) {
141
	ofMatrix3x3 result;
142
	result.a = a + B.a;
143
	result.b = b + B.b;
144
	result.c = c + B.c;
145
	result.d = d + B.d;
146
	result.e = e + B.e;
147
	result.f = f + B.f;
148
	result.g = g + B.g;
149
	result.h = h + B.h;
150
	result.i = i + B.i;
151
	return result;
152
}
153

154
void ofMatrix3x3::operator+=(const ofMatrix3x3& B) {
155
	a += B.a;
156
	b += B.b;
157
	c += B.c;
158
	d += B.d;
159
	e += B.e;
160
	f += B.f;
161
	g += B.g;
162
	h += B.h;
163
	i += B.i;
164
}
165

166
/*
167
* Subtract two matrices
168
*/
169
ofMatrix3x3 ofMatrix3x3::operator-(const ofMatrix3x3& B) {
170
	ofMatrix3x3 result;
171
	result.a = a - B.a;
172
	result.b = b - B.b;
173
	result.c = c - B.c;
174
	result.d = d - B.d;
175
	result.e = e - B.e;
176
	result.f = f - B.f;
177
	result.g = g - B.g;
178
	result.h = h - B.h;
179
	result.i = i - B.i;
180
	return result;
181
}
182

183
void ofMatrix3x3::operator-=(const ofMatrix3x3& B) {
184
	a -= B.a;
185
	b -= B.b;
186
	c -= B.c;
187
	d -= B.d;
188
	e -= B.e;
189
	f -= B.f;
190
	g -= B.g;
191
	h -= B.h;
192
	i -= B.i;
193
}
194

195

196
/*
197
* Multiply a matrix with a scalar
198
*/
199
ofMatrix3x3 ofMatrix3x3::operator*(float scalar) {
200
	ofMatrix3x3 result;
201
	result.a = a * scalar;
202
	result.b = b * scalar;
203
	result.c = c * scalar;
204
	result.d = d * scalar;
205
	result.e = e * scalar;
206
	result.f = f * scalar;
207
	result.g = g * scalar;
208
	result.h = h * scalar;
209
	result.i = i * scalar;
210
	return result;
211
}
212

213

214
void ofMatrix3x3::operator*=(const ofMatrix3x3& B) {
215
  *this = *this*B;
216
}
217

218
ofMatrix3x3 ofMatrix3x3::entrywiseTimes(const ofMatrix3x3& B){
219
  ofMatrix3x3 C = *this;
220
	C.a *= B.a;
221
	C.b *= B.b;
222
	C.c *= B.c;
223
	C.d *= B.d;
224
	C.e *= B.e;
225
	C.f *= B.f;
226
	C.g *= B.g;
227
	C.h *= B.h;
228
	C.i *= B.i;
229
  return C;
230
}
231

232
void ofMatrix3x3::operator*=(float scalar) {
233
	a *= scalar;
234
	b *= scalar;
235
	c *= scalar;
236
	d *= scalar;
237
	e *= scalar;
238
	f *= scalar;
239
	g *= scalar;
240
	h *= scalar;
241
	i *= scalar;
242
}
243

244
 /*
245
 * Multiply a 3x3 matrix with a 3x3 matrix
246
 */
247
ofMatrix3x3 ofMatrix3x3::operator*(const ofMatrix3x3& B) {
248
	ofMatrix3x3 C;
249
	C.a = a * B.a + b * B.d + c * B.g;
250
	C.b = a * B.b + b * B.e + c * B.h;
251
	C.c = a * B.c + b * B.f + c * B.i;
252
	C.d = d * B.a + e * B.d + f * B.g;
253
	C.e = d * B.b + e * B.e + f * B.h;
254
	C.f = d * B.c + e * B.f + f * B.i;
255
	C.g = g * B.a + h * B.d + i * B.g;
256
	C.h = g * B.b + h * B.e + i * B.h;
257
	C.i = g * B.c + h * B.f + i * B.i;
258
	return C;
259
}
260

261
/*
262
* Divide a matrix through a scalar
263
*/
264
ofMatrix3x3 ofMatrix3x3::operator/(float scalar) {
265
	ofMatrix3x3 result;
266
	result.a = a / scalar;
267
	result.b = b / scalar;
268
	result.c = c / scalar;
269
	result.d = d / scalar;
270
	result.e = e / scalar;
271
	result.f = f / scalar;
272
	result.g = g / scalar;
273
	result.h = h / scalar;
274
	result.i = i / scalar;
275
	return result;
276
}
277

278

279
void ofMatrix3x3::operator/=(const ofMatrix3x3& B) {
280
	a /= B.a;
281
	b /= B.b;
282
	c /= B.c;
283
	d /= B.d;
284
	e /= B.e;
285
	f /= B.f;
286
	g /= B.g;
287
	h /= B.h;
288
	i /= B.i;
289
}
290

291
void ofMatrix3x3::operator/=(float scalar) {
292
	a /= scalar;
293
	b /= scalar;
294
	c /= scalar;
295
	d /= scalar;
296
	e /= scalar;
297
	f /= scalar;
298
	g /= scalar;
299
	h /= scalar;
300
	i /= scalar;
301
}
302

303

304
std::ostream& operator<<(std::ostream& os, const ofMatrix3x3& M) {
305
	int w = 8;
306
	os	<< std::setw(w)
307
		<< M.a << ", " << std::setw(w)
308
		<< M.b << ", " << std::setw(w)
309
		<< M.c << std::endl;
310

311
	os	<< std::setw(w)
312
		<< M.d << ", " << std::setw(w)
313
		<< M.e << ", " << std::setw(w)
314
		<< M.f << std::endl;
315

316
	os	<< std::setw(w)
317
		<< M.g << ", " << std::setw(w)
318
		<< M.h << ", " << std::setw(w)
319
		<< M.i;
320
	return os;
321
}
322

323
std::istream& operator>>(std::istream& is, ofMatrix3x3& M) {
324
	is >> M.a; is.ignore(2);
325
	is >> M.b; is.ignore(2);
326
	is >> M.c; is.ignore(1);
327

328
	is >> M.d; is.ignore(2);
329
	is >> M.e; is.ignore(2);
330
	is >> M.f; is.ignore(1);
331

332
	is >> M.g; is.ignore(2);
333
	is >> M.h; is.ignore(2);
334
	is >> M.i;
335
	return is;
336
}
337

338

339

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.