350 lines · 5.9 KB · Text
RawDownload
.TH MATRIX 2.SH NAMEident, matmul, matmulr, determinant, adjoint, invertmat, xformpoint, xformpointd, xformplane, pushmat, popmat, rot, qrot, scale, move, xform, ixform, persp, look, viewport \- Geometric transformations.SH SYNOPSIS.PP.B#include <draw.h>.PP.B#include <geometry.h>.PP.Bvoid ident(Matrix m).PP.Bvoid matmul(Matrix a, Matrix b).PP.Bvoid matmulr(Matrix a, Matrix b).PP.Bdouble determinant(Matrix m).PP.Bvoid adjoint(Matrix m, Matrix madj).PP.Bdouble invertmat(Matrix m, Matrix inv).PP.BPoint3 xformpoint(Point3 p, Space *to, Space *from).PP.BPoint3 xformpointd(Point3 p, Space *to, Space *from).PP.BPoint3 xformplane(Point3 p, Space *to, Space *from).PP.BSpace *pushmat(Space *t).PP.BSpace *popmat(Space *t).PP.Bvoid rot(Space *t, double theta, int axis).PP.Bvoid qrot(Space *t, Quaternion q).PP.Bvoid scale(Space *t, double x, double y, double z).PP.Bvoid move(Space *t, double x, double y, double z).PP.Bvoid xform(Space *t, Matrix m).PP.Bvoid ixform(Space *t, Matrix m, Matrix inv).PP.Bint persp(Space *t, double fov, double n, double f).PP.Bvoid look(Space *t, Point3 eye, Point3 look, Point3 up).PP.Bvoid viewport(Space *t, Rectangle r, double aspect).SH DESCRIPTIONThese routines manipulate 3-space affine and projective transformations,represented as 4\(mu4 matrices, thus:.IP.EX.ta 6ntypedef double Matrix[4][4];.EE.PP.I Identstores an identity matrix in its argument..I Matmulstores.I a\(mubin.IR a ..I Matmulrstores.I b\(muain.IR b ..I Determinantreturns the determinant of matrix.IR m ..I Adjointstores the adjoint (matrix of cofactors) of.I min.IR madj ..I Invertmatstores the inverse of matrix.I min.IR minv ,returning.IR m 'sdeterminant.Should.I mbe singular (determinant zero),.I invertmatstores itsadjoint in.IR minv ..PPThe rest of the routines described heremanipulate.I Spacesand transform.IR Point3s .A.I Point3is a point in three-space, represented by itshomogeneous coordinates:.IP.EXtypedef struct Point3 Point3;struct Point3{	double x, y, z, w;};.EE.PPThe homogeneous coordinates.RI ( x ,.IR y ,.IR z ,.IR w )represent the Euclidean point.RI ( x / w ,.IR y / w ,.IR z / w )if.IR w ≠0,and a ``point at infinity'' if.IR w =0..PPA.I Spaceis just a data structure describing a coordinate system:.IP.EXtypedef struct Space Space;struct Space{	Matrix t;	Matrix tinv;	Space *next;};.EE.PPIt contains a pair of transformation matrices and a pointerto the.IR Space 'sparent.  The matrices transform points to and from the ``rootcoordinate system,'' which is represented by a null.I Spacepointer..PP.I Pushmatcreates a new.IR Space .Its argument is a pointer to the parent space.  Its resultis a newly allocated copy of the parent, but with its.B nextpointer pointing at the parent..I Popmatdiscards the.B Spacethat is its argument, returning a pointer to the stack.Nominally, these two functions define a stack of transformations,but.B pushmatcan be called multiple timeson the same.B Spacemultiple times, creating a transformation tree..PP.I Xformpointand.I Xformpointdboth transform points from the.B Spacepointed to by.I fromto the space pointed to by.IR to .Either pointer may be null, indicating the root coordinate system.The difference between the two functions is that.B xformpointddivides.IR x ,.IR y ,.IR z ,and.I wby.IR w ,if.IR w ≠0,making.RI ( x ,.IR y ,.IR z )the Euclidean coordinates of the point..PP.I Xformplanetransforms planes or normal vectors.  A plane is specified by thecoefficients.RI ( a ,.IR b ,.IR c ,.IR d )of its implicit equation.IR ax+by+cz+d =0.Since this representation is dual to the homogeneous representation of points,.B libgeometryrepresents planes by.B Point3structures, with.RI ( a ,.IR b ,.IR c ,.IR d )stored in.RI ( x ,.IR y ,.IR z ,.IR w )..PPThe remaining functions transform the coordinate system representedby a.BR Space .Their.B Space *argument must be non-null \(em you can't modify the root.BR Space ..I Rotrotates by angle.I theta(in radians) about the given.IR axis ,which must be one of.BR XAXIS ,.B YAXISor.BR ZAXIS ..I Qrottransforms by a rotation about an arbitrary axis, specified by.B Quaternion.IR q ..PP.I Scalescales the coordinate system by the given scale factors in the directions of the three axes..IB Movetranslates by the given displacement in the three axial directions..PP.I Xformtransforms the coordinate system by the given.BR Matrix .If the matrix's inverse is known.I a.IR priori ,calling.I ixformwill save the work of recomputing it..PP.I Perspdoes a perspective transformation.The transformation maps the frustum with apex at the origin,central axis down the positive.I yaxis, and apex angle.I fovand clipping planes.IR y = nand.IR y = finto the double-unit cube.The plane.IR y = nmaps to.IR y '=-1,.IR y = fmaps to.IR y '=1..PP.I Lookdoes a view-pointing transformation.  The.B eyepoint is moved to the origin.The line through the.I eyeand.I lookpoints is aligned with the y axis,and the plane containing the.BR eye ,.B lookand.B uppoints is rotated into the.IR x - yplane..PP.I Viewportmaps the unit-cube window into the given screen viewport.The viewport rectangle.I rhas.IB r .minat the top left-hand corner, and.IB r .maxjust outside the lower right-hand corner.Argument.I aspectis the aspect ratio.RI ( dx / dy )of the viewport's pixels (not of the whole viewport).The whole window is transformed to fit centered inside the viewport with equalslop on either top and bottom or left and right, depending on the viewport'saspect ratio.The window is viewed down the.I yaxis, with.I xto the left and.I zup.  The viewporthas.I xincreasing to the right and.I yincreasing down.  The window's.I ycoordinates are mapped, unchanged, into the viewport's.I zcoordinates..SH SOURCE.B /sys/src/libgeometry/matrix.c.SH "SEE ALSO.IR arith3 (2)