Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 1 | /* |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 17 | // Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 20 | * rs_quaternion.rsh: Quaternion Functions |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 21 | * |
Pirama Arumuga Nainar | db74586 | 2015-05-11 14:34:37 -0700 | [diff] [blame] | 22 | * The following functions manipulate quaternions. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 23 | */ |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 24 | |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 25 | #ifndef RENDERSCRIPT_RS_QUATERNION_RSH |
| 26 | #define RENDERSCRIPT_RS_QUATERNION_RSH |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 27 | |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 28 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 29 | * rsQuaternionAdd: Add two quaternions |
| 30 | * |
| 31 | * Adds two quaternions, i.e. *q += *rhs; |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 32 | * |
| 33 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 34 | * q: Destination quaternion to add to. |
| 35 | * rhs: Quaternion to add. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 36 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 37 | static inline void __attribute__((overloadable)) |
| 38 | rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 39 | q->w *= rhs->w; |
| 40 | q->x *= rhs->x; |
| 41 | q->y *= rhs->y; |
| 42 | q->z *= rhs->z; |
| 43 | } |
| 44 | |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 45 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 46 | * rsQuaternionConjugate: Conjugate a quaternion |
| 47 | * |
| 48 | * Conjugates the quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 49 | * |
| 50 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 51 | * q: Quaternion to modify. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 52 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 53 | static 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 Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 61 | * rsQuaternionDot: Dot product of two quaternions |
| 62 | * |
| 63 | * Returns the dot product of two quaternions. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 64 | * |
| 65 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 66 | * q0: First quaternion. |
| 67 | * q1: Second quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 68 | */ |
| 69 | static 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 Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 75 | * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion |
| 76 | * |
| 77 | * Computes a rotation matrix from the normalized quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 78 | * |
| 79 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 80 | * m: Resulting matrix. |
| 81 | * q: Normalized quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 82 | */ |
| 83 | static 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 Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 109 | * 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 112 | * |
| 113 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 114 | * 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 119 | */ |
| 120 | static inline void __attribute__((overloadable)) |
| 121 | rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 122 | 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 132 | /* |
Pirama Arumuga Nainar | db74586 | 2015-05-11 14:34:37 -0700 | [diff] [blame] | 133 | * rsQuaternionSet: Create a quaternion |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 134 | * |
| 135 | * Creates a quaternion from its four components or from another quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 136 | * |
| 137 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 138 | * q: Destination quaternion. |
| 139 | * w: W component. |
| 140 | * x: X component. |
| 141 | * y: Y component. |
| 142 | * z: Z component. |
| 143 | * rhs: Source quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 144 | */ |
| 145 | static 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 | |
| 153 | static 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 Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 162 | * rsQuaternionLoadRotate: Create a rotation quaternion |
| 163 | * |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 164 | * Loads a quaternion that represents a rotation about an arbitrary vector |
| 165 | * (doesn't have to be unit) |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 166 | * |
| 167 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 168 | * 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 Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 173 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 174 | static inline void __attribute__((overloadable)) |
| 175 | rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 176 | 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 186 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 187 | * rsQuaternionNormalize: Normalize a quaternion |
| 188 | * |
| 189 | * Normalizes the quaternion. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 190 | * |
| 191 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 192 | * q: Quaternion to normalize. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 193 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 194 | static inline void __attribute__((overloadable)) |
| 195 | rsQuaternionNormalize(rs_quaternion* q) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 196 | const float len = rsQuaternionDot(q, q); |
| 197 | if (len != 1) { |
| 198 | const float recipLen = 1.f / sqrt(len); |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 199 | q->w *= recipLen; |
| 200 | q->x *= recipLen; |
| 201 | q->y *= recipLen; |
| 202 | q->z *= recipLen; |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 206 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 207 | * 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 211 | * |
| 212 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 213 | * q: Destination quaternion. |
Pirama Arumuga Nainar | db74586 | 2015-05-11 14:34:37 -0700 | [diff] [blame] | 214 | * scalar: Scalar to multiply the quaternion by. |
| 215 | * rhs: Quaternion to multiply the destination quaternion by. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 216 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 217 | static inline void __attribute__((overloadable)) |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 218 | rsQuaternionMultiply(rs_quaternion* q, float scalar) { |
| 219 | q->w *= scalar; |
| 220 | q->x *= scalar; |
| 221 | q->y *= scalar; |
| 222 | q->z *= scalar; |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void __attribute__((overloadable)) |
| 226 | rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 227 | 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 237 | /* |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 238 | * rsQuaternionSlerp: Spherical linear interpolation between two quaternions |
| 239 | * |
| 240 | * Performs spherical linear interpolation between two quaternions. |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 241 | * |
| 242 | * Parameters: |
Stephen Hines | 3f86823 | 2015-04-10 09:22:19 -0700 | [diff] [blame] | 243 | * q: Result quaternion from the interpolation. |
| 244 | * q0: First input quaternion. |
| 245 | * q1: Second input quaternion. |
| 246 | * t: How much to interpolate by. |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 247 | */ |
Stephen Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 248 | static inline void __attribute__((overloadable)) |
| 249 | rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) { |
Ying Wang | a672014 | 2011-12-20 14:43:20 -0800 | [diff] [blame] | 250 | 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 Hines | b4d9c8b | 2015-03-30 16:04:04 -0700 | [diff] [blame] | 290 | #endif // RENDERSCRIPT_RS_QUATERNION_RSH |