blob: 524eeb0dc9c1606e232cab2c6aa32bdc7872a54f [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 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070037static inline void __attribute__((overloadable))
38 rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
Jason Sams044e2ee2011-08-08 16:52:30 -070039 q->w *= rhs->w;
40 q->x *= rhs->x;
41 q->y *= rhs->y;
42 q->z *= rhs->z;
43}
44
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070045/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070046 * rsQuaternionConjugate: Conjugate a quaternion
47 *
48 * Conjugates the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070049 *
50 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070051 * q: Quaternion to modify.
Jason Sams044e2ee2011-08-08 16:52:30 -070052 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -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/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070061 * rsQuaternionDot: Dot product of two quaternions
62 *
63 * Returns the dot product of two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070064 *
65 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070066 * q0: First quaternion.
67 * q1: Second quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -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/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070075 * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion
76 *
77 * Computes a rotation matrix from the normalized quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070078 *
79 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -070080 * m: Resulting matrix.
81 * q: Normalized quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -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/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -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.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700112 *
113 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -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.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700119 */
120static inline void __attribute__((overloadable))
121 rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700122 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700132/*
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -0700133 * rsQuaternionSet: Create a quaternion
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700134 *
135 * Creates a quaternion from its four components or from another quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700136 *
137 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700138 * q: Destination quaternion.
139 * w: W component.
140 * x: X component.
141 * y: Y component.
142 * z: Z component.
143 * rhs: Source quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -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/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700162 * rsQuaternionLoadRotate: Create a rotation quaternion
163 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700164 * Loads a quaternion that represents a rotation about an arbitrary vector
165 * (doesn't have to be unit)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700166 *
167 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -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.
Jason Sams044e2ee2011-08-08 16:52:30 -0700173 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700174static inline void __attribute__((overloadable))
175 rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700176 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700186/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700187 * rsQuaternionNormalize: Normalize a quaternion
188 *
189 * Normalizes the quaternion.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700190 *
191 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700192 * q: Quaternion to normalize.
Jason Sams044e2ee2011-08-08 16:52:30 -0700193 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700194static inline void __attribute__((overloadable))
195 rsQuaternionNormalize(rs_quaternion* q) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700196 const float len = rsQuaternionDot(q, q);
197 if (len != 1) {
198 const float recipLen = 1.f / sqrt(len);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700199 q->w *= recipLen;
200 q->x *= recipLen;
201 q->y *= recipLen;
202 q->z *= recipLen;
Jason Sams044e2ee2011-08-08 16:52:30 -0700203 }
204}
205
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700206/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -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;.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700211 *
212 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700213 * q: Destination quaternion.
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -0700214 * scalar: Scalar to multiply the quaternion by.
215 * rhs: Quaternion to multiply the destination quaternion by.
Alex Sakhartchoukbd7b1a92011-10-18 11:54:49 -0700216 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700217static inline void __attribute__((overloadable))
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700218 rsQuaternionMultiply(rs_quaternion* q, float scalar) {
219 q->w *= scalar;
220 q->x *= scalar;
221 q->y *= scalar;
222 q->z *= scalar;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700223}
224
225static inline void __attribute__((overloadable))
226 rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
Alex Sakhartchoukbd7b1a92011-10-18 11:54:49 -0700227 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700237/*
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700238 * rsQuaternionSlerp: Spherical linear interpolation between two quaternions
239 *
240 * Performs spherical linear interpolation between two quaternions.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700241 *
242 * Parameters:
Jean-Luc Brouillet20b27d62015-04-03 14:39:53 -0700243 * q: Result quaternion from the interpolation.
244 * q0: First input quaternion.
245 * q1: Second input quaternion.
246 * t: How much to interpolate by.
Jason Sams044e2ee2011-08-08 16:52:30 -0700247 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700248static inline void __attribute__((overloadable))
249 rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700250 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700290#endif // RENDERSCRIPT_RS_QUATERNION_RSH