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
20 package jalview.math;
\r
24 public class RotatableMatrix {
\r
34 public RotatableMatrix(int rows, int cols) {
\r
36 matrix = new float[rows][cols];
\r
38 temp = new float[3];
\r
40 rot = new float[3][3];
\r
46 public void addElement(int i, int j, float value) {
\r
48 matrix[i][j] = value;
\r
54 public void print() {
\r
56 System.out.println(matrix[0][0] + " " + matrix[0][1] + " " + matrix[0][2]);
\r
58 System.out.println(matrix[1][0] + " " + matrix[1][1] + " " + matrix[1][2]);
\r
60 System.out.println(matrix[2][0] + " " + matrix[2][1] + " " + matrix[2][2]);
\r
66 public void rotate (float degrees, char axis) {
\r
70 float costheta = (float)Math.cos(degrees*Math.PI/(float)180.0);
\r
72 float sintheta = (float)Math.sin(degrees*Math.PI/(float)180.0);
\r
80 rot[0][0] = (float)costheta;
\r
82 rot[0][1] = (float)-sintheta;
\r
84 rot[0][2] = (float)0.0;
\r
88 rot[1][0] = (float)sintheta;
\r
90 rot[1][1] = (float)costheta;
\r
92 rot[1][2] = (float)0.0;
\r
96 rot[2][0] = (float)0.0;
\r
98 rot[2][1] = (float)0.0;
\r
100 rot[2][2] = (float)1.0;
\r
110 rot[0][0] = (float)1.0;
\r
112 rot[0][1] = (float)0.0;
\r
114 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
126 rot[2][0] = (float)0.0;
\r
128 rot[2][1] = (float)-sintheta;
\r
130 rot[2][2] = (float)costheta;
\r
142 rot[0][0] = (float)costheta;
\r
144 rot[0][1] = (float)0.0;
\r
146 rot[0][2] = (float)-sintheta;
\r
150 rot[1][0] = (float)0.0;
\r
152 rot[1][1] = (float)1.0;
\r
154 rot[1][2] = (float)0.0;
\r
158 rot[2][0] = (float)sintheta;
\r
160 rot[2][1] = (float)0.0;
\r
162 rot[2][2] = (float)costheta;
\r
178 public float[] vectorMultiply(float[] vect) {
\r
188 for (int i = 0; i < 3; i++) {
\r
190 temp[i] = matrix[i][0]*vect[0] + matrix[i][1]*vect[1] + matrix[i][2]*vect[2];
\r
210 public void preMultiply(float mat[][]) {
\r
212 float tmp[][] = new float[3][3];
\r
216 for (int i = 0; i < 3 ; i++) {
\r
218 for (int j = 0; j < 3; j++ ) {
\r
220 tmp[i][j] = mat[i][0]*matrix[0][j] +
\r
222 mat[i][1]*matrix[1][j] +
\r
224 mat[i][2]*matrix[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
246 public void postMultiply(float mat[][]) {
\r
248 float tmp[][] = new float[3][3];
\r
252 for (int i = 0; i < 3 ; i++) {
\r
254 for (int j = 0; j < 3; j++ ) {
\r
256 tmp[i][j] = matrix[i][0]*mat[0][j] +
\r
258 matrix[i][1]*mat[1][j] +
\r
260 matrix[i][2]*mat[2][j];
\r
268 for (int i = 0; i < 3 ; i++) {
\r
270 for (int j = 0; j < 3; j++ ) {
\r
272 matrix[i][j] = tmp[i][j];
\r
282 public static void main(String[] args) {
\r
286 RotatableMatrix m = new RotatableMatrix(3,3);
\r
288 m.addElement(0,0,1);
\r
290 m.addElement(0,1,0);
\r
292 m.addElement(0,2,0);
\r
294 m.addElement(1,0,0);
\r
296 m.addElement(1,1,2);
\r
298 m.addElement(1,2,0);
\r
300 m.addElement(2,0,0);
\r
302 m.addElement(2,1,0);
\r
304 m.addElement(2,2,1);
\r
312 RotatableMatrix n = new RotatableMatrix(3,3);
\r
314 n.addElement(0,0,2);
\r
316 n.addElement(0,1,1);
\r
318 n.addElement(0,2,1);
\r
320 n.addElement(1,0,2);
\r
322 n.addElement(1,1,1);
\r
324 n.addElement(1,2,1);
\r
326 n.addElement(2,0,2);
\r
328 n.addElement(2,1,1);
\r
330 n.addElement(2,2,1);
\r
338 //m.postMultiply(n.matrix);
\r
342 // m.rotate(45,'z',new RotatableMatrix(3,3));
\r
346 float vect[] = new float[3];
\r
356 vect = m.vectorMultiply(vect);
\r
358 System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
\r
364 public void setIdentity() {
\r
366 matrix[0][0] = (float)1.0;
\r
368 matrix[1][1] = (float)1.0;
\r
370 matrix[2][2] = (float)1.0;
\r
372 matrix[0][1] = (float)0.0;
\r
374 matrix[0][2] = (float)0.0;
\r
376 matrix[1][0] = (float)0.0;
\r
378 matrix[1][2] = (float) 0.0;
\r
380 matrix[2][0] = (float)0.0;
\r
382 matrix[2][1] = (float)0.0;
\r