-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquat.h
76 lines (66 loc) · 1.42 KB
/
quat.h
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
#ifndef __QUAT_H__
#define __QUAT_H__
typedef struct{
float x, y, z, w;
}quat;
__inline quat quat_make( float x, float y, float z, float w ){
quat temp;
temp.x = x;
temp.y = y;
temp.z = z;
temp.w = w;
return temp;
}
__inline float quat_magnitude( quat q ){
return (float)sqrt(q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w);
}
__inline quat quat_scale(quat q, const float scale){
quat temp;
temp.x = q.x*scale;
temp.y = q.y*scale;
temp.z = q.z*scale;
temp.w = q.w*scale;
return temp;
}
__inline quat quat_normalize(quat q){
quat temp;
float scale = 1.f/quat_magnitude(q);
temp.x = q.x*scale;
temp.y = q.y*scale;
temp.z = q.z*scale;
temp.w = q.w*scale;
return temp;
}
__inline quat quat_interpolate( quat from, quat to, float t){
quat to1;
quat temp;
float omega, cosom, sinom, scale0, scale1;
float prediv = 0.f;
cosom = from.x*to.x+
from.y*to.y+
from.z*to.z+
from.w*to.w;
if(cosom<0.f){
cosom = -cosom;
to1.x = -to.x;
to1.y = -to.y;
to1.z = -to.z;
to1.w = -to.w;
}else{
to1.x = to.x;
to1.y = to.y;
to1.z = to.z;
to1.w = to.w;
}
omega = (float)acos(cosom);
sinom = (float)sin(omega);
if(sinom!=0) prediv = 1.f/sinom;
scale0 = (float)sin((1.f-t)*omega)*prediv;
scale1 = (float)sin(t*omega)*prediv;
temp.x = scale0*from.x + scale1*to1.x;
temp.y = scale0*from.y + scale1*to1.y;
temp.z = scale0*from.z + scale1*to1.z;
temp.w = scale0*from.w + scale1*to1.w;
return temp;
}
#endif /* __QUAT_H__ */