From a0acfa9ca61a7ee6a4e56882c0ddf07e1d7cf0ca Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 27 Jul 2026 07:30:28 -0500 Subject: [PATCH] fix: quaternion normalization divides by zero for anti-parallel --- utils/uniform_sphere.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/utils/uniform_sphere.py b/utils/uniform_sphere.py index fdf029c..0f55aa9 100644 --- a/utils/uniform_sphere.py +++ b/utils/uniform_sphere.py @@ -29,8 +29,15 @@ def quat_between_vectors(u, v): """Return the quaternion transformation between two vectors.""" xyz = np.cross(u, v) w = np.sqrt(np.linalg.norm(u)**2 * np.linalg.norm(v)**2) + np.dot(u, v) - q = [xyz[0], xyz[1], xyz[2], w] - return q / np.linalg.norm(q) + q = np.array([xyz[0], xyz[1], xyz[2], w]) + norm = np.linalg.norm(q) + if norm < 1e-12: + # Anti-parallel vectors: choose any axis perpendicular to u + tmp = np.array([1.0, 0.0, 0.0]) if abs(u[0]) < abs(u[1]) else np.array([0.0, 1.0, 0.0]) + xyz = np.cross(u, tmp) + q = np.array([xyz[0], xyz[1], xyz[2], 0.0]) + norm = np.linalg.norm(q) + return q / norm def point_from_spherical_coords(r, theta, phi):