Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Sphere.cxx

Go to the documentation of this file.
00001 // $Id: Sphere.cxx,v 1.2 2000/12/14 23:18:11 burnett Exp $
00002 //
00003 //
00004 #include "geometry/Sphere.h"
00005 
00006 inline static double sqr(double x){return x*x;}
00007 #include <cfloat>
00008 
00009 Sphere::Sphere( const Point& origin, double radius)
00010 : Surface(origin, Vector(0,0,0) )
00011 , _radius(radius)
00012 {}
00013 
00014 
00015 void Sphere::printOn( std::ostream&  ) const
00016 {
00017 }
00018 
00019 double
00020 Sphere::how_near( const Point& x ) const
00021 {  //  Distance from the point x to the nearest point on the Sphere.
00022    //  The distance will be positive if the point is inside the Sphere,
00023    //  negative if the point is outside.
00024         Vector r = x - origin();
00025         double rabs = r.mag();
00026 
00027         return radius()>0?  radius()-rabs  : rabs+radius() ;
00028  }
00029 
00030 
00031 Vector
00032 Sphere::normal( const Point& x )const
00033 {
00034     // returns normal-vector at Point x, assuming x to be "on" the surface,
00035     // and not coinciding with the origin!
00036 
00037     if(radius()>0 ) return (x - origin()).unit() ;
00038     else            return (origin() - x).unit() ;
00039 }
00040 double
00041 Sphere::distance(const Point& x, const Vector& v, int inout)const
00042 {
00043    // note very similar logic to Cylinder::distance
00044    Vector r = x-origin();
00045    double b = r*v,
00046           c= r.mag2()-sqr(radius()),
00047           disc = b*b-c,
00048           s;
00049 
00050    // now want appropriate root of s^2 + 2*b*s + c = 0
00051 
00052    if( disc<=0 ) return FLT_MAX;  // misses
00053    if( radius()<0 ) inout *=-1;   // reverse enter/leave sense
00054    int leaving = inout==1;
00055    if( b<0 ) // velocity toward axis
00056    {  s = (leaving) ? -b+sqrt(disc) :
00057                          c/(-b+sqrt(disc));
00058    }
00059    else // heading away from axis: can only leave
00060    {  s = (leaving) ? c/(-b-sqrt(disc)) : FLT_MAX;
00061    }
00062   return s>0? s : FLT_MAX;
00063 
00064 }
00065 

Generated at Mon Nov 26 18:18:21 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000