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: |
| 18 | summary: Quaternion routines |
| 19 | description: |
| 20 | end: |
| 21 | |
| 22 | function: rsQuaternionAdd |
| 23 | ret: void |
| 24 | arg: rs_quaternion* q, "destination quaternion to add to" |
| 25 | arg: const rs_quaternion* rhs, "right hand side quaternion to add" |
| 26 | summary: |
| 27 | description: |
| 28 | Add two quaternions |
| 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 |
| 39 | arg: rs_quaternion* q, "quaternion to conjugate" |
| 40 | summary: |
| 41 | description: |
| 42 | Conjugates the quaternion |
| 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 |
| 51 | ret: float, "dot product between q0 and q1" |
| 52 | arg: const rs_quaternion* q0, "first quaternion" |
| 53 | arg: const rs_quaternion* q1, "second quaternion" |
| 54 | summary: |
| 55 | description: |
| 56 | Dot product of two quaternions |
| 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 |
| 64 | arg: rs_matrix4x4* m, "resulting matrix" |
| 65 | arg: const rs_quaternion* q, "normalized quaternion" |
| 66 | summary: |
| 67 | description: |
| 68 | Computes rotation matrix from the normalized quaternion |
| 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 |
| 96 | arg: rs_quaternion* q, "quaternion to set" |
| 97 | arg: float rot, "rot angle to rotate by" |
| 98 | arg: float x, "component of a vector" |
| 99 | arg: float y, "component of a vector" |
| 100 | arg: float z, "component of a vector" |
| 101 | summary: |
| 102 | description: |
| 103 | Loads a quaternion that represents a rotation about an arbitrary unit vector |
| 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 |
| 118 | arg: rs_quaternion* q, "destination quaternion" |
| 119 | arg: float w, "component" |
| 120 | arg: float x, "component" |
| 121 | arg: float y, "component" |
| 122 | arg: float z, "component" |
| 123 | summary: |
| 124 | description: |
| 125 | Set the quaternion from components or from another quaternion. |
| 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 |
| 137 | arg: const rs_quaternion* rhs, "source quaternion" |
| 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 |
| 151 | arg: rs_quaternion* q, "quaternion to set" |
| 152 | arg: float rot, "angle to rotate by" |
| 153 | arg: float x, "component of a vector" |
| 154 | arg: float y, "component of a vector" |
| 155 | arg: float z, "component of a vector" |
| 156 | summary: |
| 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 |
| 174 | arg: rs_quaternion* q, "quaternion to normalize" |
| 175 | summary: |
| 176 | description: |
| 177 | Normalizes the quaternion |
| 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 |
| 192 | arg: rs_quaternion* q, "destination quaternion" |
| 193 | arg: float s, "scalar" |
| 194 | summary: |
| 195 | description: |
| 196 | Multiply quaternion by a scalar or another quaternion |
| 197 | inline: |
| 198 | q->w *= s; |
| 199 | q->x *= s; |
| 200 | q->y *= s; |
| 201 | q->z *= s; |
| 202 | test: none |
| 203 | end: |
| 204 | |
| 205 | function: rsQuaternionMultiply |
| 206 | ret: void |
| 207 | arg: rs_quaternion* q |
| 208 | arg: const rs_quaternion* rhs, "right hand side quaternion to multiply by" |
| 209 | inline: |
| 210 | rs_quaternion qtmp; |
| 211 | rsQuaternionSet(&qtmp, q); |
| 212 | |
| 213 | q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z; |
| 214 | q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y; |
| 215 | q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z; |
| 216 | q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x; |
| 217 | rsQuaternionNormalize(q); |
| 218 | test: none |
| 219 | end: |
| 220 | |
| 221 | function: rsQuaternionSlerp |
| 222 | ret: void |
| 223 | arg: rs_quaternion* q, "result quaternion from interpolation" |
| 224 | arg: const rs_quaternion* q0, "first param" |
| 225 | arg: const rs_quaternion* q1, "second param" |
| 226 | arg: float t, "how much to interpolate by" |
| 227 | summary: |
| 228 | description: |
| 229 | Performs spherical linear interpolation between two quaternions |
| 230 | inline: |
| 231 | if (t <= 0.0f) { |
| 232 | rsQuaternionSet(q, q0); |
| 233 | return; |
| 234 | } |
| 235 | if (t >= 1.0f) { |
| 236 | rsQuaternionSet(q, q1); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | rs_quaternion tempq0, tempq1; |
| 241 | rsQuaternionSet(&tempq0, q0); |
| 242 | rsQuaternionSet(&tempq1, q1); |
| 243 | |
| 244 | float angle = rsQuaternionDot(q0, q1); |
| 245 | if (angle < 0) { |
| 246 | rsQuaternionMultiply(&tempq0, -1.0f); |
| 247 | angle *= -1.0f; |
| 248 | } |
| 249 | |
| 250 | float scale, invScale; |
| 251 | if (angle + 1.0f > 0.05f) { |
| 252 | if (1.0f - angle >= 0.05f) { |
| 253 | float theta = acos(angle); |
| 254 | float invSinTheta = 1.0f / sin(theta); |
| 255 | scale = sin(theta * (1.0f - t)) * invSinTheta; |
| 256 | invScale = sin(theta * t) * invSinTheta; |
| 257 | } else { |
| 258 | scale = 1.0f - t; |
| 259 | invScale = t; |
| 260 | } |
| 261 | } else { |
| 262 | rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w); |
| 263 | scale = sin(M_PI * (0.5f - t)); |
| 264 | invScale = sin(M_PI * t); |
| 265 | } |
| 266 | |
| 267 | rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale, |
| 268 | tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale); |
| 269 | test: none |
| 270 | end: |