| 250 | | |
| 251 | | def LERP(self, undefined): |
| 252 | | pass |
| 253 | | |
| 254 | | def SLERP(self, undefined): |
| 255 | | pass |
| | 251 | |
| | 252 | @staticmethod |
| | 253 | def dot(Q1, Q2): |
| | 254 | if isinstance(Q1, Quaternion): |
| | 255 | if isinstance(Q2, Quaternion): |
| | 256 | return Q1.x * Q2.x + Q1.y * Q2.y + Q1.z * Q2.z + Q1.w * Q2.w |
| | 257 | else: |
| | 258 | raise TypeError("Invalid argument") |
| | 259 | else: |
| | 260 | raise TypeError("Invalid argument") |
| | 261 | |
| | 262 | # Linearly interpolate each component, then normalize the Quaternion |
| | 263 | @staticmethod |
| | 264 | def LERP(Q1, Q2, t): |
| | 265 | if isinstance(Q1, Quaternion): |
| | 266 | if isinstance(Q2, Quaternion): |
| | 267 | return (Q1 * (1-t) + Q2 * t).normalize() |
| | 268 | else: |
| | 269 | raise TypeError("Invalid argument") |
| | 270 | else: |
| | 271 | raise TypeError("Invalid argument") |
| | 272 | |
| | 273 | #Spherical linear interpolation |
| | 274 | @staticmethod |
| | 275 | def SLERP(Q1, Q2, t): |
| | 276 | if isinstance(Q1, Quaternion): |
| | 277 | if isinstance(Q2, Quaternion): |
| | 278 | |
| | 279 | dot = Quaternion.dot(Q1, Q2) |
| | 280 | if dot < 0: |
| | 281 | dot = -dot |
| | 282 | Q3 = -Q2 |
| | 283 | else: |
| | 284 | Q3 = Q2 |
| | 285 | |
| | 286 | if dot < 0.95: |
| | 287 | angle = math.acos(dot) |
| | 288 | w1 = math.sin(angle * (1-t)) / math.sin(angle) |
| | 289 | w2 = math.sin(angle * t) / math.sin(angle) |
| | 290 | return Q1 * w1 + Q3 * w2 |
| | 291 | else: |
| | 292 | # The angle is small. We use linear interpolation |
| | 293 | Quaternion.LERP(Q1, Q3, t) |
| | 294 | |
| | 295 | else: |
| | 296 | raise TypeError("Invalid argument") |
| | 297 | else: |
| | 298 | raise TypeError("Invalid argument") |