Skip to content

Commit a763a0b

Browse files
authored
Merge pull request #11 from hsorby/main
Don't divide by zero in axis_angle_to_rotation_matrix.
2 parents a66b8b6 + 65c50cf commit a763a0b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/cmlibs/maths/vectorops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,10 @@ def axis_angle_to_rotation_matrix(axis, theta):
318318
:param theta: Angle of rotation in right hand sense around axis, in radians.
319319
:return: 3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v' = Mv
320320
"""
321-
axis = div(axis, sqrt(dot(axis, axis)))
321+
mag_axis = magnitude(axis)
322+
norm_axis = axis if mag_axis == 0.0 else div(axis, mag_axis)
322323
a = cos(theta / 2.0)
323-
b, c, d = mult(axis, -sin(theta / 2.0))
324+
b, c, d = mult(norm_axis, -sin(theta / 2.0))
324325
aa, bb, cc, dd = a * a, b * b, c * c, d * d
325326
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
326327
return [[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],

0 commit comments

Comments
 (0)