2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
28 public class RotatableMatrix
35 * Creates a new RotatableMatrix object.
37 * @param rows DOCUMENT ME!
38 * @param cols DOCUMENT ME!
40 public RotatableMatrix(int rows, int cols)
42 matrix = new float[rows][cols];
46 rot = new float[3][3];
52 * @param i DOCUMENT ME!
53 * @param j DOCUMENT ME!
54 * @param value DOCUMENT ME!
56 public void addElement(int i, int j, float value)
66 System.out.println(matrix[0][0] + " " + matrix[0][1] + " " +
69 System.out.println(matrix[1][0] + " " + matrix[1][1] + " " +
72 System.out.println(matrix[2][0] + " " + matrix[2][1] + " " +
79 * @param degrees DOCUMENT ME!
80 * @param axis DOCUMENT ME!
82 public void rotate(float degrees, char axis)
84 float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
86 float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
90 rot[0][0] = (float) costheta;
92 rot[0][1] = (float) -sintheta;
94 rot[0][2] = (float) 0.0;
96 rot[1][0] = (float) sintheta;
98 rot[1][1] = (float) costheta;
100 rot[1][2] = (float) 0.0;
102 rot[2][0] = (float) 0.0;
104 rot[2][1] = (float) 0.0;
106 rot[2][2] = (float) 1.0;
113 rot[0][0] = (float) 1.0;
115 rot[0][1] = (float) 0.0;
117 rot[0][2] = (float) 0.0;
119 rot[1][0] = (float) 0.0;
121 rot[1][1] = (float) costheta;
123 rot[1][2] = (float) sintheta;
125 rot[2][0] = (float) 0.0;
127 rot[2][1] = (float) -sintheta;
129 rot[2][2] = (float) costheta;
136 rot[0][0] = (float) costheta;
138 rot[0][1] = (float) 0.0;
140 rot[0][2] = (float) -sintheta;
142 rot[1][0] = (float) 0.0;
144 rot[1][1] = (float) 1.0;
146 rot[1][2] = (float) 0.0;
148 rot[2][0] = (float) sintheta;
150 rot[2][1] = (float) 0.0;
152 rot[2][2] = (float) costheta;
161 * @param vect DOCUMENT ME!
163 * @return DOCUMENT ME!
165 public float[] vectorMultiply(float[] vect)
173 for (int i = 0; i < 3; i++)
175 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1]) +
176 (matrix[i][2] * vect[2]);
191 * @param mat DOCUMENT ME!
193 public void preMultiply(float[][] mat)
195 float[][] tmp = new float[3][3];
197 for (int i = 0; i < 3; i++)
199 for (int j = 0; j < 3; j++)
201 tmp[i][j] = (mat[i][0] * matrix[0][j]) +
202 (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);
206 for (int i = 0; i < 3; i++)
208 for (int j = 0; j < 3; j++)
210 matrix[i][j] = tmp[i][j];
218 * @param mat DOCUMENT ME!
220 public void postMultiply(float[][] mat)
222 float[][] tmp = new float[3][3];
224 for (int i = 0; i < 3; i++)
226 for (int j = 0; j < 3; j++)
228 tmp[i][j] = (matrix[i][0] * mat[0][j]) +
229 (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);
233 for (int i = 0; i < 3; i++)
235 for (int j = 0; j < 3; j++)
237 matrix[i][j] = tmp[i][j];
245 * @param args DOCUMENT ME!
247 public static void main(String[] args)
249 RotatableMatrix m = new RotatableMatrix(3, 3);
251 m.addElement(0, 0, 1);
253 m.addElement(0, 1, 0);
255 m.addElement(0, 2, 0);
257 m.addElement(1, 0, 0);
259 m.addElement(1, 1, 2);
261 m.addElement(1, 2, 0);
263 m.addElement(2, 0, 0);
265 m.addElement(2, 1, 0);
267 m.addElement(2, 2, 1);
271 RotatableMatrix n = new RotatableMatrix(3, 3);
273 n.addElement(0, 0, 2);
275 n.addElement(0, 1, 1);
277 n.addElement(0, 2, 1);
279 n.addElement(1, 0, 2);
281 n.addElement(1, 1, 1);
283 n.addElement(1, 2, 1);
285 n.addElement(2, 0, 2);
287 n.addElement(2, 1, 1);
289 n.addElement(2, 2, 1);
293 //m.postMultiply(n.matrix);
295 // m.rotate(45,'z',new RotatableMatrix(3,3));
296 float[] vect = new float[3];
304 vect = m.vectorMultiply(vect);
306 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
312 public void setIdentity()
314 matrix[0][0] = (float) 1.0;
316 matrix[1][1] = (float) 1.0;
318 matrix[2][2] = (float) 1.0;
320 matrix[0][1] = (float) 0.0;
322 matrix[0][2] = (float) 0.0;
324 matrix[1][0] = (float) 0.0;
326 matrix[1][2] = (float) 0.0;
328 matrix[2][0] = (float) 0.0;
330 matrix[2][1] = (float) 0.0;