2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 public class RotatableMatrix
36 * Creates a new RotatableMatrix object.
43 public RotatableMatrix(int rows, int cols)
45 matrix = new float[rows][cols];
49 rot = new float[3][3];
62 public void addElement(int i, int j, float value)
72 System.out.println(matrix[0][0] + " " + matrix[0][1] + " "
75 System.out.println(matrix[1][0] + " " + matrix[1][1] + " "
78 System.out.println(matrix[2][0] + " " + matrix[2][1] + " "
90 public void rotate(float degrees, char axis)
92 float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
94 float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
98 rot[0][0] = (float) costheta;
100 rot[0][1] = (float) -sintheta;
102 rot[0][2] = (float) 0.0;
104 rot[1][0] = (float) sintheta;
106 rot[1][1] = (float) costheta;
108 rot[1][2] = (float) 0.0;
110 rot[2][0] = (float) 0.0;
112 rot[2][1] = (float) 0.0;
114 rot[2][2] = (float) 1.0;
121 rot[0][0] = (float) 1.0;
123 rot[0][1] = (float) 0.0;
125 rot[0][2] = (float) 0.0;
127 rot[1][0] = (float) 0.0;
129 rot[1][1] = (float) costheta;
131 rot[1][2] = (float) sintheta;
133 rot[2][0] = (float) 0.0;
135 rot[2][1] = (float) -sintheta;
137 rot[2][2] = (float) costheta;
144 rot[0][0] = (float) costheta;
146 rot[0][1] = (float) 0.0;
148 rot[0][2] = (float) -sintheta;
150 rot[1][0] = (float) 0.0;
152 rot[1][1] = (float) 1.0;
154 rot[1][2] = (float) 0.0;
156 rot[2][0] = (float) sintheta;
158 rot[2][1] = (float) 0.0;
160 rot[2][2] = (float) costheta;
172 * @return DOCUMENT ME!
174 public float[] vectorMultiply(float[] vect)
182 for (int i = 0; i < 3; i++)
184 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1])
185 + (matrix[i][2] * vect[2]);
203 public void preMultiply(float[][] mat)
205 float[][] tmp = new float[3][3];
207 for (int i = 0; i < 3; i++)
209 for (int j = 0; j < 3; j++)
211 tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
212 + (mat[i][2] * matrix[2][j]);
216 for (int i = 0; i < 3; i++)
218 for (int j = 0; j < 3; j++)
220 matrix[i][j] = tmp[i][j];
231 public void postMultiply(float[][] mat)
233 float[][] tmp = new float[3][3];
235 for (int i = 0; i < 3; i++)
237 for (int j = 0; j < 3; j++)
239 tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
240 + (matrix[i][2] * mat[2][j]);
244 for (int i = 0; i < 3; i++)
246 for (int j = 0; j < 3; j++)
248 matrix[i][j] = tmp[i][j];
259 public static void main(String[] args)
261 RotatableMatrix m = new RotatableMatrix(3, 3);
263 m.addElement(0, 0, 1);
265 m.addElement(0, 1, 0);
267 m.addElement(0, 2, 0);
269 m.addElement(1, 0, 0);
271 m.addElement(1, 1, 2);
273 m.addElement(1, 2, 0);
275 m.addElement(2, 0, 0);
277 m.addElement(2, 1, 0);
279 m.addElement(2, 2, 1);
283 RotatableMatrix n = new RotatableMatrix(3, 3);
285 n.addElement(0, 0, 2);
287 n.addElement(0, 1, 1);
289 n.addElement(0, 2, 1);
291 n.addElement(1, 0, 2);
293 n.addElement(1, 1, 1);
295 n.addElement(1, 2, 1);
297 n.addElement(2, 0, 2);
299 n.addElement(2, 1, 1);
301 n.addElement(2, 2, 1);
305 // m.postMultiply(n.matrix);
307 // m.rotate(45,'z',new RotatableMatrix(3,3));
308 float[] vect = new float[3];
316 vect = m.vectorMultiply(vect);
318 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
324 public void setIdentity()
326 matrix[0][0] = (float) 1.0;
328 matrix[1][1] = (float) 1.0;
330 matrix[2][2] = (float) 1.0;
332 matrix[0][1] = (float) 0.0;
334 matrix[0][2] = (float) 0.0;
336 matrix[1][0] = (float) 0.0;
338 matrix[1][2] = (float) 0.0;
340 matrix[2][0] = (float) 0.0;
342 matrix[2][1] = (float) 0.0;