Skip to content

Commit

Permalink
Fix Plane::project_vector
Browse files Browse the repository at this point in the history
Turns out that it worked only for plane coordinate systems in which the
`u` and `v` vectors where orthogonal.
  • Loading branch information
hannobraun committed Feb 2, 2023
1 parent 0c83922 commit 1a0fa27
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions crates/fj-math/src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,29 @@ impl Plane {

/// Project a vector into the plane
pub fn project_vector(&self, vector: impl Into<Vector<3>>) -> Vector<2> {
let vector = vector.into();

Vector::from([
vector.scalar_projection_onto(&self.u()),
vector.scalar_projection_onto(&self.v()),
])
// The vector we want to project can be expressed as a linear
// combination of `self.u()`, `self.v()`, and `self.normal()`:
// `v = a*u + b*v + c*n`
//
// All we need to do is to solve this equation. `a` and `b` are the
// components of the projected vector. `c` is the distance of the points
// that the original and projected vectors point to.
//
// To solve the equation, let's change it into the standard `Mx = b`
// form, then we can let nalgebra do the actual solving.
let m =
nalgebra::Matrix::<_, _, nalgebra::Const<3>, _>::from_columns(&[
self.u().to_na(),
self.v().to_na(),
self.normal().to_na(),
]);
let b = vector.into();
let x = m
.lu()
.solve(&b.to_na())
.expect("Expected matrix to be invertible");

Vector::from([x.x, x.y])
}

/// Project a line into the plane
Expand Down Expand Up @@ -114,5 +131,9 @@ mod tests {

assert_eq!(plane.project_vector([1., 0., 1.]), Vector::from([1., 0.]));
assert_eq!(plane.project_vector([0., 1., 1.]), Vector::from([0., 1.]));

let plane =
Plane::from_parametric([1., 1., 1.], [1., 0., 0.], [1., 1., 0.]);
assert_eq!(plane.project_vector([0., 1., 0.]), Vector::from([-1., 1.]));
}
}

0 comments on commit 1a0fa27

Please sign in to comment.