diff options
author | David Thompson <dthompson2@worcester.edu> | 2014-10-21 19:40:50 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2014-10-21 19:40:50 -0400 |
commit | 1ee2090e9394ffbeef00609bce2e3fe8a7109088 (patch) | |
tree | 2f975346b7a8a446abf3950c0b74345dd38fd570 | |
parent | c9445d4215756c279881217b43d0479c38deb8d6 (diff) |
quaternion: Add axis angle conversion to constructor.
* sly/quaternion.scm (%make-quaternion, quaternion): New procedures.
(axis-angle->quaternion): Delete procedure.
(make-quaternion): Support building from axis angle.
(rotate): Remove axis angle special-case.
-rw-r--r-- | sly/quaternion.scm | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/sly/quaternion.scm b/sly/quaternion.scm index 8f34602..711de9c 100644 --- a/sly/quaternion.scm +++ b/sly/quaternion.scm @@ -28,23 +28,35 @@ #:use-module (sly math) #:use-module (sly transform) #:use-module (sly math vector) - #:export (make-quaternion + #:export (make-quaternion quaternion quaternion? quaternion-w quaternion-x quaternion-y quaternion-z identity-quaternion null-quaternion quaternion* quaternion-slerp quaternion-magnitude quaternion-normalize - vector->quaternion axis-angle->quaternion quaternion->vector + vector->quaternion quaternion->vector rotate)) (define-record-type <quaternion> - (make-quaternion w x y z) + (%make-quaternion w x y z) quaternion? (w quaternion-w) (x quaternion-x) (y quaternion-y) (z quaternion-z)) +(define make-quaternion + (match-lambda* + ((($ <vector3> x y z) (? number? theta)) + ;; Convert an axis angle to a quaternion + (let* ((theta/2 (/ theta 2)) + (sin (sin theta/2))) + (%make-quaternion (cos theta/2) (* x sin) (* y sin) (* z sin)))) + ((w x y z) + (%make-quaternion w x y z)))) + +(define quaternion make-quaternion) + (define identity-quaternion (make-quaternion 1 0 0 0)) (define null-quaternion (make-quaternion 0 0 0 0)) @@ -91,15 +103,6 @@ Q2 and blending factor DELTA." (/ (quaternion-y q) m) (/ (quaternion-z q) m))))) -(define (axis-angle->quaternion axis theta) - "Convert the rotation of THETA radians about AXIS to a quaternion. -AXIS must be a 3D vector." - (match axis - (($ <vector3> x y z) - (let* ((theta/2 (/ theta 2)) - (sin (sin theta/2))) - (make-quaternion (cos theta/2) (* x sin) (* y sin) (* z sin)))))) - (define quaternion->vector (match-lambda (($ <quaternion> w x y z) @@ -111,11 +114,8 @@ AXIS must be a 3D vector." (make-quaternion x y z w)))) (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)) + (match-lambda + (($ <quaternion> w x y z) (make-transform (- 1 (* 2 (square y)) (* 2 (square z))) (- (* 2 x y) (* 2 w z)) |