blob: 7291725c62ed4f89f0a57dfebd6ce95f6d96f097 [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH MATRIX 3
2.SH NAME
3ident, matmul, matmulr, determinant, adjoint, invertmat, xformpoint, xformpointd, xformplane, pushmat, popmat, rot, qrot, scale, move, xform, ixform, persp, look, viewport \- Geometric transformations
4.SH SYNOPSIS
5.PP
6.B
7#include <draw.h>
8.PP
9.B
10#include <geometry.h>
11.PP
12.B
13void ident(Matrix m)
14.PP
15.B
16void matmul(Matrix a, Matrix b)
17.PP
18.B
19void matmulr(Matrix a, Matrix b)
20.PP
21.B
22double determinant(Matrix m)
23.PP
24.B
25void adjoint(Matrix m, Matrix madj)
26.PP
27.B
28double invertmat(Matrix m, Matrix inv)
29.PP
30.B
31Point3 xformpoint(Point3 p, Space *to, Space *from)
32.PP
33.B
34Point3 xformpointd(Point3 p, Space *to, Space *from)
35.PP
36.B
37Point3 xformplane(Point3 p, Space *to, Space *from)
38.PP
39.B
40Space *pushmat(Space *t)
41.PP
42.B
43Space *popmat(Space *t)
44.PP
45.B
46void rot(Space *t, double theta, int axis)
47.PP
48.B
49void qrot(Space *t, Quaternion q)
50.PP
51.B
52void scale(Space *t, double x, double y, double z)
53.PP
54.B
55void move(Space *t, double x, double y, double z)
56.PP
57.B
58void xform(Space *t, Matrix m)
59.PP
60.B
61void ixform(Space *t, Matrix m, Matrix inv)
62.PP
63.B
64int persp(Space *t, double fov, double n, double f)
65.PP
66.B
67void look(Space *t, Point3 eye, Point3 look, Point3 up)
68.PP
69.B
70void viewport(Space *t, Rectangle r, double aspect)
71.SH DESCRIPTION
72These routines manipulate 3-space affine and projective transformations,
73represented as 4\(mu4 matrices, thus:
74.IP
75.EX
76.ta 6n
77typedef double Matrix[4][4];
78.EE
79.PP
80.I Ident
81stores an identity matrix in its argument.
82.I Matmul
83stores
84.I a\(mub
85in
86.IR a .
87.I Matmulr
88stores
89.I b\(mua
90in
91.IR b .
92.I Determinant
93returns the determinant of matrix
94.IR m .
95.I Adjoint
96stores the adjoint (matrix of cofactors) of
97.I m
98in
99.IR madj .
100.I Invertmat
101stores the inverse of matrix
102.I m
103in
104.IR minv ,
105returning
106.IR m 's
107determinant.
108Should
109.I m
110be singular (determinant zero),
111.I invertmat
112stores its
113adjoint in
114.IR minv .
115.PP
116The rest of the routines described here
117manipulate
118.I Spaces
119and transform
120.IR Point3s .
121A
122.I Point3
123is a point in three-space, represented by its
124homogeneous coordinates:
125.IP
126.EX
127typedef struct Point3 Point3;
128struct Point3{
129 double x, y, z, w;
130};
131.EE
132.PP
133The homogeneous coordinates
134.RI ( x ,
135.IR y ,
136.IR z ,
137.IR w )
138represent the Euclidean point
139.RI ( x / w ,
140.IR y / w ,
141.IR z / w )
142if
143.IR w ≠0,
144and a ``point at infinity'' if
145.IR w =0.
146.PP
147A
148.I Space
149is just a data structure describing a coordinate system:
150.IP
151.EX
152typedef struct Space Space;
153struct Space{
154 Matrix t;
155 Matrix tinv;
156 Space *next;
157};
158.EE
159.PP
160It contains a pair of transformation matrices and a pointer
161to the
162.IR Space 's
163parent. The matrices transform points to and from the ``root
164coordinate system,'' which is represented by a null
165.I Space
166pointer.
167.PP
168.I Pushmat
169creates a new
170.IR Space .
171Its argument is a pointer to the parent space. Its result
172is a newly allocated copy of the parent, but with its
173.B next
174pointer pointing at the parent.
175.I Popmat
176discards the
177.B Space
178that is its argument, returning a pointer to the stack.
179Nominally, these two functions define a stack of transformations,
180but
181.B pushmat
182can be called multiple times
183on the same
184.B Space
185multiple times, creating a transformation tree.
186.PP
187.I Xformpoint
188and
189.I Xformpointd
190both transform points from the
191.B Space
192pointed to by
193.I from
194to the space pointed to by
195.IR to .
196Either pointer may be null, indicating the root coordinate system.
197The difference between the two functions is that
198.B xformpointd
199divides
200.IR x ,
201.IR y ,
202.IR z ,
203and
204.I w
205by
206.IR w ,
207if
208.IR w 0,
209making
210.RI ( x ,
211.IR y ,
212.IR z )
213the Euclidean coordinates of the point.
214.PP
215.I Xformplane
216transforms planes or normal vectors. A plane is specified by the
217coefficients
218.RI ( a ,
219.IR b ,
220.IR c ,
221.IR d )
222of its implicit equation
223.IR ax+by+cz+d =0.
224Since this representation is dual to the homogeneous representation of points,
225.B libgeometry
226represents planes by
227.B Point3
228structures, with
229.RI ( a ,
230.IR b ,
231.IR c ,
232.IR d )
233stored in
234.RI ( x ,
235.IR y ,
236.IR z ,
237.IR w ).
238.PP
239The remaining functions transform the coordinate system represented
240by a
241.BR Space .
242Their
243.B Space *
244argument must be non-null \(em you can't modify the root
245.BR Space .
246.I Rot
247rotates by angle
248.I theta
249(in radians) about the given
250.IR axis ,
251which must be one of
252.BR XAXIS ,
253.B YAXIS
254or
255.BR ZAXIS .
256.I Qrot
257transforms by a rotation about an arbitrary axis, specified by
258.B Quaternion
259.IR q .
260.PP
261.I Scale
262scales the coordinate system by the given scale factors in the directions of the three axes.
263.IB Move
264translates by the given displacement in the three axial directions.
265.PP
266.I Xform
267transforms the coordinate system by the given
268.BR Matrix .
269If the matrix's inverse is known
270.I a
271.IR priori ,
272calling
273.I ixform
274will save the work of recomputing it.
275.PP
276.I Persp
277does a perspective transformation.
278The transformation maps the frustum with apex at the origin,
279central axis down the positive
280.I y
281axis, and apex angle
282.I fov
283and clipping planes
284.IR y = n
285and
286.IR y = f
287into the double-unit cube.
288The plane
289.IR y = n
290maps to
291.IR y '=-1,
292.IR y = f
293maps to
294.IR y '=1.
295.PP
296.I Look
297does a view-pointing transformation. The
298.B eye
299point is moved to the origin.
300The line through the
301.I eye
302and
303.I look
304points is aligned with the y axis,
305and the plane containing the
306.BR eye ,
307.B look
308and
309.B up
310points is rotated into the
311.IR x - y
312plane.
313.PP
314.I Viewport
315maps the unit-cube window into the given screen viewport.
316The viewport rectangle
317.I r
318has
319.IB r .min
320at the top left-hand corner, and
321.IB r .max
322just outside the lower right-hand corner.
323Argument
324.I aspect
325is the aspect ratio
326.RI ( dx / dy )
327of the viewport's pixels (not of the whole viewport).
328The whole window is transformed to fit centered inside the viewport with equal
329slop on either top and bottom or left and right, depending on the viewport's
330aspect ratio.
331The window is viewed down the
332.I y
333axis, with
334.I x
335to the left and
336.I z
337up. The viewport
338has
339.I x
340increasing to the right and
341.I y
342increasing down. The window's
343.I y
344coordinates are mapped, unchanged, into the viewport's
345.I z
346coordinates.
347.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000348.B \*9/src/libgeometry/matrix.c
rsc058b0112005-01-03 06:40:20 +0000349.SH "SEE ALSO
350.IR arith3 (3)