1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/matrix.h"
24 
25 #include "common/scummsys.h"
26 
27 namespace BladeRunner {
28 
Matrix3x2()29 Matrix3x2::Matrix3x2() {
30 	for (int r = 0; r != 2; ++r)
31 		for (int c = 0; c != 3; ++c)
32 			_m[r][c] = (r == c) ? 1.0f : 0.0f;
33 }
34 
Matrix3x2(float d[6])35 Matrix3x2::Matrix3x2(float d[6]) {
36 	for (int r = 0; r != 2; ++r)
37 		for (int c = 0; c != 3; ++c)
38 			_m[r][c] = d[r*3+c];
39 }
40 
Matrix3x2(float m00,float m01,float m02,float m10,float m11,float m12)41 Matrix3x2::Matrix3x2(
42 	float m00, float m01, float m02,
43 	float m10, float m11, float m12) {
44 	_m[0][0] = m00;
45 	_m[0][1] = m01;
46 	_m[0][2] = m02;
47 	_m[1][0] = m10;
48 	_m[1][1] = m11;
49 	_m[1][2] = m12;
50 }
51 
Matrix4x3()52 Matrix4x3::Matrix4x3() {
53 	for (int r = 0; r != 3; ++r)
54 		for (int c = 0; c != 4; ++c)
55 			_m[r][c] = (r == c) ? 1.0f : 0.0f;
56 }
57 
Matrix4x3(float d[12])58 Matrix4x3::Matrix4x3(float d[12]) {
59 	for (int r = 0; r != 3; ++r)
60 		for (int c = 0; c != 4; ++c)
61 			_m[r][c] = d[r*4+c];
62 }
63 
Matrix4x3(float m00,float m01,float m02,float m03,float m10,float m11,float m12,float m13,float m20,float m21,float m22,float m23)64 Matrix4x3::Matrix4x3(
65 	float m00, float m01, float m02, float m03,
66 	float m10, float m11, float m12, float m13,
67 	float m20, float m21, float m22, float m23) {
68 	_m[0][0] = m00;
69 	_m[0][1] = m01;
70 	_m[0][2] = m02;
71 	_m[0][3] = m03;
72 	_m[1][0] = m10;
73 	_m[1][1] = m11;
74 	_m[1][2] = m12;
75 	_m[1][3] = m13;
76 	_m[2][0] = m20;
77 	_m[2][1] = m21;
78 	_m[2][2] = m22;
79 	_m[2][3] = m23;
80 }
81 
rotationMatrixX(float angle)82 Matrix4x3 rotationMatrixX(float angle) {
83 	float ca = cos(angle);
84 	float sa = sin(angle);
85 
86 	return Matrix4x3( 1.0f, 0.0f, 0.0f, 0.0f,
87 	                  0.0f,   ca,   sa, 0.0f,
88 	                  0.0f,  -sa,   ca, 0.0f );
89 }
90 
swapRows(double * r1,double * r2)91 static inline void swapRows(double *r1, double *r2) {
92 	for (int c = 0; c != 8; ++c) {
93 		double t = r1[c];
94 		r1[c] = r2[c];
95 		r2[c] = t;
96 	}
97 }
98 
subtractRow(double * r1,double factor,double * r2)99 static inline void subtractRow(double *r1, double factor, double *r2) {
100 	for (int c = 0; c != 8; ++c)
101 		r1[c] -= factor * r2[c];
102 }
103 
divideRow(double * r1,double d)104 static inline void divideRow(double *r1, double d) {
105 	for (int c = 0; c != 8; ++c)
106 		r1[c] /= d;
107 }
108 
invertMatrix(const Matrix4x3 & m)109 Matrix4x3 invertMatrix(const Matrix4x3 &m) {
110 	double w[3][8];
111 
112 	for (int r = 0; r != 3; ++r) {
113 		for (int c = 0; c != 4; ++c) {
114 			w[r][c] = m(r, c);
115 			w[r][c+4] = (r == c) ? 1.0 : 0.0;
116 		}
117 	}
118 
119 	if (w[0][0] == 0.0) {
120 		if (w[1][0] != 0.0)
121 			swapRows(w[0], w[1]);
122 		else
123 			swapRows(w[0], w[2]);
124 	}
125 	divideRow(w[0], w[0][0]);
126 	subtractRow(w[1], w[1][0], w[0]);
127 	subtractRow(w[2], w[2][0], w[0]);
128 
129 	if (w[1][1] == 0.0)
130 		swapRows(w[1], w[2]);
131 
132 	divideRow(w[1], w[1][1]);
133 	subtractRow(w[0], w[0][1], w[1]);
134 	subtractRow(w[2], w[2][1], w[1]);
135 
136 	divideRow(w[2], w[2][2]);
137 	subtractRow(w[0], w[0][2], w[2]);
138 	subtractRow(w[1], w[1][2], w[2]);
139 
140 	for (int r = 0; r != 3; ++r) {
141 		w[r][7] = -w[r][3];
142 		w[r][3] = 0.0;
143 	}
144 
145 	Matrix4x3 result;
146 
147 	for (int r = 0; r != 3; ++r)
148 		for (int c = 0; c != 4; ++c)
149 			result(r, c) = float(w[r][c+4]);
150 
151 	return result;
152 }
153 
unknown()154 void Matrix4x3::unknown() {
155 	Matrix4x3 t;
156 
157 	// Transpose the 3x3 top left submatrix
158 	for (int r = 0; r != 3; ++r)
159 		for (int c = 0; c != 3; ++c)
160 			t(r, c) = _m[c][r];
161 
162 	t(0,3) = -(_m[0][3] * _m[0][0] + _m[1][3] * _m[1][0] + _m[2][3] * _m[2][0]);
163 	t(1,3) = -(_m[0][3] * _m[0][1] + _m[1][3] * _m[1][1] + _m[2][3] * _m[2][1]);
164 	t(2,3) = -(_m[0][3] * _m[0][2] + _m[1][3] * _m[1][2] + _m[2][3] * _m[2][2]);
165 
166 	*this = t;
167 }
168 
169 } // End of namespace BladeRunner
170