Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2015 The Android Open Source Project |
| 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 | |
| 17 | header: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 18 | summary: Quaternion Functions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 19 | description: |
| 20 | end: |
| 21 | |
| 22 | function: rsQuaternionAdd |
| 23 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 24 | arg: rs_quaternion* q, "Destination quaternion to add to." |
| 25 | arg: const rs_quaternion* rhs, "Quaternion to add." |
| 26 | summary: Add two quaternions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 27 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 28 | Adds two quaternions, i.e. <code>*q += *rhs;</code> |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 29 | inline: |
| 30 | q->w *= rhs->w; |
| 31 | q->x *= rhs->x; |
| 32 | q->y *= rhs->y; |
| 33 | q->z *= rhs->z; |
| 34 | test: none |
| 35 | end: |
| 36 | |
| 37 | function: rsQuaternionConjugate |
| 38 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 39 | arg: rs_quaternion* q, "Quaternion to modify." |
| 40 | summary: Conjugate a quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 41 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 42 | Conjugates the quaternion. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 43 | inline: |
| 44 | q->x = -q->x; |
| 45 | q->y = -q->y; |
| 46 | q->z = -q->z; |
| 47 | test: none |
| 48 | end: |
| 49 | |
| 50 | function: rsQuaternionDot |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 51 | ret: float |
| 52 | arg: const rs_quaternion* q0, "First quaternion." |
| 53 | arg: const rs_quaternion* q1, "Second quaternion." |
| 54 | summary: Dot product of two quaternions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 55 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 56 | Returns the dot product of two quaternions. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 57 | inline: |
| 58 | return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z; |
| 59 | test: none |
| 60 | end: |
| 61 | |
| 62 | function: rsQuaternionGetMatrixUnit |
| 63 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 64 | arg: rs_matrix4x4* m, "Resulting matrix." |
| 65 | arg: const rs_quaternion* q, "Normalized quaternion." |
| 66 | summary: Get a rotation matrix from a quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 67 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 68 | Computes a rotation matrix from the normalized quaternion. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 69 | inline: |
| 70 | float xx = q->x * q->x; |
| 71 | float xy = q->x * q->y; |
| 72 | float xz = q->x * q->z; |
| 73 | float xw = q->x * q->w; |
| 74 | float yy = q->y * q->y; |
| 75 | float yz = q->y * q->z; |
| 76 | float yw = q->y * q->w; |
| 77 | float zz = q->z * q->z; |
| 78 | float zw = q->z * q->w; |
| 79 | |
| 80 | m->m[0] = 1.0f - 2.0f * ( yy + zz ); |
| 81 | m->m[4] = 2.0f * ( xy - zw ); |
| 82 | m->m[8] = 2.0f * ( xz + yw ); |
| 83 | m->m[1] = 2.0f * ( xy + zw ); |
| 84 | m->m[5] = 1.0f - 2.0f * ( xx + zz ); |
| 85 | m->m[9] = 2.0f * ( yz - xw ); |
| 86 | m->m[2] = 2.0f * ( xz - yw ); |
| 87 | m->m[6] = 2.0f * ( yz + xw ); |
| 88 | m->m[10] = 1.0f - 2.0f * ( xx + yy ); |
| 89 | m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f; |
| 90 | m->m[15] = 1.0f; |
| 91 | test: none |
| 92 | end: |
| 93 | |
| 94 | function: rsQuaternionLoadRotateUnit |
| 95 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 96 | arg: rs_quaternion* q, "Destination quaternion." |
| 97 | arg: float rot, "Angle to rotate by, in radians." |
| 98 | arg: float x, "X component of the vector." |
| 99 | arg: float y, "Y component of the vector." |
| 100 | arg: float z, "Z component of the vector." |
| 101 | summary: Quaternion that represents a rotation about an arbitrary unit vector |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 102 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 103 | Loads a quaternion that represents a rotation about an arbitrary unit vector. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 104 | inline: |
| 105 | rot *= (float)(M_PI / 180.0f) * 0.5f; |
| 106 | float c = cos(rot); |
| 107 | float s = sin(rot); |
| 108 | |
| 109 | q->w = c; |
| 110 | q->x = x * s; |
| 111 | q->y = y * s; |
| 112 | q->z = z * s; |
| 113 | test: none |
| 114 | end: |
| 115 | |
| 116 | function: rsQuaternionSet |
| 117 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 118 | arg: rs_quaternion* q, "Destination quaternion." |
| 119 | arg: float w, "W component." |
| 120 | arg: float x, "X component." |
| 121 | arg: float y, "Y component." |
| 122 | arg: float z, "Z component." |
| 123 | summary: Create a quarternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 124 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 125 | Creates a quaternion from its four components or from another quaternion. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 126 | inline: |
| 127 | q->w = w; |
| 128 | q->x = x; |
| 129 | q->y = y; |
| 130 | q->z = z; |
| 131 | test: none |
| 132 | end: |
| 133 | |
| 134 | function: rsQuaternionSet |
| 135 | ret: void |
| 136 | arg: rs_quaternion* q |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 137 | arg: const rs_quaternion* rhs, "Source quaternion." |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 138 | inline: |
| 139 | q->w = rhs->w; |
| 140 | q->x = rhs->x; |
| 141 | q->y = rhs->y; |
| 142 | q->z = rhs->z; |
| 143 | test: none |
| 144 | end: |
| 145 | |
| 146 | # NOTE: The following inline definitions depend on each other. The order must be preserved |
| 147 | # for the compilation to work. |
| 148 | |
| 149 | function: rsQuaternionLoadRotate |
| 150 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 151 | arg: rs_quaternion* q, "Destination quaternion." |
| 152 | arg: float rot, "Angle to rotate by." |
| 153 | arg: float x, "X component of a vector." |
| 154 | arg: float y, "Y component of a vector." |
| 155 | arg: float z, "Z component of a vector." |
| 156 | summary: Create a rotation quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 157 | description: |
| 158 | Loads a quaternion that represents a rotation about an arbitrary vector |
| 159 | (doesn't have to be unit) |
| 160 | inline: |
| 161 | const float len = x*x + y*y + z*z; |
| 162 | if (len != 1) { |
| 163 | const float recipLen = 1.f / sqrt(len); |
| 164 | x *= recipLen; |
| 165 | y *= recipLen; |
| 166 | z *= recipLen; |
| 167 | } |
| 168 | rsQuaternionLoadRotateUnit(q, rot, x, y, z); |
| 169 | test: none |
| 170 | end: |
| 171 | |
| 172 | function: rsQuaternionNormalize |
| 173 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 174 | arg: rs_quaternion* q, "Quaternion to normalize." |
| 175 | summary: Normalize a quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 176 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 177 | Normalizes the quaternion. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 178 | inline: |
| 179 | const float len = rsQuaternionDot(q, q); |
| 180 | if (len != 1) { |
| 181 | const float recipLen = 1.f / sqrt(len); |
| 182 | q->w *= recipLen; |
| 183 | q->x *= recipLen; |
| 184 | q->y *= recipLen; |
| 185 | q->z *= recipLen; |
| 186 | } |
| 187 | test: none |
| 188 | end: |
| 189 | |
| 190 | function: rsQuaternionMultiply |
| 191 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 192 | arg: rs_quaternion* q, "Destination quaternion." |
| 193 | arg: float scalar, "Scalar to multiply the quarternion by." |
| 194 | summary: Multiply a quaternion by a scalar or another quaternion |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 195 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 196 | Multiplies a quaternion by a scalar or by another quaternion, e.g |
| 197 | <code>*q = *q * scalar;</code> or <code>*q = *q * *rhs;</code>. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 198 | inline: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 199 | q->w *= scalar; |
| 200 | q->x *= scalar; |
| 201 | q->y *= scalar; |
| 202 | q->z *= scalar; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 203 | test: none |
| 204 | end: |
| 205 | |
| 206 | function: rsQuaternionMultiply |
| 207 | ret: void |
| 208 | arg: rs_quaternion* q |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 209 | arg: const rs_quaternion* rhs, "Quarternion to multiply the destination quaternion by." |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 210 | inline: |
| 211 | rs_quaternion qtmp; |
| 212 | rsQuaternionSet(&qtmp, q); |
| 213 | |
| 214 | q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z; |
| 215 | q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y; |
| 216 | q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z; |
| 217 | q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x; |
| 218 | rsQuaternionNormalize(q); |
| 219 | test: none |
| 220 | end: |
| 221 | |
| 222 | function: rsQuaternionSlerp |
| 223 | ret: void |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 224 | arg: rs_quaternion* q, "Result quaternion from the interpolation." |
| 225 | arg: const rs_quaternion* q0, "First input quaternion." |
| 226 | arg: const rs_quaternion* q1, "Second input quaternion." |
| 227 | arg: float t, "How much to interpolate by." |
| 228 | summary: Spherical linear interpolation between two quaternions |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 229 | description: |
Jean-Luc Brouillet | 20b27d6 | 2015-04-03 14:39:53 -0700 | [diff] [blame^] | 230 | Performs spherical linear interpolation between two quaternions. |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 231 | inline: |
| 232 | if (t <= 0.0f) { |
| 233 | rsQuaternionSet(q, q0); |
| 234 | return; |
| 235 | } |
| 236 | if (t >= 1.0f) { |
| 237 | rsQuaternionSet(q, q1); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | rs_quaternion tempq0, tempq1; |
| 242 | rsQuaternionSet(&tempq0, q0); |
| 243 | rsQuaternionSet(&tempq1, q1); |
| 244 | |
| 245 | float angle = rsQuaternionDot(q0, q1); |
| 246 | if (angle < 0) { |
| 247 | rsQuaternionMultiply(&tempq0, -1.0f); |
| 248 | angle *= -1.0f; |
| 249 | } |
| 250 | |
| 251 | float scale, invScale; |
| 252 | if (angle + 1.0f > 0.05f) { |
| 253 | if (1.0f - angle >= 0.05f) { |
| 254 | float theta = acos(angle); |
| 255 | float invSinTheta = 1.0f / sin(theta); |
| 256 | scale = sin(theta * (1.0f - t)) * invSinTheta; |
| 257 | invScale = sin(theta * t) * invSinTheta; |
| 258 | } else { |
| 259 | scale = 1.0f - t; |
| 260 | invScale = t; |
| 261 | } |
| 262 | } else { |
| 263 | rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w); |
| 264 | scale = sin(M_PI * (0.5f - t)); |
| 265 | invScale = sin(M_PI * t); |
| 266 | } |
| 267 | |
| 268 | rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale, |
| 269 | tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale); |
| 270 | test: none |
| 271 | end: |