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];
261 public static void main(String[] args)
263 RotatableMatrix m = new RotatableMatrix(3, 3);
265 m.addElement(0, 0, 1);
267 m.addElement(0, 1, 0);
269 m.addElement(0, 2, 0);
271 m.addElement(1, 0, 0);
273 m.addElement(1, 1, 2);
275 m.addElement(1, 2, 0);
277 m.addElement(2, 0, 0);
279 m.addElement(2, 1, 0);
281 m.addElement(2, 2, 1);
285 RotatableMatrix n = new RotatableMatrix(3, 3);
287 n.addElement(0, 0, 2);
289 n.addElement(0, 1, 1);
291 n.addElement(0, 2, 1);
293 n.addElement(1, 0, 2);
295 n.addElement(1, 1, 1);
297 n.addElement(1, 2, 1);
299 n.addElement(2, 0, 2);
301 n.addElement(2, 1, 1);
303 n.addElement(2, 2, 1);
307 // m.postMultiply(n.matrix);
309 // m.rotate(45,'z',new RotatableMatrix(3,3));
310 float[] vect = new float[3];
318 vect = m.vectorMultiply(vect);
320 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
326 public void setIdentity()
328 matrix[0][0] = (float) 1.0;
330 matrix[1][1] = (float) 1.0;
332 matrix[2][2] = (float) 1.0;
334 matrix[0][1] = (float) 0.0;
336 matrix[0][2] = (float) 0.0;
338 matrix[1][0] = (float) 0.0;
340 matrix[1][2] = (float) 0.0;
342 matrix[2][0] = (float) 0.0;
344 matrix[2][1] = (float) 0.0;