diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 18:18:03 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 18:18:03 +0000 |
commit | 075963a55fe8801a8a61f86cd37303f8246cbdc3 (patch) | |
tree | f6e1ecf5259b953546f799e078b772b7564431c1 /libdimension/geometry.c | |
parent | 6d3e9c207696a331da03a168a8dfa5a4a9d1e3bf (diff) | |
download | dimension-075963a55fe8801a8a61f86cd37303f8246cbdc3.tar.xz |
Handle zero rotation vectors nicely in dmnsn_rotation_matrix().
Diffstat (limited to 'libdimension/geometry.c')
-rw-r--r-- | libdimension/geometry.c | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/libdimension/geometry.c b/libdimension/geometry.c index e9c2122..edb73cd 100644 --- a/libdimension/geometry.c +++ b/libdimension/geometry.c @@ -80,35 +80,39 @@ dmnsn_rotation_matrix(dmnsn_vector theta) dmnsn_vector axis, n1, n2, n3; double angle, s, t, x, y, z; - axis = dmnsn_vector_normalize(theta); angle = dmnsn_vector_norm(theta); - - /* Shorthand to fit logical lines on one line */ - - s = sin(angle); - t = 1.0 - cos(angle); - - x = axis.x; - y = axis.y; - z = axis.z; - - /* Construct vectors, then a matrix, so our dmnsn_matrix_construct() call - is reasonably small */ - - n1 = dmnsn_vector_construct(1.0 + t*(x*x - 1.0), - z*s + t*x*y, - -y*s + t*x*z); - n2 = dmnsn_vector_construct(-z*s + t*x*y, - 1.0 + t*(y*y - 1.0), - x*s + t*y*z); - n3 = dmnsn_vector_construct(y*s + t*x*z, - -x*s + t*y*z, - 1.0 + t*(z*z - 1.0)); - - return dmnsn_matrix_construct(n1.x, n2.x, n3.x, 0.0, - n1.y, n2.y, n3.y, 0.0, - n1.z, n2.z, n3.z, 0.0, - 0.0, 0.0, 0.0, 1.0); + if (angle != 0.0) { + axis = dmnsn_vector_normalize(theta); + + /* Shorthand to fit logical lines on one line */ + + s = sin(angle); + t = 1.0 - cos(angle); + + x = axis.x; + y = axis.y; + z = axis.z; + + /* Construct vectors, then a matrix, so our dmnsn_matrix_construct() call + is reasonably small */ + + n1 = dmnsn_vector_construct(1.0 + t*(x*x - 1.0), + z*s + t*x*y, + -y*s + t*x*z); + n2 = dmnsn_vector_construct(-z*s + t*x*y, + 1.0 + t*(y*y - 1.0), + x*s + t*y*z); + n3 = dmnsn_vector_construct(y*s + t*x*z, + -x*s + t*y*z, + 1.0 + t*(z*z - 1.0)); + + return dmnsn_matrix_construct(n1.x, n2.x, n3.x, 0.0, + n1.y, n2.y, n3.y, 0.0, + n1.z, n2.z, n3.z, 0.0, + 0.0, 0.0, 0.0, 1.0); + } else { + return dmnsn_identity_matrix(); + } } /* Add two vectors */ |