File: unit_wrapper.rs

package info (click to toggle)
rust-nalgebra 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,340 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 896 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#![allow(clippy::float_cmp)]
extern crate nalgebra as na;

use na::{Unit, Vector3};

fn length_on_direction_with_unit(v: &Vector3<f32>, dir: &Unit<Vector3<f32>>) -> f32 {
    // No need to normalize `dir`: we know that it is non-zero and normalized.
    v.dot(dir.as_ref())
}

fn length_on_direction_without_unit(v: &Vector3<f32>, dir: &Vector3<f32>) -> f32 {
    // Obligatory normalization of the direction vector (and test, for robustness).
    if let Some(unit_dir) = dir.try_normalize(1.0e-6) {
        v.dot(&unit_dir)
    } else {
        // Normalization failed because the norm was too small.
        panic!("Invalid input direction.")
    }
}

fn main() {
    let v = Vector3::new(1.0, 2.0, 3.0);

    let l1 = length_on_direction_with_unit(&v, &Vector3::y_axis());
    let l2 = length_on_direction_without_unit(&v, &Vector3::y());

    assert_eq!(l1, l2)
}