• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Plane.h

Go to the documentation of this file.
00001 // GMTL is (C) Copyright 2001-2010 by Allen Bierbaum
00002 // Distributed under the GNU Lesser General Public License 2.1 with an
00003 // addendum covering inlined code. (See accompanying files LICENSE and
00004 // LICENSE.addendum or http://www.gnu.org/copyleft/lesser.txt)
00005 
00006 #ifndef _GMTL_PLANE_H
00007 #define _GMTL_PLANE_H
00008 
00009 #include <gmtl/Vec.h>
00010 #include <gmtl/Point.h>
00011 #include <gmtl/VecOps.h>
00012 
00013 namespace gmtl
00014 {
00015 
00035 template< class DATA_TYPE>
00036 class Plane
00037 {
00038 public:
00043    Plane()
00044       : mOffset( 0 )
00045    {}
00046 
00054    Plane( const Point<DATA_TYPE, 3>& pt1, const Point<DATA_TYPE, 3>& pt2,
00055           const Point<DATA_TYPE, 3>& pt3)
00056    {
00057       Vec<DATA_TYPE, 3> vec12( pt2-pt1 );
00058       Vec<DATA_TYPE, 3> vec13( pt3-pt1 );
00059 
00060       cross( mNorm, vec12, vec13 );
00061       normalize( mNorm );
00062 
00063       mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt1), mNorm );  // Graphics Gems I: Page 390
00064    }
00065 
00072    Plane( const Vec<DATA_TYPE, 3>& norm, const Point<DATA_TYPE, 3>& pt )
00073       : mNorm( norm )
00074    {
00075       mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt), norm );
00076    }
00077 
00084    Plane( const Vec<DATA_TYPE, 3>& norm, const DATA_TYPE& dPlaneConst )
00085       : mNorm( norm ), mOffset( dPlaneConst )
00086    {}
00087 
00093    Plane( const Plane<DATA_TYPE>& plane )
00094       : mNorm( plane.mNorm ), mOffset( plane.mOffset )
00095    {}
00096 
00102    const Vec<DATA_TYPE, 3>& getNormal() const
00103    {
00104       return mNorm;
00105    }
00106 
00114    void setNormal( const Vec<DATA_TYPE, 3>& norm )
00115    {
00116       mNorm = norm;
00117    }
00118 
00125    const DATA_TYPE& getOffset() const
00126    {
00127       return mOffset;
00128    }
00129 
00135    void setOffset( const DATA_TYPE& offset )
00136    {
00137       mOffset = offset;
00138    }
00139 
00140 public:
00141    // dot(Pt,mNorm) = mOffset
00146    Vec<DATA_TYPE, 3> mNorm;
00147 
00153    DATA_TYPE mOffset;
00154 };
00155 
00156 typedef Plane<float> Planef;
00157 typedef Plane<double> Planed;
00158 
00159 /*
00160 #include <geomdist.h>
00161 
00162 
00163 // Intersects the plane with a given segment.
00164 // Returns TRUE if there is a hit (within the seg).
00165 // Also returns the distance "down" the segment of the hit in t.
00166 //
00167 // PRE: seg.dir must be normalized
00168 //
00169 int sgPlane::isect(sgSeg& seg, float* t)
00170 {
00171    // Graphic Gems I: Page 391
00172    float denom = normal.dot(seg.dir);
00173    if (SG_IS_ZERO(denom))     // No Hit
00174    {
00175       //: So now, it is just dist to plane tested against length
00176       sgVec3   hit_pt;
00177       float    hit_dist;      // Dist to hit
00178       
00179       hit_dist = findNearestPt(seg.pos, hit_pt);
00180       *t = hit_dist;       // Since dir is normalized
00181 
00182       if(seg.tValueOnSeg(*t))
00183          return 1;
00184       else
00185          return 0;
00186    }
00187    else
00188    {
00189       float numer = offset + normal.dot(seg.pos);
00190       (*t) = -1.0f * (numer/denom);
00191       
00192       if(seg.tValueOnSeg(*t))
00193          return 1;
00194       else
00195          return 0;
00196    }
00197 }
00198 
00200  // Intersects the plane with the line defined
00201  // by the given segment
00202  //
00203  // seg - seg that represents the line to isect
00204  // t   - the t value of the isect
00205  //
00206 int sgPlane::isectLine(const sgSeg& seg, float* t)
00207 {
00208    // GGI: Pg 299
00209    // Lu = seg.pos;
00210    // Lv = seg.dir;
00211    // Jn = normal;
00212    // Jd = offset;
00213    
00214    float denom = normal.dot(seg.dir);
00215    if(denom == 0.0f)
00216       return 0;
00217    else
00218    {
00219       *t = - (offset+ normal.dot(seg.pos))/(denom);
00220       return 1;
00221    }
00222 }
00223 */
00224 
00225 } // namespace gmtl
00226 #endif

Generated on Sun Sep 19 2010 14:35:14 for GenericMathTemplateLibrary by  doxygen 1.7.1