angles#

namespace saltro
namespace math#

Functions

double wrap_to_2pi(double rad)#

Wrap an angle in radians to the interval \([0, 2\pi)\).

Given an input angle \(\theta\) in radians, this function returns an equivalent angle wrapped into the range \([0, 2\pi)\):

\[ \theta_{\text{wrapped}} = \theta \bmod 2\pi \]

This is useful for maintaining angles within a canonical range for orbital or attitude calculations.

Parameters:

rad – Input angle in radians.

Returns:

Wrapped angle in radians within \([0, 2\pi)\).

double wrap_to_360(double deg)#

Wrap an angle in degrees to the interval \([0, 360)\).

Given an input angle \(\alpha\) in degrees, this function returns an equivalent angle wrapped into the range \([0, 360)\):

\[ \alpha_{\text{wrapped}} = \alpha \bmod 360 \]

Parameters:

deg – Input angle in degrees.

Returns:

Wrapped angle in degrees within \([0, 360)\).

double deg2rad(double deg)#

Convert degrees to radians.

Converts an angle from degrees to radians using:

\[ \theta_{\text{rad}} = \frac{\pi}{180}\,\theta_{\text{deg}} \]

Parameters:

deg – Angle in degrees.

Returns:

Angle in radians.

double rad2deg(double rad)#

Convert radians to degrees.

Converts an angle from radians to degrees using:

\[ \theta_{\text{deg}} = \frac{180}{\pi}\,\theta_{\text{rad}} \]

Parameters:

rad – Angle in radians.

Returns:

Angle in degrees.

Eigen::Matrix3d rot_z(double rad)#

Rotation matrix about the \(z\) axis.

Returns the 3×3 rotation matrix representing a right-handed rotation about the \(z\) axis by angle \(\theta\):

\[\begin{split} \mathbf{R}_z(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \end{split}\]

Parameters:

rad – Rotation angle in radians.

Returns:

3×3 rotation matrix.

Eigen::Vector3d rotate_about_z(const Eigen::Vector3d &v, const double angle_rad)#

Rotate a vector about the \(z\) axis.

Applies a right-handed rotation about the \(z\) axis to a vector:

\[ \mathbf{v}_{\text{out}} = \mathbf{R}_z(\theta)\,\mathbf{v} \]

Parameters:
  • v – Input 3D vector.

  • angle_rad – Rotation angle in radians.

Returns:

Rotated vector.

bool unit_vector(const Eigen::Vector3d &v, Eigen::Vector3d &out_unit)#

Compute the unit vector of a given vector.

Normalizes the input vector:

\[ \hat{\mathbf{v}} = \frac{\mathbf{v}}{\|\mathbf{v}\|} \]

If the input vector has zero norm, normalization fails and the function returns false.

Parameters:
  • v – Input vector.

  • out_unit – Output unit vector.

Returns:

True if normalization succeeds, false if the input vector has zero norm.

double clamp(const double x, const double lo, const double hi)#

Clamp a scalar value to a closed interval.

Restricts the value \(x\) to lie within the interval \([l, h]\):

\[ \mathrm{clamp}(x) = \min\!\bigl(\max(x,l),h\bigr) \]

Parameters:
  • x – Input value.

  • lo – Lower bound.

  • hi – Upper bound.

Returns:

Clamped value within \([lo, hi]\).