update author list in license for (JAL-826)
[jalview.git] / src / jalview / math / RotatableMatrix.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  * 
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.math;
19
20 /**
21  * DOCUMENT ME!
22  * 
23  * @author $author$
24  * @version $Revision$
25  */
26 public class RotatableMatrix
27 {
28   float[][] matrix;
29
30   float[] temp;
31
32   float[][] rot;
33
34   /**
35    * Creates a new RotatableMatrix object.
36    * 
37    * @param rows
38    *          DOCUMENT ME!
39    * @param cols
40    *          DOCUMENT ME!
41    */
42   public RotatableMatrix(int rows, int cols)
43   {
44     matrix = new float[rows][cols];
45
46     temp = new float[3];
47
48     rot = new float[3][3];
49   }
50
51   /**
52    * DOCUMENT ME!
53    * 
54    * @param i
55    *          DOCUMENT ME!
56    * @param j
57    *          DOCUMENT ME!
58    * @param value
59    *          DOCUMENT ME!
60    */
61   public void addElement(int i, int j, float value)
62   {
63     matrix[i][j] = value;
64   }
65
66   /**
67    * DOCUMENT ME!
68    */
69   public void print()
70   {
71     System.out.println(matrix[0][0] + " " + matrix[0][1] + " "
72             + matrix[0][2]);
73
74     System.out.println(matrix[1][0] + " " + matrix[1][1] + " "
75             + matrix[1][2]);
76
77     System.out.println(matrix[2][0] + " " + matrix[2][1] + " "
78             + matrix[2][2]);
79   }
80
81   /**
82    * DOCUMENT ME!
83    * 
84    * @param degrees
85    *          DOCUMENT ME!
86    * @param axis
87    *          DOCUMENT ME!
88    */
89   public void rotate(float degrees, char axis)
90   {
91     float costheta = (float) Math.cos((degrees * Math.PI) / (float) 180.0);
92
93     float sintheta = (float) Math.sin((degrees * Math.PI) / (float) 180.0);
94
95     if (axis == 'z')
96     {
97       rot[0][0] = (float) costheta;
98
99       rot[0][1] = (float) -sintheta;
100
101       rot[0][2] = (float) 0.0;
102
103       rot[1][0] = (float) sintheta;
104
105       rot[1][1] = (float) costheta;
106
107       rot[1][2] = (float) 0.0;
108
109       rot[2][0] = (float) 0.0;
110
111       rot[2][1] = (float) 0.0;
112
113       rot[2][2] = (float) 1.0;
114
115       preMultiply(rot);
116     }
117
118     if (axis == 'x')
119     {
120       rot[0][0] = (float) 1.0;
121
122       rot[0][1] = (float) 0.0;
123
124       rot[0][2] = (float) 0.0;
125
126       rot[1][0] = (float) 0.0;
127
128       rot[1][1] = (float) costheta;
129
130       rot[1][2] = (float) sintheta;
131
132       rot[2][0] = (float) 0.0;
133
134       rot[2][1] = (float) -sintheta;
135
136       rot[2][2] = (float) costheta;
137
138       preMultiply(rot);
139     }
140
141     if (axis == 'y')
142     {
143       rot[0][0] = (float) costheta;
144
145       rot[0][1] = (float) 0.0;
146
147       rot[0][2] = (float) -sintheta;
148
149       rot[1][0] = (float) 0.0;
150
151       rot[1][1] = (float) 1.0;
152
153       rot[1][2] = (float) 0.0;
154
155       rot[2][0] = (float) sintheta;
156
157       rot[2][1] = (float) 0.0;
158
159       rot[2][2] = (float) costheta;
160
161       preMultiply(rot);
162     }
163   }
164
165   /**
166    * DOCUMENT ME!
167    * 
168    * @param vect
169    *          DOCUMENT ME!
170    * 
171    * @return DOCUMENT ME!
172    */
173   public float[] vectorMultiply(float[] vect)
174   {
175     temp[0] = vect[0];
176
177     temp[1] = vect[1];
178
179     temp[2] = vect[2];
180
181     for (int i = 0; i < 3; i++)
182     {
183       temp[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1])
184               + (matrix[i][2] * vect[2]);
185     }
186
187     vect[0] = temp[0];
188
189     vect[1] = temp[1];
190
191     vect[2] = temp[2];
192
193     return vect;
194   }
195
196   /**
197    * DOCUMENT ME!
198    * 
199    * @param mat
200    *          DOCUMENT ME!
201    */
202   public void preMultiply(float[][] mat)
203   {
204     float[][] tmp = new float[3][3];
205
206     for (int i = 0; i < 3; i++)
207     {
208       for (int j = 0; j < 3; j++)
209       {
210         tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
211                 + (mat[i][2] * matrix[2][j]);
212       }
213     }
214
215     for (int i = 0; i < 3; i++)
216     {
217       for (int j = 0; j < 3; j++)
218       {
219         matrix[i][j] = tmp[i][j];
220       }
221     }
222   }
223
224   /**
225    * DOCUMENT ME!
226    * 
227    * @param mat
228    *          DOCUMENT ME!
229    */
230   public void postMultiply(float[][] mat)
231   {
232     float[][] tmp = new float[3][3];
233
234     for (int i = 0; i < 3; i++)
235     {
236       for (int j = 0; j < 3; j++)
237       {
238         tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
239                 + (matrix[i][2] * mat[2][j]);
240       }
241     }
242
243     for (int i = 0; i < 3; i++)
244     {
245       for (int j = 0; j < 3; j++)
246       {
247         matrix[i][j] = tmp[i][j];
248       }
249     }
250   }
251
252   /**
253    * DOCUMENT ME!
254    * 
255    * @param args
256    *          DOCUMENT ME!
257    */
258   public static void main(String[] args)
259   {
260     RotatableMatrix m = new RotatableMatrix(3, 3);
261
262     m.addElement(0, 0, 1);
263
264     m.addElement(0, 1, 0);
265
266     m.addElement(0, 2, 0);
267
268     m.addElement(1, 0, 0);
269
270     m.addElement(1, 1, 2);
271
272     m.addElement(1, 2, 0);
273
274     m.addElement(2, 0, 0);
275
276     m.addElement(2, 1, 0);
277
278     m.addElement(2, 2, 1);
279
280     m.print();
281
282     RotatableMatrix n = new RotatableMatrix(3, 3);
283
284     n.addElement(0, 0, 2);
285
286     n.addElement(0, 1, 1);
287
288     n.addElement(0, 2, 1);
289
290     n.addElement(1, 0, 2);
291
292     n.addElement(1, 1, 1);
293
294     n.addElement(1, 2, 1);
295
296     n.addElement(2, 0, 2);
297
298     n.addElement(2, 1, 1);
299
300     n.addElement(2, 2, 1);
301
302     n.print();
303
304     // m.postMultiply(n.matrix);
305     // m.print();
306     // m.rotate(45,'z',new RotatableMatrix(3,3));
307     float[] vect = new float[3];
308
309     vect[0] = 2;
310
311     vect[1] = 4;
312
313     vect[2] = 6;
314
315     vect = m.vectorMultiply(vect);
316
317     System.out.println(vect[0] + " " + vect[1] + " " + vect[2]);
318   }
319
320   /**
321    * DOCUMENT ME!
322    */
323   public void setIdentity()
324   {
325     matrix[0][0] = (float) 1.0;
326
327     matrix[1][1] = (float) 1.0;
328
329     matrix[2][2] = (float) 1.0;
330
331     matrix[0][1] = (float) 0.0;
332
333     matrix[0][2] = (float) 0.0;
334
335     matrix[1][0] = (float) 0.0;
336
337     matrix[1][2] = (float) 0.0;
338
339     matrix[2][0] = (float) 0.0;
340
341     matrix[2][1] = (float) 0.0;
342   }
343 }