Skip to main content

Direction

Struct Direction 

Source
pub struct Direction<F: ReferenceFrame> { /* private fields */ }
Expand description

A unit vector representing orientation in 3D space.

Directions are frame-dependent but center-independent (free vectors). The internal storage is a Vector3<f64> with magnitude 1 (dimensionless).

§Type Parameters

  • F: The reference frame (e.g., ICRS, EclipticMeanJ2000, Equatorial)

§Invariants

All public constructors ensure the direction is normalized. For unchecked construction, use from_xyz_unchecked.

§Zero-Cost Abstraction

This type is #[repr(transparent)] over XYZ<f64>, ensuring no runtime overhead compared to raw Vector3<f64>.

§Renormalization Policy

The Mul<Direction> implementation for crate::Rotation3 uses Direction::new_unchecked internally and does not auto-renormalize the result. A single rotation preserves the unit-norm invariant to within machine epsilon, but long composition chains (r_n * ... * r_2 * r_1 * dir) accumulate floating-point error and the magnitude will drift away from 1.0. For pipelines that apply many rotations in sequence (e.g. integrators, repeated frame transforms), call renormalize or renormalized periodically to restore the invariant.

Implementations§

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn new(x: f64, y: f64, z: f64) -> Self

Creates a direction from components, normalizing to unit length.

§Panics

Panics if the input vector has zero magnitude (cannot normalize a zero vector). Use Direction::try_new for fallible construction when the input may be zero.

§Example
use affn::cartesian::Direction;
use affn::frames::ReferenceFrame;

#[derive(Debug, Copy, Clone)]
struct WorldFrame;
impl ReferenceFrame for WorldFrame {
    fn frame_name() -> &'static str { "WorldFrame" }
}

let dir = Direction::<WorldFrame>::new(3.0, 4.0, 0.0);
assert!((dir.x() - 0.6).abs() < 1e-12);
assert!((dir.y() - 0.8).abs() < 1e-12);
Source

pub fn try_new(x: f64, y: f64, z: f64) -> Option<Self>

Attempts to create a direction, returning None if the input is zero.

Source

pub fn normalize(x: f64, y: f64, z: f64) -> Self

Creates a direction from components (alias for new).

Provided for API symmetry with earlier versions.

Source

pub fn from_array(v: [f64; 3]) -> Self

Creates a direction from a [f64; 3] array, normalizing to unit length.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn new_unchecked(x: f64, y: f64, z: f64) -> Self

Creates a direction from raw components without normalization.

§Safety

The caller must ensure the components form a unit vector.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn renormalize(&mut self)

Restores the unit-norm invariant in place.

Divides each component by the current magnitude. If the magnitude is non-finite (NaN or infinite) or smaller than f64::EPSILON, the value is left unchanged rather than panicking or producing NaNs.

Use this after long chains of Rotation3 * Direction operations to counteract accumulated floating-point drift.

Source

pub fn renormalized(self) -> Self

By-value variant of renormalize.

Returns a renormalized copy of self. If the current magnitude is non-finite or below f64::EPSILON, the original value is returned unchanged.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn x(&self) -> f64

Returns the x-component.

Source

pub fn y(&self) -> f64

Returns the y-component.

Source

pub fn z(&self) -> f64

Returns the z-component.

Source

pub fn as_array(&self) -> [f64; 3]

Returns the components as a [f64; 3] array.

Source

pub fn reinterpret_frame<F2: ReferenceFrame>(self) -> Direction<F2>

Reinterprets this direction as belonging to a different reference frame.

This is a zero-cost operation: the unit-vector components are preserved unchanged; only the compile-time frame tag is replaced.

Use after applying a mathematical rotation whose result still carries the original frame tag.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn scale<U: LengthUnit>(&self, magnitude: Quantity<U>) -> Displacement<F, U>

Scales the direction by a length to produce a displacement vector.

§Example
use affn::cartesian::Direction;
use affn::frames::ReferenceFrame;
use qtty::units::*; use qtty::{Quantity, M, KM, DEG, RAD, SEC}; use qtty::angular::{Degrees, Radians}; use qtty::length::{Meters, Kilometers};

#[derive(Debug, Copy, Clone)]
struct WorldFrame;
impl ReferenceFrame for WorldFrame {
    fn frame_name() -> &'static str { "WorldFrame" }
}

let dir = Direction::<WorldFrame>::new(1.0, 0.0, 0.0);
let vec = dir.scale(5.0 * M);
assert!((vec.x().value() - 5.0).abs() < 1e-12);
Source

