blob: 50b14b3d8126e3a9bb80adc0796ff16cf8f7c855 [file] [log] [blame]
Jason Sams044e2ee2011-08-08 16:52:30 -07001/*
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07002 * Copyright (C) 2015 The Android Open Source Project
Jason Sams044e2ee2011-08-08 16:52:30 -07003 *
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
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070017// Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070018
19/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070020 * rs_quaternion.rsh: Quaternion Functions
Jason Sams044e2ee2011-08-08 16:52:30 -070021 *
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -070022 * The following functions manipulate quaternions.
Jason Sams044e2ee2011-08-08 16:52:30 -070023 */
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070024
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070025#ifndef RENDERSCRIPT_RS_QUATERNION_RSH
26#define RENDERSCRIPT_RS_QUATERNION_RSH
Jason Sams044e2ee2011-08-08 16:52:30 -070027
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070028/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070029 * rsQuaternionAdd: Add two quaternions
30 *
31 * Adds two quaternions, i.e. *q += *rhs;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070032 *
33 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070034 * q: Destination quaternion to add to.
35 * rhs: Quaternion to add.
Jason Sams044e2ee2011-08-08 16:52:30 -070036 */
Verena Beckham336fc4b2015-11-18 10:44:03 +000037#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070038static inline void __attribute__((overloadable))
39 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
Yang Nie7fd36a2016-02-01 10:42:29 -080040 q->w += rhs->w;
41 q->x += rhs->x;
42 q->y += rhs->y;
43 q->z += rhs->z;
Jason Sams044e2ee2011-08-08 16:52:30 -070044}
Verena Beckham336fc4b2015-11-18 10:44:03 +000045#endif
Jason Sams044e2ee2011-08-08 16:52:30 -070046
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070047/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070048 * rsQuaternionConjugate: Conjugate a quaternion
49 *
50 * Conjugates the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070051 *
52 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070053 * q: Quaternion to modify.
Jason Sams044e2ee2011-08-08 16:52:30 -070054 */
Verena Beckham336fc4b2015-11-18 10:44:03 +000055#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070056static inline void __attribute__((overloadable))
57 rsQuaternionConjugate(rs_quaternion* q) {
58 q->x = -q->x;
59 q->y = -q->y;
60 q->z = -q->z;
61}
Verena Beckham336fc4b2015-11-18 10:44:03 +000062#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070063
64/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070065 * rsQuaternionDot: Dot product of two quaternions
66 *
67 * Returns the dot product of two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070068 *
69 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070070 * q0: First quaternion.
71 * q1: Second quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070072 */
Verena Beckham336fc4b2015-11-18 10:44:03 +000073#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070074static inline float __attribute__((overloadable))
75 rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1) {
76 return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
77}
Verena Beckham336fc4b2015-11-18 10:44:03 +000078#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070079
80/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070081 * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion
82 *
83 * Computes a rotation matrix from the normalized quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070084 *
85 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070086 * m: Resulting matrix.
87 * q: Normalized quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070088 */
Verena Beckham336fc4b2015-11-18 10:44:03 +000089#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070090static inline void __attribute__((overloadable))
91 rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q) {
92 float xx = q->x * q->x;
93 float xy = q->x * q->y;
94 float xz = q->x * q->z;
95 float xw = q->x * q->w;
96 float yy = q->y * q->y;
97 float yz = q->y * q->z;
98 float yw = q->y * q->w;
99 float zz = q->z * q->z;
100 float zw = q->z * q->w;
101
102 m->m[0] = 1.0f - 2.0f * ( yy + zz );
103 m->m[4] = 2.0f * ( xy - zw );
104 m->m[8] = 2.0f * ( xz + yw );
105 m->m[1] = 2.0f * ( xy + zw );
106 m->m[5] = 1.0f - 2.0f * ( xx + zz );
107 m->m[9] = 2.0f * ( yz - xw );
108 m->m[2] = 2.0f * ( xz - yw );
109 m->m[6] = 2.0f * ( yz + xw );
110 m->m[10] = 1.0f - 2.0f * ( xx + yy );
111 m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
112 m->m[15] = 1.0f;
113}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000114#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700115
116/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700117 * rsQuaternionLoadRotateUnit: Quaternion that represents a rotation about an arbitrary unit vector
118 *
119 * Loads a quaternion that represents a rotation about an arbitrary unit vector.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700120 *
121 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700122 * q: Destination quaternion.
123 * rot: Angle to rotate by, in radians.
124 * x: X component of the vector.
125 * y: Y component of the vector.
126 * z: Z component of the vector.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700127 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000128#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700129static inline void __attribute__((overloadable))
130 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700131 rot *= (float)(M_PI / 180.0f) * 0.5f;
132 float c = cos(rot);
133 float s = sin(rot);
134
135 q->w = c;
136 q->x = x * s;
137 q->y = y * s;
138 q->z = z * s;
139}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000140#endif
Jason Sams044e2ee2011-08-08 16:52:30 -0700141
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700142/*
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -0700143 * rsQuaternionSet: Create a quaternion
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700144 *
145 * Creates a quaternion from its four components or from another quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700146 *
147 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700148 * q: Destination quaternion.
149 * w: W component.
150 * x: X component.
151 * y: Y component.
152 * z: Z component.
153 * rhs: Source quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700154 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000155#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700156static inline void __attribute__((overloadable))
157 rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) {
158 q->w = w;
159 q->x = x;
160 q->y = y;
161 q->z = z;
162}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000163#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700164
Verena Beckham336fc4b2015-11-18 10:44:03 +0000165#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700166static inline void __attribute__((overloadable))
167 rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) {
168 q->w = rhs->w;
169 q->x = rhs->x;
170 q->y = rhs->y;
171 q->z = rhs->z;
172}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000173#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700174
175/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700176 * rsQuaternionLoadRotate: Create a rotation quaternion
177 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700178 * Loads a quaternion that represents a rotation about an arbitrary vector
179 * (doesn't have to be unit)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700180 *
181 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700182 * q: Destination quaternion.
183 * rot: Angle to rotate by.
184 * x: X component of a vector.
185 * y: Y component of a vector.
186 * z: Z component of a vector.
Jason Sams044e2ee2011-08-08 16:52:30 -0700187 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000188#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700189static inline void __attribute__((overloadable))
190 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700191 const float len = x*x + y*y + z*z;
192 if (len != 1) {
193 const float recipLen = 1.f / sqrt(len);
194 x *= recipLen;
195 y *= recipLen;
196 z *= recipLen;
197 }
198 rsQuaternionLoadRotateUnit(q, rot, x, y, z);
199}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000200#endif
Jason Sams044e2ee2011-08-08 16:52:30 -0700201
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700202/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700203 * rsQuaternionNormalize: Normalize a quaternion
204 *
205 * Normalizes the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700206 *
207 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700208 * q: Quaternion to normalize.
Jason Sams044e2ee2011-08-08 16:52:30 -0700209 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000210#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700211static inline void __attribute__((overloadable))
212 rsQuaternionNormalize(rs_quaternion* q) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700213 const float len = rsQuaternionDot(q, q);
214 if (len != 1) {
215 const float recipLen = 1.f / sqrt(len);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700216 q->w *= recipLen;
217 q->x *= recipLen;
218 q->y *= recipLen;
219 q->z *= recipLen;
Jason Sams044e2ee2011-08-08 16:52:30 -0700220 }
221}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000222#endif
Jason Sams044e2ee2011-08-08 16:52:30 -0700223
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700224/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700225 * rsQuaternionMultiply: Multiply a quaternion by a scalar or another quaternion
226 *
227 * Multiplies a quaternion by a scalar or by another quaternion, e.g
228 * *q = *q * scalar; or *q = *q * *rhs;.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700229 *
230 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700231 * q: Destination quaternion.
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -0700232 * scalar: Scalar to multiply the quaternion by.
233 * rhs: Quaternion to multiply the destination quaternion by.
Alex Sakhartchoukbd7b1a92011-10-18 11:54:49 -0700234 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000235#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700236static inline void __attribute__((overloadable))
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700237 rsQuaternionMultiply(rs_quaternion* q, float scalar) {
238 q->w *= scalar;
239 q->x *= scalar;
240 q->y *= scalar;
241 q->z *= scalar;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700242}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000243#endif
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700244
Verena Beckham336fc4b2015-11-18 10:44:03 +0000245#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700246static inline void __attribute__((overloadable))
247 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
Alex Sakhartchoukbd7b1a92011-10-18 11:54:49 -0700248 rs_quaternion qtmp;
249 rsQuaternionSet(&qtmp, q);
250
251 q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
252 q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
253 q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
254 q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
255 rsQuaternionNormalize(q);
256}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000257#endif
Alex Sakhartchoukbd7b1a92011-10-18 11:54:49 -0700258
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700259/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700260 * rsQuaternionSlerp: Spherical linear interpolation between two quaternions
261 *
262 * Performs spherical linear interpolation between two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700263 *
264 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700265 * q: Result quaternion from the interpolation.
266 * q0: First input quaternion.
267 * q1: Second input quaternion.
268 * t: How much to interpolate by.
Jason Sams044e2ee2011-08-08 16:52:30 -0700269 */
Verena Beckham336fc4b2015-11-18 10:44:03 +0000270#if !defined(RS_VERSION) || (RS_VERSION <= 23)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700271static inline void __attribute__((overloadable))
272 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700273 if (t <= 0.0f) {
274 rsQuaternionSet(q, q0);
275 return;
276 }
277 if (t >= 1.0f) {
278 rsQuaternionSet(q, q1);
279 return;
280 }
281
282 rs_quaternion tempq0, tempq1;
283 rsQuaternionSet(&tempq0, q0);
284 rsQuaternionSet(&tempq1, q1);
285
286 float angle = rsQuaternionDot(q0, q1);
287 if (angle < 0) {
288 rsQuaternionMultiply(&tempq0, -1.0f);
289 angle *= -1.0f;
290 }
291
292 float scale, invScale;
293 if (angle + 1.0f > 0.05f) {
294 if (1.0f - angle >= 0.05f) {
295 float theta = acos(angle);
296 float invSinTheta = 1.0f / sin(theta);
297 scale = sin(theta * (1.0f - t)) * invSinTheta;
298 invScale = sin(theta * t) * invSinTheta;
299 } else {
300 scale = 1.0f - t;
301 invScale = t;
302 }
303 } else {
304 rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
305 scale = sin(M_PI * (0.5f - t));
306 invScale = sin(M_PI * t);
307 }
308
309 rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
310 tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
311}
Verena Beckham336fc4b2015-11-18 10:44:03 +0000312#endif
313
314#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
315extern void __attribute__((overloadable))
316 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs);
317#endif
318
319#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
320extern void __attribute__((overloadable))
321 rsQuaternionConjugate(rs_quaternion* q);
322#endif
323
324#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
325extern float __attribute__((overloadable))
326 rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1);
327#endif
328
329#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
330extern void __attribute__((overloadable))
331 rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q);
332#endif
333
334#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
335extern void __attribute__((overloadable))
336 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z);
337#endif
338
339#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
340extern void __attribute__((overloadable))
341 rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z);
342#endif
343
344#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
345extern void __attribute__((overloadable))
346 rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs);
347#endif
348
349#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
350extern void __attribute__((overloadable))
351 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z);
352#endif
353
354#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
355extern void __attribute__((overloadable))
356 rsQuaternionNormalize(rs_quaternion* q);
357#endif
358
359#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
360extern void __attribute__((overloadable))
361 rsQuaternionMultiply(rs_quaternion* q, float scalar);
362#endif
363
364#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
365extern void __attribute__((overloadable))
366 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs);
367#endif
368
369#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
370extern void __attribute__((overloadable))
371 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t);
372#endif
Jason Sams044e2ee2011-08-08 16:52:30 -0700373
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700374#endif // RENDERSCRIPT_RS_QUATERNION_RSH