home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_4 / plane.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  1.0 KB  |  43 lines

  1. // -*- C++ -*-
  2. //  plane.cc by George Vanecek Jr., June 1994
  3. //
  4.  
  5. #include "plane.h"
  6.  
  7. // Computes the plane equation using Newell's averaging algorithm.
  8. Plane::Plane( const Counter nPoints, const Point points[] )
  9. : n(0.0,0.0,0.0), d(0.0), eps(0.0)
  10. {
  11.   assert( nPoints > 2 );
  12.   Vector avrPnt = Point(0,0,0);
  13.   for( Index i = 0; i < nPoints; ++i ) {
  14.     avrPnt += points[i-1];
  15.     n      += Vector(points[i-1]) ^ Vector(points[i]);
  16.     updateEpsilon( points[i] );
  17.   }
  18.   avrPnt += points[nPoints-1];
  19.   n      += Vector(points[nPoints-1]) ^ Vector(points[0]);
  20.   n.normalize();
  21.   d = normal() * ((-1.0 / nPoints) * avrPnt );
  22. }
  23.  
  24. // Compute the intersection point with the transversal line (p,q).
  25. Point Plane::onPoint( const Point &p, const Point &q ) const
  26. {
  27.   const Vector v(q - p);
  28.   const double c = normal() * v;
  29.   assert( c != 0.0 );
  30.   const double t = -sDistance(p) / c;
  31.   return p + t * v;
  32. }
  33.  
  34. void Plane::updateEpsilon ( const Point& p )
  35. {
  36.   double d = sDistance(p);
  37.   if( d < 0.0 )
  38.     d = -d;
  39.   if( d > eps)
  40.     eps = d;
  41. }
  42.  
  43.