33//! Includes vertices, polygons, planes, rays, and more.
44
55use alloc:: vec:: Vec ;
6-
76use core:: fmt:: { self , Debug , Formatter } ;
87
98use crate :: math:: {
@@ -402,9 +401,9 @@ impl<B> Plane3<B> {
402401 ///
403402 /// ```
404403 pub fn from_point_and_normal ( pt : Point3 < B > , n : Normal3 ) -> Self {
405- let n = n. normalize ( ) ;
406- let d = dot ( & pt. 0 , & n . 0 ) ;
407- Plane :: new ( n . x ( ) , n . y ( ) , n . z ( ) , d )
404+ let mut n = n. to ( ) . normalize ( ) . to_hom ( ) ;
405+ n [ 3 ] = -n . dot ( & pt. to_hom ( ) ) ;
406+ Self ( n )
408407 }
409408
410409 /// Returns the normal vector of `self`.
@@ -439,8 +438,7 @@ impl<B> Plane3<B> {
439438 /// ```
440439 #[ inline]
441440 pub fn offset ( & self ) -> f32 {
442- // plane dist from origin is origin dist from plane, negated
443- -self . signed_dist ( Point3 :: origin ( ) )
441+ -self . 0 [ 3 ]
444442 }
445443
446444 /// Returns the perpendicular projection of a point on `self`.
@@ -470,9 +468,9 @@ impl<B> Plane3<B> {
470468 /// assert_eq!(<Plane3>::new(0.0, 0.0, 1.0, 2.0).project(pt), pt3(1.0, 2.0, 2.0));
471469 /// ```
472470 pub fn project ( & self , pt : Point3 < B > ) -> Point3 < B > {
473- // t = -(plane · orig) / ( plane · dir)
474- // In this case dir is normal to the plane and (plane · dir) = 1
475- let t = -self . dot ( pt) ;
471+ // The vector that projects pt on the plane is parallel with the plane
472+ // normal and its length is the distance of pt from the plane.
473+ let t = -self . signed_dist ( pt) ;
476474 let dir = self . normal ( ) ;
477475 pt + t * dir. to ( )
478476 }
@@ -494,7 +492,7 @@ impl<B> Plane3<B> {
494492 /// ```
495493 #[ inline]
496494 pub fn signed_dist ( & self , pt : Point3 < B > ) -> f32 {
497- self . dot ( pt )
495+ self . 0 . dot ( & pt . to_hom ( ) )
498496 }
499497
500498 /// Returns whether a point is in the half-space that the normal of `self`
@@ -513,19 +511,7 @@ impl<B> Plane3<B> {
513511 // TODO "plane.is_inside(point)" reads wrong
514512 #[ inline]
515513 pub fn is_inside ( & self , pt : Point3 < B > ) -> bool {
516- self . dot ( pt) <= 0.0
517- }
518-
519- /// Returns the dot product of the coefficients of self and a point,
520- /// interpreted as a homogeneous vector.
521- ///
522- /// Let `self` = (*a*, *b*, *c*, *d*) and `pt` = (*x*, *y*, *z*).
523- /// Then `self.dot(pt)` = *ax + by + cz* + d.
524- #[ inline]
525- pub fn dot ( & self , pt : Point3 < B > ) -> f32 {
526- // Use homogeneous pt to get self · pt = ax + by + cz + d
527- // Could also just add d manually to ax + by + cz
528- dot ( & self . 0 . 0 , & pt. to_hom ( ) . 0 )
514+ self . signed_dist ( pt) <= 0.0
529515 }
530516
531517 /// Returns an orthonormal affine basis on `self`.
@@ -686,7 +672,7 @@ impl<B> Line2<B> {
686672 ///
687673 /// # Panics
688674 /// If the vector (a, b) is not unit-length.
689- pub const fn new ( a : f32 , b : f32 , c : f32 ) -> Self {
675+ pub fn new ( a : f32 , b : f32 , c : f32 ) -> Self {
690676 // TODO This method can't itself normalize because const
691677 assert ! ( ( a * a + b * b - 1.0 ) . abs( ) < 1e-6 , "non-unit normal" ) ;
692678 Self ( Vector :: new ( [ a, b, -c] ) )
0 commit comments