blob: 524eeb0dc9c1606e232cab2c6aa32bdc7872a54f [file] [log] [blame]
Ying Wanga6720142011-12-20 14:43:20 -08001/*
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -07002 * Copyright (C) 2015 The Android Open Source Project
Ying Wanga6720142011-12-20 14:43:20 -08003 *
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
Stephen Hines3f868232015-04-10 09:22:19 -070017// Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070018
19/*
Stephen Hines3f868232015-04-10 09:22:19 -070020 * rs_quaternion.rsh: Quaternion Functions
Ying Wanga6720142011-12-20 14:43:20 -080021 *
Pirama Arumuga Nainardb745862015-05-11 14:34:37 -070022 * The following functions manipulate quaternions.
Ying Wanga6720142011-12-20 14:43:20 -080023 */
Stephen Hines3f868232015-04-10 09:22:19 -070024
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070025#ifndef RENDERSCRIPT_RS_QUATERNION_RSH
26#define RENDERSCRIPT_RS_QUATERNION_RSH
Ying Wanga6720142011-12-20 14:43:20 -080027
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070028/*
Stephen Hines3f868232015-04-10 09:22:19 -070029 * rsQuaternionAdd: Add two quaternions
30 *
31 * Adds two quaternions, i.e. *q += *rhs;
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070032 *
33 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -070034 * q: Destination quaternion to add to.
35 * rhs: Quaternion to add.
Ying Wanga6720142011-12-20 14:43:20 -080036 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070037static inline void __attribute__((overloadable))
38 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
Ying Wanga6720142011-12-20 14:43:20 -080039 q->w *= rhs->w;
40 q->x *= rhs->x;
41 q->y *= rhs->y;
42 q->z *= rhs->z;
43}
44
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070045/*
Stephen Hines3f868232015-04-10 09:22:19 -070046 * rsQuaternionConjugate: Conjugate a quaternion
47 *
48 * Conjugates the quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070049 *
50 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -070051 * q: Quaternion to modify.
Ying Wanga6720142011-12-20 14:43:20 -080052 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070053static inline void __attribute__((overloadable))
54 rsQuaternionConjugate(rs_quaternion* q) {
55 q->x = -q->x;
56 q->y = -q->y;
57 q->z = -q->z;
58}
59
60/*
Stephen Hines3f868232015-04-10 09:22:19 -070061 * rsQuaternionDot: Dot product of two quaternions
62 *
63 * Returns the dot product of two quaternions.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070064 *
65 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -070066 * q0: First quaternion.
67 * q1: Second quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070068 */
69static inline float __attribute__((overloadable))
70 rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1) {
71 return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
72}
73
74/*
Stephen Hines3f868232015-04-10 09:22:19 -070075 * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion
76 *
77 * Computes a rotation matrix from the normalized quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070078 *
79 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -070080 * m: Resulting matrix.
81 * q: Normalized quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -070082 */
83static inline void __attribute__((overloadable))
84 rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q) {
85 float xx = q->x * q->x;
86 float xy = q->x * q->y;
87 float xz = q->x * q->z;
88 float xw = q->x * q->w;
89 float yy = q->y * q->y;
90 float yz = q->y * q->z;
91 float yw = q->y * q->w;
92 float zz = q->z * q->z;
93 float zw = q->z * q->w;
94
95 m->m[0] = 1.0f - 2.0f * ( yy + zz );
96 m->m[4] = 2.0f * ( xy - zw );
97 m->m[8] = 2.0f * ( xz + yw );
98 m->m[1] = 2.0f * ( xy + zw );
99 m->m[5] = 1.0f - 2.0f * ( xx + zz );
100 m->m[9] = 2.0f * ( yz - xw );
101 m->m[2] = 2.0f * ( xz - yw );
102 m->m[6] = 2.0f * ( yz + xw );
103 m->m[10] = 1.0f - 2.0f * ( xx + yy );
104 m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
105 m->m[15] = 1.0f;
106}
107
108/*
Stephen Hines3f868232015-04-10 09:22:19 -0700109 * rsQuaternionLoadRotateUnit: Quaternion that represents a rotation about an arbitrary unit vector
110 *
111 * Loads a quaternion that represents a rotation about an arbitrary unit vector.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700112 *
113 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700114 * q: Destination quaternion.
115 * rot: Angle to rotate by, in radians.
116 * x: X component of the vector.
117 * y: Y component of the vector.
118 * z: Z component of the vector.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700119 */
120static inline void __attribute__((overloadable))
121 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
Ying Wanga6720142011-12-20 14:43:20 -0800122 rot *= (float)(M_PI / 180.0f) * 0.5f;
123 float c = cos(rot);
124 float s = sin(rot);
125
126 q->w = c;
127 q->x = x * s;
128 q->y = y * s;
129 q->z = z * s;
130}
131
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700132/*
Pirama Arumuga Nainardb745862015-05-11 14:34:37 -0700133 * rsQuaternionSet: Create a quaternion
Stephen Hines3f868232015-04-10 09:22:19 -0700134 *
135 * Creates a quaternion from its four components or from another quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700136 *
137 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700138 * q: Destination quaternion.
139 * w: W component.
140 * x: X component.
141 * y: Y component.
142 * z: Z component.
143 * rhs: Source quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700144 */
145static inline void __attribute__((overloadable))
146 rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) {
147 q->w = w;
148 q->x = x;
149 q->y = y;
150 q->z = z;
151}
152
153static inline void __attribute__((overloadable))
154 rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) {
155 q->w = rhs->w;
156 q->x = rhs->x;
157 q->y = rhs->y;
158 q->z = rhs->z;
159}
160
161/*
Stephen Hines3f868232015-04-10 09:22:19 -0700162 * rsQuaternionLoadRotate: Create a rotation quaternion
163 *
Ying Wanga6720142011-12-20 14:43:20 -0800164 * Loads a quaternion that represents a rotation about an arbitrary vector
165 * (doesn't have to be unit)
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700166 *
167 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700168 * q: Destination quaternion.
169 * rot: Angle to rotate by.
170 * x: X component of a vector.
171 * y: Y component of a vector.
172 * z: Z component of a vector.
Ying Wanga6720142011-12-20 14:43:20 -0800173 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700174static inline void __attribute__((overloadable))
175 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
Ying Wanga6720142011-12-20 14:43:20 -0800176 const float len = x*x + y*y + z*z;
177 if (len != 1) {
178 const float recipLen = 1.f / sqrt(len);
179 x *= recipLen;
180 y *= recipLen;
181 z *= recipLen;
182 }
183 rsQuaternionLoadRotateUnit(q, rot, x, y, z);
184}
185
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700186/*
Stephen Hines3f868232015-04-10 09:22:19 -0700187 * rsQuaternionNormalize: Normalize a quaternion
188 *
189 * Normalizes the quaternion.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700190 *
191 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700192 * q: Quaternion to normalize.
Ying Wanga6720142011-12-20 14:43:20 -0800193 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700194static inline void __attribute__((overloadable))
195 rsQuaternionNormalize(rs_quaternion* q) {
Ying Wanga6720142011-12-20 14:43:20 -0800196 const float len = rsQuaternionDot(q, q);
197 if (len != 1) {
198 const float recipLen = 1.f / sqrt(len);
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700199 q->w *= recipLen;
200 q->x *= recipLen;
201 q->y *= recipLen;
202 q->z *= recipLen;
Ying Wanga6720142011-12-20 14:43:20 -0800203 }
204}
205
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700206/*
Stephen Hines3f868232015-04-10 09:22:19 -0700207 * rsQuaternionMultiply: Multiply a quaternion by a scalar or another quaternion
208 *
209 * Multiplies a quaternion by a scalar or by another quaternion, e.g
210 * *q = *q * scalar; or *q = *q * *rhs;.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700211 *
212 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700213 * q: Destination quaternion.
Pirama Arumuga Nainardb745862015-05-11 14:34:37 -0700214 * scalar: Scalar to multiply the quaternion by.
215 * rhs: Quaternion to multiply the destination quaternion by.
Ying Wanga6720142011-12-20 14:43:20 -0800216 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700217static inline void __attribute__((overloadable))
Stephen Hines3f868232015-04-10 09:22:19 -0700218 rsQuaternionMultiply(rs_quaternion* q, float scalar) {
219 q->w *= scalar;
220 q->x *= scalar;
221 q->y *= scalar;
222 q->z *= scalar;
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700223}
224
225static inline void __attribute__((overloadable))
226 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
Ying Wanga6720142011-12-20 14:43:20 -0800227 rs_quaternion qtmp;
228 rsQuaternionSet(&qtmp, q);
229
230 q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
231 q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
232 q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
233 q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
234 rsQuaternionNormalize(q);
235}
236
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700237/*
Stephen Hines3f868232015-04-10 09:22:19 -0700238 * rsQuaternionSlerp: Spherical linear interpolation between two quaternions
239 *
240 * Performs spherical linear interpolation between two quaternions.
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700241 *
242 * Parameters:
Stephen Hines3f868232015-04-10 09:22:19 -0700243 * q: Result quaternion from the interpolation.
244 * q0: First input quaternion.
245 * q1: Second input quaternion.
246 * t: How much to interpolate by.
Ying Wanga6720142011-12-20 14:43:20 -0800247 */
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700248static inline void __attribute__((overloadable))
249 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
Ying Wanga6720142011-12-20 14:43:20 -0800250 if (t <= 0.0f) {
251 rsQuaternionSet(q, q0);
252 return;
253 }
254 if (t >= 1.0f) {
255 rsQuaternionSet(q, q1);
256 return;
257 }
258
259 rs_quaternion tempq0, tempq1;
260 rsQuaternionSet(&tempq0, q0);
261 rsQuaternionSet(&tempq1, q1);
262
263 float angle = rsQuaternionDot(q0, q1);
264 if (angle < 0) {
265 rsQuaternionMultiply(&tempq0, -1.0f);
266 angle *= -1.0f;
267 }
268
269 float scale, invScale;
270 if (angle + 1.0f > 0.05f) {
271 if (1.0f - angle >= 0.05f) {
272 float theta = acos(angle);
273 float invSinTheta = 1.0f / sin(theta);
274 scale = sin(theta * (1.0f - t)) * invSinTheta;
275 invScale = sin(theta * t) * invSinTheta;
276 } else {
277 scale = 1.0f - t;
278 invScale = t;
279 }
280 } else {
281 rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
282 scale = sin(M_PI * (0.5f - t));
283 invScale = sin(M_PI * t);
284 }
285
286 rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
287 tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
288}
289
Stephen Hinesb4d9c8b2015-03-30 16:04:04 -0700290#endif // RENDERSCRIPT_RS_QUATERNION_RSH