Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / math / RotatableMatrix.js
1 Clazz.declarePackage ("jalview.math");
2 c$ = Clazz.decorateAsClass (function () {
3 this.matrix = null;
4 this.temp = null;
5 this.rot = null;
6 Clazz.instantialize (this, arguments);
7 }, jalview.math, "RotatableMatrix");
8 Clazz.makeConstructor (c$, 
9 function (rows, cols) {
10 this.matrix =  Clazz.newFloatArray (rows, cols, 0);
11 this.temp =  Clazz.newFloatArray (3, 0);
12 this.rot =  Clazz.newFloatArray (3, 3, 0);
13 }, "~N,~N");
14 Clazz.defineMethod (c$, "addElement", 
15 function (i, j, value) {
16 this.matrix[i][j] = value;
17 }, "~N,~N,~N");
18 Clazz.defineMethod (c$, "print", 
19 function () {
20 System.out.println (this.matrix[0][0] + " " + this.matrix[0][1] + " " + this.matrix[0][2]);
21 System.out.println (this.matrix[1][0] + " " + this.matrix[1][1] + " " + this.matrix[1][2]);
22 System.out.println (this.matrix[2][0] + " " + this.matrix[2][1] + " " + this.matrix[2][2]);
23 });
24 Clazz.defineMethod (c$, "rotate", 
25 function (degrees, axis) {
26 var costheta = Math.cos ((degrees * 3.141592653589793) / 180.0);
27 var sintheta = Math.sin ((degrees * 3.141592653589793) / 180.0);
28 if (axis == 'z') {
29 this.rot[0][0] = costheta;
30 this.rot[0][1] = -sintheta;
31 this.rot[0][2] = 0.0;
32 this.rot[1][0] = sintheta;
33 this.rot[1][1] = costheta;
34 this.rot[1][2] = 0.0;
35 this.rot[2][0] = 0.0;
36 this.rot[2][1] = 0.0;
37 this.rot[2][2] = 1.0;
38 this.preMultiply (this.rot);
39 }if (axis == 'x') {
40 this.rot[0][0] = 1.0;
41 this.rot[0][1] = 0.0;
42 this.rot[0][2] = 0.0;
43 this.rot[1][0] = 0.0;
44 this.rot[1][1] = costheta;
45 this.rot[1][2] = sintheta;
46 this.rot[2][0] = 0.0;
47 this.rot[2][1] = -sintheta;
48 this.rot[2][2] = costheta;
49 this.preMultiply (this.rot);
50 }if (axis == 'y') {
51 this.rot[0][0] = costheta;
52 this.rot[0][1] = 0.0;
53 this.rot[0][2] = -sintheta;
54 this.rot[1][0] = 0.0;
55 this.rot[1][1] = 1.0;
56 this.rot[1][2] = 0.0;
57 this.rot[2][0] = sintheta;
58 this.rot[2][1] = 0.0;
59 this.rot[2][2] = costheta;
60 this.preMultiply (this.rot);
61 }}, "~N,~S");
62 Clazz.defineMethod (c$, "vectorMultiply", 
63 function (vect) {
64 this.temp[0] = vect[0];
65 this.temp[1] = vect[1];
66 this.temp[2] = vect[2];
67 for (var i = 0; i < 3; i++) {
68 this.temp[i] = (this.matrix[i][0] * vect[0]) + (this.matrix[i][1] * vect[1]) + (this.matrix[i][2] * vect[2]);
69 }
70 vect[0] = this.temp[0];
71 vect[1] = this.temp[1];
72 vect[2] = this.temp[2];
73 return vect;
74 }, "~A");
75 Clazz.defineMethod (c$, "preMultiply", 
76 function (mat) {
77 var tmp =  Clazz.newFloatArray (3, 3, 0);
78 for (var i = 0; i < 3; i++) {
79 for (var j = 0; j < 3; j++) {
80 tmp[i][j] = (mat[i][0] * this.matrix[0][j]) + (mat[i][1] * this.matrix[1][j]) + (mat[i][2] * this.matrix[2][j]);
81 }
82 }
83 for (var i = 0; i < 3; i++) {
84 for (var j = 0; j < 3; j++) {
85 this.matrix[i][j] = tmp[i][j];
86 }
87 }
88 }, "~A");
89 Clazz.defineMethod (c$, "postMultiply", 
90 function (mat) {
91 var tmp =  Clazz.newFloatArray (3, 3, 0);
92 for (var i = 0; i < 3; i++) {
93 for (var j = 0; j < 3; j++) {
94 tmp[i][j] = (this.matrix[i][0] * mat[0][j]) + (this.matrix[i][1] * mat[1][j]) + (this.matrix[i][2] * mat[2][j]);
95 }
96 }
97 for (var i = 0; i < 3; i++) {
98 for (var j = 0; j < 3; j++) {
99 this.matrix[i][j] = tmp[i][j];
100 }
101 }
102 }, "~A");
103 Clazz.defineMethod (c$, "setIdentity", 
104 function () {
105 this.matrix[0][0] = 1.0;
106 this.matrix[1][1] = 1.0;
107 this.matrix[2][2] = 1.0;
108 this.matrix[0][1] = 0.0;
109 this.matrix[0][2] = 0.0;
110 this.matrix[1][0] = 0.0;
111 this.matrix[1][2] = 0.0;
112 this.matrix[2][0] = 0.0;
113 this.matrix[2][1] = 0.0;
114 });