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