Geometry utilities#
The genesis.utils.geom module provides geometry helpers for rotations, quaternions, and rigid transforms, also exposed at the top level as gs.<name>. They accept NumPy arrays or PyTorch tensors and operate on single values or batches.
They follow the project conventions: quaternions are (w, x, y, z) scalar-first (Hamilton), Euler angles are degrees in extrinsic x-y-z order, and the world frame is right-handed and Z-up. See Conventions.
Rotation conversions#
- genesis.utils.geom.quat_to_rotvec(quat: ndarray, out: ndarray | None = None) ndarray[source]#
Compute the angle-axis representation of a single or a batch of quaternions (qw, qx, qy, qz).
- Parameters:
quat – N-dimensional array whose last dimension gathers the 4 quaternion coordinates (qw, qx, qy, qz).
out – Pre-allocated array into which to store the result. If not provided, a new array is freshly-allocated and returned, which is slower.
- genesis.utils.geom.rotvec_to_quat(rotvec: ndarray, out: ndarray | None = None) ndarray[source]#
Compute the quaternion representation (qw, qx, qy, qz) of a single or a batch of angle-axis vectors.
- Parameters:
rotvec – N-dimensional array whose last dimension gathers the 3 angle-axis components angle * (ax, ay, az).
out – Pre-allocated array into which to store the result. If not provided, a new array is freshly-allocated and returned, which is slower.
Quaternion operations#
- genesis.utils.geom.transform_quat_by_quat(v, u)[source]#
This method transforms quat_v by quat_u.
This is equivalent to quatmul(quat_u, quat_v) or R_u @ R_v
- genesis.utils.geom.transform_by_quat(v, quat)[source]#
This method transforms quat_v by quat_u.
This is equivalent to quatmul(quat_u, quat_v) or R_u @ R_v
- genesis.utils.geom.slerp(q0, q1, t)[source]#
Perform spherical linear interpolation between two quaternions.
- Parameters:
q0 (numpy.array | torch.Tensor) – The start quaternion (w, x, y, z), can be batched.
q1 (numpy.array | torch.Tensor) – The end quaternion (w, x, y, z), can be batched.
t (numpy.array | torch.Tensor) – The interpolation parameter between 0 and 1.
- Returns:
The interpolated quaternion (w, x, y, z).
- Return type:
numpy.array | torch.Tensor
Rigid transforms#
- genesis.utils.geom.transform_by_T(pos, T)[source]#
Transforms 3D points by a 4x4 transformation matrix or a batch of matrices, supporting both NumPy arrays and PyTorch tensors.
- Parameters:
pos (np.ndarray | torch.Tensor) – A numpy array or torch tensor of 3D points. Can be a single point (3,), a batch of points (B, 3), or a batched batch of points (B, N, 3).
T (np.ndarray | torch.Tensor) – The 4x4 transformation matrix or a batch of B transformation matrices of shape (B, 4, 4). Must be of the same type as
pos.
- Return type:
The transformed points in a shape corresponding to the input dimensions.
Vectors and sampling#
- genesis.utils.geom.spherical_to_cartesian(theta: Tensor, phi: Tensor) tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]#
Convert spherical coordinates to Cartesian coordinates.
- Parameters:
theta (torch.Tensor) – Horizontal angles in radians.
phi (torch.Tensor) – Vertical angles in radians.
- Returns:
vectors – Vectors in cartesian coordinates as tensor of shape (…, 3).
- Return type:
torch.Tensor
- genesis.utils.geom.generate_grid_points_on_plane(lo: tuple[float, float, float], hi: tuple[float, float, float], normal: tuple[float, float, float], nx: int, ny: int) ndarray[source]#
Build an nx-by-ny grid of points on the plane defined by the bounds and normal.
- Parameters:
lo (array-like[float, float, float]) – Lower bound of the plane
hi (array-like[float, float, float]) – Upper bound of the plane
normal (array-like[float, float, float]) – Normal of the plane
nx (int) – Number of grid points in x direction
ny (int) – Number of grid points in y direction
- Returns:
grid – Grid points on the plane
- Return type:
np.ndarray, shape (ny, nx, 3)
- genesis.utils.geom.generate_ring_points_on_sphere(radius: float, cap_axis: tuple[float, float, float], n_rings: int, arc_spacing: float, return_normals: bool = False) ndarray | tuple[numpy.ndarray, numpy.ndarray][source]#
Build
n_ringsconcentric latitude rings on the upper spherical cap (pole atcap_axis).Ring index 0 is the pole; ring
n_rings - 1is the equatorial plane of the cap. The number of samples on each ring is chosen from the ring circumference andarc_spacing.- Parameters:
radius (float) – Radius of points on the sphere, used to determine the number of points on each ring.
cap_axis ((float, float, float)) – Unit-ish axis from sphere center toward the dome pole (probes lie on the cap around this axis).
n_rings (int) – Number of latitude rings, including the pole as a degenerate ring.
arc_spacing (float) – Target arc length between neighboring probes along each ring.
return_normals (bool) – Whether to return the normal vectors of the points.
- Returns:
points (np.ndarray, shape (N, 3)) – Points on the sphere surface.
normals (np.ndarray, shape (N, 3), optional) – Normal vectors of the points. Only returned if
return_normalsis True.
- class genesis.utils.geom.SpatialHasher(cell_size, grid_res, n_slots=None)[source]#
- compute_reordered_idx(n, pos, active, reordered_idx)[source]#
Reordered element idx based on the given positions and active flags.
- Parameters:
n (int) – The number of elements in the positions and active arrays.
pos – The array of positions.
active – The array of active flags.
reordered_idx – The array to store the computed reordered indices.
- Returns:
None
- for_all_neighbors(i_p, pos, task_range, ret: <quadrants.types.annotations.Template object at 0x730bd7cfc1a0>, task: <quadrants.types.annotations.Template object at 0x730bd8023a10>, i_b)[source]#
Iterates over all neighbors of a given position and performs a task on each neighbor. Elements are considered neighbors if they are within task_range.
- Parameters:
i_p (int) – Index of the querying particle.
pos – Template for the positions of all particles.
task – Template for the task to be performed on each neighbor of the querying particle.
task_range – Range within which the task should be performed.
ret – Template for the return value of the task.
- Returns:
None
See also#
Conventions: coordinate frame, rotation, and quaternion conventions.
Tensor utilities: converting between NumPy, PyTorch, and Genesis tensors.