00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00053 K3dVector3Obj kNormal = m_pVector3Build->Normal ( *_pV0, *_pV1, *_pV2 );
00054
00055 if ( kNormal == *_pNormal )
00056 {
00057 m_pTriangleBuild->CreateNewTriangleObj ( _pV0, _pV1, _pV2 );
00058 }
00059 else
00060 {
00061 m_pTriangleBuild->CreateNewTriangleObj ( _pV1, _pV0, _pV2 );
00062 }
00063 }
00064
00067 void K3dTriangulation::SortVector3ArrayByAngle ( K3dPolyPlane *_pPolyPlane )
00068 {
00069 cout << "SortVector3ArrayByAngle" << endl;
00070
00071 TVector3Array vVector;
00072 TFloatIntMap mapFloatInt;
00073
00074 K3dVector3Obj kCentre;
00075 kCentre.Reset();
00076 m_pVector3Build->CalcVectorArrayCentre ( kCentre, _pPolyPlane->GetOnPlaneVertexArray() );
00077
00078 K3dVector3Obj *pV0 = _pPolyPlane->GetOnPlaneVertexArray() [0];
00079
00080 mapFloatInt.insert ( TFloatIntPair ( 0, 0 ) );
00081 cout << "Vector3 array size = " << _pPolyPlane->GetOnPlaneVertexArray().size() << endl;
00082
00083 for ( size_t i = 1; i<_pPolyPlane->GetOnPlaneVertexArray().size(); i++ )
00084 {
00085 K3dVector3Obj *pV1 = _pPolyPlane->GetOnPlaneVertexArray() [i];
00086
00087 float fAngle = m_pVector3Build->AngleBetweenVectors ( *pV0, *pV1, kCentre );
00088
00089 mapFloatInt.insert ( TFloatIntPair ( fAngle, i ) );
00090 }
00091
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
00098 vVector.push_back ( _pPolyPlane->GetOnPlaneVertexArray() [it->second] );
00099 }
00100
00101 _pPolyPlane->GetOnPlaneVertexArray() = vVector;
00102 }
00103
00104
00105
00107 void K3dTriangulation::TriangulatePolyPlane ( K3dPolyPlane *_pPolyPlane )
00108 {
00109 cout << "TriangulatePolyPlane" << endl;
00110
00111 SortVector3ArrayByAngle ( _pPolyPlane );
00112 cout << "Vector3 array size = " << _pPolyPlane->GetOnPlaneVertexArray().size() << endl;
00113
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
00134 for ( size_t i = 0; i<_pPoly->GetPlaneArray().size(); i++ )
00135 {
00136 K3dPolyPlane *pPolyPlane = _pPoly->GetPlaneArray() [i];
00137
00138 TriangulatePolyPlane ( pPolyPlane );
00139 }
00140 }
00141
00145 void K3dTriangulation::Triangulate ( const TVertexArray &_rvVertex )
00146 {
00147
00148 K3dPolyObj *pPoly = CreatePolyFromVertexArray ( _rvVertex );
00149
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
00162 Triangulate ( pFace->GetVertexArray() );
00163 cout << "Number of vertices in vertex array after face triangulation = " << pFace->GetVertexArray().size() << endl;
00164 }
00165 }
00166
00167