aboutsummaryrefslogtreecommitdiff
path: root/src/libgeometry/quaternion.c
blob: 1f920f5a8cd4768fe9352b710aaa00087721e021 (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
/*
 * Quaternion arithmetic:
 *	qadd(q, r)	returns q+r
 *	qsub(q, r)	returns q-r
 *	qneg(q)		returns -q
 *	qmul(q, r)	returns q*r
 *	qdiv(q, r)	returns q/r, can divide check.
 *	qinv(q)		returns 1/q, can divide check.
 *	double qlen(p)	returns modulus of p
 *	qunit(q)	returns a unit quaternion parallel to q
 * The following only work on unit quaternions and rotation matrices:
 *	slerp(q, r, a)	returns q*(r*q^-1)^a
 *	qmid(q, r)	slerp(q, r, .5) 
 *	qsqrt(q)	qmid(q, (Quaternion){1,0,0,0})
 *	qtom(m, q)	converts a unit quaternion q into a rotation matrix m
 *	mtoq(m)		returns a quaternion equivalent to a rotation matrix m
 */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <geometry.h>
void qtom(Matrix m, Quaternion q){
#ifndef new
	m[0][0]=1-2*(q.j*q.j+q.k*q.k);
	m[0][1]=2*(q.i*q.j+q.r*q.k);
	m[0][2]=2*(q.i*q.k-q.r*q.j);
	m[0][3]=0;
	m[1][0]=2*(q.i*q.j-q.r*q.k);
	m[1][1]=1-2*(q.i*q.i+q.k*q.k);
	m[1][2]=2*(q.j*q.k+q.r*q.i);
	m[1][3]=0;
	m[2][0]=2*(q.i*q.k+q.r*q.j);
	m[2][1]=2*(q.j*q.k-q.r*q.i);
	m[2][2]=1-2*(q.i*q.i+q.j*q.j);
	m[2][3]=0;
	m[3][0]=0;
	m[3][1]=0;
	m[3][2]=0;
	m[3][3]=1;
#else
	/*
	 * Transcribed from Ken Shoemake's new code -- not known to work
	 */
	double Nq = q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k;
	double s = (Nq > 0.0) ? (2.0 / Nq) : 0.0;
	double xs = q.i*s,		ys = q.j*s,		zs = q.k*s;
	double wx = q.r*xs,		wy = q.r*ys,		wz = q.r*zs;
	double xx = q.i*xs,		xy = q.i*ys,		xz = q.i*zs;
	double yy = q.j*ys,		yz = q.j*zs,		zz = q.k*zs;
	m[0][0] = 1.0 - (yy + zz); m[1][0] = xy + wz;         m[2][0] = xz - wy;
	m[0][1] = xy - wz;         m[1][1] = 1.0 - (xx + zz); m[2][1] = yz + wx;
	m[0][2] = xz + wy;         m[1][2] = yz - wx;         m[2][2] = 1.0 - (xx + yy);
	m[0][3] = m[1][3] = m[2][3] = m[3][0] = m[3][1] = m[3][2] = 0.0;
	m[3][3] = 1.0;
#endif
}
Quaternion mtoq(Matrix mat){
#ifndef new
#define	EPS	1.387778780781445675529539585113525e-17	/* 2^-56 */
	double t;
	Quaternion q;
	q.r=0.;
	q.i=0.;
	q.j=0.;
	q.k=1.;
	if((t=.25*(1+mat[0][0]+mat[1][1]+mat[2][2]))>EPS){
		q.r=sqrt(t);
		t=4*q.r;
		q.i=(mat[1][2]-mat[2][1])/t;
		q.j=(mat[2][0]-mat[0][2])/t;
		q.k=(mat[0][1]-mat[1][0])/t;
	}
	else if((t=-.5*(mat[1][1]+mat[2][2]))>EPS){
		q.i=sqrt(t);
		t=2*q.i;
		q.j=mat[0][1]/t;
		q.k=mat[0][2]/t;
	}
	else if((t=.5*(1-mat[2][2]))>EPS){
		q.j=sqrt(t);
		q.k=mat[1][2]/(2*q.j);
	}
	return q;
#else
	/*
	 * Transcribed from Ken Shoemake's new code -- not known to work
	 */
	/* This algorithm avoids near-zero divides by looking for a large
	 * component -- first r, then i, j, or k.  When the trace is greater than zero,
	 * |r| is greater than 1/2, which is as small as a largest component can be.
	 * Otherwise, the largest diagonal entry corresponds to the largest of |i|,
	 * |j|, or |k|, one of which must be larger than |r|, and at least 1/2.
	 */
	Quaternion qu;
	double tr, s;
	
	tr = mat[0][0] + mat[1][1] + mat[2][2];
	if (tr >= 0.0) {
		s = sqrt(tr + mat[3][3]);
		qu.r = s*0.5;
		s = 0.5 / s;
		qu.i = (mat[2][1] - mat[1][2]) * s;
		qu.j = (mat[0][2] - mat[2][0]) * s;
		qu.k = (mat[1][0] - mat[0][1]) * s;
	}
	else {
		int i = 0;
		if (mat[1][1] > mat[0][0]) i = 1;
		if (mat[2][2] > mat[i][i]) i = 2;
		switch(i){
		case 0:
			s = sqrt( (mat[0][0] - (mat[1][1]+mat[2][2])) + mat[3][3] );
			qu.i = s*0.5;
			s = 0.5 / s;
			qu.j = (mat[0][1] + mat[1][0]) * s;
			qu.k = (mat[2][0] + mat[0][2]) * s;
			qu.r = (mat[2][1] - mat[1][2]) * s;
			break;
		case 1:
			s = sqrt( (mat[1][1] - (mat[2][2]+mat[0][0])) + mat[3][3] );
			qu.j = s*0.5;
			s = 0.5 / s;
			qu.k = (mat[1][2] + mat[2][1]) * s;
			qu.i = (mat[0][1] + mat[1][0]) * s;
			qu.r = (mat[0][2] - mat[2][0]) * s;
			break;
		case 2:
			s = sqrt( (mat[2][2] - (mat[0][0]+mat[1][1])) + mat[3][3] );
			qu.k = s*0.5;
			s = 0.5 / s;
			qu.i = (mat[2][0] + mat[0][2]) * s;
			qu.j = (mat[1][2] + mat[2][1]) * s;
			qu.r = (mat[1][0] - mat[0][1]) * s;
			break;
		}
	}
	if (mat[3][3] != 1.0){
		s=1/sqrt(mat[3][3]);
		qu.r*=s;
		qu.i*=s;
		qu.j*=s;
		qu.k*=s;
	}
	return (qu);
#endif
}
Quaternion qadd(Quaternion q, Quaternion r){
	q.r+=r.r;
	q.i+=r.i;
	q.j+=r.j;
	q.k+=r.k;
	return q;
}
Quaternion qsub(Quaternion q, Quaternion r){
	q.r-=r.r;
	q.i-=r.i;
	q.j-=r.j;
	q.k-=r.k;
	return q;
}
Quaternion qneg(Quaternion q){
	q.r=-q.r;
	q.i=-q.i;
	q.j=-q.j;
	q.k=-q.k;
	return q;
}
Quaternion qmul(Quaternion q, Quaternion r){
	Quaternion s;
	s.r=q.r*r.r-q.i*r.i-q.j*r.j-q.k*r.k;
	s.i=q.r*r.i+r.r*q.i+q.j*r.k-q.k*r.j;
	s.j=q.r*r.j+r.r*q.j+q.k*r.i-q.i*r.k;
	s.k=q.r*r.k+r.r*q.k+q.i*r.j-q.j*r.i;
	return s;
}
Quaternion qdiv(Quaternion q, Quaternion r){
	return qmul(q, qinv(r));
}
Quaternion qunit(Quaternion q){
	double l=qlen(q);
	q.r/=l;
	q.i/=l;
	q.j/=l;
	q.k/=l;
	return q;
}
/*
 * Bug?: takes no action on divide check
 */
Quaternion qinv(Quaternion q){
	double l=q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k;
	q.r/=l;
	q.i=-q.i/l;
	q.j=-q.j/l;
	q.k=-q.k/l;
	return q;
}
double qlen(Quaternion p){
	return sqrt(p.r*p.r+p.i*p.i+p.j*p.j+p.k*p.k);
}
Quaternion slerp(Quaternion q, Quaternion r, double a){
	double u, v, ang, s;
	double dot=q.r*r.r+q.i*r.i+q.j*r.j+q.k*r.k;
	ang=dot<-1?PI:dot>1?0:acos(dot); /* acos gives NaN for dot slightly out of range */
	s=sin(ang);
	if(s==0) return ang<PI/2?q:r;
	u=sin((1-a)*ang)/s;
	v=sin(a*ang)/s;
	q.r=u*q.r+v*r.r;
	q.i=u*q.i+v*r.i;
	q.j=u*q.j+v*r.j;
	q.k=u*q.k+v*r.k;
	return q;
}
/*
 * Only works if qlen(q)==qlen(r)==1
 */
Quaternion qmid(Quaternion q, Quaternion r){
	double l;
	q=qadd(q, r);
	l=qlen(q);
	if(l<1e-12){
		q.r=r.i;
		q.i=-r.r;
		q.j=r.k;
		q.k=-r.j;
	}
	else{
		q.r/=l;
		q.i/=l;
		q.j/=l;
		q.k/=l;
	}
	return q;
}
/*
 * Only works if qlen(q)==1
 */
static Quaternion qident={1,0,0,0};
Quaternion qsqrt(Quaternion q){
	return qmid(q, qident);
}