update author list in license for (JAL-826)
[jalview.git] / src / MCview / MCMatrix.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 MCview;
19
20 public class MCMatrix
21 {
22   float[][] matrix;
23
24   float[][] tmp;
25
26   float mycos;
27
28   float mysin;
29
30   float myconst = (float) (Math.PI / 180);
31
32   public MCMatrix(int rows, int cols)
33   {
34     matrix = new float[rows][cols];
35     tmp = new float[rows][cols];
36   }
37
38   public void addElement(int i, int j, float value)
39   {
40     matrix[i][j] = value;
41   }
42
43   public void rotatex(float degrees)
44   {
45     mycos = (float) (Math.cos(degrees * myconst));
46     mysin = (float) (Math.sin(degrees * myconst));
47
48     tmp[0][0] = 1;
49     tmp[0][1] = 0;
50     tmp[0][2] = 0;
51     tmp[1][0] = 0;
52     tmp[1][1] = mycos;
53     tmp[1][2] = mysin;
54     tmp[2][0] = 0;
55     tmp[2][1] = -mysin;
56     tmp[2][2] = mycos;
57     preMultiply(tmp);
58   }
59
60   public void rotatez(float degrees)
61   {
62     mycos = (float) (Math.cos(degrees * myconst));
63     mysin = (float) (Math.sin(degrees * myconst));
64
65     tmp[0][0] = mycos;
66     tmp[0][1] = -mysin;
67     tmp[0][2] = 0;
68     tmp[1][0] = mysin;
69     tmp[1][1] = mycos;
70     tmp[1][2] = 0;
71     tmp[2][0] = 0;
72     tmp[2][1] = 0;
73     tmp[2][2] = 1;
74
75     preMultiply(tmp);
76   }
77
78   public void rotatey(float degrees)
79   {
80     mycos = (float) (Math.cos(degrees * myconst));
81     mysin = (float) (Math.sin(degrees * myconst));
82
83     tmp[0][0] = mycos;
84     tmp[0][1] = 0;
85     tmp[0][2] = -mysin;
86     tmp[1][0] = 0;
87     tmp[1][1] = 1;
88     tmp[1][2] = 0;
89     tmp[2][0] = mysin;
90     tmp[2][1] = 0;
91     tmp[2][2] = mycos;
92
93     preMultiply(tmp);
94   }
95
96   public float[] vectorMultiply(float[] vect)
97   {
98     float[] temp = new float[3];
99
100     temp[0] = vect[0];
101     temp[1] = vect[1];
102     temp[2] = vect[2];
103
104     for (int i = 0; i < 3; i++)
105     {
106       temp[i] = ((float) matrix[i][0] * vect[0])
107               + ((float) matrix[i][1] * vect[1])
108               + ((float) matrix[i][2] * vect[2]);
109     }
110
111     vect[0] = temp[0];
112     vect[1] = temp[1];
113     vect[2] = temp[2];
114
115     return vect;
116   }
117
118   public void preMultiply(float[][] mat)
119   {
120     float[][] tmp = new float[3][3];
121
122     for (int i = 0; i < 3; i++)
123     {
124       for (int j = 0; j < 3; j++)
125       {
126         tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
127                 + (mat[i][2] * matrix[2][j]);
128       }
129     }
130
131     for (int i = 0; i < 3; i++)
132     {
133       for (int j = 0; j < 3; j++)
134       {
135         matrix[i][j] = tmp[i][j];
136       }
137     }
138   }
139
140   public void postMultiply(float[][] mat)
141   {
142     float[][] tmp = new float[3][3];
143
144     for (int i = 0; i < 3; i++)
145     {
146       for (int j = 0; j < 3; j++)
147       {
148         tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
149                 + (matrix[i][2] * mat[2][j]);
150       }
151     }
152
153     for (int i = 0; i < 3; i++)
154     {
155       for (int j = 0; j < 3; j++)
156       {
157         matrix[i][j] = tmp[i][j];
158       }
159     }
160   }
161
162   public void setIdentity()
163   {
164     matrix[0][0] = 1;
165     matrix[1][1] = 1;
166     matrix[2][2] = 1;
167     matrix[0][1] = 0;
168     matrix[0][2] = 0;
169     matrix[1][0] = 0;
170     matrix[1][2] = 0;
171     matrix[2][0] = 0;
172     matrix[2][1] = 0;
173   }
174 }