updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / MCview / MCMatrix.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package MCview;
20
21 public class MCMatrix {
22     float[][] matrix;
23     float[][] tmp;
24     float mycos;
25     float mysin;
26     float myconst = (float) (Math.PI / 180);
27
28     public MCMatrix(int rows, int cols) {
29         matrix = new float[rows][cols];
30         tmp = new float[rows][cols];
31     }
32
33     public void addElement(int i, int j, float value) {
34         matrix[i][j] = value;
35     }
36
37     public void rotatex(float degrees) {
38         mycos = (float) (Math.cos(degrees * myconst));
39         mysin = (float) (Math.sin(degrees * myconst));
40
41         tmp[0][0] = 1;
42         tmp[0][1] = 0;
43         tmp[0][2] = 0;
44         tmp[1][0] = 0;
45         tmp[1][1] = mycos;
46         tmp[1][2] = mysin;
47         tmp[2][0] = 0;
48         tmp[2][1] = -mysin;
49         tmp[2][2] = mycos;
50         preMultiply(tmp);
51     }
52
53     public void rotatez(float degrees) {
54         mycos = (float) (Math.cos(degrees * myconst));
55         mysin = (float) (Math.sin(degrees * myconst));
56
57         tmp[0][0] = mycos;
58         tmp[0][1] = -mysin;
59         tmp[0][2] = 0;
60         tmp[1][0] = mysin;
61         tmp[1][1] = mycos;
62         tmp[1][2] = 0;
63         tmp[2][0] = 0;
64         tmp[2][1] = 0;
65         tmp[2][2] = 1;
66
67         preMultiply(tmp);
68     }
69
70     public void rotatey(float degrees) {
71         mycos = (float) (Math.cos(degrees * myconst));
72         mysin = (float) (Math.sin(degrees * myconst));
73
74         tmp[0][0] = mycos;
75         tmp[0][1] = 0;
76         tmp[0][2] = -mysin;
77         tmp[1][0] = 0;
78         tmp[1][1] = 1;
79         tmp[1][2] = 0;
80         tmp[2][0] = mysin;
81         tmp[2][1] = 0;
82         tmp[2][2] = mycos;
83
84         preMultiply(tmp);
85     }
86
87     public float[] vectorMultiply(float[] vect) {
88         float[] temp = new float[3];
89
90         temp[0] = vect[0];
91         temp[1] = vect[1];
92         temp[2] = vect[2];
93
94         for (int i = 0; i < 3; i++) {
95             temp[i] = ((float) matrix[i][0] * vect[0]) +
96                 ((float) matrix[i][1] * vect[1]) +
97                 ((float) matrix[i][2] * vect[2]);
98         }
99
100         vect[0] = temp[0];
101         vect[1] = temp[1];
102         vect[2] = temp[2];
103
104         return vect;
105     }
106
107     public void preMultiply(float[][] mat) {
108         float[][] tmp = new float[3][3];
109
110         for (int i = 0; i < 3; i++) {
111             for (int j = 0; j < 3; j++) {
112                 tmp[i][j] = (mat[i][0] * matrix[0][j]) +
113                     (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);
114             }
115         }
116
117         for (int i = 0; i < 3; i++) {
118             for (int j = 0; j < 3; j++) {
119                 matrix[i][j] = tmp[i][j];
120             }
121         }
122     }
123
124     public void postMultiply(float[][] mat) {
125         float[][] tmp = new float[3][3];
126
127         for (int i = 0; i < 3; i++) {
128             for (int j = 0; j < 3; j++) {
129                 tmp[i][j] = (matrix[i][0] * mat[0][j]) +
130                     (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);
131             }
132         }
133
134         for (int i = 0; i < 3; i++) {
135             for (int j = 0; j < 3; j++) {
136                 matrix[i][j] = tmp[i][j];
137             }
138         }
139     }
140
141     public void setIdentity() {
142         matrix[0][0] = 1;
143         matrix[1][1] = 1;
144         matrix[2][2] = 1;
145         matrix[0][1] = 0;
146         matrix[0][2] = 0;
147         matrix[1][0] = 0;
148         matrix[1][2] = 0;
149         matrix[2][0] = 0;
150         matrix[2][1] = 0;
151     }
152 }