K3dTriangulation.cpp

Go to the documentation of this file.
00001 
00013 /***************************************************************************
00014  *   Copyright (C) 2007 by Jan Koci   *
00015  *   honza.koci@email.cz   *
00016  *   http://kengine.sourceforge.net/tutorial/
00017  *                                                                         *
00018  *   This program is free software; you can redistribute it and/or modify  *
00019  *   it under the terms of the GNU General Public License as published by  *
00020  *   the Free Software Foundation; either version 2 of the License, or     *
00021  *   (at your option) any later version.                                   *
00022  *                                                                         *
00023  *   This program is distributed in the hope that it will be useful,       *
00024  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00025  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00026  *   GNU General Public License for more details.                          *
00027  *                                                                         *
00028  *   You should have received a copy of the GNU General Public License     *
00029  *   along with this program; if not, write to the                         *
00030  *   Free Software Foundation, Inc.,                                       *
00031  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00032  ***************************************************************************/
00033 
00034 #include "K3dTriangulation.h"
00035 
00036 K3dTriangulation::K3dTriangulation ( K3dGameData *_pGameData ) : K3dConvex ( _pGameData )
00037 {
00038         m_pGameData = _pGameData;
00039         m_pBoxWork = m_pGameData->GetBoxWork();
00040         m_pVector3Build = m_pGameData->GetVector3Build();
00041         m_pTriangleBuild = m_pGameData->GetTriangleBuild();
00042 }
00043 
00044 K3dTriangulation::~K3dTriangulation()
00045 {
00046         cout << "K3dTriangulation::~K3dTriangulation()" << endl;
00047 }
00048 
00050 void K3dTriangulation::CreateNewTriangle ( K3dVector3Obj *_pV0, K3dVector3Obj *_pV1, K3dVector3Obj *_pV2, const K3dVector3Obj *_pNormal )
00051 {
00052         // Calculate triangle normal
00053         K3dVector3Obj kNormal = m_pVector3Build->Normal ( *_pV0, *_pV1, *_pV2 );
00054         // If triangle normal equal with plane normal create new triangle
00055         if ( kNormal == *_pNormal )
00056         {
00057                 m_pTriangleBuild->CreateNewTriangleObj ( _pV0, _pV1, _pV2 );
00058         }
00059         else    // Swap first ant second vertex and create new triangle
00060         {
00061                 m_pTriangleBuild->CreateNewTriangleObj ( _pV1, _pV0, _pV2 );
00062         }
00063 }
00064 
00067 void K3dTriangulation::SortVector3ArrayByAngle ( K3dPolyPlane *_pPolyPlane )
00068 {
00069         cout << "SortVector3ArrayByAngle" << endl;
00070         // Result vertex array
00071         TVector3Array vVector;
00072         TFloatIntMap mapFloatInt;
00073         // Calculate vertex array centre
00074         K3dVector3Obj kCentre;
00075         kCentre.Reset();
00076         m_pVector3Build->CalcVectorArrayCentre ( kCentre, _pPolyPlane->GetOnPlaneVertexArray() );
00077         // Get first vertex
00078         K3dVector3Obj *pV0 = _pPolyPlane->GetOnPlaneVertexArray() [0];
00079         // Insert angle to the float int map. First angle is zero
00080         mapFloatInt.insert ( TFloatIntPair ( 0, 0 ) );
00081         cout << "Vector3 array size = " << _pPolyPlane->GetOnPlaneVertexArray().size() << endl;
00082         // Go through all on plane vertices
00083         for ( size_t i = 1; i<_pPolyPlane->GetOnPlaneVertexArray().size(); i++ )
00084         {
00085                 K3dVector3Obj *pV1 = _pPolyPlane->GetOnPlaneVertexArray() [i];
00086                 // Calculate angle between vectors toward to vectors centre
00087                 float fAngle = m_pVector3Build->AngleBetweenVectors ( *pV0, *pV1, kCentre );
00088                 // Insert angle to the float int map
00089                 mapFloatInt.insert ( TFloatIntPair ( fAngle, i ) );
00090         }
00091         // Check map sorting
00092         for ( TFloatIntMap::iterator it=mapFloatInt.begin(); it!=mapFloatInt.end(); ++it )
00093         {
00094                 cout << endl;
00095                 cout << "Key:" << it->first << endl;
00096                 cout << " Value:" << it->second << endl;
00097                 // Save sorted vertices
00098                 vVector.push_back ( _pPolyPlane->GetOnPlaneVertexArray() [it->second] );
00099         }
00100         // Copy sorted vertex array to the poly plane vertex array
00101         _pPolyPlane->GetOnPlaneVertexArray() = vVector;
00102 }
00103 
00104 
00105 
00107 void K3dTriangulation::TriangulatePolyPlane ( K3dPolyPlane *_pPolyPlane )
00108 {
00109         cout << "TriangulatePolyPlane" << endl;
00110         // Sort vertex array by angle
00111         SortVector3ArrayByAngle ( _pPolyPlane );
00112         cout << "Vector3 array size = " << _pPolyPlane->GetOnPlaneVertexArray().size() << endl;
00113         // Go through all sorted on plane vertices
00114         for ( size_t i = 0; i<_pPolyPlane->GetOnPlaneVertexArray().size(); i++ )
00115         {
00116                 K3dVector3Obj *pV0;
00117                 K3dVector3Obj *pV1;
00118                 K3dVector3Obj *pV2;
00119                 if ( i+2 < _pPolyPlane->GetOnPlaneVertexArray().size() )
00120                 {
00121                         pV0 = _pPolyPlane->GetOnPlaneVertexArray() [0];
00122                         pV1 = _pPolyPlane->GetOnPlaneVertexArray() [i+1];
00123                         pV2 = _pPolyPlane->GetOnPlaneVertexArray() [i+2];
00124                         CreateNewTriangle ( pV0,pV1,pV2,_pPolyPlane->GetNormal() );
00125                 }
00126         }
00127 }
00128 
00131 void K3dTriangulation::TriangulatePoly ( K3dPolyObj *_pPoly )
00132 {
00133         // Go through all planes
00134         for ( size_t i = 0; i<_pPoly->GetPlaneArray().size(); i++ )
00135         {
00136                 K3dPolyPlane *pPolyPlane = _pPoly->GetPlaneArray() [i];
00137                 // Triangulate poly plane
00138                 TriangulatePolyPlane ( pPolyPlane );
00139         }
00140 }
00141 
00145 void K3dTriangulation::Triangulate ( const TVertexArray &_rvVertex )
00146 {
00147         // Try calculate polyhedron from vertex array
00148         K3dPolyObj *pPoly = CreatePolyFromVertexArray ( _rvVertex );
00149         // Triangulate polyhedron
00150         TriangulatePoly ( pPoly );
00151 }
00152 
00154 void K3dTriangulation::BuildTriangles()
00155 {
00156         cout << "void K3dTriangleBuild::BuildTriangles()" << endl;
00157         int iNumFaces = m_pGameData->GetFaceObjSP().GetNum();
00158         for ( int i=0; i<iNumFaces; i++ )
00159         {
00160                 K3dFaceObj *pFace =  m_pGameData->GetFaceObjSP().Get ( i );
00161                 // Triangulate vertex array and return result vertex array
00162                 Triangulate ( pFace->GetVertexArray() );
00163                 cout << "Number of vertices in vertex array after face triangulation = " << pFace->GetVertexArray().size() << endl;
00164         }
00165 }
00166 
00167 

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