2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
29 public class RotatableMatrix
38 * Creates a new RotatableMatrix object.
45 public RotatableMatrix(int rows, int cols)
47 matrix = new float[rows][cols];
51 rot = new float[3][3];
64 public void addElement(int i, int j, float value)
74 System.out.println(matrix[0][0] + " " + matrix[0][1] + " "
77 System.out.println(matrix[1][0] + " " + matrix[1][1] + " "
80 System.out.println(matrix[2][0] + " " + matrix[2][1] + " "
92 public void rotate(float degrees, char axis)
94 float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
96 float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
100 rot[0][0] = (float) costheta;
102 rot[0][1] = (float) -sintheta;
104 rot[0][2] = (float) 0.0;
106 rot[1][0] = (float) sintheta;
108 rot[1][1] = (float) costheta;
110 rot[1][2] = (float) 0.0;
112 rot[2][0] = (float) 0.0;
114 rot[2][1] = (float) 0.0;
116 rot[2][2] = (float) 1.0;
123 rot[0][0] = (float) 1.0;
125 rot[0][1] = (float) 0.0;
127 rot[0][2] = (float) 0.0;
129 rot[1][0] = (float) 0.0;
131 rot[1][1] = (float) costheta;
133 rot[1][2] = (float) sintheta;
135 rot[2][0] = (float) 0.0;
137 rot[2][1] = (float) -sintheta;
139 rot[2][2] = (float) costheta;
146 rot[0][0] = (float) costheta;
148 rot[0][1] = (float) 0.0;
150 rot[0][2] = (float) -sintheta;
152 rot[1][0] = (float) 0.0;
154 rot[1][1] = (float) 1.0;
156 rot[1][2] = (float) 0.0;
158 rot[2][0] = (float) sintheta;
160 rot[2][1] = (float) 0.0;
162 rot[2][2] = (float) costheta;
174 * @return DOCUMENT ME!
176 public float[] vectorMultiply(float[] vect)
184 for (int i = 0; i < 3; i++)
186 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1])
187 + (matrix[i][2] * vect[2]);
205 public void preMultiply(float[][] mat)
207 float[][] tmp = new float[3][3];
209 for (int i = 0; i < 3; i++)
211 for (int j = 0; j < 3; j++)
213 tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
214 + (mat[i][2] * matrix[2][j]);
218 for (int i = 0; i < 3; i++)
220 for (int j = 0; j < 3; j++)
222 matrix[i][j] = tmp[i][j];
233 public void postMultiply(float[][] mat)
235 float[][] tmp = new float[3][3];
237 for (int i = 0; i < 3; i++)
239 for (int j = 0; j < 3; j++)
241 tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
242 + (matrix[i][2] * mat[2][j]);
246 for (int i = 0; i < 3; i++)
248 for (int j = 0; j < 3; j++)
250 matrix[i][j] = tmp[i][j];
260 public static void main(String[] args)
262 RotatableMatrix m = new RotatableMatrix(3, 3);
264 m.addElement(0, 0, 1);
266 m.addElement(0, 1, 0);
268 m.addElement(0, 2, 0);
270 m.addElement(1, 0, 0);
272 m.addElement(1, 1, 2);
274 m.addElement(1, 2, 0);
276 m.addElement(2, 0, 0);
278 m.addElement(2, 1, 0);
280 m.addElement(2, 2, 1);
284 RotatableMatrix n = new RotatableMatrix(3, 3);
286 n.addElement(0, 0, 2);
288 n.addElement(0, 1, 1);
290 n.addElement(0, 2, 1);
292 n.addElement(1, 0, 2);
294 n.addElement(1, 1, 1);
296 n.addElement(1, 2, 1);
298 n.addElement(2, 0, 2);
300 n.addElement(2, 1, 1);
302 n.addElement(2, 2, 1);
306 // m.postMultiply(n.matrix);
308 // m.rotate(45,'z',new RotatableMatrix(3,3));
309 float[] vect = new float[3];
317 vect = m.vectorMultiply(vect);
319 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
325 public void setIdentity()
327 matrix[0][0] = (float) 1.0;
329 matrix[1][1] = (float) 1.0;
331 matrix[2][2] = (float) 1.0;
333 matrix[0][1] = (float) 0.0;
335 matrix[0][2] = (float) 0.0;
337 matrix[1][0] = (float) 0.0;
339 matrix[1][2] = (float) 0.0;
341 matrix[2][0] = (float) 0.0;
343 matrix[2][1] = (float) 0.0;