GPL license added
[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 \r
20 package jalview.math;\r
21 \r
22 \r
23 \r
24 public class RotatableMatrix {\r
25 \r
26   float matrix[][];\r
27 \r
28   float[] temp;\r
29 \r
30   float[][] rot;\r
31 \r
32 \r
33 \r
34   public RotatableMatrix(int rows, int cols) {\r
35 \r
36     matrix = new float[rows][cols];\r
37 \r
38     temp = new float[3];\r
39 \r
40     rot = new float[3][3];\r
41 \r
42   }\r
43 \r
44 \r
45 \r
46   public void addElement(int i, int j, float value) {\r
47 \r
48     matrix[i][j] = value;\r
49 \r
50   }\r
51 \r
52 \r
53 \r
54   public void print() {\r
55 \r
56     System.out.println(matrix[0][0] + " " + matrix[0][1] + " " + matrix[0][2]);\r
57 \r
58     System.out.println(matrix[1][0] + " " + matrix[1][1] + " " + matrix[1][2]);\r
59 \r
60     System.out.println(matrix[2][0] + " " + matrix[2][1] + " " + matrix[2][2]);\r
61 \r
62   }\r
63 \r
64 \r
65 \r
66   public void rotate (float degrees, char axis) {\r
67 \r
68 \r
69 \r
70     float costheta = (float)Math.cos(degrees*Math.PI/(float)180.0);\r
71 \r
72     float sintheta = (float)Math.sin(degrees*Math.PI/(float)180.0);\r
73 \r
74 \r
75 \r
76     if (axis == 'z') {\r
77 \r
78 \r
79 \r
80       rot[0][0] = (float)costheta;\r
81 \r
82       rot[0][1] = (float)-sintheta;\r
83 \r
84       rot[0][2] = (float)0.0;\r
85 \r
86 \r
87 \r
88       rot[1][0] = (float)sintheta;\r
89 \r
90       rot[1][1] = (float)costheta;\r
91 \r
92       rot[1][2] = (float)0.0;\r
93 \r
94 \r
95 \r
96       rot[2][0] = (float)0.0;\r
97 \r
98       rot[2][1] = (float)0.0;\r
99 \r
100       rot[2][2] = (float)1.0;\r
101 \r
102 \r
103 \r
104       preMultiply(rot);\r
105 \r
106     }\r
107 \r
108     if (axis == 'x') {\r
109 \r
110       rot[0][0] = (float)1.0;\r
111 \r
112       rot[0][1] = (float)0.0;\r
113 \r
114       rot[0][2] = (float)0.0;\r
115 \r
116 \r
117 \r
118       rot[1][0] = (float)0.0;\r
119 \r
120       rot[1][1] = (float)costheta;\r
121 \r
122       rot[1][2] = (float)sintheta;\r
123 \r
124 \r
125 \r
126       rot[2][0] = (float)0.0;\r
127 \r
128       rot[2][1] = (float)-sintheta;\r
129 \r
130       rot[2][2] = (float)costheta;\r
131 \r
132 \r
133 \r
134       preMultiply(rot);\r
135 \r
136 \r
137 \r
138     }\r
139 \r
140     if (axis == 'y') {\r
141 \r
142       rot[0][0] = (float)costheta;\r
143 \r
144       rot[0][1] = (float)0.0;\r
145 \r
146       rot[0][2] = (float)-sintheta;\r
147 \r
148 \r
149 \r
150       rot[1][0] = (float)0.0;\r
151 \r
152       rot[1][1] = (float)1.0;\r
153 \r
154       rot[1][2] = (float)0.0;\r
155 \r
156 \r
157 \r
158       rot[2][0] = (float)sintheta;\r
159 \r
160       rot[2][1] = (float)0.0;\r
161 \r
162       rot[2][2] = (float)costheta;\r
163 \r
164 \r
165 \r
166       preMultiply(rot);\r
167 \r
168 \r
169 \r
170     }\r
171 \r
172 \r
173 \r
174   }\r
175 \r
176 \r
177 \r
178   public float[] vectorMultiply(float[] vect) {\r
179 \r
180     temp[0] = vect[0];\r
181 \r
182     temp[1] = vect[1];\r
183 \r
184     temp[2] = vect[2];\r
185 \r
186 \r
187 \r
188     for (int i = 0; i < 3; i++) {\r
189 \r
190       temp[i] = matrix[i][0]*vect[0] + matrix[i][1]*vect[1] + matrix[i][2]*vect[2];\r
191 \r
192     }\r
193 \r
194 \r
195 \r
196     vect[0] = temp[0];\r
197 \r
198     vect[1] = temp[1];\r
199 \r
200     vect[2] = temp[2];\r
201 \r
202 \r
203 \r
204     return vect;\r
205 \r
206   }\r
207 \r
208 \r
209 \r
210   public void preMultiply(float mat[][]) {\r
211 \r
212     float tmp[][]  = new float[3][3];\r
213 \r
214 \r
215 \r
216     for (int i = 0; i < 3 ; i++) {\r
217 \r
218       for (int j = 0; j < 3; j++ ) {\r
219 \r
220         tmp[i][j] = mat[i][0]*matrix[0][j] +\r
221 \r
222                     mat[i][1]*matrix[1][j] +\r
223 \r
224                     mat[i][2]*matrix[2][j];\r
225 \r
226       }\r
227 \r
228     }\r
229 \r
230 \r
231 \r
232     for (int i = 0; i < 3 ; i++) {\r
233 \r
234       for (int j = 0; j < 3; j++ ) {\r
235 \r
236         matrix[i][j] = tmp[i][j];\r
237 \r
238       }\r
239 \r
240     }\r
241 \r
242   }\r
243 \r
244 \r
245 \r
246   public void postMultiply(float mat[][]) {\r
247 \r
248     float tmp[][]  = new float[3][3];\r
249 \r
250 \r
251 \r
252     for (int i = 0; i < 3 ; i++) {\r
253 \r
254       for (int j = 0; j < 3; j++ ) {\r
255 \r
256         tmp[i][j] = matrix[i][0]*mat[0][j] +\r
257 \r
258                     matrix[i][1]*mat[1][j] +\r
259 \r
260                     matrix[i][2]*mat[2][j];\r
261 \r
262       }\r
263 \r
264     }\r
265 \r
266 \r
267 \r
268     for (int i = 0; i < 3 ; i++) {\r
269 \r
270       for (int j = 0; j < 3; j++ ) {\r
271 \r
272         matrix[i][j] = tmp[i][j];\r
273 \r
274       }\r
275 \r
276     }\r
277 \r
278   }\r
279 \r
280 \r
281 \r
282   public static void main(String[] args) {\r
283 \r
284 \r
285 \r
286     RotatableMatrix m = new  RotatableMatrix(3,3);\r
287 \r
288     m.addElement(0,0,1);\r
289 \r
290     m.addElement(0,1,0);\r
291 \r
292     m.addElement(0,2,0);\r
293 \r
294     m.addElement(1,0,0);\r
295 \r
296     m.addElement(1,1,2);\r
297 \r
298     m.addElement(1,2,0);\r
299 \r
300     m.addElement(2,0,0);\r
301 \r
302     m.addElement(2,1,0);\r
303 \r
304     m.addElement(2,2,1);\r
305 \r
306 \r
307 \r
308     m.print();\r
309 \r
310 \r
311 \r
312     RotatableMatrix n = new  RotatableMatrix(3,3);\r
313 \r
314     n.addElement(0,0,2);\r
315 \r
316     n.addElement(0,1,1);\r
317 \r
318     n.addElement(0,2,1);\r
319 \r
320     n.addElement(1,0,2);\r
321 \r
322     n.addElement(1,1,1);\r
323 \r
324     n.addElement(1,2,1);\r
325 \r
326     n.addElement(2,0,2);\r
327 \r
328     n.addElement(2,1,1);\r
329 \r
330     n.addElement(2,2,1);\r
331 \r
332 \r
333 \r
334     n.print();\r
335 \r
336 \r
337 \r
338     //m.postMultiply(n.matrix);\r
339 \r
340     //m.print();\r
341 \r
342     //     m.rotate(45,'z',new RotatableMatrix(3,3));\r
343 \r
344 \r
345 \r
346     float vect[] = new float[3];\r
347 \r
348     vect[0] = 2;\r
349 \r
350     vect[1] = 4;\r
351 \r
352     vect[2] = 6;\r
353 \r
354 \r
355 \r
356     vect = m.vectorMultiply(vect);\r
357 \r
358     System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);\r
359 \r
360 \r
361 \r
362   }\r
363 \r
364   public void setIdentity() {\r
365 \r
366     matrix[0][0] = (float)1.0;\r
367 \r
368     matrix[1][1] = (float)1.0;\r
369 \r
370     matrix[2][2] = (float)1.0;\r
371 \r
372     matrix[0][1] = (float)0.0;\r
373 \r
374     matrix[0][2] = (float)0.0;\r
375 \r
376     matrix[1][0] = (float)0.0;\r
377 \r
378     matrix[1][2] = (float) 0.0;\r
379 \r
380     matrix[2][0] = (float)0.0;\r
381 \r
382     matrix[2][1] = (float)0.0;\r
383 \r
384   }\r
385 \r
386 }\r
387 \r
388 \r
389 \r