2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.math;
\r
26 * @version $Revision$
\r
28 public class RotatableMatrix
\r
35 * Creates a new RotatableMatrix object.
\r
37 * @param rows DOCUMENT ME!
\r
38 * @param cols DOCUMENT ME!
\r
40 public RotatableMatrix(int rows, int cols)
\r
42 matrix = new float[rows][cols];
\r
44 temp = new float[3];
\r
46 rot = new float[3][3];
\r
52 * @param i DOCUMENT ME!
\r
53 * @param j DOCUMENT ME!
\r
54 * @param value DOCUMENT ME!
\r
56 public void addElement(int i, int j, float value)
\r
58 matrix[i][j] = value;
\r
66 System.out.println(matrix[0][0] + " " + matrix[0][1] + " " +
\r
69 System.out.println(matrix[1][0] + " " + matrix[1][1] + " " +
\r
72 System.out.println(matrix[2][0] + " " + matrix[2][1] + " " +
\r
79 * @param degrees DOCUMENT ME!
\r
80 * @param axis DOCUMENT ME!
\r
82 public void rotate(float degrees, char axis)
\r
84 float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
\r
86 float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
\r
90 rot[0][0] = (float) costheta;
\r
92 rot[0][1] = (float) -sintheta;
\r
94 rot[0][2] = (float) 0.0;
\r
96 rot[1][0] = (float) sintheta;
\r
98 rot[1][1] = (float) costheta;
\r
100 rot[1][2] = (float) 0.0;
\r
102 rot[2][0] = (float) 0.0;
\r
104 rot[2][1] = (float) 0.0;
\r
106 rot[2][2] = (float) 1.0;
\r
113 rot[0][0] = (float) 1.0;
\r
115 rot[0][1] = (float) 0.0;
\r
117 rot[0][2] = (float) 0.0;
\r
119 rot[1][0] = (float) 0.0;
\r
121 rot[1][1] = (float) costheta;
\r
123 rot[1][2] = (float) sintheta;
\r
125 rot[2][0] = (float) 0.0;
\r
127 rot[2][1] = (float) -sintheta;
\r
129 rot[2][2] = (float) costheta;
\r
136 rot[0][0] = (float) costheta;
\r
138 rot[0][1] = (float) 0.0;
\r
140 rot[0][2] = (float) -sintheta;
\r
142 rot[1][0] = (float) 0.0;
\r
144 rot[1][1] = (float) 1.0;
\r
146 rot[1][2] = (float) 0.0;
\r
148 rot[2][0] = (float) sintheta;
\r
150 rot[2][1] = (float) 0.0;
\r
152 rot[2][2] = (float) costheta;
\r
161 * @param vect DOCUMENT ME!
\r
163 * @return DOCUMENT ME!
\r
165 public float[] vectorMultiply(float[] vect)
\r
173 for (int i = 0; i < 3; i++)
\r
175 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1]) +
\r
176 (matrix[i][2] * vect[2]);
\r
191 * @param mat DOCUMENT ME!
\r
193 public void preMultiply(float[][] mat)
\r
195 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
202 (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);
\r
206 for (int i = 0; i < 3; i++)
\r
208 for (int j = 0; j < 3; j++)
\r
210 matrix[i][j] = tmp[i][j];
\r
218 * @param mat DOCUMENT ME!
\r
220 public void postMultiply(float[][] mat)
\r
222 float[][] tmp = new float[3][3];
\r
224 for (int i = 0; i < 3; i++)
\r
226 for (int j = 0; j < 3; j++)
\r
228 tmp[i][j] = (matrix[i][0] * mat[0][j]) +
\r
229 (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);
\r
233 for (int i = 0; i < 3; i++)
\r
235 for (int j = 0; j < 3; j++)
\r
237 matrix[i][j] = tmp[i][j];
\r
245 * @param args DOCUMENT ME!
\r
247 public static void main(String[] args)
\r
249 RotatableMatrix m = new RotatableMatrix(3, 3);
\r
251 m.addElement(0, 0, 1);
\r
253 m.addElement(0, 1, 0);
\r
255 m.addElement(0, 2, 0);
\r
257 m.addElement(1, 0, 0);
\r
259 m.addElement(1, 1, 2);
\r
261 m.addElement(1, 2, 0);
\r
263 m.addElement(2, 0, 0);
\r
265 m.addElement(2, 1, 0);
\r
267 m.addElement(2, 2, 1);
\r
271 RotatableMatrix n = new RotatableMatrix(3, 3);
\r
273 n.addElement(0, 0, 2);
\r
275 n.addElement(0, 1, 1);
\r
277 n.addElement(0, 2, 1);
\r
279 n.addElement(1, 0, 2);
\r
281 n.addElement(1, 1, 1);
\r
283 n.addElement(1, 2, 1);
\r
285 n.addElement(2, 0, 2);
\r
287 n.addElement(2, 1, 1);
\r
289 n.addElement(2, 2, 1);
\r
293 //m.postMultiply(n.matrix);
\r
295 // m.rotate(45,'z',new RotatableMatrix(3,3));
\r
296 float[] vect = new float[3];
\r
304 vect = m.vectorMultiply(vect);
\r
306 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
\r
312 public void setIdentity()
\r
314 matrix[0][0] = (float) 1.0;
\r
316 matrix[1][1] = (float) 1.0;
\r
318 matrix[2][2] = (float) 1.0;
\r
320 matrix[0][1] = (float) 0.0;
\r
322 matrix[0][2] = (float) 0.0;
\r
324 matrix[1][0] = (float) 0.0;
\r
326 matrix[1][2] = (float) 0.0;
\r
328 matrix[2][0] = (float) 0.0;
\r
330 matrix[2][1] = (float) 0.0;
\r