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