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