Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 1 | /* |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [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 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 17 | // Don't edit this file! It is auto-generated by frameworks/rs/api/gen_runtime. |
| 18 | |
| 19 | /* |
| 20 | * rs_quaternion.rsh: Quaternion routines |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 21 | * |
| 22 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 23 | #ifndef RENDERSCRIPT_RS_QUATERNION_RSH |
| 24 | #define RENDERSCRIPT_RS_QUATERNION_RSH |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 25 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 26 | /* |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 27 | * Add two quaternions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 28 | * |
| 29 | * Parameters: |
| 30 | * q destination quaternion to add to |
| 31 | * rhs right hand side quaternion to add |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 32 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 33 | static inline void __attribute__((overloadable)) |
| 34 | rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) { |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 35 | q->w *= rhs->w; |
| 36 | q->x *= rhs->x; |
| 37 | q->y *= rhs->y; |
| 38 | q->z *= rhs->z; |
| 39 | } |
| 40 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 41 | /* |
| 42 | * Conjugates the quaternion |
| 43 | * |
| 44 | * Parameters: |
| 45 | * q quaternion to conjugate |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 46 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 47 | static inline void __attribute__((overloadable)) |
| 48 | rsQuaternionConjugate(rs_quaternion* q) { |
| 49 | q->x = -q->x; |
| 50 | q->y = -q->y; |
| 51 | q->z = -q->z; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Dot product of two quaternions |
| 56 | * |
| 57 | * Parameters: |
| 58 | * q0 first quaternion |
| 59 | * q1 second quaternion |
| 60 | * |
| 61 | * Returns: dot product between q0 and q1 |
| 62 | */ |
| 63 | static inline float __attribute__((overloadable)) |
| 64 | rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1) { |
| 65 | return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Computes rotation matrix from the normalized quaternion |
| 70 | * |
| 71 | * Parameters: |
| 72 | * m resulting matrix |
| 73 | * q normalized quaternion |
| 74 | */ |
| 75 | static inline void __attribute__((overloadable)) |
| 76 | rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q) { |
| 77 | float xx = q->x * q->x; |
| 78 | float xy = q->x * q->y; |
| 79 | float xz = q->x * q->z; |
| 80 | float xw = q->x * q->w; |
| 81 | float yy = q->y * q->y; |
| 82 | float yz = q->y * q->z; |
| 83 | float yw = q->y * q->w; |
| 84 | float zz = q->z * q->z; |
| 85 | float zw = q->z * q->w; |
| 86 | |
| 87 | m->m[0] = 1.0f - 2.0f * ( yy + zz ); |
| 88 | m->m[4] = 2.0f * ( xy - zw ); |
| 89 | m->m[8] = 2.0f * ( xz + yw ); |
| 90 | m->m[1] = 2.0f * ( xy + zw ); |
| 91 | m->m[5] = 1.0f - 2.0f * ( xx + zz ); |
| 92 | m->m[9] = 2.0f * ( yz - xw ); |
| 93 | m->m[2] = 2.0f * ( xz - yw ); |
| 94 | m->m[6] = 2.0f * ( yz + xw ); |
| 95 | m->m[10] = 1.0f - 2.0f * ( xx + yy ); |
| 96 | m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f; |
| 97 | m->m[15] = 1.0f; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Loads a quaternion that represents a rotation about an arbitrary unit vector |
| 102 | * |
| 103 | * Parameters: |
| 104 | * q quaternion to set |
| 105 | * rot rot angle to rotate by |
| 106 | * x component of a vector |
| 107 | * y component of a vector |
| 108 | * z component of a vector |
| 109 | */ |
| 110 | static inline void __attribute__((overloadable)) |
| 111 | rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) { |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 112 | rot *= (float)(M_PI / 180.0f) * 0.5f; |
| 113 | float c = cos(rot); |
| 114 | float s = sin(rot); |
| 115 | |
| 116 | q->w = c; |
| 117 | q->x = x * s; |
| 118 | q->y = y * s; |
| 119 | q->z = z * s; |
| 120 | } |
| 121 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 122 | /* |
| 123 | * Set the quaternion from components or from another quaternion. |
| 124 | * |
| 125 | * Parameters: |
| 126 | * q destination quaternion |
| 127 | * w component |
| 128 | * x component |
| 129 | * y component |
| 130 | * z component |
| 131 | * rhs source quaternion |
| 132 | */ |
| 133 | static inline void __attribute__((overloadable)) |
| 134 | rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) { |
| 135 | q->w = w; |
| 136 | q->x = x; |
| 137 | q->y = y; |
| 138 | q->z = z; |
| 139 | } |
| 140 | |
| 141 | static inline void __attribute__((overloadable)) |
| 142 | rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) { |
| 143 | q->w = rhs->w; |
| 144 | q->x = rhs->x; |
| 145 | q->y = rhs->y; |
| 146 | q->z = rhs->z; |
| 147 | } |
| 148 | |
| 149 | /* |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 150 | * Loads a quaternion that represents a rotation about an arbitrary vector |
| 151 | * (doesn't have to be unit) |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 152 | * |
| 153 | * Parameters: |
| 154 | * q quaternion to set |
| 155 | * rot angle to rotate by |
| 156 | * x component of a vector |
| 157 | * y component of a vector |
| 158 | * z component of a vector |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 159 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 160 | static inline void __attribute__((overloadable)) |
| 161 | rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) { |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 162 | const float len = x*x + y*y + z*z; |
| 163 | if (len != 1) { |
| 164 | const float recipLen = 1.f / sqrt(len); |
| 165 | x *= recipLen; |
| 166 | y *= recipLen; |
| 167 | z *= recipLen; |
| 168 | } |
| 169 | rsQuaternionLoadRotateUnit(q, rot, x, y, z); |
| 170 | } |
| 171 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 172 | /* |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 173 | * Normalizes the quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 174 | * |
| 175 | * Parameters: |
| 176 | * q quaternion to normalize |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 177 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 178 | static inline void __attribute__((overloadable)) |
| 179 | rsQuaternionNormalize(rs_quaternion* q) { |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 180 | const float len = rsQuaternionDot(q, q); |
| 181 | if (len != 1) { |
| 182 | const float recipLen = 1.f / sqrt(len); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 183 | q->w *= recipLen; |
| 184 | q->x *= recipLen; |
| 185 | q->y *= recipLen; |
| 186 | q->z *= recipLen; |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 190 | /* |
| 191 | * Multiply quaternion by a scalar or another quaternion |
| 192 | * |
| 193 | * Parameters: |
| 194 | * q destination quaternion |
| 195 | * s scalar |
| 196 | * rhs right hand side quaternion to multiply by |
Alex Sakhartchouk | bd7b1a9 | 2011-10-18 11:54:49 -0700 | [diff] [blame] | 197 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 198 | static inline void __attribute__((overloadable)) |
| 199 | rsQuaternionMultiply(rs_quaternion* q, float s) { |
| 200 | q->w *= s; |
| 201 | q->x *= s; |
| 202 | q->y *= s; |
| 203 | q->z *= s; |
| 204 | } |
| 205 | |
| 206 | static inline void __attribute__((overloadable)) |
| 207 | rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) { |
Alex Sakhartchouk | bd7b1a9 | 2011-10-18 11:54:49 -0700 | [diff] [blame] | 208 | rs_quaternion qtmp; |
| 209 | rsQuaternionSet(&qtmp, q); |
| 210 | |
| 211 | q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z; |
| 212 | q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y; |
| 213 | q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z; |
| 214 | q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x; |
| 215 | rsQuaternionNormalize(q); |
| 216 | } |
| 217 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 218 | /* |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 219 | * Performs spherical linear interpolation between two quaternions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 220 | * |
| 221 | * Parameters: |
| 222 | * q result quaternion from interpolation |
| 223 | * q0 first param |
| 224 | * q1 second param |
| 225 | * t how much to interpolate by |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 226 | */ |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 227 | static inline void __attribute__((overloadable)) |
| 228 | rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) { |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 229 | if (t <= 0.0f) { |
| 230 | rsQuaternionSet(q, q0); |
| 231 | return; |
| 232 | } |
| 233 | if (t >= 1.0f) { |
| 234 | rsQuaternionSet(q, q1); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | rs_quaternion tempq0, tempq1; |
| 239 | rsQuaternionSet(&tempq0, q0); |
| 240 | rsQuaternionSet(&tempq1, q1); |
| 241 | |
| 242 | float angle = rsQuaternionDot(q0, q1); |
| 243 | if (angle < 0) { |
| 244 | rsQuaternionMultiply(&tempq0, -1.0f); |
| 245 | angle *= -1.0f; |
| 246 | } |
| 247 | |
| 248 | float scale, invScale; |
| 249 | if (angle + 1.0f > 0.05f) { |
| 250 | if (1.0f - angle >= 0.05f) { |
| 251 | float theta = acos(angle); |
| 252 | float invSinTheta = 1.0f / sin(theta); |
| 253 | scale = sin(theta * (1.0f - t)) * invSinTheta; |
| 254 | invScale = sin(theta * t) * invSinTheta; |
| 255 | } else { |
| 256 | scale = 1.0f - t; |
| 257 | invScale = t; |
| 258 | } |
| 259 | } else { |
| 260 | rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w); |
| 261 | scale = sin(M_PI * (0.5f - t)); |
| 262 | invScale = sin(M_PI * t); |
| 263 | } |
| 264 | |
| 265 | rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale, |
| 266 | tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale); |
| 267 | } |
| 268 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 269 | #endif // RENDERSCRIPT_RS_QUATERNION_RSH |