-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1157 from hannobraun/plane
Add `Plane`
- Loading branch information
Showing
3 changed files
with
101 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{Line, Point, Scalar, Vector}; | ||
|
||
/// A plane | ||
#[derive(Clone, Copy, Default, Eq, PartialEq, Hash, Ord, PartialOrd)] | ||
#[repr(C)] | ||
pub struct Plane { | ||
origin: Point<3>, | ||
u: Vector<3>, | ||
v: Vector<3>, | ||
} | ||
|
||
impl Plane { | ||
/// Create a `Plane` from a parametric description | ||
pub fn from_parametric( | ||
origin: Point<3>, | ||
u: Vector<3>, | ||
v: Vector<3>, | ||
) -> Self { | ||
Self { origin, u, v } | ||
} | ||
|
||
/// Access the origin of the plane | ||
pub fn origin(&self) -> Point<3> { | ||
self.origin | ||
} | ||
|
||
/// Access the u-vector of the plane | ||
pub fn u(&self) -> Vector<3> { | ||
self.u | ||
} | ||
|
||
/// Access the v-vector of the plane | ||
pub fn v(&self) -> Vector<3> { | ||
self.v | ||
} | ||
|
||
/// Convert the plane to three-point form | ||
pub fn three_point_form(&self) -> [Point<3>; 3] { | ||
let a = self.origin(); | ||
let b = self.origin() + self.u(); | ||
let c = self.origin() + self.v(); | ||
|
||
[a, b, c] | ||
} | ||
|
||
/// Convert the plane to constant-normal form | ||
pub fn constant_normal_form(&self) -> (Scalar, Vector<3>) { | ||
let [a, b, c] = self.three_point_form(); | ||
|
||
// See Real-Time Collision Detection by Christer Ericson, section 3.6, | ||
// Planes and Halfspaces. | ||
let normal = (b - a).cross(&(c - a)).normalize(); | ||
let distance = normal.dot(&a.coords); | ||
|
||
(distance, normal) | ||
} | ||
|
||
/// Project a line into the plane | ||
pub fn project_line(&self, line: &Line<3>) -> Line<2> { | ||
let line_origin_relative_to_plane = line.origin() - self.origin(); | ||
let line_origin_in_plane = Point { | ||
coords: Vector::from([ | ||
self.u() | ||
.scalar_projection_onto(&line_origin_relative_to_plane), | ||
self.v() | ||
.scalar_projection_onto(&line_origin_relative_to_plane), | ||
]), | ||
}; | ||
|
||
let line_direction_in_plane = Vector::from([ | ||
self.u().scalar_projection_onto(&line.direction()), | ||
self.v().scalar_projection_onto(&line.direction()), | ||
]); | ||
|
||
Line::from_origin_and_direction( | ||
line_origin_in_plane, | ||
line_direction_in_plane, | ||
) | ||
} | ||
} |