| Title: | An R package providing spatial transformation methods for movement data |
|---|---|
| Description: | An R package providing spatial transformation methods for movement data. |
| Authors: | Mikkel Roald-Arbøl [aut, cre] (ORCID: <https://orcid.org/0000-0002-9998-0058>) |
| Maintainer: | Mikkel Roald-Arbøl <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3 |
| Built: | 2026-06-03 06:49:22 UTC |
| Source: | https://github.com/animovement/anispace |
Computes the shortest signed angular distance (in radians) from
from_angle to to_angle.
calculate_angular_difference(from_angle, to_angle)calculate_angular_difference(from_angle, to_angle)
from_angle |
Numeric. Starting angle (radians). |
to_angle |
Numeric. Target angle (radians). |
Numeric scalar – the angular difference wrapped to [-π, π].
Returns the planar angle measured from the positive x‑axis toward the
positive y‑axis. By default the result is mapped to [0, 2π); setting
centered = TRUE leaves the native atan2 range [-π, π].
cartesian_to_phi(x, y, centered = FALSE)cartesian_to_phi(x, y, centered = FALSE)
x |
numeric vector of x‑coordinates |
y |
numeric vector of y‑coordinates |
centered |
logical; if |
numeric vector of azimuth angles (φ) in radians
Computes the Euclidean distance from the origin to a point in either 2‑D
(z omitted) or 3‑D space.
cartesian_to_rho(x, y, z = NULL)cartesian_to_rho(x, y, z = NULL)
x |
numeric vector of x‑coordinates |
y |
numeric vector of y‑coordinates |
z |
optional numeric vector of z‑coordinates; if |
numeric vector of radii (ρ)
Calculates the inclination angle measured from the positive z‑axis (the “polar” angle) for each point.
cartesian_to_theta(x, y, z)cartesian_to_theta(x, y, z)
x |
numeric vector of x‑coordinates |
y |
numeric vector of y‑coordinates |
z |
numeric vector of z‑coordinates |
numeric vector of polar angles (θ) in radians
Computes lagged differences between successive angles (in radians) and
converts each raw subtraction into the shortest signed angular distance
using calculate_angular_difference(). The output mimics base::diff(),
returning NAs for the first lag positions so it works nicely inside
dplyr::mutate().
diff_angle(x, lag = 1L)diff_angle(x, lag = 1L)
x |
Numeric vector of angles (radians). |
lag |
Positive integer indicating the lag (default = 1L). Must be an integer ≥ 1. |
Numeric vector of the same length as x. The first lag entries
are NA; subsequent entries contain the angular differences.
# Simple example angles <- c(0, pi/2, pi, 3*pi/2) diff_angle(angles) # Using a lag of 2 diff_angle(angles, lag = 2L)# Simple example angles <- c(0, pi/2, pi, 3*pi/2) diff_angle(angles) # Using a lag of 2 diff_angle(angles, lag = 2L)
Map from polar to Cartesian coordinates
map_to_cartesian(data)map_to_cartesian(data)
data |
an aniframe with polar coordinates |
an aniframe with Cartesian coordinates
Map from Cartesian to cylindrical coordinates
map_to_cylindrical(data)map_to_cylindrical(data)
data |
movement data frame with Cartesian coordinates |
movement data frame with cylindrical coordinates
Map from Cartesian to polar coordinates
map_to_polar(data)map_to_polar(data)
data |
movement data frame with Cartesian coordinates |
movement data frame with polar coordinates
Map from Cartesian to spherical coordinates
map_to_spherical(data)map_to_spherical(data)
data |
A data frame/tibble containing columns |
Same data frame with columns rho, theta, phi
Convert polar radius to Cartesian x‑coordinate
polar_to_x(rho, phi)polar_to_x(rho, phi)
rho |
numeric vector of radial distances |
phi |
numeric vector of azimuth angles (radians) |
numeric vector of x‑coordinates
Convert polar radius to Cartesian y‑coordinate
polar_to_y(rho, phi)polar_to_y(rho, phi)
rho |
numeric vector of radial distances |
phi |
numeric vector of azimuth angles (radians) |
numeric vector of y‑coordinates
Automatically detects whether data are 2D or 3D and applies the corresponding rotation method.
rotate_coords(data, alignment_points, align_perpendicular = FALSE)rotate_coords(data, alignment_points, align_perpendicular = FALSE)
data |
movement data frame with columns: time, individual, keypoint, x, y, z (optional) |
alignment_points |
character vector of length 2 specifying the keypoints used for alignment |
align_perpendicular |
logical; if TRUE, alignment_points are rotated to be perpendicular to the 0-degree axis (y-axis). If FALSE (default), they are aligned with the x-axis. |
movement data frame with rotated coordinates
Handles regular points as well as the two pole regions (θ≈0 and θ≈π).
Non‑finite inputs remain NA.
spherical_to_z(rho, theta)spherical_to_z(rho, theta)
rho |
Numeric vector – cylindrical radius (√(x² + y²)). |
theta |
Numeric vector – polar angle measured from the +z axis (radians). |
Numeric vector of z‑coordinates (same length as input)
Transforms Cartesian coordinates into an egocentric reference frame through a two-step process: translation followed by rotation. First translates all coordinates relative to a reference keypoint, then rotates the coordinate system based on specified alignment points.
transform_to_egocentric( data, to_keypoint, alignment_points, align_perpendicular = FALSE )transform_to_egocentric( data, to_keypoint, alignment_points, align_perpendicular = FALSE )
data |
movement data frame with columns: time, individual, keypoint, x, y |
to_keypoint |
character; keypoint to use as the new origin |
alignment_points |
character vector of length 2 specifying the keypoint names to use for alignment |
align_perpendicular |
logical; if TRUE, alignment_points will be rotated to be perpendicular to the 0-degree axis. If FALSE (default), alignment_points will be rotated to align with the 0-degree axis |
This function combines translation and rotation to create an egocentric reference frame. It:
Translates all coordinates relative to the specified keypoint (to_keypoint)
Rotates the coordinate system based on the alignment points
The translation makes the reference keypoint the new origin (0,0), while the rotation standardizes the orientation. This is particularly useful for:
Creating egocentric reference frames
Standardizing pose data across frames or individuals
Analyzing relative motion patterns
movement data frame in egocentric reference frame
## Not run: # Transform coordinates to make nose the origin and align body axis transformed_data <- transform_to_egocentric( data, to_keypoint = "nose", alignment_points = c("nose", "tail"), align_perpendicular = FALSE ) # Transform to make nose origin and ears perpendicular to forward axis transformed_data <- transform_to_egocentric( data, to_keypoint = "nose", alignment_points = c("ear_left", "ear_right"), align_perpendicular = TRUE ) ## End(Not run)## Not run: # Transform coordinates to make nose the origin and align body axis transformed_data <- transform_to_egocentric( data, to_keypoint = "nose", alignment_points = c("nose", "tail"), align_perpendicular = FALSE ) # Transform to make nose origin and ears perpendicular to forward axis transformed_data <- transform_to_egocentric( data, to_keypoint = "nose", alignment_points = c("ear_left", "ear_right"), align_perpendicular = TRUE ) ## End(Not run)
Translates coordinates in Cartesian space. Takes either a single point
(to_x and to_y), a vector with the same length as the time dimension or a
keypoint (to_keypoint), which can be used to transform the data into an
egocentric reference frame.
translate_coords(data, to_x = 0, to_y = 0, to_z = NULL, to_keypoint = NULL)translate_coords(data, to_x = 0, to_y = 0, to_z = NULL, to_keypoint = NULL)
data |
movement data frame with columns: time, individual, keypoint, x, y |
to_x |
x coordinates; either a single value or a time-length vector |
to_y |
y coordinates; either a single value or a time-length vector |
to_z |
z coordinates (only if 3D); either a single value or a time-length vector |
to_keypoint |
all other coordinates becomes relative to this keypoint |
movement data frame with translated coordinates
Unwraps any numeric vector from the interval [0, 2π).
unwrap_angle(x)unwrap_angle(x)
x |
Numeric vector of angles (radians). |
Numeric vector of the same length.
Wraps any numeric vector of angles (in radians) to a standard interval using modulo arithmetic.
wrap_angle(x, modulo = c("2pi", "pi", "asis"))wrap_angle(x, modulo = c("2pi", "pi", "asis"))
x |
Numeric vector of angles (radians). |
modulo |
Character string specifying the target range:
|
Numeric vector of the same length as x, with angles wrapped
to the specified range.
angles <- c(-pi, 0, pi, 2 * pi, 3 * pi) # Wrap to [0, 2π) wrap_angle(angles, "2pi") # Wrap to (-π, π] wrap_angle(angles, "pi") # No wrapping wrap_angle(angles, "asis")angles <- c(-pi, 0, pi, 2 * pi, 3 * pi) # Wrap to [0, 2π) wrap_angle(angles, "2pi") # Wrap to (-π, π] wrap_angle(angles, "pi") # No wrapping wrap_angle(angles, "asis")