📄 appendix-e.html
字号:
* Resets the matrix.
*/
public void makeIdentity(){
xx = 1; xy = 0; xz = 0; xo = 0;
yx = 0; yy = 1; yz = 0; yo = 0;
zx = 0; zy = 0; zz = 1; zo = 0;
}
/**
* "Smart" multiplies a rotation about Z-axis
*/
public void concatRz(double az){
double ct = Math.cos(az);
double st = Math.sin(az);
double Nyx = (yx * ct + xx * st);
double Nyy = (yy * ct + xy * st);
double Nyz = (yz * ct + xz * st);
double Nyo = (yo * ct + xo * st);
double Nxx = (xx * ct - yx * st);
double Nxy = (xy * ct - yy * st);
double Nxz = (xz * ct - yz * st);
double Nxo = (xo * ct - yo * st);
xx = Nxx; xy = Nxy; xz = Nxz; xo = Nxo;
yx = Nyx; yy = Nyy; yz = Nyz; yo = Nyo;
}
/**
* "Smart" multiplies a rotation about Y-axis
*/
public void concatRy(double ay){
double ct = Math.cos(ay);
double st = Math.sin(ay);
double Nxx = (xx * ct + zx * st);
double Nxy = (xy * ct + zy * st);
double Nxz = (xz * ct + zz * st);
double Nxo = (xo * ct + zo * st);
double Nzx = (zx * ct - xx * st);
double Nzy = (zy * ct - xy * st);
double Nzz = (zz * ct - xz * st);
double Nzo = (zo * ct - xo * st);
xx = Nxx; xy = Nxy; xz = Nxz; xo = Nxo;
zx = Nzx; zy = Nzy; zz = Nzz; zo = Nzo;
}
/**
* "Smart" multiplies a rotation about X-axis
*/
public void concatRx(double ax){
double ct = Math.cos(ax);
double st = Math.sin(ax);
double Nyx = (yx * ct + zx * st);
double Nyy = (yy * ct + zy * st);
double Nyz = (yz * ct + zz * st);
double Nyo = (yo * ct + zo * st);
double Nzx = (zx * ct - yx * st);
double Nzy = (zy * ct - yy * st);
double Nzz = (zz * ct - yz * st);
double Nzo = (zo * ct - yo * st);
yx = Nyx; yy = Nyy; yz = Nyz; yo = Nyo;
zx = Nzx; zy = Nzy; zz = Nzz; zo = Nzo;
}
/**
* "Smart" multiplies a translation
*/
public void concatT(double x,double y,double z){
xo+=x; yo+=y; zo+=z;
}
/**
* "Smart" multiplies scaling
*/
public void concatS(double sx,double sy,double sz){
xx *= sx; xy *= sx; xz *= sx; xo *= sx;
yx *= sy; yy *= sy; yz *= sy; yo *= sy;
zx *= sz; zy *= sz; zz *= sz; zo *= sz;
}
/**
* Multiplies the vector "ps" of 3d points and stores the result
* in "pd".
*/
public void transform(fArrayOf3dPoints ps,fArrayOf3dPoints pd){
for (int i=0; i<ps.npoints; i++) {
double x=ps.x[i]; double y=ps.y[i]; double z=ps.z[i];
pd.x[i] = x*xx + y*xy + z*xz + xo;
pd.y[i] = x*yx + y*yy + z*yz + yo;
pd.z[i] = x*zx + y*zy + z*zz + zo;
}
}
}
</PRE>
<!-- END CODE //-->
<P>Using the matrix class from Listing E-4, the code for making the transformations seen in Figure E-27 would be
</P>
<!-- CODE SNIP //-->
<PRE>
fGeneric3dMatrix M;
M.makeIdentity(); //-- make identity matrix
M.concatS(2,2,2); //-- scale the points
M.concatRz(Math.PI/2); //-- rotate about z-axis
M.concatT(4,4,0); //-- translate by 4,4,0 units
M.transformPoints(xf,yf,zf,xt,yt,zt,pts); //-- transform the points
</PRE>
<!-- END CODE SNIP //-->
<P>In the matrix class, all methods starting with “concat” are actually selective matrix multiplications. What this means is that only the elements that are affected are recalculated, saving lots of time.
</P>
<!-- CODE //-->
<PRE>
/**
* A 3d matrix that hides the making of the different
* transforms
*/
class fMatrix3d extends fGeneric3dMatrix {
/**
* construct the matrix
*/
public fMatrix3d(){
super();
}
/**
* let matrix contain the MCS to WCS transform
*/
public void makeMCStoWCStransform(fPoint3d pos,fAngle3d agl,fPoint3d scale){
makeIdentity();
concatS(scale.x,scale.y,scale.z);
concatRx(agl.x);
concatRy(agl.y);
concatRz(agl.z);
concatT(pos.x,pos.y,pos.z);
}
/**
* let matrix contain the WCS to MCS transform
*/
public void makeWCStoVCStransform(fPoint3d pos,fAngle3d agl){
makeIdentity();
concatT(-pos.x,-pos.y,-pos.z);
concatRz(-agl.z);
concatRy(-agl.y);
concatRx(-agl.x);
}
/**
* A transform used in camera classes.
*/
public void makeLookAtPointTransform(fPoint3d p0,fPoint3d p1){
fPoint3d vecZaxis=new fPoint3d(p1,p0);
vecZaxis.normalize(1);
fPoint3d vecXaxis=new fPoint3d();
vecXaxis.vectorProduct(new fPoint3d(0,1,0), vecZaxis);
vecXaxis.normalize(1);
fPoint3d vecYaxis=new fPoint3d();
vecYaxis.vectorProduct(vecZaxis,vecXaxis);
xx=vecXaxis.x; xy=vecXaxis.y; xz=vecXaxis.z;
yx=vecYaxis.x; yy=vecYaxis.y; yz=vecYaxis.z;
zx=vecZaxis.x; zy=vecZaxis.y; zz=vecZaxis.z;
xo = yo = zo = 0;
xo = xx*(-p0.x) + xy*(-p0.y) + xz*(-p0.z) + xo;
yo = yx*(-p0.x) + yy*(-p0.y) + yz*(-p0.z) + yo;
zo = zx*(-p0.x) + zy*(-p0.y) + zz*(-p0.z) + zo;
}
}
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>Summary</B></FONT></P>
<P>Armed with the math and the classes supplied in this chapter you have all the basic tools to make 3D transforms. In Chapter 11 you learned how to use these classes to create some real 3D graphics. If you wonder where the classes fPoint3d and fAngle3d came from, don’t worry. You can find them on the CD just like all other classes. The fPoint3d and fAngle3d classes contain all the basic and not so basic operations that can be performed with vectors. The most important operations are described in this appendix and the implementation is very straightforward.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="ewtoc.html">Table of Contents</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -