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