blob: 07051a956821e72465dba122262f0bbb2333ae33 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17header:
18summary: Quaternion routines
19description:
20end:
21
22function: rsQuaternionAdd
23ret: void
24arg: rs_quaternion* q, "destination quaternion to add to"
25arg: const rs_quaternion* rhs, "right hand side quaternion to add"
26summary:
27description:
28 Add two quaternions
29inline:
30 q->w *= rhs->w;
31 q->x *= rhs->x;
32 q->y *= rhs->y;
33 q->z *= rhs->z;
34test: none
35end:
36
37function: rsQuaternionConjugate
38ret: void
39arg: rs_quaternion* q, "quaternion to conjugate"
40summary:
41description:
42 Conjugates the quaternion
43inline:
44 q->x = -q->x;
45 q->y = -q->y;
46 q->z = -q->z;
47test: none
48end:
49
50function: rsQuaternionDot
51ret: float, "dot product between q0 and q1"
52arg: const rs_quaternion* q0, "first quaternion"
53arg: const rs_quaternion* q1, "second quaternion"
54summary:
55description:
56 Dot product of two quaternions
57inline:
58 return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
59test: none
60end:
61
62function: rsQuaternionGetMatrixUnit
63ret: void
64arg: rs_matrix4x4* m, "resulting matrix"
65arg: const rs_quaternion* q, "normalized quaternion"
66summary:
67description:
68 Computes rotation matrix from the normalized quaternion
69inline:
70 float xx = q->x * q->x;
71 float xy = q->x * q->y;
72 float xz = q->x * q->z;
73 float xw = q->x * q->w;
74 float yy = q->y * q->y;
75 float yz = q->y * q->z;
76 float yw = q->y * q->w;
77 float zz = q->z * q->z;
78 float zw = q->z * q->w;
79
80 m->m[0] = 1.0f - 2.0f * ( yy + zz );
81 m->m[4] = 2.0f * ( xy - zw );
82 m->m[8] = 2.0f * ( xz + yw );
83 m->m[1] = 2.0f * ( xy + zw );
84 m->m[5] = 1.0f - 2.0f * ( xx + zz );
85 m->m[9] = 2.0f * ( yz - xw );
86 m->m[2] = 2.0f * ( xz - yw );
87 m->m[6] = 2.0f * ( yz + xw );
88 m->m[10] = 1.0f - 2.0f * ( xx + yy );
89 m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
90 m->m[15] = 1.0f;
91test: none
92end:
93
94function: rsQuaternionLoadRotateUnit
95ret: void
96arg: rs_quaternion* q, "quaternion to set"
97arg: float rot, "rot angle to rotate by"
98arg: float x, "component of a vector"
99arg: float y, "component of a vector"
100arg: float z, "component of a vector"
101summary:
102description:
103 Loads a quaternion that represents a rotation about an arbitrary unit vector
104inline:
105 rot *= (float)(M_PI / 180.0f) * 0.5f;
106 float c = cos(rot);
107 float s = sin(rot);
108
109 q->w = c;
110 q->x = x * s;
111 q->y = y * s;
112 q->z = z * s;
113test: none
114end:
115
116function: rsQuaternionSet
117ret: void
118arg: rs_quaternion* q, "destination quaternion"
119arg: float w, "component"
120arg: float x, "component"
121arg: float y, "component"
122arg: float z, "component"
123summary:
124description:
125 Set the quaternion from components or from another quaternion.
126inline:
127 q->w = w;
128 q->x = x;
129 q->y = y;
130 q->z = z;
131test: none
132end:
133
134function: rsQuaternionSet
135ret: void
136arg: rs_quaternion* q
137arg: const rs_quaternion* rhs, "source quaternion"
138inline:
139 q->w = rhs->w;
140 q->x = rhs->x;
141 q->y = rhs->y;
142 q->z = rhs->z;
143test: none
144end:
145
146# NOTE: The following inline definitions depend on each other. The order must be preserved
147# for the compilation to work.
148
149function: rsQuaternionLoadRotate
150ret: void
151arg: rs_quaternion* q, "quaternion to set"
152arg: float rot, "angle to rotate by"
153arg: float x, "component of a vector"
154arg: float y, "component of a vector"
155arg: float z, "component of a vector"
156summary:
157description:
158 Loads a quaternion that represents a rotation about an arbitrary vector
159 (doesn't have to be unit)
160inline:
161 const float len = x*x + y*y + z*z;
162 if (len != 1) {
163 const float recipLen = 1.f / sqrt(len);
164 x *= recipLen;
165 y *= recipLen;
166 z *= recipLen;
167 }
168 rsQuaternionLoadRotateUnit(q, rot, x, y, z);
169test: none
170end:
171
172function: rsQuaternionNormalize
173ret: void
174arg: rs_quaternion* q, "quaternion to normalize"
175summary:
176description:
177 Normalizes the quaternion
178inline:
179 const float len = rsQuaternionDot(q, q);
180 if (len != 1) {
181 const float recipLen = 1.f / sqrt(len);
182 q->w *= recipLen;
183 q->x *= recipLen;
184 q->y *= recipLen;
185 q->z *= recipLen;
186 }
187test: none
188end:
189
190function: rsQuaternionMultiply
191ret: void
192arg: rs_quaternion* q, "destination quaternion"
193arg: float s, "scalar"
194summary:
195description:
196 Multiply quaternion by a scalar or another quaternion
197inline:
198 q->w *= s;
199 q->x *= s;
200 q->y *= s;
201 q->z *= s;
202test: none
203end:
204
205function: rsQuaternionMultiply
206ret: void
207arg: rs_quaternion* q
208arg: const rs_quaternion* rhs, "right hand side quaternion to multiply by"
209inline:
210 rs_quaternion qtmp;
211 rsQuaternionSet(&qtmp, q);
212
213 q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
214 q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
215 q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
216 q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
217 rsQuaternionNormalize(q);
218test: none
219end:
220
221function: rsQuaternionSlerp
222ret: void
223arg: rs_quaternion* q, "result quaternion from interpolation"
224arg: const rs_quaternion* q0, "first param"
225arg: const rs_quaternion* q1, "second param"
226arg: float t, "how much to interpolate by"
227summary:
228description:
229 Performs spherical linear interpolation between two quaternions
230inline:
231 if (t <= 0.0f) {
232 rsQuaternionSet(q, q0);
233 return;
234 }
235 if (t >= 1.0f) {
236 rsQuaternionSet(q, q1);
237 return;
238 }
239
240 rs_quaternion tempq0, tempq1;
241 rsQuaternionSet(&tempq0, q0);
242 rsQuaternionSet(&tempq1, q1);
243
244 float angle = rsQuaternionDot(q0, q1);
245 if (angle < 0) {
246 rsQuaternionMultiply(&tempq0, -1.0f);
247 angle *= -1.0f;
248 }
249
250 float scale, invScale;
251 if (angle + 1.0f > 0.05f) {
252 if (1.0f - angle >= 0.05f) {
253 float theta = acos(angle);
254 float invSinTheta = 1.0f / sin(theta);
255 scale = sin(theta * (1.0f - t)) * invSinTheta;
256 invScale = sin(theta * t) * invSinTheta;
257 } else {
258 scale = 1.0f - t;
259 invScale = t;
260 }
261 } else {
262 rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
263 scale = sin(M_PI * (0.5f - t));
264 invScale = sin(M_PI * t);
265 }
266
267 rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
268 tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
269test: none
270end: