2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
25 * @version $Revision$
\r
27 public class RotatableMatrix
\r
34 * Creates a new RotatableMatrix object.
\r
36 * @param rows DOCUMENT ME!
\r
37 * @param cols DOCUMENT ME!
\r
39 public RotatableMatrix(int rows, int cols)
\r
41 matrix = new float[rows][cols];
\r
43 temp = new float[3];
\r
45 rot = new float[3][3];
\r
51 * @param i DOCUMENT ME!
\r
52 * @param j DOCUMENT ME!
\r
53 * @param value DOCUMENT ME!
\r
55 public void addElement(int i, int j, float value)
\r
57 matrix[i][j] = value;
\r
65 System.out.println(matrix[0][0] + " " + matrix[0][1] + " " +
\r
68 System.out.println(matrix[1][0] + " " + matrix[1][1] + " " +
\r
71 System.out.println(matrix[2][0] + " " + matrix[2][1] + " " +
\r
78 * @param degrees DOCUMENT ME!
\r
79 * @param axis DOCUMENT ME!
\r
81 public void rotate(float degrees, char axis)
\r
83 float costheta = (float) Math.cos( (degrees * Math.PI) / (float) 180.0);
\r
85 float sintheta = (float) Math.sin( (degrees * Math.PI) / (float) 180.0);
\r
89 rot[0][0] = (float) costheta;
\r
91 rot[0][1] = (float) - sintheta;
\r
93 rot[0][2] = (float) 0.0;
\r
95 rot[1][0] = (float) sintheta;
\r
97 rot[1][1] = (float) costheta;
\r
99 rot[1][2] = (float) 0.0;
\r
101 rot[2][0] = (float) 0.0;
\r
103 rot[2][1] = (float) 0.0;
\r
105 rot[2][2] = (float) 1.0;
\r
112 rot[0][0] = (float) 1.0;
\r
114 rot[0][1] = (float) 0.0;
\r
116 rot[0][2] = (float) 0.0;
\r
118 rot[1][0] = (float) 0.0;
\r
120 rot[1][1] = (float) costheta;
\r
122 rot[1][2] = (float) sintheta;
\r
124 rot[2][0] = (float) 0.0;
\r
126 rot[2][1] = (float) - sintheta;
\r
128 rot[2][2] = (float) costheta;
\r
135 rot[0][0] = (float) costheta;
\r
137 rot[0][1] = (float) 0.0;
\r
139 rot[0][2] = (float) - sintheta;
\r
141 rot[1][0] = (float) 0.0;
\r
143 rot[1][1] = (float) 1.0;
\r
145 rot[1][2] = (float) 0.0;
\r
147 rot[2][0] = (float) sintheta;
\r
149 rot[2][1] = (float) 0.0;
\r
151 rot[2][2] = (float) costheta;
\r
160 * @param vect DOCUMENT ME!
\r
162 * @return DOCUMENT ME!
\r
164 public float[] vectorMultiply(float[] vect)
\r
172 for (int i = 0; i < 3; i++)
\r
174 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1]) +
\r
175 (matrix[i][2] * vect[2]);
\r
190 * @param mat DOCUMENT ME!
\r
192 public void preMultiply(float[][] mat)
\r
194 float[][] tmp = new float[3][3];
\r
196 for (int i = 0; i < 3; i++)
\r
198 for (int j = 0; j < 3; j++)
\r
200 tmp[i][j] = (mat[i][0] * matrix[0][j]) +
\r
201 (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);
\r
205 for (int i = 0; i < 3; i++)
\r
207 for (int j = 0; j < 3; j++)
\r
209 matrix[i][j] = tmp[i][j];
\r
217 * @param mat DOCUMENT ME!
\r
219 public void postMultiply(float[][] mat)
\r
221 float[][] tmp = new float[3][3];
\r
223 for (int i = 0; i < 3; i++)
\r
225 for (int j = 0; j < 3; j++)
\r
227 tmp[i][j] = (matrix[i][0] * mat[0][j]) +
\r
228 (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);
\r
232 for (int i = 0; i < 3; i++)
\r
234 for (int j = 0; j < 3; j++)
\r
236 matrix[i][j] = tmp[i][j];
\r
244 * @param args DOCUMENT ME!
\r
246 public static void main(String[] args)
\r
248 RotatableMatrix m = new RotatableMatrix(3, 3);
\r
250 m.addElement(0, 0, 1);
\r
252 m.addElement(0, 1, 0);
\r
254 m.addElement(0, 2, 0);
\r
256 m.addElement(1, 0, 0);
\r
258 m.addElement(1, 1, 2);
\r
260 m.addElement(1, 2, 0);
\r
262 m.addElement(2, 0, 0);
\r
264 m.addElement(2, 1, 0);
\r
266 m.addElement(2, 2, 1);
\r
270 RotatableMatrix n = new RotatableMatrix(3, 3);
\r
272 n.addElement(0, 0, 2);
\r
274 n.addElement(0, 1, 1);
\r
276 n.addElement(0, 2, 1);
\r
278 n.addElement(1, 0, 2);
\r
280 n.addElement(1, 1, 1);
\r
282 n.addElement(1, 2, 1);
\r
284 n.addElement(2, 0, 2);
\r
286 n.addElement(2, 1, 1);
\r
288 n.addElement(2, 2, 1);
\r
292 //m.postMultiply(n.matrix);
\r
294 // m.rotate(45,'z',new RotatableMatrix(3,3));
\r
295 float[] vect = new float[3];
\r
303 vect = m.vectorMultiply(vect);
\r
305 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
\r
311 public void setIdentity()
\r
313 matrix[0][0] = (float) 1.0;
\r
315 matrix[1][1] = (float) 1.0;
\r
317 matrix[2][2] = (float) 1.0;
\r
319 matrix[0][1] = (float) 0.0;
\r
321 matrix[0][2] = (float) 0.0;
\r
323 matrix[1][0] = (float) 0.0;
\r
325 matrix[1][2] = (float) 0.0;
\r
327 matrix[2][0] = (float) 0.0;
\r
329 matrix[2][1] = (float) 0.0;
\r