AaronCameron.net
I care not for your petty politics.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 02:09am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cPlane3.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #ifndef _n2l4_cPlane3_h
00027 #define _n2l4_cPlane3_h
00028 
00029 #include "n2l/n2l.h"
00030 #include "cVector3.h"
00031 
00032 namespace n2l
00033 {
00034 
00035     /**************************************************************************/
00038     template <class TComponent>
00039     class cPlane3
00040     {
00041     public:
00042         typedef cPlane3<TComponent> tThisPlane;
00043         typedef cVector3<TComponent,TComponent,TComponent> tVector;
00044         typedef TComponent tComponent;
00045 
00046         // Constructors
00047         /**********************************************************************/
00048         cPlane3()
00049         {
00050         }
00051 
00052         /**********************************************************************/
00055         cPlane3( const tVector &iNormal, const tComponent &iD ) :
00056             mNormal( iNormal ),
00057             mD( iD )
00058         {
00059         }
00060 
00061         /**********************************************************************/
00062         cPlane3( const tVector &iNormal, const tVector &iPoint ) :
00063             mNormal( iNormal ),
00064             mD( -mNormal.dot(iPoint) )
00065         {
00066         }
00067 
00068         /**********************************************************************/
00071         cPlane3( const tComponent &iA, const tComponent &iB,
00072             const tComponent &iC, const tComponent &iD ) :
00073             mNormal( iA,iB,iC ),
00074             mD( iD )
00075         {
00076         }
00077 
00078         /**********************************************************************/
00079         tThisPlane &set( const tVector &iNormal, const tVector &iPoint )
00080         {
00081             mNormal = iNormal;
00082             mD = -mNormal.dot(iPoint);
00083             return *this;
00084         }
00085 
00086         /**********************************************************************/
00087         void normal( const tVector &iNormal )
00088         {
00089             mNormal = iNormal;
00090         }
00091 
00092         /**********************************************************************/
00093         void normal( const tComponent &iA, const tComponent &iB,
00094             const tComponent &iC  )
00095         {
00096             mNormal.set(iA,iB,iC);
00097         }
00098 
00099         /**********************************************************************/
00100         const tVector &normal() const
00101         {
00102             return mNormal;
00103         }
00104 
00105         /**********************************************************************/
00106         void normalize()
00107         {
00108             TComponent mag = mNormal.magnitude();
00109             mNormal.normalize();
00110             mD /= mag;
00111         }
00112 
00113         /**********************************************************************/
00114         void a( const tComponent &iA ) { mNormal.x(iA); }
00115         void b( const tComponent &iB ) { mNormal.y(iB); }
00116         void c( const tComponent &iC ) { mNormal.z(iC); }
00117         void d( const tComponent &iD ) { mD = iD; }
00118 
00119          const tComponent &a() const { return mNormal.x(); }
00120          const tComponent &b() const { return mNormal.y(); }
00121          const tComponent &c() const { return mNormal.z(); }
00122          const tComponent &d() const { return mD; }
00123 
00124          tComponent &a() { return mNormal.x(); }
00125          tComponent &b() { return mNormal.y(); }
00126          tComponent &c() { return mNormal.z(); }
00127          tComponent &d() { return mD; }
00128 
00129         /**********************************************************************/
00130         void set( const tComponent &iA, const tComponent &iB,
00131             const tComponent &iC, const tComponent &iD )
00132         {
00133             mNormal.set(iA,iB,iC);
00134             mD = iD;
00135         }
00136 
00137         /**********************************************************************/
00138         const tVector pointOn() const
00139         {
00140             if (fabs(mNormal.x())>0.00001)
00141                 return tVector( -mD/mNormal.x(), 0, 0 );
00142 
00143             else if (fabs(mNormal.y())>0.00001)
00144                 return tVector( 0, -mD/mNormal.y(), 0 );
00145 
00146             else
00147                 return tVector( 0, 0, -mD/mNormal.z() );
00148         }
00149 
00150         /**********************************************************************/
00151         const tVector intersect( const tThisPlane &iP2,
00152             const tThisPlane &iP3 ) const
00153         {
00154             cMatrix44<tComponent> m;
00155             m.row(0, mNormal.x(),mNormal.y(),mNormal.z(), mD);
00156             m.row(1, iP2.mNormal.x(),iP2.mNormal.y(),iP2.mNormal.z(), iP2.mD);
00157             m.row(2, iP3.mNormal.x(),iP3.mNormal.y(),iP3.mNormal.z(), iP3.mD);
00158             m.row(3, 0,0,0,1);
00159 
00160             m.invert();
00161             if (m.isZero())
00162                 throw cNoResultException( "cPlane3::intersect(plane,plane)",
00163                     "Two or more planes are parallel" );
00164 
00165             return tVector( m[cMatrix44<tComponent>::_03],
00166                 m[cMatrix44<tComponent>::_13],
00167                 m[cMatrix44<tComponent>::_23] );
00168         }
00169 
00170         /**********************************************************************/
00171         const tVector intersect( const tVector &iP1, const tVector &iP2 ) const
00172         {
00173             const tVector U = iP2 - iP1;
00174             const tVector W = iP1 - pointOn();
00175 
00176             const tFloat D = mNormal.dot( U );
00177             const tFloat N = -mNormal.dot( W );
00178 
00179             if (fabs(D) < 0.000001f)
00180                 throw cNoResultException( "cPlane3::intersect(line)",
00181                     "The line is parallel to the plane." );
00182 
00183             const tFloat SI = N / D;
00184 //          if ( SI < 0 || SI > 1 )
00185 //              return 0;
00186 
00187             const tVector Res = iP1 + (U * SI);
00188             return Res;
00189         }
00190 
00191         /**********************************************************************/
00192         const tComponent distanceTo( const tVector &iV ) const
00193         {
00194             return (mNormal.x()*iV.x() + mNormal.y()*iV.y() +
00195                 mNormal.z()*iV.z() + mD) / mNormal.magnitude();
00196         }
00197 
00198         /**********************************************************************/
00199         const tComponent pointInFront( const tVector &iV ) const
00200         {
00201             return (distanceTo(iV)>0);
00202         }
00203 
00204         /**********************************************************************/
00205         const tString dump() const
00206         {
00207             tString temp("Plane (");
00208             temp += asString(mNormal.x());
00209             temp += mNormal.y()<.0?"x ":"x +";
00210             temp += asString(mNormal.y());
00211             temp += mNormal.z()<.0?"y ":"y +";
00212             temp += asString(mNormal.z());
00213             temp += mD<.0?"z ":"z +";
00214             temp += asString(mD);
00215             temp += " = 0)";
00216             return temp;
00217         }
00218 
00219     private:
00220         tVector mNormal;
00221         tComponent mD;
00222 
00223     }; // class
00224 
00225 } // namespace n2l
00226 
00227 #endif
©2012 Aaron Cameron