aboutsummaryrefslogtreecommitdiff
path: root/man/man3/quaternion.3
blob: 224baea7206af6c92d7df6a859f9c8c5be309abe (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
.TH QUATERNION 3
.SH NAME
qtom, mtoq, qadd, qsub, qneg, qmul, qdiv, qunit, qinv, qlen, slerp, qmid, qsqrt \- Quaternion arithmetic
.SH SYNOPSIS
.PP
.B
#include <draw.h>
.PP
.B
#include <geometry.h>
.PP
.B
Quaternion qadd(Quaternion q, Quaternion r)
.PP
.B
Quaternion qsub(Quaternion q, Quaternion r)
.PP
.B
Quaternion qneg(Quaternion q)
.PP
.B
Quaternion qmul(Quaternion q, Quaternion r)
.PP
.B
Quaternion qdiv(Quaternion q, Quaternion r)
.PP
.B
Quaternion qinv(Quaternion q)
.PP
.B
double qlen(Quaternion p)
.PP
.B
Quaternion qunit(Quaternion q)
.PP
.B
void qtom(Matrix m, Quaternion q)
.PP
.B
Quaternion mtoq(Matrix mat)
.PP
.B
Quaternion slerp(Quaternion q, Quaternion r, double a)
.PP
.B
Quaternion qmid(Quaternion q, Quaternion r)
.PP
.B
Quaternion qsqrt(Quaternion q)
.SH DESCRIPTION
The Quaternions are a non-commutative extension field of the Real numbers, designed
to do for rotations in 3-space what the complex numbers do for rotations in 2-space.
Quaternions have a real component
.I r
and an imaginary vector component \fIv\fP=(\fIi\fP,\fIj\fP,\fIk\fP).
Quaternions add componentwise and multiply according to the rule
(\fIr\fP,\fIv\fP)(\fIs\fP,\fIw\fP)=(\fIrs\fP-\fIv\fP\v'-.3m'.\v'.3m'\fIw\fP, \fIrw\fP+\fIvs\fP+\fIv\fP×\fIw\fP),
where \v'-.3m'.\v'.3m' and × are the ordinary vector dot and cross products.
The multiplicative inverse of a non-zero quaternion (\fIr\fP,\fIv\fP)
is (\fIr\fP,\fI-v\fP)/(\fIr\^\fP\u\s-22\s+2\d-\fIv\fP\v'-.3m'.\v'.3m'\fIv\fP).
.PP
The following routines do arithmetic on quaternions, represented as
.IP
.EX
.ta 6n
typedef struct Quaternion Quaternion;
struct Quaternion{
	double r, i, j, k;
};
.EE
.TF qunit
.TP
Name
Description
.TP
.B qadd
Add two quaternions.
.TP
.B qsub
Subtract two quaternions.
.TP
.B qneg
Negate a quaternion.
.TP
.B qmul
Multiply two quaternions.
.TP
.B qdiv
Divide two quaternions.
.TP
.B qinv
Return the multiplicative inverse of a quaternion.
.TP
.B qlen
Return
.BR sqrt(q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k) ,
the length of a quaternion.
.TP
.B qunit
Return a unit quaternion 
.RI ( length=1 )
with components proportional to
.IR q 's.
.PD
.PP
A rotation by angle \fIθ\fP about axis
.I A
(where
.I A
is a unit vector) can be represented by
the unit quaternion \fIq\fP=(cos \fIθ\fP/2, \fIA\fPsin \fIθ\fP/2).
The same rotation is represented by \(mi\fIq\fP; a rotation by \(mi\fIθ\fP about \(mi\fIA\fP is the same as a rotation by \fIθ\fP about \fIA\fP.
The quaternion \fIq\fP transforms points by
(0,\fIx',y',z'\fP) = \%\fIq\fP\u\s-2-1\s+2\d(0,\fIx,y,z\fP)\fIq\fP.
Quaternion multiplication composes rotations.
The orientation of an object in 3-space can be represented by a quaternion
giving its rotation relative to some `standard' orientation.
.PP
The following routines operate on rotations or orientations represented as unit quaternions:
.TF slerp
.TP
.B mtoq
Convert a rotation matrix (see
.IR matrix (3))
to a unit quaternion.
.TP
.B qtom
Convert a unit quaternion to a rotation matrix.
.TP
.B slerp
Spherical lerp.  Interpolate between two orientations.
The rotation that carries
.I q
to
.I r
is \%\fIq\fP\u\s-2-1\s+2\d\fIr\fP, so
.B slerp(q, r, t)
is \fIq\fP(\fIq\fP\u\s-2-1\s+2\d\fIr\fP)\u\s-2\fIt\fP\s+2\d.
.TP
.B qmid
.B slerp(q, r, .5)
.TP
.B qsqrt
The square root of
.IR q .
This is just a rotation about the same axis by half the angle.
.PD
.SH SOURCE
.B \*9/src/libgeometry/quaternion.c
.SH SEE ALSO
.IR matrix (3),
.IR qball (3)
.SH BUGS
To avoid name conflicts with NetBSD,
.I qdiv
is a preprocessor macro defined as 
.IR p9qdiv ;
see
.IR intro (3).