Skip to content

baglab.geometry

baglab.geometry

Geometry utilities for ROS2 message data in DataFrames.

Provides conversions and computations commonly needed when working with geometry_msgs types (Quaternion, Point, Pose, Twist, etc.) stored as DataFrames or :class:~baglab.io.FieldGroup objects.

angle_diff(a, b)

Shortest signed angle difference a - b, wrapped to [-pi, pi].

Parameters

a, b : float | np.ndarray | pd.Series Angles in radians.

Returns

float | np.ndarray | pd.Series Same type as a.

normalize_angle(angle)

Normalize angle to [-pi, pi].

Parameters

angle : float | np.ndarray | pd.Series Angle in radians.

Returns

float | np.ndarray | pd.Series Same type as input.

unwrap(angle)

Remove discontinuities in angle series (Series-aware np.unwrap).

Parameters

angle : pd.Series Angle in radians, possibly with ±pi jumps.

Returns

pd.Series

align_time(df1, df2, time_col=None)

Align two DataFrames by time via linear interpolation.

The output DataFrames share the same time index (the union of both input indices, restricted to the overlapping time range). Numeric columns are interpolated linearly; non-numeric columns are forward-filled.

Parameters

df1, df2 : pd.DataFrame DataFrames to align. If time_col is None, the existing index (assumed to be a DatetimeIndex or numeric) is used. Otherwise the named column is used as the time axis. time_col : str | None Column name to use as time. When provided, this column is set as the index before interpolation.

Returns

tuple[pd.DataFrame, pd.DataFrame] Aligned copies of df1 and df2.

interp_pose(df, target_time, position_cols=('position.x', 'position.y'), orientation_cols=('orientation.x', 'orientation.y', 'orientation.z', 'orientation.w'))

Interpolate pose data at target times.

Position is linearly interpolated. Orientation is interpolated via Spherical Linear Interpolation (SLERP) of quaternions.

Parameters

df : pd.DataFrame Must have a numeric index (time in seconds) and contain the position and orientation columns. target_time : array-like Times at which to interpolate. position_cols : tuple[str, str] Column names for position x and y. orientation_cols : tuple[str, str, str, str] Column names for quaternion x, y, z, w.

Returns

pd.DataFrame Interpolated pose with columns matching position_cols and orientation_cols.

match_by_time(t_query, t_source)

Find the nearest source index for each query time.

Uses binary search (np.searchsorted) for O(N log M) performance.

Parameters

t_query : pd.Series | ndarray Query times (need not be sorted). t_source : pd.Series | ndarray Source times (must be monotonically increasing).

Returns

np.ndarray Integer indices into t_source, one per element in t_query.

resample(df, dt, time_col=None)

Resample a DataFrame at uniform time intervals.

Parameters

df : pd.DataFrame Input data. dt : float Desired time step in seconds. time_col : str | None Column name to use as time. If None, the index is used (must be numeric, e.g. from :func:~baglab.stamp_to_sec).

Returns

pd.DataFrame Resampled data with uniform time index.

to_numpy_2d(fg)

Convert FieldGroup with x, y fields to ndarray of shape (N, 2).

Parameters

fg : FieldGroup | DataFrame | object Must contain fields/attributes x, y. Scalar input returns shape (1, 2).

Returns

np.ndarray Shape (N, 2) or (1, 2).

to_numpy_3d(fg)

Convert FieldGroup with x, y, z fields to ndarray of shape (N, 3).

Parameters

fg : FieldGroup | DataFrame | object Must contain fields/attributes x, y, z. Scalar input returns shape (1, 3).

Returns

np.ndarray Shape (N, 3) or (1, 3).

cumulative_distance(position)

Cumulative travel distance from consecutive xy positions.

Parameters

position : FieldGroup | DataFrame Must contain fields x, y.

Returns

pd.Series Cumulative distance starting from 0.

distance_2d(pos1, pos2)

Euclidean distance between two positions in the xy-plane.

Parameters

pos1, pos2 : FieldGroup | DataFrame | object Must contain fields/attributes x, y.

Returns

float | pd.Series

distance_3d(pos1, pos2)

Euclidean distance between two positions in 3D.

Parameters

pos1, pos2 : FieldGroup | DataFrame | object Must contain fields/attributes x, y, z.

Returns

float | pd.Series

to_xy(position)

Extract x, y from a position/point.

Parameters

position : FieldGroup | DataFrame | object Must contain fields/attributes x, y.

Returns

