JAL-4059 update Jmol and JSmol to 15.2.69 - revised Jalview classes interating with...
[jalview.git] / src / mc_view / MCMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 mc_view;
22
23 public class MCMatrix
24 {
25   double[][] matrix;
26
27   double[][] tmp;
28
29   double mycos;
30
31   double mysin;
32
33   double myconst = Math.PI / 180;
34
35   public MCMatrix(int rows, int cols)
36   {
37     matrix = new double[rows][cols];
38     tmp = new double[rows][cols];
39   }
40
41   public void addElement(int i, int j, double value)
42   {
43     matrix[i][j] = value;
44   }
45
46   public void rotatex(double degrees)
47   {
48     mycos = (Math.cos(degrees * myconst));
49     mysin = (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(double degrees)
64   {
65     mycos = (Math.cos(degrees * myconst));
66     mysin = (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(double degrees)
82   {
83     mycos = (Math.cos(degrees * myconst));
84     mysin = (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 double[] vectorMultiply(double[] vect)
100   {
101     double[] temp = new double[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] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1])
110               + (matrix[i][2] * vect[2]);
111     }
112
113     vect[0] = temp[0];
114     vect[1] = temp[1];
115     vect[2] = temp[2];
116
117     return vect;
118   }
119
120   public void preMultiply(double[][] mat)
121   {
122     double[][] tmp = new double[3][3];
123
124     for (int i = 0; i < 3; i++)
125     {
126       for (int j = 0; j < 3; j++)
127       {
128         tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j])
129                 + (mat[i][2] * matrix[2][j]);
130       }
131     }
132
133     for (int i = 0; i < 3; i++)
134     {
135       for (int j = 0; j < 3; j++)
136       {
137         matrix[i][j] = tmp[i][j];
138       }
139     }
140   }
141
142   public void postMultiply(double[][] mat)
143   {
144     double[][] tmp = new double[3][3];
145
146     for (int i = 0; i < 3; i++)
147     {
148       for (int j = 0; j < 3; j++)
149       {
150         tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j])
151                 + (matrix[i][2] * mat[2][j]);
152       }
153     }
154
155     for (int i = 0; i < 3; i++)
156     {
157       for (int j = 0; j < 3; j++)
158       {
159         matrix[i][j] = tmp[i][j];
160       }
161     }
162   }
163
164   public void setIdentity()
165   {
166     matrix[0][0] = 1;
167     matrix[1][1] = 1;
168     matrix[2][2] = 1;
169     matrix[0][1] = 0;
170     matrix[0][2] = 0;
171     matrix[1][0] = 0;
172     matrix[1][2] = 0;
173     matrix[2][0] = 0;
174     matrix[2][1] = 0;
175   }
176 }