2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh.
20 * rs_quaternion.rsh: Quaternion Functions
22 * The following functions manipulate quaternions.
25 #ifndef RENDERSCRIPT_RS_QUATERNION_RSH
26 #define RENDERSCRIPT_RS_QUATERNION_RSH
29 * rsQuaternionAdd: Add two quaternions
31 * Adds two quaternions, i.e. *q += *rhs;
34 * q: Destination quaternion to add to.
35 * rhs: Quaternion to add.
37 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
38 static inline void __attribute__((overloadable))
39 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
48 * rsQuaternionConjugate: Conjugate a quaternion
50 * Conjugates the quaternion.
53 * q: Quaternion to modify.
55 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
56 static inline void __attribute__((overloadable))
57 rsQuaternionConjugate(rs_quaternion* q) {
65 * rsQuaternionDot: Dot product of two quaternions
67 * Returns the dot product of two quaternions.
70 * q0: First quaternion.
71 * q1: Second quaternion.
73 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
74 static 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;
81 * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion
83 * Computes a rotation matrix from the normalized quaternion.
86 * m: Resulting matrix.
87 * q: Normalized quaternion.
89 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
90 static 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;
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;
117 * rsQuaternionLoadRotateUnit: Quaternion that represents a rotation about an arbitrary unit vector
119 * Loads a quaternion that represents a rotation about an arbitrary unit vector.
122 * 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.
128 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
129 static inline void __attribute__((overloadable))
130 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
131 rot *= (float)(M_PI / 180.0f) * 0.5f;
143 * rsQuaternionSet: Create a quaternion
145 * Creates a quaternion from its four components or from another quaternion.
148 * q: Destination quaternion.
153 * rhs: Source quaternion.
155 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
156 static inline void __attribute__((overloadable))
157 rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) {
165 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
166 static inline void __attribute__((overloadable))
167 rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) {
176 * rsQuaternionLoadRotate: Create a rotation quaternion
178 * Loads a quaternion that represents a rotation about an arbitrary vector
179 * (doesn't have to be unit)
182 * 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.
188 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
189 static inline void __attribute__((overloadable))
190 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
191 const float len = x*x + y*y + z*z;
193 const float recipLen = 1.f / sqrt(len);
198 rsQuaternionLoadRotateUnit(q, rot, x, y, z);
203 * rsQuaternionNormalize: Normalize a quaternion
205 * Normalizes the quaternion.
208 * q: Quaternion to normalize.
210 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
211 static inline void __attribute__((overloadable))
212 rsQuaternionNormalize(rs_quaternion* q) {
213 const float len = rsQuaternionDot(q, q);
215 const float recipLen = 1.f / sqrt(len);
225 * rsQuaternionMultiply: Multiply a quaternion by a scalar or another quaternion
227 * Multiplies a quaternion by a scalar or by another quaternion, e.g
228 * *q = *q * scalar; or *q = *q * *rhs;.
231 * q: Destination quaternion.
232 * scalar: Scalar to multiply the quaternion by.
233 * rhs: Quaternion to multiply the destination quaternion by.
235 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
236 static inline void __attribute__((overloadable))
237 rsQuaternionMultiply(rs_quaternion* q, float scalar) {
245 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
246 static inline void __attribute__((overloadable))
247 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
249 rsQuaternionSet(&qtmp, q);
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);
260 * rsQuaternionSlerp: Spherical linear interpolation between two quaternions
262 * Performs spherical linear interpolation between two quaternions.
265 * q: Result quaternion from the interpolation.
266 * q0: First input quaternion.
267 * q1: Second input quaternion.
268 * t: How much to interpolate by.
270 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
271 static inline void __attribute__((overloadable))
272 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
274 rsQuaternionSet(q, q0);
278 rsQuaternionSet(q, q1);
282 rs_quaternion tempq0, tempq1;
283 rsQuaternionSet(&tempq0, q0);
284 rsQuaternionSet(&tempq1, q1);
286 float angle = rsQuaternionDot(q0, q1);
288 rsQuaternionMultiply(&tempq0, -1.0f);
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;
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);
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);
314 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
315 extern void __attribute__((overloadable))
316 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs);
319 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
320 extern void __attribute__((overloadable))
321 rsQuaternionConjugate(rs_quaternion* q);
324 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
325 extern float __attribute__((overloadable))
326 rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1);
329 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
330 extern void __attribute__((overloadable))
331 rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q);
334 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
335 extern void __attribute__((overloadable))
336 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z);
339 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
340 extern void __attribute__((overloadable))
341 rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z);
344 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
345 extern void __attribute__((overloadable))
346 rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs);
349 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
350 extern void __attribute__((overloadable))
351 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z);
354 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
355 extern void __attribute__((overloadable))
356 rsQuaternionNormalize(rs_quaternion* q);
359 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
360 extern void __attribute__((overloadable))
361 rsQuaternionMultiply(rs_quaternion* q, float scalar);
364 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
365 extern void __attribute__((overloadable))
366 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs);
369 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
370 extern void __attribute__((overloadable))
371 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t);
374 #endif // RENDERSCRIPT_RS_QUATERNION_RSH