1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.math; |
22 |
|
|
23 |
|
import jalview.datamodel.Point; |
24 |
|
|
25 |
|
import java.io.PrintStream; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
|
|
| 0% |
Uncovered Elements: 91 (91) |
Complexity: 22 |
Complexity Density: 0.34 |
|
32 |
|
public class RotatableMatrix |
33 |
|
{ |
34 |
|
private static final int DIMS = 3; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
private static Map<Axis, Map<Float, float[][]>> cachedRotations; |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
41 |
0 |
static... |
42 |
|
{ |
43 |
0 |
cachedRotations = new HashMap<>(); |
44 |
0 |
for (Axis axis : Axis.values()) |
45 |
|
{ |
46 |
0 |
HashMap<Float, float[][]> map = new HashMap<>(); |
47 |
0 |
cachedRotations.put(axis, map); |
48 |
0 |
for (int deg = 1; deg < 5; deg++) |
49 |
|
{ |
50 |
0 |
float[][] rotation = getRotation(deg, axis); |
51 |
0 |
map.put(Float.valueOf(deg), rotation); |
52 |
0 |
rotation = getRotation(-deg, axis); |
53 |
0 |
map.put(Float.valueOf(-deg), rotation); |
54 |
|
} |
55 |
|
} |
56 |
|
} |
57 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
58 |
|
public enum Axis |
59 |
|
{ |
60 |
|
X, Y, Z |
61 |
|
} |
62 |
|
|
63 |
|
float[][] matrix; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
69 |
0 |
public RotatableMatrix()... |
70 |
|
{ |
71 |
0 |
matrix = new float[DIMS][DIMS]; |
72 |
0 |
for (int j = 0; j < DIMS; j++) |
73 |
|
{ |
74 |
0 |
matrix[j][j] = 1f; |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@param |
82 |
|
@param |
83 |
|
@param |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
0 |
public void setValue(int i, int j, float value)... |
86 |
|
{ |
87 |
0 |
matrix[i][j] = value; |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@param |
94 |
|
@param |
95 |
|
@return |
96 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
0 |
public float getValue(int i, int j)... |
98 |
|
{ |
99 |
0 |
return matrix[i][j]; |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
105 |
0 |
public void print(PrintStream ps)... |
106 |
|
{ |
107 |
0 |
ps.println(matrix[0][0] + " " + matrix[0][1] + " " + matrix[0][2]); |
108 |
0 |
ps.println(matrix[1][0] + " " + matrix[1][1] + " " + matrix[1][2]); |
109 |
0 |
ps.println(matrix[2][0] + " " + matrix[2][1] + " " + matrix[2][2]); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@param |
117 |
|
@param |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
119 |
0 |
public void rotate(float degrees, Axis axis)... |
120 |
|
{ |
121 |
0 |
float[][] rot = getRotation(degrees, axis); |
122 |
|
|
123 |
0 |
preMultiply(rot); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
@param |
132 |
|
@return |
133 |
|
@see |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 5 |
Complexity Density: 0.17 |
|
135 |
0 |
protected static float[][] getRotation(float degrees, Axis axis)... |
136 |
|
{ |
137 |
0 |
Float floatValue = Float.valueOf(degrees); |
138 |
0 |
if (cachedRotations.get(axis).containsKey(floatValue)) |
139 |
|
{ |
140 |
|
|
141 |
|
|
142 |
0 |
return cachedRotations.get(axis).get(floatValue); |
143 |
|
} |
144 |
|
|
145 |
0 |
float costheta = (float) Math.cos(degrees * Math.PI / 180f); |
146 |
|
|
147 |
0 |
float sintheta = (float) Math.sin(degrees * Math.PI / 180f); |
148 |
|
|
149 |
0 |
float[][] rot = new float[DIMS][DIMS]; |
150 |
|
|
151 |
0 |
switch (axis) |
152 |
|
{ |
153 |
0 |
case X: |
154 |
0 |
rot[0][0] = 1f; |
155 |
0 |
rot[1][1] = costheta; |
156 |
0 |
rot[1][2] = sintheta; |
157 |
0 |
rot[2][1] = -sintheta; |
158 |
0 |
rot[2][2] = costheta; |
159 |
0 |
break; |
160 |
0 |
case Y: |
161 |
0 |
rot[0][0] = costheta; |
162 |
0 |
rot[0][2] = -sintheta; |
163 |
0 |
rot[1][1] = 1f; |
164 |
0 |
rot[2][0] = sintheta; |
165 |
0 |
rot[2][2] = costheta; |
166 |
0 |
break; |
167 |
0 |
case Z: |
168 |
0 |
rot[0][0] = costheta; |
169 |
0 |
rot[0][1] = -sintheta; |
170 |
0 |
rot[1][0] = sintheta; |
171 |
0 |
rot[1][1] = costheta; |
172 |
0 |
rot[2][2] = 1f; |
173 |
0 |
break; |
174 |
|
} |
175 |
0 |
return rot; |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
@param |
185 |
|
|
186 |
|
@return |
187 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
188 |
0 |
public float[] vectorMultiply(float[] vect)... |
189 |
|
{ |
190 |
0 |
float[] result = new float[DIMS]; |
191 |
|
|
192 |
0 |
for (int i = 0; i < DIMS; i++) |
193 |
|
{ |
194 |
0 |
result[i] = (matrix[i][0] * vect[0]) + (matrix[i][1] * vect[1]) |
195 |
|
+ (matrix[i][2] * vect[2]); |
196 |
|
} |
197 |
|
|
198 |
0 |
return result; |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@param |
207 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
208 |
0 |
public void preMultiply(float[][] mat)... |
209 |
|
{ |
210 |
0 |
float[][] tmp = new float[DIMS][DIMS]; |
211 |
|
|
212 |
0 |
for (int i = 0; i < DIMS; i++) |
213 |
|
{ |
214 |
0 |
for (int j = 0; j < DIMS; j++) |
215 |
|
{ |
216 |
0 |
tmp[i][j] = (mat[i][0] * matrix[0][j]) + (mat[i][1] * matrix[1][j]) |
217 |
|
+ (mat[i][2] * matrix[2][j]); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
0 |
matrix = tmp; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
@param |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
231 |
0 |
public void postMultiply(float[][] mat)... |
232 |
|
{ |
233 |
0 |
float[][] tmp = new float[DIMS][DIMS]; |
234 |
|
|
235 |
0 |
for (int i = 0; i < DIMS; i++) |
236 |
|
{ |
237 |
0 |
for (int j = 0; j < DIMS; j++) |
238 |
|
{ |
239 |
0 |
tmp[i][j] = (matrix[i][0] * mat[0][j]) + (matrix[i][1] * mat[1][j]) |
240 |
|
+ (matrix[i][2] * mat[2][j]); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
0 |
matrix = tmp; |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
@param |
252 |
|
@return |
253 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
254 |
0 |
public Point vectorMultiply(Point coord)... |
255 |
|
{ |
256 |
0 |
float[] v = vectorMultiply(new float[] { coord.x, coord.y, coord.z }); |
257 |
0 |
return new Point(v[0], v[1], v[2]); |
258 |
|
} |
259 |
|
} |