Skip to main content

Vector3

Struct Vector3 

Source
pub struct Vector3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}
Expand description

A 3D Cartesian vector for coordinate calculations.

Used throughout the library for position vectors, direction vectors, and as intermediate representations during coordinate transformations.

§Fields

Components are public for direct access when performance matters:

  • x: First component (toward vernal equinox in equatorial coordinates)
  • y: Second component (90° east in equatorial coordinates)
  • z: Third component (toward celestial pole in equatorial coordinates)

§Construction

use celestial_core::Vector3;

// Direct construction
let v = Vector3::new(1.0, 2.0, 3.0);

// Unit vectors along axes
let x = Vector3::x_axis();
let y = Vector3::y_axis();
let z = Vector3::z_axis();

// From spherical coordinates (RA, Dec in radians)
let star = Vector3::from_spherical(0.5, 0.3);

// From an array
let v = Vector3::from_array([1.0, 2.0, 3.0]);

Fields§

§x: f64§y: f64§z: f64

Implementations§

Source§

impl Vector3

Source

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

Creates a new vector from x, y, z components.

Source

pub fn zeros() -> Self

Returns the zero vector [0, 0, 0].

Source

pub fn x_axis() -> Self

Returns the unit vector along the X axis [1, 0, 0].

In equatorial coordinates, this points toward the vernal equinox.

Source

pub fn y_axis() -> Self

Returns the unit vector along the Y axis [0, 1, 0].

In equatorial coordinates, this is 90° east of the vernal equinox on the equator.

Source

pub fn z_axis() -> Self

Returns the unit vector along the Z axis [0, 0, 1].

In equatorial coordinates, this points toward the north celestial pole.

Source

pub fn get(&self, index: usize) -> AstroResult<f64>

Returns the component at the given index (0=x, 1=y, 2=z).

Returns an error for indices outside 0-2. For unchecked access, use indexing syntax v[i] or the public fields directly.

Source

pub fn set(&mut self, index: usize, value: f64) -> AstroResult<()>

Sets the component at the given index (0=x, 1=y, 2=z).

Returns an error for indices outside 0-2. For unchecked access, use indexing syntax v[i] = value or the public fields directly.

Source

pub fn magnitude(&self) -> f64

Returns the Euclidean length (L2 norm) of the vector.

For a unit vector, this returns 1.0. For the zero vector, returns 0.0.

Source

pub fn magnitude_squared(&self) -> f64

Returns the squared magnitude.

Faster than magnitude when you only need to compare lengths or don’t need the actual distance.

Source

pub fn normalize(&self) -> Self

Returns a unit vector pointing in the same direction.

If the vector has zero length, returns the zero vector unchanged (avoids NaN).

use celestial_core::Vector3;

let v = Vector3::new(3.0, 4.0, 0.0);
let unit = v.normalize();
assert!((unit.magnitude() - 1.0).abs() < 1e-15);
assert_eq!(unit, Vector3::new(0.6, 0.8, 0.0));
Source

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

Computes the dot product (inner product) with another vector.

For unit vectors, this equals the cosine of the angle between them: a.dot(&b) = cos(θ). Use this to compute angular separation between celestial positions.

use celestial_core::Vector3;

let a = Vector3::x_axis();
let b = Vector3::y_axis();
assert_eq!(a.dot(&b), 0.0);  // Perpendicular

let c = Vector3::new(1.0, 2.0, 3.0);
let d = Vector3::new(4.0, 5.0, 6.0);
assert_eq!(c.dot(&d), 32.0);  // 1*4 + 2*5 + 3*6
Source

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

Computes the cross product with another vector.

The result is perpendicular to both input vectors, with direction given by the right-hand rule. The magnitude equals |a||b|sin(θ).

use celestial_core::Vector3;

let x = Vector3::x_axis();
let y = Vector3::y_axis();
let z = x.cross(&y);
assert_eq!(z, Vector3::z_axis());  // X × Y = Z
Source

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

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

Source

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

Creates a vector from a [f64; 3] array.

Source

pub fn from_spherical(ra: f64, dec: f64) -> Self

Creates a unit vector from spherical coordinates.

  • ra: Azimuthal angle from +X toward +Y (right ascension), in radians
  • dec: Elevation from XY plane (declination), in radians

The result is always a unit vector (magnitude = 1).

use celestial_core::Vector3;
use std::f64::consts::FRAC_PI_2;

// RA=0, Dec=0 → points along +X
let v = Vector3::from_spherical(0.0, 0.0);
assert!((v.x - 1.0).abs() < 1e-15);

// RA=90°, Dec=0 → points along +Y
let v = Vector3::from_spherical(FRAC_PI_2, 0.0);
assert!((v.y - 1.0).abs() < 1e-15);

// RA=0, Dec=90° → points along +Z (north pole)
let v = Vector3::from_spherical(0.0, FRAC_PI_2);
assert!((v.z - 1.0).abs() < 1e-15);
Source

pub fn to_spherical(&self) -> (f64, f64)

Converts the vector to spherical coordinates (θ, φ).

Returns (theta, phi) where:

  • theta: Azimuthal angle from +X toward +Y (like RA), in radians (-π, π]
  • phi: Elevation from XY plane (like Dec), in radians [-π/2, π/2]

The vector does not need to be normalized; direction is preserved regardless of magnitude. For the zero vector, returns (0.0, 0.0).

use celestial_core::Vector3;
use std::f64::consts::FRAC_PI_2;

let v = Vector3::new(0.0, 0.0, 1.0);  // North pole
let (theta, phi) = v.to_spherical();
assert_eq!(theta, 0.0);
assert_eq!(phi, FRAC_PI_2);

Trait Implementations§

Source§

impl Add for Vector3

Vector + Vector

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Vector3

Source§

fn clone(&self) -> Vector3

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Vector3

Source§

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

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

impl Display for Vector3

Source§

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

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

impl Div<f64> for Vector3

Vector / scalar

Source§

type Output = Vector3

The resulting type after applying the / operator.
Source§

fn div(self, scalar: f64) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<f64> for Vector3

Vector /= scalar

Source§

fn div_assign(&mut self, scalar: f64)

Performs the /= operation. Read more
Source§

impl Index<usize> for Vector3

v[i] indexing (panics if i > 2)

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &f64

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Vector3

v[i] = value mutable indexing (panics if i > 2)

Source§

fn index_mut(&mut self, index: usize) -> &mut f64

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Mul<Vector3> for &RotationMatrix3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, vec: Vector3) -> Vector3

Performs the * operation. Read more
Source§

impl Mul<Vector3> for RotationMatrix3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, vec: Vector3) -> Vector3

Performs the * operation. Read more
Source§

impl Mul<Vector3> for f64

scalar * Vector

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, vec: Vector3) -> Vector3

Performs the * operation. Read more
Source§

impl Mul<f64> for Vector3

Vector * scalar

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: f64) -> Self

Performs the * operation. Read more
Source§

impl Neg for Vector3

-Vector

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector3

Source§

fn eq(&self, other: &Vector3) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Vector3

Vector - Vector

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl Copy for Vector3

Source§

impl StructuralPartialEq for Vector3

Auto Trait Implementations§

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> 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.