pub fn position<C, U>(&self, magnitude: Quantity<U>) -> Position<C, F, U>
where C: ReferenceCenter<Params = ()>, U: LengthUnit,

Creates a position at the given distance from the origin in this direction.

For centers with Params = (), this is a convenience method.

Source

pub fn position_with_params<C, U>( &self, center_params: C::Params, magnitude: Quantity<U>, ) -> Position<C, F, U>

Creates a position with explicit center parameters.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn dot(&self, other: &Self) -> f64

Computes the dot product with another direction.

Returns cosine of the angle between directions (range: -1 to 1).

Source

pub fn cross(&self, other: &Self) -> Option<Self>

Computes the cross product with another direction.

The result is normalized if non-zero (perpendicular directions).

Source

pub fn negate(&self) -> Self

Negates the direction (points in opposite direction).

Source

pub fn angle_to(&self, other: &Self) -> f64

Returns the angle between this direction and another, in radians.

Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn to_spherical(&self) -> Direction<F>

Converts this Cartesian direction to spherical coordinates.

Returns a spherical direction with polar (latitude) and azimuth (longitude) angles in degrees. Angles are canonicalized to:

  • polar in [-90°, +90°]
  • azimuth in [0°, 360°)
Source§

impl<F: ReferenceFrame> Direction<F>

Source

pub fn display(&self) -> String

Returns a formatted string representation.

Trait Implementations§

Source§

impl<F: Clone + ReferenceFrame> Clone for Direction<F>

Source§

fn clone(&self) -> Direction<F>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug + ReferenceFrame> Debug for Direction<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F> Display for Direction<F>
where F: ReferenceFrame,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F> LowerExp for Direction<F>
where F: ReferenceFrame,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for &Quantity<U>

Source§

type Output = <Quantity<U> as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Direction<F>) -> <Self as Mul<&Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame> Mul<&Direction<F>> for &Rotation3

Source§

type Output = <Rotation3 as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Direction<F>) -> <Self as Mul<&Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for Quantity<U>

Source§

type Output = <Quantity<U> as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Direction<F>) -> <Self as Mul<&Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame> Mul<&Direction<F>> for Rotation3

Source§

type Output = <Rotation3 as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Direction<F>) -> <Self as Mul<&Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for &Direction<F>

Source§

type Output = <Direction<F> as Mul<Quantity<U>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Quantity<U>) -> <Self as Mul<&Quantity<U>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for Direction<F>

Source§

type Output = <Direction<F> as Mul<Quantity<U>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Quantity<U>) -> <Self as Mul<&Quantity<U>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for &Quantity<U>

Source§

type Output = <Quantity<U> as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Direction<F>) -> <Self as Mul<Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame> Mul<Direction<F>> for &Rotation3

Source§

type Output = <Rotation3 as Mul<Direction<F>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Direction<F>) -> <Self as Mul<Direction<F>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for Quantity<U>

Source§

type Output = Vector<F, U>

The resulting type after applying the * operator.
Source§

fn mul(self, dir: Direction<F>) -> Self::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame> Mul<Direction<F>> for Rotation3

Rotation3 * Direction<F> — rotates a unit direction, preserving frame.

Translations do not apply to directions; only rotation changes their orientation. The result is guaranteed to remain a unit vector because rotation preserves norms.

Source§

type Output = Direction<F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Direction<F>) -> Direction<F>

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<Quantity<U>> for &Direction<F>

Source§

type Output = <Direction<F> as Mul<Quantity<U>>>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quantity<U>) -> <Self as Mul<Quantity<U>>>::Output

Performs the * operation. Read more
Source§

impl<F: ReferenceFrame, U: LengthUnit> Mul<Quantity<U>> for Direction<F>

Source§

type Output = Vector<F, U>

The resulting type after applying the * operator.
Source§

fn mul(self, magnitude: Quantity<U>) -> Self::Output

Performs the * operation. Read more
Source§

impl<F> UpperExp for Direction<F>
where F: ReferenceFrame,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Copy + ReferenceFrame> Copy for Direction<F>

Auto Trait Implementations§

§

impl<F> Freeze for Direction<F>

§

impl<F> RefUnwindSafe for Direction<F>
where F: RefUnwindSafe,

§

impl<F> Send for Direction<F>
where F: Send,

§

impl<F> Sync for Direction<F>
where F: Sync,

§

impl<F> Unpin for Direction<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for Direction<F>

§

impl<F> UnwindSafe for Direction<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.