blob: 6cb280d46b63cfe991929a318478fd35bf278afd [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:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070018summary: Quaternion Functions
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070019description:
20end:
21
22function: rsQuaternionAdd
23ret: void
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070024arg: rs_quaternion* q, "Destination quaternion to add to."
25arg: const rs_quaternion* rhs, "Quaternion to add."
26summary: Add two quaternions
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070027description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070028 Adds two quaternions, i.e. <code>*q += *rhs;</code>
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070029inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070039arg: rs_quaternion* q, "Quaternion to modify."
40summary: Conjugate a quaternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070041description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070042 Conjugates the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070043inline:
44 q->x = -q->x;
45 q->y = -q->y;
46 q->z = -q->z;
47test: none
48end:
49
50function: rsQuaternionDot
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070051ret: float
52arg: const rs_quaternion* q0, "First quaternion."
53arg: const rs_quaternion* q1, "Second quaternion."
54summary: Dot product of two quaternions
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070055description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070056 Returns the dot product of two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070057inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070064arg: rs_matrix4x4* m, "Resulting matrix."
65arg: const rs_quaternion* q, "Normalized quaternion."
66summary: Get a rotation matrix from a quaternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070067description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070068 Computes a rotation matrix from the normalized quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070069inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070096arg: rs_quaternion* q, "Destination quaternion."
97arg: float rot, "Angle to rotate by, in radians."
98arg: float x, "X component of the vector."
99arg: float y, "Y component of the vector."
100arg: float z, "Z component of the vector."
101summary: Quaternion that represents a rotation about an arbitrary unit vector
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700102description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700103 Loads a quaternion that represents a rotation about an arbitrary unit vector.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700104inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700118arg: rs_quaternion* q, "Destination quaternion."
119arg: float w, "W component."
120arg: float x, "X component."
121arg: float y, "Y component."
122arg: float z, "Z component."
123summary: Create a quarternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700124description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700125 Creates a quaternion from its four components or from another quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700126inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700137arg: const rs_quaternion* rhs, "Source quaternion."
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700138inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700151arg: rs_quaternion* q, "Destination quaternion."
152arg: float rot, "Angle to rotate by."
153arg: float x, "X component of a vector."
154arg: float y, "Y component of a vector."
155arg: float z, "Z component of a vector."
156summary: Create a rotation quaternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700157description:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700174arg: rs_quaternion* q, "Quaternion to normalize."
175summary: Normalize a quaternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700176description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700177 Normalizes the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700178inline:
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
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700192arg: rs_quaternion* q, "Destination quaternion."
193arg: float scalar, "Scalar to multiply the quarternion by."
194summary: Multiply a quaternion by a scalar or another quaternion
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700195description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700196 Multiplies a quaternion by a scalar or by another quaternion, e.g
197 <code>*q = *q * scalar;</code> or <code>*q = *q * *rhs;</code>.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700198inline:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700199 q->w *= scalar;
200 q->x *= scalar;
201 q->y *= scalar;
202 q->z *= scalar;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700203test: none
204end:
205
206function: rsQuaternionMultiply
207ret: void
208arg: rs_quaternion* q
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700209arg: const rs_quaternion* rhs, "Quarternion to multiply the destination quaternion by."
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700210inline:
211 rs_quaternion qtmp;
212 rsQuaternionSet(&qtmp, q);
213
214 q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
215 q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
216 q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
217 q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
218 rsQuaternionNormalize(q);
219test: none
220end:
221
222function: rsQuaternionSlerp
223ret: void
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700224arg: rs_quaternion* q, "Result quaternion from the interpolation."
225arg: const rs_quaternion* q0, "First input quaternion."
226arg: const rs_quaternion* q1, "Second input quaternion."
227arg: float t, "How much to interpolate by."
228summary: Spherical linear interpolation between two quaternions
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700229description:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700230 Performs spherical linear interpolation between two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700231inline:
232 if (t <= 0.0f) {
233 rsQuaternionSet(q, q0);
234 return;
235 }
236 if (t >= 1.0f) {
237 rsQuaternionSet(q, q1);
238 return;
239 }
240
241 rs_quaternion tempq0, tempq1;
242 rsQuaternionSet(&tempq0, q0);
243 rsQuaternionSet(&tempq1, q1);
244
245 float angle = rsQuaternionDot(q0, q1);
246 if (angle < 0) {
247 rsQuaternionMultiply(&tempq0, -1.0f);
248 angle *= -1.0f;
249 }
250
251 float scale, invScale;
252 if (angle + 1.0f > 0.05f) {
253 if (1.0f - angle >= 0.05f) {
254 float theta = acos(angle);
255 float invSinTheta = 1.0f / sin(theta);
256 scale = sin(theta * (1.0f - t)) * invSinTheta;
257 invScale = sin(theta * t) * invSinTheta;
258 } else {
259 scale = 1.0f - t;
260 invScale = t;
261 }
262 } else {
263 rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
264 scale = sin(M_PI * (0.5f - t));
265 invScale = sin(M_PI * t);
266 }
267
268 rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
269 tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
270test: none
271end: