260a82c7450f583f21931b84220cf784fea37ee8
[jalview.git] / src2 / javajs / util / M34.java
1 package javajs.util;
2
3 /**
4  * A base class for both M3 and M4 to conserve code size.
5  * 
6  * @author Kenji hiranabe
7  * 
8  *         additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique
9  *         constructor and method names for the optimization of compiled
10  *         JavaScript using Java2Script and for subclassing to M3 and M4
11  * 
12  */
13 public abstract class M34 {
14
15   /**
16    * The first element of the first row
17    */
18   public float m00;
19
20   /**
21    * The second element of the first row.
22    */
23   public float m01;
24
25   /**
26    * third element of the first row.
27    */
28   public float m02;
29
30   /**
31    * The first element of the second row.
32    */
33   public float m10;
34
35   /**
36    * The second element of the second row.
37    */
38   public float m11;
39
40   /**
41    * The third element of the second row.
42    */
43   public float m12;
44
45   /**
46    * The first element of the third row.
47    */
48   public float m20;
49
50   /**
51    * The second element of the third row.
52    */
53   public float m21;
54
55   /**
56    * The third element of the third row.
57    */
58   public float m22;
59
60   protected void setAA33(A4 a) {
61     double x = a.x;
62     double y = a.y;
63     double z = a.z;
64     double angle = a.angle;
65     // Taken from Rick's which is taken from Wertz. pg. 412
66     // Bug Fixed and changed into right-handed by hiranabe
67     double n = Math.sqrt(x * x + y * y + z * z);
68     // zero-div may occur
69     n = 1 / n;
70     x *= n;
71     y *= n;
72     z *= n;
73     double c = Math.cos(angle);
74     double s = Math.sin(angle);
75     double omc = 1.0 - c;
76     m00 = (float) (c + x * x * omc);
77     m11 = (float) (c + y * y * omc);
78     m22 = (float) (c + z * z * omc);
79
80     double tmp1 = x * y * omc;
81     double tmp2 = z * s;
82     m01 = (float) (tmp1 - tmp2);
83     m10 = (float) (tmp1 + tmp2);
84
85     tmp1 = x * z * omc;
86     tmp2 = y * s;
87     m02 = (float) (tmp1 + tmp2);
88     m20 = (float) (tmp1 - tmp2);
89
90     tmp1 = y * z * omc;
91     tmp2 = x * s;
92     m12 = (float) (tmp1 - tmp2);
93     m21 = (float) (tmp1 + tmp2);
94   }
95
96   public void rotate(T3 t) {
97     // alias-safe
98     rotate2(t, t);
99   }
100
101   /**
102    * Transform the vector vec using this Matrix3f and place the result into
103    * vecOut.
104    * 
105    * @param t
106    *        the single precision vector to be transformed
107    * @param result
108    *        the vector into which the transformed values are placed
109    */
110   public void rotate2(T3 t, T3 result) {
111     // alias-safe
112     result.set(m00 * t.x + m01 * t.y + m02 * t.z, m10 * t.x + m11 * t.y + m12
113         * t.z, m20 * t.x + m21 * t.y + m22 * t.z);
114   }
115
116
117   /**
118    * Sets the value of this matrix to the double value of the Matrix3f argument.
119    * 
120    * @param m1
121    *        the matrix3f
122    */
123   protected void setM33(M34 m1) {
124     m00 = m1.m00;
125     m01 = m1.m01;
126     m02 = m1.m02;
127     m10 = m1.m10;
128     m11 = m1.m11;
129     m12 = m1.m12;
130     m20 = m1.m20;
131     m21 = m1.m21;
132     m22 = m1.m22;
133   }
134
135   protected void clear33() {
136     m00 = m01 = m02 = m10 = m11 = m12 = m20 = m21 = m22 = 0.0f;
137   }
138
139   protected void set33(int row, int col, float v) {
140     switch (row) {
141     case 0:
142       switch (col) {
143       case 0:
144         m00 = v;
145         return;
146       case 1:
147         m01 = v;
148         return;
149       case 2: 
150         m02 = v;
151         return;
152       }
153       break;
154     case 1:
155       switch (col) {
156       case 0:
157         m10 = v;
158         return;
159       case 1:
160         m11 = v;
161         return;
162       case 2: 
163         m12 = v;
164         return;
165       }
166       break;
167     case 2: 
168       switch (col) {
169       case 0:
170         m20 = v;
171         return;
172       case 1:
173         m21 = v;
174         return;
175       case 2: 
176         m22 = v;
177         return;
178       }
179       break;
180     }
181     err();
182   }
183
184   protected float get33(int row, int col) {
185     switch (row) {
186     case 0:
187       switch (col) {
188       case 0:
189         return m00;
190       case 1:
191         return m01;
192       case 2:
193         return m02;
194       }
195       break;
196     case 1:
197       switch (col) {
198       case 0:
199         return m10;
200       case 1:
201         return m11;
202       case 2:
203         return m12;
204       }
205       break;
206     case 2:
207       switch (col) {
208       case 0:
209         return m20;
210       case 1:
211         return m21;
212       case 2:
213         return m22;
214       }
215       break;
216     }
217     err();
218     return 0;
219   }
220
221   protected void setRow33(int row, float v[]) {
222     switch (row) {
223     case 0:
224       m00 = v[0];
225       m01 = v[1];
226       m02 = v[2];
227       return;
228     case 1:
229       m10 = v[0];
230       m11 = v[1];
231       m12 = v[2];
232       return;
233     case 2:
234       m20 = v[0];
235       m21 = v[1];
236       m22 = v[2];
237       return;
238     default:
239       err();
240     }
241   }
242   
243   public abstract void getRow(int row, float v[]);
244
245   protected void getRow33(int row, float v[]) {
246     switch (row) {
247     case 0:
248       v[0] = m00;
249       v[1] = m01;
250       v[2] = m02;
251       return;
252     case 1:
253       v[0] = m10;
254       v[1] = m11;
255       v[2] = m12;
256       return;
257     case 2:
258       v[0] = m20;
259       v[1] = m21;
260       v[2] = m22;
261       return;
262     }
263     err();
264   }
265
266   protected void setColumn33(int column, float v[]) {
267     switch(column) {
268     case 0:
269       m00 = v[0];
270       m10 = v[1];
271       m20 = v[2];
272       break;
273     case 1:
274       m01 = v[0];
275       m11 = v[1];
276       m21 = v[2];
277       break;
278     case 2:
279       m02 = v[0];
280       m12 = v[1];
281       m22 = v[2];
282       break;
283      default:
284       err();
285     }
286   }
287
288   protected void getColumn33(int column, float v[]) {
289     switch(column) {
290     case 0:
291       v[0] = m00;
292       v[1] = m10;
293       v[2] = m20;
294       break;
295     case 1:
296       v[0] = m01;
297       v[1] = m11;
298       v[2] = m21;
299       break;
300     case 2:
301       v[0] = m02;
302       v[1] = m12;
303       v[2] = m22;
304       break;
305     default:
306       err();
307     }
308   }
309
310   protected void add33(M34 m1) {
311     m00 += m1.m00;
312     m01 += m1.m01;
313     m02 += m1.m02;
314     m10 += m1.m10;
315     m11 += m1.m11;
316     m12 += m1.m12;
317     m20 += m1.m20;
318     m21 += m1.m21;
319     m22 += m1.m22;
320   }
321
322   protected void sub33(M34 m1) {
323     m00 -= m1.m00;
324     m01 -= m1.m01;
325     m02 -= m1.m02;
326     m10 -= m1.m10;
327     m11 -= m1.m11;
328     m12 -= m1.m12;
329     m20 -= m1.m20;
330     m21 -= m1.m21;
331     m22 -= m1.m22;
332   }
333
334   protected void mul33(float x) {
335     m00 *= x;
336     m01 *= x;
337     m02 *= x;
338     m10 *= x;
339     m11 *= x;
340     m12 *= x;
341     m20 *= x;
342     m21 *= x;
343     m22 *= x;
344   }
345
346   protected void transpose33() {
347     float tmp = m01;
348     m01 = m10;
349     m10 = tmp;
350
351     tmp = m02;
352     m02 = m20;
353     m20 = tmp;
354
355     tmp = m12;
356     m12 = m21;
357     m21 = tmp;
358   }
359
360   protected void setXRot(float angle) {
361     double c = Math.cos(angle);
362     double s = Math.sin(angle);
363     m00 = 1.0f;
364     m01 = 0.0f;
365     m02 = 0.0f;
366     m10 = 0.0f;
367     m11 = (float) c;
368     m12 = (float) -s;
369     m20 = 0.0f;
370     m21 = (float) s;
371     m22 = (float) c;
372   }
373
374   protected void setYRot(float angle) {
375     double c = Math.cos(angle);
376     double s = Math.sin(angle);
377     m00 = (float) c;
378     m01 = 0.0f;
379     m02 = (float) s;
380     m10 = 0.0f;
381     m11 = 1.0f;
382     m12 = 0.0f;
383     m20 = (float) -s;
384     m21 = 0.0f;
385     m22 = (float) c;
386   }
387   
388   protected void setZRot(float angle) {
389     double c = Math.cos(angle);
390     double s = Math.sin(angle);
391     m00 = (float) c;
392     m01 = (float) -s;
393     m02 = 0.0f;
394     m10 = (float) s;
395     m11 = (float) c;
396     m12 = 0.0f;
397     m20 = 0.0f;
398     m21 = 0.0f;
399     m22 = 1.0f;
400   }
401   
402   /**
403    * @return 3x3 determinant
404    */
405   public float determinant3() {
406     return m00 * (m11 * m22 - m21 * m12) - m01 * (m10 * m22 - m20 * m12) + m02
407         * (m10 * m21 - m20 * m11);
408   }
409   
410   protected void err() {
411     throw new ArrayIndexOutOfBoundsException(
412         "matrix column/row out of bounds");
413   }
414
415
416 }