1 package jalview.math;
\r
5 public class RotatableMatrix {
\r
15 public RotatableMatrix(int rows, int cols) {
\r
17 matrix = new float[rows][cols];
\r
19 temp = new float[3];
\r
21 rot = new float[3][3];
\r
27 public void addElement(int i, int j, float value) {
\r
29 matrix[i][j] = value;
\r
35 public void print() {
\r
37 System.out.println(matrix[0][0] + " " + matrix[0][1] + " " + matrix[0][2]);
\r
39 System.out.println(matrix[1][0] + " " + matrix[1][1] + " " + matrix[1][2]);
\r
41 System.out.println(matrix[2][0] + " " + matrix[2][1] + " " + matrix[2][2]);
\r
47 public void rotate (float degrees, char axis) {
\r
51 float costheta = (float)Math.cos(degrees*Math.PI/(float)180.0);
\r
53 float sintheta = (float)Math.sin(degrees*Math.PI/(float)180.0);
\r
61 rot[0][0] = (float)costheta;
\r
63 rot[0][1] = (float)-sintheta;
\r
65 rot[0][2] = (float)0.0;
\r
69 rot[1][0] = (float)sintheta;
\r
71 rot[1][1] = (float)costheta;
\r
73 rot[1][2] = (float)0.0;
\r
77 rot[2][0] = (float)0.0;
\r
79 rot[2][1] = (float)0.0;
\r
81 rot[2][2] = (float)1.0;
\r
91 rot[0][0] = (float)1.0;
\r
93 rot[0][1] = (float)0.0;
\r
95 rot[0][2] = (float)0.0;
\r
99 rot[1][0] = (float)0.0;
\r
101 rot[1][1] = (float)costheta;
\r
103 rot[1][2] = (float)sintheta;
\r
107 rot[2][0] = (float)0.0;
\r
109 rot[2][1] = (float)-sintheta;
\r
111 rot[2][2] = (float)costheta;
\r
123 rot[0][0] = (float)costheta;
\r
125 rot[0][1] = (float)0.0;
\r
127 rot[0][2] = (float)-sintheta;
\r
131 rot[1][0] = (float)0.0;
\r
133 rot[1][1] = (float)1.0;
\r
135 rot[1][2] = (float)0.0;
\r
139 rot[2][0] = (float)sintheta;
\r
141 rot[2][1] = (float)0.0;
\r
143 rot[2][2] = (float)costheta;
\r
159 public float[] vectorMultiply(float[] vect) {
\r
169 for (int i = 0; i < 3; i++) {
\r
171 temp[i] = matrix[i][0]*vect[0] + matrix[i][1]*vect[1] + matrix[i][2]*vect[2];
\r
191 public void preMultiply(float mat[][]) {
\r
193 float tmp[][] = new float[3][3];
\r
197 for (int i = 0; i < 3 ; i++) {
\r
199 for (int j = 0; j < 3; j++ ) {
\r
201 tmp[i][j] = mat[i][0]*matrix[0][j] +
\r
203 mat[i][1]*matrix[1][j] +
\r
205 mat[i][2]*matrix[2][j];
\r
213 for (int i = 0; i < 3 ; i++) {
\r
215 for (int j = 0; j < 3; j++ ) {
\r
217 matrix[i][j] = tmp[i][j];
\r
227 public void postMultiply(float mat[][]) {
\r
229 float tmp[][] = new float[3][3];
\r
233 for (int i = 0; i < 3 ; i++) {
\r
235 for (int j = 0; j < 3; j++ ) {
\r
237 tmp[i][j] = matrix[i][0]*mat[0][j] +
\r
239 matrix[i][1]*mat[1][j] +
\r
241 matrix[i][2]*mat[2][j];
\r
249 for (int i = 0; i < 3 ; i++) {
\r
251 for (int j = 0; j < 3; j++ ) {
\r
253 matrix[i][j] = tmp[i][j];
\r
263 public static void main(String[] args) {
\r
267 RotatableMatrix m = new RotatableMatrix(3,3);
\r
269 m.addElement(0,0,1);
\r
271 m.addElement(0,1,0);
\r
273 m.addElement(0,2,0);
\r
275 m.addElement(1,0,0);
\r
277 m.addElement(1,1,2);
\r
279 m.addElement(1,2,0);
\r
281 m.addElement(2,0,0);
\r
283 m.addElement(2,1,0);
\r
285 m.addElement(2,2,1);
\r
293 RotatableMatrix n = new RotatableMatrix(3,3);
\r
295 n.addElement(0,0,2);
\r
297 n.addElement(0,1,1);
\r
299 n.addElement(0,2,1);
\r
301 n.addElement(1,0,2);
\r
303 n.addElement(1,1,1);
\r
305 n.addElement(1,2,1);
\r
307 n.addElement(2,0,2);
\r
309 n.addElement(2,1,1);
\r
311 n.addElement(2,2,1);
\r
319 //m.postMultiply(n.matrix);
\r
323 // m.rotate(45,'z',new RotatableMatrix(3,3));
\r
327 float vect[] = new float[3];
\r
337 vect = m.vectorMultiply(vect);
\r
339 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
\r
345 public void setIdentity() {
\r
347 matrix[0][0] = (float)1.0;
\r
349 matrix[1][1] = (float)1.0;
\r
351 matrix[2][2] = (float)1.0;
\r
353 matrix[0][1] = (float)0.0;
\r
355 matrix[0][2] = (float)0.0;
\r
357 matrix[1][0] = (float)0.0;
\r
359 matrix[1][2] = (float) 0.0;
\r
361 matrix[2][0] = (float)0.0;
\r
363 matrix[2][1] = (float)0.0;
\r