summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-10-19 15:19:50 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-10-19 15:25:28 -0400
commitae7af4e30ff752ca7fa173df8c171c344991f6a1 (patch)
tree8b246b9db78db86ad84203256ae2d816405e6624
parent283ec414e995fac7c8fc3024c94fd2113b1b701c (diff)
quaternion: Replace quaternion->transform with rotate.
* sly/quaternion.scm (quaternion->transform): Remove. (rotate): New procedure.
-rw-r--r--sly/quaternion.scm40
1 files changed, 21 insertions, 19 deletions
diff --git a/sly/quaternion.scm b/sly/quaternion.scm
index 9bf9427..663b0dd 100644
--- a/sly/quaternion.scm
+++ b/sly/quaternion.scm
@@ -34,8 +34,8 @@
identity-quaternion null-quaternion
quaternion* quaternion-slerp
quaternion-magnitude quaternion-normalize
- vector->quaternion axis-angle->quaternion
- quaternion->vector quaternion->transform))
+ vector->quaternion axis-angle->quaternion quaternion->vector
+ rotate))
(define-record-type <quaternion>
(make-quaternion w x y z)
@@ -112,20 +112,22 @@ AXIS must be a 3D vector."
((? vector4? v)
(make-quaternion (vx v) (vy v) (vz v) (vw v)))))
-(define (quaternion->transform q)
- "Convert the quaternion Q into a 4x4 transformation matrix."
- (match q
- (($ <quaternion> w x y z)
- (make-transform
- (- 1 (* 2 (square y)) (* 2 (square z)))
- (- (* 2 x y) (* 2 w z))
- (+ (* 2 x z) (* 2 w y))
- 0
- (+ (* 2 x y) (* 2 w z))
- (- 1 (* 2 (square x)) (* 2 (square z)))
- (- (* 2 y z) (* 2 w x))
- 0
- (- (* 2 x z) (* 2 w y))
- (+ (* 2 y z) (* 2 w x))
- (- 1 (* 2 (square x)) (* 2 (square y)))
- 0 0 0 0 1))))
+(define rotate
+ (match-lambda*
+ ;; Automagically convert axis angles to quaternions.
+ (((? vector3? axis) (? number? theta))
+ (rotate (axis-angle->quaternion axis theta)))
+ ((($ <quaternion> w x y z))
+ (make-transform
+ (- 1 (* 2 (square y)) (* 2 (square z)))
+ (- (* 2 x y) (* 2 w z))
+ (+ (* 2 x z) (* 2 w y))
+ 0
+ (+ (* 2 x y) (* 2 w z))
+ (- 1 (* 2 (square x)) (* 2 (square z)))
+ (- (* 2 y z) (* 2 w x))
+ 0
+ (- (* 2 x z) (* 2 w y))
+ (+ (* 2 y z) (* 2 w x))
+ (- 1 (* 2 (square x)) (* 2 (square y)))
+ 0 0 0 0 1))))