applied copyright 2008
[jalview.git] / src / MCview / MCMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package MCview;
20
21 public class MCMatrix
22 {
23   float[][] matrix;
24   float[][] tmp;
25   float mycos;
26   float mysin;
27   float myconst = (float) (Math.PI / 180);
28
29   public MCMatrix(int rows, int cols)
30   {
31     matrix = new float[rows][cols];
32     tmp = new float[rows][cols];
33   }
34
35   public void addElement(int i, int j, float value)
36   {
37     matrix[i][j] = value;
38   }
39
40   public void rotatex(float degrees)
41   {
42     mycos = (float) (Math.cos(degrees * myconst));
43     mysin = (float) (Math.sin(degrees * myconst));
44
45     tmp[0][0] = 1;
46     tmp[0][1] = 0;
47     tmp[0][2] = 0;
48     tmp[1][0] = 0;
49     tmp[1][1] = mycos;
50     tmp[1][2] = mysin;
51     tmp[2][0] = 0;
52     tmp[2][1] = -mysin;
53     tmp[2][2] = mycos;
54     preMultiply(tmp);
55   }
56
57   public void rotatez(float degrees)
58   {
59     mycos = (float) (Math.cos(degrees * myconst));
60     mysin = (float) (Math.sin(degrees * myconst));
61
62     tmp[0][0] = mycos;
63     tmp[0][1] = -mysin;
64     tmp[0][2] = 0;
65     tmp[1][0] = mysin;
66     tmp[1][1] = mycos;
67     tmp[1][2] = 0;
68     tmp[2][0] = 0;
69     tmp[2][1] = 0;
70     tmp[2][2] = 1;
71
72     preMultiply(tmp);
73   }
74
75   public void rotatey(float degrees)
76   {
77     mycos = (float) (Math.cos(degrees * myconst));
78     mysin = (float) (Math.sin(degrees * myconst));
79
80     tmp[0][0] = mycos;
81     tmp[0][1] = 0;
82     tmp[0][2] = -mysin;
83     tmp[1][0] = 0;
84     tmp[1][1] = 1;
85     tmp[1][2] = 0;
86     tmp[2][0] = mysin;
87     tmp[2][1] = 0;
88     tmp[2][2] = mycos;
89
90     preMultiply(tmp);
91   }
92
93   public float[] vectorMultiply(float[] vect)
94   {
95     float[] temp = new float[3];
96
97     temp[0] = vect[0];
98     temp[1] = vect[1];
99     temp[2] = vect[2];
100
101     for (int i = 0; i < 3; i++)
102     {
103       temp[i] = ( (float) matrix[i][0] * vect[0]) +
104           ( (float) matrix[i][1] * vect[1]) +
105           ( (float) matrix[i][2] * vect[2]);
106     }
107
108     vect[0] = temp[0];
109     vect[1] = temp[1];
110     vect[2] = temp[2];
111
112     return vect;
113   }
114
115   public void preMultiply(float[][] mat)
116   {
117     float[][] tmp = new float[3][3];
118
119     for (int i = 0; i < 3; i++)
120     {
121       for (int j = 0; j < 3; j++)
122       {
123         tmp[i][j] = (mat[i][0] * matrix[0][j]) +
124             (mat[i][1] * matrix[1][j]) + (mat[i][2] * matrix[2][j]);
125       }
126     }
127
128     for (int i = 0; i < 3; i++)
129     {
130       for (int j = 0; j < 3; j++)
131       {
132         matrix[i][j] = tmp[i][j];
133       }
134     }
135   }
136
137   public void postMultiply(float[][] mat)
138   {
139     float[][] tmp = new float[3][3];
140
141     for (int i = 0; i < 3; i++)
142     {
143       for (int j = 0; j < 3; j++)
144       {
145         tmp[i][j] = (matrix[i][0] * mat[0][j]) +
146             (matrix[i][1] * mat[1][j]) + (matrix[i][2] * mat[2][j]);
147       }
148     }
149
150     for (int i = 0; i < 3; i++)
151     {
152       for (int j = 0; j < 3; j++)
153       {
154         matrix[i][j] = tmp[i][j];
155       }
156     }
157   }
158
159   public void setIdentity()
160   {
161     matrix[0][0] = 1;
162     matrix[1][1] = 1;
163     matrix[2][2] = 1;
164     matrix[0][1] = 0;
165     matrix[0][2] = 0;
166     matrix[1][0] = 0;
167     matrix[1][2] = 0;
168     matrix[2][0] = 0;
169     matrix[2][1] = 0;
170   }
171 }