JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / MCview / MCMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package MCview;
22
23 public class MCMatrix
24 {
25   float[][] matrix;
26
27   float[][] tmp;
28
29   float mycos;
30
31   float mysin;
32
33   float myconst = (float) (Math.PI / 180);
34
35   public MCMatrix(int rows, int cols)
36   {
37     matrix = new float[rows][cols];
38     tmp = new float[rows][cols];
39   }
40
41   public void addElement(int i, int j, float value)
42   {
43     matrix[i][j] = value;
44   }
45
46   public void rotatex(float degrees)
47   {
48     mycos = (float) (Math.cos(degrees * myconst));
49     mysin = (float) (Math.sin(degrees * myconst));
50
51     tmp[0][0] = 1;
52     tmp[0][1] = 0;
53     tmp[0][2] = 0;
54     tmp[1][0] = 0;
55     tmp[1][1] = mycos;
56     tmp[1][2] = mysin;
57     tmp[2][0] = 0;
58     tmp[2][1] = -mysin;
59     tmp[2][2] = mycos;
60     preMultiply(tmp);
61   }
62
63   public void rotatez(float degrees)
64   {
65     mycos = (float) (Math.cos(degrees * myconst));
66     mysin = (float) (Math.sin(degrees * myconst));
67
68     tmp[0][0] = mycos;
69     tmp[0][1] = -mysin;
70     tmp[0][2] = 0;
71     tmp[1][0] = mysin;
72     tmp[1][1] = mycos;
73     tmp[1][2] = 0;
74     tmp[2][0] = 0;
75     tmp[2][1] = 0;
76     tmp[2][2] = 1;
77
78     preMultiply(tmp);
79   }
80
81   public void rotatey(float degrees)
82   {
83     mycos = (float) (Math.cos(degrees * myconst));
84     mysin = (float) (Math.sin(degrees * myconst));
85
86     tmp[0][0] = mycos;
87     tmp[0][1] = 0;
88     tmp[0][2] = -mysin;
89     tmp[1][0] = 0;
90     tmp[1][1] = 1;
91     tmp[1][2] = 0;
92     tmp[2][0] = mysin;
93     tmp[2][1] = 0;
94     tmp[2][2] = mycos;
95
96     preMultiply(tmp);
97   }
98
99   public float[] vectorMultiply(float[] vect)
100   {
101     float[] temp = new float[3];
102
103     temp[0] = vect[0];
104     temp[1] = vect[1];
105     temp[2] = vect[2];
106
107     for (int i = 0; i < 3; i++)
108     {
109       temp[i] = ((float) matrix[i][0] * vect[0])
110               + ((float) matrix[i][1] * vect[1])
111               + ((float) matrix[i][2] * vect[2]);
112     }
113
114     vect[0] = temp[0];
115     vect[1] = temp[1];
116     vect[2] = temp[2];
117
118     return vect;
119   }
120
121   public void preMultiply(float[][] mat)
122   {
123     float[][] tmp = new float[3][3];
124
125     for (int i = 0; i < 3; i++)
126     {
127       for (int j = 0; j < 3; j++)
128       {
129         tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
130                 + (mat[i][2] * matrix[2][j]);
131       }
132     }
133
134     for (int i = 0; i < 3; i++)
135     {
136       for (int j = 0; j < 3; j++)
137       {
138         matrix[i][j] = tmp[i][j];
139       }
140     }
141   }
142
143   public void postMultiply(float[][] mat)
144   {
145     float[][] tmp = new float[3][3];
146
147     for (int i = 0; i < 3; i++)
148     {
149       for (int j = 0; j < 3; j++)
150       {
151         tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
152                 + (matrix[i][2] * mat[2][j]);
153       }
154     }
155
156     for (int i = 0; i < 3; i++)
157     {
158       for (int j = 0; j < 3; j++)
159       {
160         matrix[i][j] = tmp[i][j];
161       }
162     }
163   }
164
165   public void setIdentity()
166   {
167     matrix[0][0] = 1;
168     matrix[1][1] = 1;
169     matrix[2][2] = 1;
170     matrix[0][1] = 0;
171     matrix[0][2] = 0;
172     matrix[1][0] = 0;
173     matrix[1][2] = 0;
174     matrix[2][0] = 0;
175     matrix[2][1] = 0;
176   }
177 }