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