dict[str, float] | pd.DataFrame {"x": …, "y": …} for scalar input, or DataFrame with columns x, y.

to_xyz(position)

Extract x, y, z from a position/point.

Parameters

position : FieldGroup | DataFrame | object Must contain fields/attributes x, y, z.

Returns

dict[str, float] | pd.DataFrame {"x": …, "y": …, "z": …} for scalar input, or DataFrame with columns x, y, z.

pose_error(actual_pose, ref_pose, frame='xy')

Compute pose error between actual and reference poses.

Returns along-track, cross-track, and yaw errors. The decomposition axes depend on frame.

Parameters

actual_pose, ref_pose : FieldGroup | DataFrame | dict | object Pose data. Accepted formats:

- DataFrame with ``position.x``, ``position.y``,
  ``orientation.x/y/z/w`` (Pose format).
- DataFrame with ``x``, ``y``, ``yaw`` columns (xyyaw format).
- dict or object with ``x``, ``y``, ``yaw`` keys/attributes
  (scalar — returns dict).

frame : str Coordinate frame for along/cross decomposition:

- ``"xy"`` (default): world axes. along = dx, cross = dy.
- ``"ref_heading"``: reference yaw direction.
- ``"ref_path"``: tangent of the reference path (estimated from
  consecutive positions).
Returns

pd.DataFrame | dict[str, float] Columns/keys: along, cross, yaw.

pose_to_xyyaw(pose)

Extract x, y, yaw from a Pose.

Parameters

pose : FieldGroup | DataFrame | object Accepted formats:

- DataFrame with ``position.x``, ``position.y``,
  ``orientation.x/y/z/w`` columns.
- FieldGroup pointing at a Pose level.
- ROS msg Pose object with ``position`` and ``orientation``
  attributes (returns dict).
Returns

dict[str, float] | pd.DataFrame {"x": …, "y": …, "yaw": …} for scalar input, or DataFrame with columns x, y, yaw.

quat_to_rpy(orientation)

Convert quaternion (x, y, z, w) to roll, pitch, yaw in radians.

Parameters

orientation : FieldGroup | DataFrame | object Must contain columns/fields/attributes x, y, z, w. Accepts a single ROS msg (returns dict) or a DataFrame/FieldGroup (returns DataFrame).

Returns

dict[str, float] | pd.DataFrame {"roll": …, "pitch": …, "yaw": …} for scalar input, or DataFrame with columns roll, pitch, yaw.

quat_to_yaw(orientation)

Convert quaternion (x, y, z, w) to yaw angle in radians.

Parameters

orientation : FieldGroup | DataFrame | object Must contain columns/fields/attributes x, y, z, w. Accepts a single ROS msg (returns float) or a DataFrame/FieldGroup (returns Series).

Returns

float | pd.Series Yaw angle in radians.

rpy_to_quat(roll, pitch, yaw)

Convert roll, pitch, yaw (radians) to quaternion.

Parameters

roll, pitch, yaw : float | pd.Series Euler angles in radians.

Returns

dict[str, float] | pd.DataFrame {"x": …, "y": …, "z": …, "w": …} for scalar input, or DataFrame with columns x, y, z, w.

yaw_to_quat(yaw)

Convert yaw angle (radians) to quaternion with zero roll and pitch.

Parameters

yaw : float | pd.Series Yaw angle in radians.

Returns

dict[str, float] | pd.DataFrame {"x": …, "y": …, "z": …, "w": …} for scalar input, or DataFrame with columns x, y, z, w.

twist_to_speed(linear)

Compute scalar speed from linear velocity (x, y, z).

Parameters

linear : FieldGroup | DataFrame | object Must contain fields/attributes x, y, z.

Returns

float | pd.Series Speed = sqrt(x² + y² + z²).

twist_to_speed_2d(linear)

Compute 2D scalar speed from linear velocity (x, y).

Parameters

linear : FieldGroup | DataFrame | object Must contain fields/attributes x, y.

Returns

float | pd.Series Speed = sqrt(x² + y²).

velocity_from_pose(position, time)

Estimate scalar velocity by differentiating position over time.

Parameters

position : FieldGroup | DataFrame Must contain fields x, y. time : pd.Series Time in seconds (float).

Returns

pd.Series Velocity (distance / dt).

yaw_rate_from_pose(orientation, time)

Estimate yaw rate by differentiating yaw over time.

Parameters

orientation : FieldGroup | DataFrame Quaternion fields x, y, z, w. time : pd.Series Time in seconds (float).

Returns

pd.Series Yaw rate in rad/s.