K3dDistance.h

Go to the documentation of this file.
00001 
00012 /***************************************************************************
00013  *   Copyright (C) 2007 by Jan Koci   *
00014  *   honza.koci@email.cz   *
00015  *   http://kengine.sourceforge.net/tutorial/
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  *   This program is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00025  *   GNU General Public License for more details.                          *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU General Public License     *
00028  *   along with this program; if not, write to the                         *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00031  ***************************************************************************/
00032 
00033 #pragma once
00034 
00035 #include "K3dVector3.h"
00036 #include "../Geometry/K3dRay.h"
00037 #include "../Geometry/K3dPlane.h"
00038 #include "../System/GameData/K3dGameData.h"
00039 #include "../System/Worker/K3dVector3Work.h"
00040 
00041 class K3dDistance
00042 {
00043                 K3dGameData *m_pGameData;       
00044         public:
00045                 K3dDistance ( K3dGameData *_pGameData );
00046                 ~K3dDistance();
00047 
00049                 float Points ( const K3dVector3Obj &_rkV0, const K3dVector3Obj &_rkV1 )
00050                 {
00051                         // Calculate vector between two points
00052                         K3dVector3Obj kV;
00053                         kV = m_pGameData->GetVector3Work()->VectorBetween ( _rkV0, _rkV1 );
00054                         // Return vector length
00055                         return m_pGameData->GetVector3Work()->Length ( kV );
00056                 }
00057 
00059                 float SqrPointRay ( const K3dVector3Obj &_rkPoint,const K3dRay &_rkRay, float* _pfParam=NULL )
00060                 {
00061                         float fDotMM;
00062                         float fT = ( float ) 0;
00063                         K3dVector3Obj kDiff;
00064 
00065                         //kDiff = kRay.Origin() - kPoint;
00066                         kDiff = _rkPoint - *_rkRay.GetOrigin();
00067                         fT = m_pGameData->GetVector3Work()->Dot ( kDiff, *_rkRay.GetDirection() );
00068 
00069 
00070                         if ( fT <= ( float ) 0.0 )
00071                         {
00072                                 fT = ( float ) 0.0;
00073                         }
00074                         else
00075                         {
00076                                 fDotMM = m_pGameData->GetVector3Work()->SquaredLength ( *_rkRay.GetDirection() );
00077                                 if ( fDotMM )
00078                                 {
00079                                         fT = fT / fDotMM;
00080                                         kDiff -= *_rkRay.GetDirection() *fT;
00081                                 }
00082                         }
00083 
00084                         if ( _pfParam )
00085                                 *_pfParam = fT;
00086 
00087                         return m_pGameData->GetVector3Work()->SquaredLength ( kDiff );
00088 
00089                 }
00090 
00099                 int PointPlane ( const K3dPlane &_rkPlane,const K3dVector3 &_rkPoint )
00100                 {
00101                         float fD;
00102                         K3dVector3 kNormal = *_rkPlane.GetNormal();
00103                         // Calculate distance from  plane
00104                         fD = kNormal[0] * _rkPoint[0] +
00105                              kNormal[1] * _rkPoint[1] +
00106                              kNormal[2] * _rkPoint[2] - _rkPlane.GetDistance();
00107 
00108                         return ( int ) fD;
00109                 }
00110 
00115                 int DistLinePlane ( const K3dPlane &_rkPlane, const K3dVector3 &_rkLineOrigin, const K3dVector3 &_rkLineDirection )
00116                 {
00117                         // Calculate distance plane and line origin point
00118                         float fDistOrigin = PointPlane ( _rkPlane, _rkLineOrigin );
00119                         // Calculate distance plane and line direction point
00120                         float fDistDirection = PointPlane ( _rkPlane, _rkLineDirection );
00121                         // If sign of origin and direction is equal then return false
00122                         // Ray direction outside of plane
00123                         int iSignOrigin = ( int ) K3dMath::Sign ( fDistOrigin );
00124                         int iSignDirection = ( int ) K3dMath::Sign ( fDistDirection );
00125                         // If line lies on plane front side return 1
00126                         // If line lies on plane back side return -1
00127                         if ( iSignOrigin == iSignDirection )
00128                         {
00129                                 return iSignOrigin;
00130                         }
00131                         else
00132                         {
00133                                 // If line intersect the plane return 0
00134                                 return 0;
00135                         }
00136                 }
00137 
00138 };
00139 

Generated on Thu Aug 16 23:53:27 2007 for K3dEngine by  doxygen 1.5.0