2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
26 public class RotatableMatrix
35 * Creates a new RotatableMatrix object.
42 public RotatableMatrix(int rows, int cols)
44 matrix = new float[rows][cols];
48 rot = new float[3][3];
61 public void addElement(int i, int j, float value)
71 System.out.println(matrix[0][0] + " " + matrix[0][1] + " "
74 System.out.println(matrix[1][0] + " " + matrix[1][1] + " "
77 System.out.println(matrix[2][0] + " " + matrix[2][1] + " "
89 public void rotate(float degrees, char axis)
91 float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
93 float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
97 rot[0][0] = (float) costheta;
99 rot[0][1] = (float) -sintheta;
101 rot[0][2] = (float) 0.0;
103 rot[1][0] = (float) sintheta;
105 rot[1][1] = (float) costheta;
107 rot[1][2] = (float) 0.0;
109 rot[2][0] = (float) 0.0;
111 rot[2][1] = (float) 0.0;
113 rot[2][2] = (float) 1.0;
120 rot[0][0] = (float) 1.0;
122 rot[0][1] = (float) 0.0;
124 rot[0][2] = (float) 0.0;
126 rot[1][0] = (float) 0.0;
128 rot[1][1] = (float) costheta;
130 rot[1][2] = (float) sintheta;
132 rot[2][0] = (float) 0.0;
134 rot[2][1] = (float) -sintheta;
136 rot[2][2] = (float) costheta;
143 rot[0][0] = (float) costheta;
145 rot[0][1] = (float) 0.0;
147 rot[0][2] = (float) -sintheta;
149 rot[1][0] = (float) 0.0;
151 rot[1][1] = (float) 1.0;
153 rot[1][2] = (float) 0.0;
155 rot[2][0] = (float) sintheta;
157 rot[2][1] = (float) 0.0;
159 rot[2][2] = (float) costheta;
171 * @return DOCUMENT ME!
173 public float[] vectorMultiply(float[] vect)
181 for (int i = 0; i < 3; i++)
183 temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1])
184 + (matrix[i][2] * vect[2]);
202 public void preMultiply(float[][] mat)
204 float[][] tmp = new float[3][3];
206 for (int i = 0; i < 3; i++)
208 for (int j = 0; j < 3; j++)
210 tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
211 + (mat[i][2] * matrix[2][j]);
215 for (int i = 0; i < 3; i++)
217 for (int j = 0; j < 3; j++)
219 matrix[i][j] = tmp[i][j];
230 public void postMultiply(float[][] mat)
232 float[][] tmp = new float[3][3];
234 for (int i = 0; i < 3; i++)
236 for (int j = 0; j < 3; j++)
238 tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
239 + (matrix[i][2] * mat[2][j]);
243 for (int i = 0; i < 3; i++)
245 for (int j = 0; j < 3; j++)
247 matrix[i][j] = tmp[i][j];
258 public static void main(String[] args)
260 RotatableMatrix m = new RotatableMatrix(3, 3);
262 m.addElement(0, 0, 1);
264 m.addElement(0, 1, 0);
266 m.addElement(0, 2, 0);
268 m.addElement(1, 0, 0);
270 m.addElement(1, 1, 2);
272 m.addElement(1, 2, 0);
274 m.addElement(2, 0, 0);
276 m.addElement(2, 1, 0);
278 m.addElement(2, 2, 1);
282 RotatableMatrix n = new RotatableMatrix(3, 3);
284 n.addElement(0, 0, 2);
286 n.addElement(0, 1, 1);
288 n.addElement(0, 2, 1);
290 n.addElement(1, 0, 2);
292 n.addElement(1, 1, 1);
294 n.addElement(1, 2, 1);
296 n.addElement(2, 0, 2);
298 n.addElement(2, 1, 1);
300 n.addElement(2, 2, 1);
304 // m.postMultiply(n.matrix);
306 // m.rotate(45,'z',new RotatableMatrix(3,3));
307 float[] vect = new float[3];
315 vect = m.vectorMultiply(vect);
317 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
323 public void setIdentity()
325 matrix[0][0] = (float) 1.0;
327 matrix[1][1] = (float) 1.0;
329 matrix[2][2] = (float) 1.0;
331 matrix[0][1] = (float) 0.0;
333 matrix[0][2] = (float) 0.0;
335 matrix[1][0] = (float) 0.0;
337 matrix[1][2] = (float) 0.0;
339 matrix[2][0] = (float) 0.0;
341 matrix[2][1] = (float) 0.0;