root / trunk / web / dojo / dojox / gfx3d / matrix.js @ 10
History | View | Annotate | Download (5.84 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.gfx3d.matrix"]){ |
||
9 | dojo._hasResource["dojox.gfx3d.matrix"]=true; |
||
10 | dojo.provide("dojox.gfx3d.matrix");
|
||
11 | dojox.gfx3d.matrix._degToRad=function(_1){ |
||
12 | return Math.PI*_1/180; |
||
13 | }; |
||
14 | dojox.gfx3d.matrix._radToDeg=function(_2){ |
||
15 | return _2/Math.PI*180; |
||
16 | }; |
||
17 | dojox.gfx3d.matrix.Matrix3D=function(_3){ |
||
18 | if(_3){
|
||
19 | if(typeof _3=="number"){ |
||
20 | this.xx=this.yy=this.zz=_3; |
||
21 | }else{
|
||
22 | if(_3 instanceof Array){ |
||
23 | if(_3.length>0){ |
||
24 | var m=dojox.gfx3d.matrix.normalize(_3[0]); |
||
25 | for(var i=1;i<_3.length;++i){ |
||
26 | var l=m;
|
||
27 | var r=dojox.gfx3d.matrix.normalize(_3[i]);
|
||
28 | m=new dojox.gfx3d.matrix.Matrix3D();
|
||
29 | m.xx=l.xx*r.xx+l.xy*r.yx+l.xz*r.zx; |
||
30 | m.xy=l.xx*r.xy+l.xy*r.yy+l.xz*r.zy; |
||
31 | m.xz=l.xx*r.xz+l.xy*r.yz+l.xz*r.zz; |
||
32 | m.yx=l.yx*r.xx+l.yy*r.yx+l.yz*r.zx; |
||
33 | m.yy=l.yx*r.xy+l.yy*r.yy+l.yz*r.zy; |
||
34 | m.yz=l.yx*r.xz+l.yy*r.yz+l.yz*r.zz; |
||
35 | m.zx=l.zx*r.xx+l.zy*r.yx+l.zz*r.zx; |
||
36 | m.zy=l.zx*r.xy+l.zy*r.yy+l.zz*r.zy; |
||
37 | m.zz=l.zx*r.xz+l.zy*r.yz+l.zz*r.zz; |
||
38 | m.dx=l.xx*r.dx+l.xy*r.dy+l.xz*r.dz+l.dx; |
||
39 | m.dy=l.yx*r.dx+l.yy*r.dy+l.yz*r.dz+l.dy; |
||
40 | m.dz=l.zx*r.dx+l.zy*r.dy+l.zz*r.dz+l.dz; |
||
41 | } |
||
42 | dojo.mixin(this,m);
|
||
43 | } |
||
44 | }else{
|
||
45 | dojo.mixin(this,_3);
|
||
46 | } |
||
47 | } |
||
48 | } |
||
49 | }; |
||
50 | dojo.extend(dojox.gfx3d.matrix.Matrix3D,{xx:1,xy:0,xz:0,yx:0,yy:1,yz:0,zx:0,zy:0,zz:1,dx:0,dy:0,dz:0}); |
||
51 | dojo.mixin(dojox.gfx3d.matrix,{identity:new dojox.gfx3d.matrix.Matrix3D(),translate:function(a,b,c){ |
||
52 | if(arguments.length>1){ |
||
53 | return new dojox.gfx3d.matrix.Matrix3D({dx:a,dy:b,dz:c}); |
||
54 | } |
||
55 | return new dojox.gfx3d.matrix.Matrix3D({dx:a.x,dy:a.y,dz:a.z}); |
||
56 | },scale:function(a,b,c){ |
||
57 | if(arguments.length>1){ |
||
58 | return new dojox.gfx3d.matrix.Matrix3D({xx:a,yy:b,zz:c}); |
||
59 | } |
||
60 | if(typeof a=="number"){ |
||
61 | return new dojox.gfx3d.matrix.Matrix3D({xx:a,yy:a,zz:a}); |
||
62 | } |
||
63 | return new dojox.gfx3d.matrix.Matrix3D({xx:a.x,yy:a.y,zz:a.z}); |
||
64 | },rotateX:function(_4){ |
||
65 | var c=Math.cos(_4);
|
||
66 | var s=Math.sin(_4);
|
||
67 | return new dojox.gfx3d.matrix.Matrix3D({yy:c,yz:-s,zy:s,zz:c}); |
||
68 | },rotateXg:function(_5){ |
||
69 | return dojox.gfx3d.matrix.rotateX(dojox.gfx3d.matrix._degToRad(_5));
|
||
70 | },rotateY:function(_6){ |
||
71 | var c=Math.cos(_6);
|
||
72 | var s=Math.sin(_6);
|
||
73 | return new dojox.gfx3d.matrix.Matrix3D({xx:c,xz:s,zx:-s,zz:c}); |
||
74 | },rotateYg:function(_7){ |
||
75 | return dojox.gfx3d.matrix.rotateY(dojox.gfx3d.matrix._degToRad(_7));
|
||
76 | },rotateZ:function(_8){ |
||
77 | var c=Math.cos(_8);
|
||
78 | var s=Math.sin(_8);
|
||
79 | return new dojox.gfx3d.matrix.Matrix3D({xx:c,xy:-s,yx:s,yy:c}); |
||
80 | },rotateZg:function(_9){ |
||
81 | return dojox.gfx3d.matrix.rotateZ(dojox.gfx3d.matrix._degToRad(_9));
|
||
82 | },cameraTranslate:function(a,b,c){ |
||
83 | if(arguments.length>1){ |
||
84 | return new dojox.gfx3d.matrix.Matrix3D({dx:-a,dy:-b,dz:-c}); |
||
85 | } |
||
86 | return new dojox.gfx3d.matrix.Matrix3D({dx:-a.x,dy:-a.y,dz:-a.z}); |
||
87 | },cameraRotateX:function(_a){ |
||
88 | var c=Math.cos(-_a);
|
||
89 | var s=Math.sin(-_a);
|
||
90 | return new dojox.gfx3d.matrix.Matrix3D({yy:c,yz:-s,zy:s,zz:c}); |
||
91 | },cameraRotateXg:function(_b){ |
||
92 | return dojox.gfx3d.matrix.rotateX(dojox.gfx3d.matrix._degToRad(_b));
|
||
93 | },cameraRotateY:function(_c){ |
||
94 | var c=Math.cos(-_c);
|
||
95 | var s=Math.sin(-_c);
|
||
96 | return new dojox.gfx3d.matrix.Matrix3D({xx:c,xz:s,zx:-s,zz:c}); |
||
97 | },cameraRotateYg:function(_d){ |
||
98 | return dojox.gfx3d.matrix.rotateY(dojox.gfx3d.matrix._degToRad(_d));
|
||
99 | },cameraRotateZ:function(_e){ |
||
100 | var c=Math.cos(-_e);
|
||
101 | var s=Math.sin(-_e);
|
||
102 | return new dojox.gfx3d.matrix.Matrix3D({xx:c,xy:-s,yx:s,yy:c}); |
||
103 | },cameraRotateZg:function(_f){ |
||
104 | return dojox.gfx3d.matrix.rotateZ(dojox.gfx3d.matrix._degToRad(_f));
|
||
105 | },normalize:function(_10){ |
||
106 | return (_10 instanceof dojox.gfx3d.matrix.Matrix3D)?_10:new dojox.gfx3d.matrix.Matrix3D(_10); |
||
107 | },clone:function(_11){ |
||
108 | var obj=new dojox.gfx3d.matrix.Matrix3D(); |
||
109 | for(var i in _11){ |
||
110 | if(typeof (_11[i])=="number"&&typeof (obj[i])=="number"&&obj[i]!=_11[i]){ |
||
111 | obj[i]=_11[i]; |
||
112 | } |
||
113 | } |
||
114 | return obj;
|
||
115 | },invert:function(_12){ |
||
116 | var m=dojox.gfx3d.matrix.normalize(_12);
|
||
117 | var D=m.xx*m.yy*m.zz+m.xy*m.yz*m.zx+m.xz*m.yx*m.zy-m.xx*m.yz*m.zy-m.xy*m.yx*m.zz-m.xz*m.yy*m.zx;
|
||
118 | var M=new dojox.gfx3d.matrix.Matrix3D({xx:(m.yy*m.zz-m.yz*m.zy)/D,xy:(m.xz*m.zy-m.xy*m.zz)/D,xz:(m.xy*m.yz-m.xz*m.yy)/D,yx:(m.yz*m.zx-m.yx*m.zz)/D,yy:(m.xx*m.zz-m.xz*m.zx)/D,yz:(m.xz*m.yx-m.xx*m.yz)/D,zx:(m.yx*m.zy-m.yy*m.zx)/D,zy:(m.xy*m.zx-m.xx*m.zy)/D,zz:(m.xx*m.yy-m.xy*m.yx)/D,dx:-1*(m.xy*m.yz*m.dz+m.xz*m.dy*m.zy+m.dx*m.yy*m.zz-m.xy*m.dy*m.zz-m.xz*m.yy*m.dz-m.dx*m.yz*m.zy)/D,dy:(m.xx*m.yz*m.dz+m.xz*m.dy*m.zx+m.dx*m.yx*m.zz-m.xx*m.dy*m.zz-m.xz*m.yx*m.dz-m.dx*m.yz*m.zx)/D,dz:-1*(m.xx*m.yy*m.dz+m.xy*m.dy*m.zx+m.dx*m.yx*m.zy-m.xx*m.dy*m.zy-m.xy*m.yx*m.dz-m.dx*m.yy*m.zx)/D}); |
||
119 | return M;
|
||
120 | },_multiplyPoint:function(m,x,y,z){ |
||
121 | return {x:m.xx*x+m.xy*y+m.xz*z+m.dx,y:m.yx*x+m.yy*y+m.yz*z+m.dy,z:m.zx*x+m.zy*y+m.zz*z+m.dz}; |
||
122 | },multiplyPoint:function(_13,a,b,c){ |
||
123 | var m=dojox.gfx3d.matrix.normalize(_13);
|
||
124 | if(typeof a=="number"&&typeof b=="number"&&typeof c=="number"){ |
||
125 | return dojox.gfx3d.matrix._multiplyPoint(m,a,b,c);
|
||
126 | } |
||
127 | return dojox.gfx3d.matrix._multiplyPoint(m,a.x,a.y,a.z);
|
||
128 | },multiply:function(_14){ |
||
129 | var m=dojox.gfx3d.matrix.normalize(_14);
|
||
130 | for(var i=1;i<arguments.length;++i){ |
||
131 | var l=m;
|
||
132 | var r=dojox.gfx3d.matrix.normalize(arguments[i]); |
||
133 | m=new dojox.gfx3d.matrix.Matrix3D();
|
||
134 | m.xx=l.xx*r.xx+l.xy*r.yx+l.xz*r.zx; |
||
135 | m.xy=l.xx*r.xy+l.xy*r.yy+l.xz*r.zy; |
||
136 | m.xz=l.xx*r.xz+l.xy*r.yz+l.xz*r.zz; |
||
137 | m.yx=l.yx*r.xx+l.yy*r.yx+l.yz*r.zx; |
||
138 | m.yy=l.yx*r.xy+l.yy*r.yy+l.yz*r.zy; |
||
139 | m.yz=l.yx*r.xz+l.yy*r.yz+l.yz*r.zz; |
||
140 | m.zx=l.zx*r.xx+l.zy*r.yx+l.zz*r.zx; |
||
141 | m.zy=l.zx*r.xy+l.zy*r.yy+l.zz*r.zy; |
||
142 | m.zz=l.zx*r.xz+l.zy*r.yz+l.zz*r.zz; |
||
143 | m.dx=l.xx*r.dx+l.xy*r.dy+l.xz*r.dz+l.dx; |
||
144 | m.dy=l.yx*r.dx+l.yy*r.dy+l.yz*r.dz+l.dy; |
||
145 | m.dz=l.zx*r.dx+l.zy*r.dy+l.zz*r.dz+l.dz; |
||
146 | } |
||
147 | return m;
|
||
148 | },_project:function(m,x,y,z){ |
||
149 | return {x:m.xx*x+m.xy*y+m.xz*z+m.dx,y:m.yx*x+m.yy*y+m.yz*z+m.dy,z:m.zx*x+m.zy*y+m.zz*z+m.dz}; |
||
150 | },project:function(_15,a,b,c){ |
||
151 | var m=dojox.gfx3d.matrix.normalize(_15);
|
||
152 | if(typeof a=="number"&&typeof b=="number"&&typeof c=="number"){ |
||
153 | return dojox.gfx3d.matrix._project(m,a,b,c);
|
||
154 | } |
||
155 | return dojox.gfx3d.matrix._project(m,a.x,a.y,a.z);
|
||
156 | }}); |
||
157 | dojox.gfx3d.Matrix3D=dojox.gfx3d.matrix.Matrix3D; |
||
158 | } |