Skip to content

fix: quaternion normalization divides by zero for anti-parallel - #26

Draft
andrewwhitecdw wants to merge 1 commit into
NVlabs:mainfrom
andrewwhitecdw:bugfix/uniform-sphere-b2722ae5
Draft

fix: quaternion normalization divides by zero for anti-parallel#26
andrewwhitecdw wants to merge 1 commit into
NVlabs:mainfrom
andrewwhitecdw:bugfix/uniform-sphere-b2722ae5

Conversation

@andrewwhitecdw

@andrewwhitecdw andrewwhitecdw commented Jul 27, 2026

Copy link
Copy Markdown

Problem

fix: quaternion normalization divides by zero for anti-parallel vectors

Fix

Replace:

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)

with:

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 = 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

Files changed

  • utils/uniform_sphere.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant