diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-06-14 08:37:55 -0600 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-06-14 08:37:55 -0600 |
commit | 9ed4a01ac4305baff9e5ee1484691e78def105a1 (patch) | |
tree | a8b5c556baf7053546e188e42278c6b88c7b0543 /libdimension | |
parent | 9b758508df283a533a4cfc605545a35f77bc9d5f (diff) | |
download | dimension-9ed4a01ac4305baff9e5ee1484691e78def105a1.tar.xz |
Fix rotational alignment.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/geometry.h | 14 | ||||
-rw-r--r-- | libdimension/geometry.c | 39 | ||||
-rw-r--r-- | libdimension/libdimension.pc.in | 2 |
3 files changed, 40 insertions, 15 deletions
diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h index ba8f531..7e466dd 100644 --- a/libdimension/dimension/geometry.h +++ b/libdimension/dimension/geometry.h @@ -178,6 +178,16 @@ dmnsn_matrix dmnsn_translation_matrix(dmnsn_vector d); * @return The transformation matrix. */ dmnsn_matrix dmnsn_rotation_matrix(dmnsn_vector theta); +/** + * An alignment matrix. + * @param[in] from The initial vector. + * @param[in] to The desired direction. + * @param[in] axis1 The first axis about which to rotate. + * @param[in] axis2 The second axis about which to rotate. + * @return A transformation matrix that will rotate \p from to \p to. + */ +dmnsn_matrix dmnsn_alignment_matrix(dmnsn_vector from, dmnsn_vector to, + dmnsn_vector axis1, dmnsn_vector axis2); /** * Construct a new line. @@ -359,10 +369,6 @@ dmnsn_vector_max(dmnsn_vector a, dmnsn_vector b) ); } -/** Return the angle between two vectors with respect to an axis. */ -double dmnsn_vector_axis_angle(dmnsn_vector v1, dmnsn_vector v2, - dmnsn_vector axis); - /** Invert a matrix. */ dmnsn_matrix dmnsn_matrix_inverse(dmnsn_matrix A); diff --git a/libdimension/geometry.c b/libdimension/geometry.c index b36b9c3..f7c6842 100644 --- a/libdimension/geometry.c +++ b/libdimension/geometry.c @@ -82,27 +82,46 @@ dmnsn_rotation_matrix(dmnsn_vector theta) } /* Find the angle between two vectors with respect to an axis */ -double -dmnsn_vector_axis_angle(dmnsn_vector v1, dmnsn_vector v2, dmnsn_vector axis) +static double +dmnsn_axis_angle(dmnsn_vector from, dmnsn_vector to, dmnsn_vector axis) { - dmnsn_vector d = dmnsn_vector_sub(v1, v2); - dmnsn_vector proj = dmnsn_vector_add(dmnsn_vector_proj(d, axis), v2); + from = dmnsn_vector_sub(from, dmnsn_vector_proj(from, axis)); + to = dmnsn_vector_sub(to, dmnsn_vector_proj(to, axis)); - double projn = dmnsn_vector_norm(proj); - if (fabs(projn) < dmnsn_epsilon) + double fromnorm = dmnsn_vector_norm(from); + double tonorm = dmnsn_vector_norm(to); + if (fromnorm < dmnsn_epsilon || tonorm < dmnsn_epsilon) { return 0.0; + } + + from = dmnsn_vector_div(from, fromnorm); + to = dmnsn_vector_div(to, tonorm); - double c = dmnsn_vector_dot(dmnsn_vector_normalized(v1), - dmnsn_vector_div(proj, projn)); - double angle = acos(c); + double angle = acos(dmnsn_vector_dot(from, to)); - if (dmnsn_vector_dot(dmnsn_vector_cross(v1, proj), axis) > 0.0) { + if (dmnsn_vector_dot(dmnsn_vector_cross(from, to), axis) > 0.0) { return angle; } else { return -angle; } } +/* Alignment matrix */ +dmnsn_matrix +dmnsn_alignment_matrix(dmnsn_vector from, dmnsn_vector to, + dmnsn_vector axis1, dmnsn_vector axis2) +{ + double theta1 = dmnsn_axis_angle(from, to, axis1); + dmnsn_matrix align1 = dmnsn_rotation_matrix(dmnsn_vector_mul(theta1, axis1)); + from = dmnsn_transform_vector(align1, from); + axis2 = dmnsn_transform_vector(align1, axis2); + + double theta2 = dmnsn_axis_angle(from, to, axis2); + dmnsn_matrix align2 = dmnsn_rotation_matrix(dmnsn_vector_mul(theta2, axis2)); + + return dmnsn_matrix_mul(align2, align1); +} + /* Matrix inversion helper functions */ /** A 2x2 matrix for inversion by partitioning. */ diff --git a/libdimension/libdimension.pc.in b/libdimension/libdimension.pc.in index 97a3cb5..fd5649f 100644 --- a/libdimension/libdimension.pc.in +++ b/libdimension/libdimension.pc.in @@ -6,6 +6,6 @@ includedir=@includedir@ Name: libdimension Description: The Dimension Library Requires: -Version: @VERSION@ +Version: @PACKAGE_VERSION@ Libs: -L${libdir} -ldimension Cflags: |