Skip to content

Commit

Permalink
Add Transform::transform_line
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed May 10, 2022
1 parent 5bbf074 commit 046b125
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops;

use nalgebra::Perspective3;

use crate::Scalar;
use crate::{Line, Scalar};

use super::{Aabb, Point, Segment, Triangle, Vector};

Expand Down Expand Up @@ -55,6 +55,14 @@ impl Transform {
Vector::from(self.0.transform_vector(&vector.to_na()))
}

/// Transform the given line
pub fn transform_line(&self, line: &Line<3>) -> Line<3> {
Line {
origin: self.transform_point(&line.origin),
direction: self.transform_vector(&line.direction),
}
}

/// Transform the given segment
pub fn transform_segment(&self, segment: &Segment<3>) -> Segment<3> {
let [a, b] = &segment.points();
Expand Down Expand Up @@ -124,3 +132,33 @@ impl ops::Mul<Self> for Transform {
Self(self.0.mul(rhs.0))
}
}

#[cfg(test)]
mod tests {
use approx::assert_abs_diff_eq;

use crate::{Line, Point, Scalar, Vector};

use super::Transform;

#[test]
fn transform() {
let line = Line {
origin: Point::from([1., 0., 0.]),
direction: Vector::from([0., 1., 0.]),
};

let transform = Transform::translation([1., 2., 3.])
* Transform::rotation(Vector::unit_z() * (Scalar::PI / 2.));
let line = transform.transform_line(&line);

assert_abs_diff_eq!(
line,
Line {
origin: Point::from([1., 3., 3.]),
direction: Vector::from([-1., 0., 0.]),
},
epsilon = 1e-8,
);
}
}

0 comments on commit 046b125

Please sign in to comment.