c0ec1c98abc4719ede8489e2c6a2c7f51e80cc2c
[jalview.git] / src / jalview / math / RotatableMatrix.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\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
9 *\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
14 *\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
18 */\r
19 package jalview.math;\r
20 \r
21 public class RotatableMatrix {\r
22     float[][] matrix;\r
23     float[] temp;\r
24     float[][] rot;\r
25 \r
26     public RotatableMatrix(int rows, int cols) {\r
27         matrix = new float[rows][cols];\r
28 \r
29         temp = new float[3];\r
30 \r
31         rot = new float[3][3];\r
32     }\r
33 \r
34     public void addElement(int i, int j, float value) {\r
35         matrix[i][j] = value;\r
36     }\r
37 \r
38     public void print() {\r
39         System.out.println(matrix[0][0] + " " + matrix[0][1] + " " +\r
40             matrix[0][2]);\r
41 \r
42         System.out.println(matrix[1][0] + " " + matrix[1][1] + " " +\r
43             matrix[1][2]);\r
44 \r
45         System.out.println(matrix[2][0] + " " + matrix[2][1] + " " +\r
46             matrix[2][2]);\r
47     }\r
48 \r
49     public void rotate(float degrees, char axis) {\r
50         float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);\r
51 \r
52         float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);\r
53 \r
54         if (axis == 'z') {\r
55             rot[0][0] = (float) costheta;\r
56 \r
57             rot[0][1] = (float) -sintheta;\r
58 \r
59             rot[0][2] = (float) 0.0;\r
60 \r
61             rot[1][0] = (float) sintheta;\r
62 \r
63             rot[1][1] = (float) costheta;\r
64 \r
65             rot[1][2] = (float) 0.0;\r
66 \r
67             rot[2][0] = (float) 0.0;\r
68 \r
69             rot[2][1] = (float) 0.0;\r
70 \r
71             rot[2][2] = (float) 1.0;\r
72 \r
73             preMultiply(rot);\r
74         }\r
75 \r
76         if (axis == 'x') {\r
77             rot[0][0] = (float) 1.0;\r
78 \r
79             rot[0][1] = (float) 0.0;\r
80 \r
81             rot[0][2] = (float) 0.0;\r
82 \r
83             rot[1][0] = (float) 0.0;\r
84 \r
85             rot[1][1] = (float) costheta;\r
86 \r
87             rot[1][2] = (float) sintheta;\r
88 \r
89             rot[2][0] = (float) 0.0;\r
90 \r
91             rot[2][1] = (float) -sintheta;\r
92 \r
93             rot[2][2] = (float) costheta;\r
94 \r
95             preMultiply(rot);\r
96         }\r
97 \r
98         if (axis == 'y') {\r
99             rot[0][0] = (float) costheta;\r
100 \r
101             rot[0][1] = (float) 0.0;\r
102 \r
103             rot[0][2] = (float) -sintheta;\r
104 \r
105             rot[1][0] = (float) 0.0;\r
106 \r
107             rot[1][1] = (float) 1.0;\r
108 \r
109             rot[1][2] = (float) 0.0;\r
110 \r
111             rot[2][0] = (float) sintheta;\r
112 \r
113             rot[2][1] = (float) 0.0;\r
114 \r
115             rot[2][2] = (float) costheta;\r
116 \r
117             preMultiply(rot);\r
118         }\r
119     }\r
120 \r
121     public float[] vectorMultiply(float[] vect) {\r
122         temp[0] = vect[0];\r
123 \r
124         temp[1] = vect[1];\r
125 \r
126         temp[2] = vect[2];\r
127 \r
128         for (int i = 0; i < 3; i++) {\r
129             temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1]) +\r
130                 (matrix[i][2] * vect[2]);\r
131         }\r
132 \r
133         vect[0] = temp[0];\r
134 \r
135         vect[1] = temp[1];\r
136 \r
137         vect[2] = temp[2];\r
138 \r
139         return vect;\r
140     }\r
141 \r
142     public void preMultiply(float[][] mat) {\r
143         float[][] tmp = new float[3][3];\r
144 \r
145         for (int i = 0; i < 3; i++) {\r
146             for (int j = 0; j < 3; j++) {\r
147                 tmp[i][j] = (mat[i][0] * matrix[0][j]) +\r
148                     (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);\r
149             }\r
150         }\r
151 \r
152         for (int i = 0; i < 3; i++) {\r
153             for (int j = 0; j < 3; j++) {\r
154                 matrix[i][j] = tmp[i][j];\r
155             }\r
156         }\r
157     }\r
158 \r
159     public void postMultiply(float[][] mat) {\r
160         float[][] tmp = new float[3][3];\r
161 \r
162         for (int i = 0; i < 3; i++) {\r
163             for (int j = 0; j < 3; j++) {\r
164                 tmp[i][j] = (matrix[i][0] * mat[0][j]) +\r
165                     (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);\r
166             }\r
167         }\r
168 \r
169         for (int i = 0; i < 3; i++) {\r
170             for (int j = 0; j < 3; j++) {\r
171                 matrix[i][j] = tmp[i][j];\r
172             }\r
173         }\r
174     }\r
175 \r
176     public static void main(String[] args) {\r
177         RotatableMatrix m = new RotatableMatrix(3, 3);\r
178 \r
179         m.addElement(0, 0, 1);\r
180 \r
181         m.addElement(0, 1, 0);\r
182 \r
183         m.addElement(0, 2, 0);\r
184 \r
185         m.addElement(1, 0, 0);\r
186 \r
187         m.addElement(1, 1, 2);\r
188 \r
189         m.addElement(1, 2, 0);\r
190 \r
191         m.addElement(2, 0, 0);\r
192 \r
193         m.addElement(2, 1, 0);\r
194 \r
195         m.addElement(2, 2, 1);\r
196 \r
197         m.print();\r
198 \r
199         RotatableMatrix n = new RotatableMatrix(3, 3);\r
200 \r
201         n.addElement(0, 0, 2);\r
202 \r
203         n.addElement(0, 1, 1);\r
204 \r
205         n.addElement(0, 2, 1);\r
206 \r
207         n.addElement(1, 0, 2);\r
208 \r
209         n.addElement(1, 1, 1);\r
210 \r
211         n.addElement(1, 2, 1);\r
212 \r
213         n.addElement(2, 0, 2);\r
214 \r
215         n.addElement(2, 1, 1);\r
216 \r
217         n.addElement(2, 2, 1);\r
218 \r
219         n.print();\r
220 \r
221         //m.postMultiply(n.matrix);\r
222         //m.print();\r
223         //     m.rotate(45,'z',new RotatableMatrix(3,3));\r
224         float[] vect = new float[3];\r
225 \r
226         vect[0] = 2;\r
227 \r
228         vect[1] = 4;\r
229 \r
230         vect[2] = 6;\r
231 \r
232         vect = m.vectorMultiply(vect);\r
233 \r
234         System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);\r
235     }\r
236 \r
237     public void setIdentity() {\r
238         matrix[0][0] = (float) 1.0;\r
239 \r
240         matrix[1][1] = (float) 1.0;\r
241 \r
242         matrix[2][2] = (float) 1.0;\r
243 \r
244         matrix[0][1] = (float) 0.0;\r
245 \r
246         matrix[0][2] = (float) 0.0;\r
247 \r
248         matrix[1][0] = (float) 0.0;\r
249 \r
250         matrix[1][2] = (float) 0.0;\r
251 \r
252         matrix[2][0] = (float) 0.0;\r
253 \r
254         matrix[2][1] = (float) 0.0;\r
255     }\r
256 }\r