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>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn new(x: f64, y: f64, z: f64) -> Self
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);Sourcepub fn try_new(x: f64, y: f64, z: f64) -> Option<Self>
pub fn try_new(x: f64, y: f64, z: f64) -> Option<Self>
Attempts to create a direction, returning None if the input is zero.
Sourcepub fn normalize(x: f64, y: f64, z: f64) -> Self
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.
Sourcepub fn from_array(v: [f64; 3]) -> Self
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>
impl<F: ReferenceFrame> Direction<F>
Source§impl<F: ReferenceFrame> Direction<F>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn renormalize(&mut self)
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.
Sourcepub fn renormalized(self) -> Self
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>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn reinterpret_frame<F2: ReferenceFrame>(self) -> Direction<F2>
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>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn scale<U: LengthUnit>(&self, magnitude: Quantity<U>) -> Displacement<F, U>
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);Sourcepub fn position<C, U>(&self, magnitude: Quantity<U>) -> Position<C, F, U>
pub fn position<C, U>(&self, magnitude: Quantity<U>) -> Position<C, F, U>
Creates a position at the given distance from the origin in this direction.
For centers with Params = (), this is a convenience method.
Sourcepub fn position_with_params<C, U>(
&self,
center_params: C::Params,
magnitude: Quantity<U>,
) -> Position<C, F, U>where
C: ReferenceCenter,
U: LengthUnit,
pub fn position_with_params<C, U>(
&self,
center_params: C::Params,
magnitude: Quantity<U>,
) -> Position<C, F, U>where
C: ReferenceCenter,
U: LengthUnit,
Creates a position with explicit center parameters.
Source§impl<F: ReferenceFrame> Direction<F>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn dot(&self, other: &Self) -> f64
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§impl<F: ReferenceFrame> Direction<F>
impl<F: ReferenceFrame> Direction<F>
Sourcepub fn to_spherical(&self) -> Direction<F>
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°)
Trait Implementations§
Source§impl<F> Display for Direction<F>where
F: ReferenceFrame,
impl<F> Display for Direction<F>where
F: ReferenceFrame,
Source§impl<F> LowerExp for Direction<F>where
F: ReferenceFrame,
impl<F> LowerExp for Direction<F>where
F: ReferenceFrame,
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for &Quantity<U>
impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for &Quantity<U>
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for Quantity<U>
impl<F: ReferenceFrame, U: LengthUnit> Mul<&Direction<F>> for Quantity<U>
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for &Direction<F>
impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for &Direction<F>
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for Direction<F>
impl<F: ReferenceFrame, U: LengthUnit> Mul<&Quantity<U>> for Direction<F>
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for &Quantity<U>
impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for &Quantity<U>
Source§impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for Quantity<U>
impl<F: ReferenceFrame, U: LengthUnit> Mul<Direction<F>> for Quantity<U>
Source§impl<F: ReferenceFrame> Mul<Direction<F>> for Rotation3
Rotation3 * Direction<F> — rotates a unit direction, preserving frame.
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.