00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "K3dConvex.h"
00037
00038 K3dConvex::K3dConvex(K3dGameData *_pGameData)
00039 {
00040 m_pGameData = _pGameData;
00041 m_vPolyPlane.clear();
00042 m_vEdge.clear();
00043 m_vVertex.clear();
00044 m_pPolyPlane = NULL;
00045 m_pEdgePlane = NULL;
00046 InitConvex();
00047 }
00048
00049 K3dConvex::~K3dConvex()
00050 {
00051 }
00052
00054 void K3dConvex::InitConvex()
00055 {
00056 if(m_pGameData->GetPlaneObjSP().FindPointer(m_pEdgePlane) == false)
00057 {
00058 m_pEdgePlane = m_pGameData->GetPlaneBuild()->CreateNewPlaneObj();
00059 }
00060 }
00061
00064 bool K3dConvex::CheckConvexPlane()
00065 {
00066
00067 m_pPolyPlane->GetOnPlaneVertexArray().clear();
00068
00069 for(size_t i=0; i<m_vVertex.size(); i++)
00070 {
00071
00072 K3dVertexObj *pVertex = m_vVertex[i];
00073
00074 int iDistance = (int) m_pGameData->GetIntersection()->PointPlane(*m_pPolyPlane, *pVertex->GetPosition());
00075
00076 if(iDistance > 0)
00077 {
00078 return false;
00079 }
00080
00081 else if(iDistance == 0)
00082 {
00083 m_pPolyPlane->GetOnPlaneVertexArray().push_back(pVertex->GetPosition());
00084 }
00085 }
00086
00087 return true;
00088 }
00089
00094 void K3dConvex::CreateConvexPlane(const K3dVector3Obj &_rkV0,const K3dVector3Obj &_rkV1,const K3dVector3Obj &_rkV2)
00095 {
00096
00097 m_pPolyPlane = m_pGameData->GetPolyPlaneBuild()->CreateNewPolyPlaneObj(_rkV0,_rkV1,_rkV2);
00098
00099 if(m_pPolyPlane->GetDistance() != (float) 0)
00100 {
00101
00102 if(!m_pGameData->GetPlaneWork()->ComparePlane(m_pPolyPlane, m_vPolyPlane))
00103 {
00104
00105 if(CheckConvexPlane())
00106 {
00107 m_vPolyPlane.push_back(m_pPolyPlane);
00108 }
00109 }
00110 }
00111 }
00112
00114 void K3dConvex::CheckEdge(const K3dVector3Obj *_pV0,const K3dVector3Obj *_pV1)
00115 {
00116
00117 bool bIsFront = false;
00118 bool bIsBack = false;
00119
00120 for(size_t i=0; i<m_pPolyPlane->GetOnPlaneVertexArray().size(); i++)
00121 {
00122 K3dVector3Obj *pV2 = m_pPolyPlane->GetOnPlaneVertexArray()[i];
00123 if((pV2 != _pV0) && (pV2 != _pV1))
00124 {
00125
00126 float fDistance = m_pGameData->GetIntersection()->PointPlane(*m_pEdgePlane, *pV2);
00127 if(fDistance > 0)
00128 {
00129 bIsFront = true;
00130 }
00131 if(fDistance < 0)
00132 {
00133 bIsBack = true;
00134 }
00135 }
00136 }
00137
00138 if(bIsFront != bIsBack)
00139 {
00140
00141 K3dLineObj *pEdge = m_pGameData->GetLineBuild()->CreateNewLineObj(*_pV0, *_pV1);
00142
00143 m_vEdge.push_back(pEdge);
00144 }
00145 }
00146
00148 void K3dConvex::CreateOnPlaneEdges()
00149 {
00150
00151 for(size_t iV0=0; iV0<m_pPolyPlane->GetOnPlaneVertexArray().size(); iV0++)
00152 {
00153
00154 K3dVector3Obj *pV0 = m_pPolyPlane->GetOnPlaneVertexArray()[iV0];
00155 for(size_t iV1=iV0+1; iV1<m_pPolyPlane->GetOnPlaneVertexArray().size(); iV1++)
00156 {
00157
00158 K3dVector3Obj *pV1 = m_pPolyPlane->GetOnPlaneVertexArray()[iV1];
00159
00160 K3dVector3Obj kDirection = *pV1 - *pV0;
00161
00162 kDirection = m_pGameData->GetVector3Work()->UnitCross(kDirection, *m_pPolyPlane->GetNormal());
00163 kDirection = m_pGameData->GetVector3Work()->Normalize(kDirection);
00164
00165 m_pGameData->GetPlaneWork()->CalcPlane(m_pEdgePlane, *pV0, kDirection);
00166
00167 CheckEdge(pV0, pV1);
00168 }
00169 }
00170 }
00171
00173 void K3dConvex::CreateEdgesFromVertexArray()
00174 {
00175
00176 m_vEdge.clear();
00177
00178 for(size_t i=0; i<m_vPolyPlane.size(); i++)
00179 {
00180 m_pPolyPlane = m_vPolyPlane[i];
00181 CreateOnPlaneEdges();
00182 }
00183 }
00184
00187 void K3dConvex::CreateResultVertexArray()
00188 {
00189 TVertexArray vResultVertex;
00190
00191 for(size_t iV=0; iV<m_vVertex.size(); iV++)
00192 {
00193
00194 K3dVertexObj *pV = m_vVertex[iV];
00195
00196 for(size_t iE=0; iE<m_vEdge.size(); iE++)
00197 {
00198 K3dLineObj *pEdge = m_vEdge[iE];
00199 if((*pV->GetPosition() == *pEdge->GetOrigin()) || (*pV->GetPosition() == *pEdge->GetDirection()))
00200 {
00201
00202 vResultVertex.push_back(pV);
00203 break;
00204 }
00205
00206 }
00207 }
00208
00209 m_vVertex = vResultVertex;
00210 }
00211
00214 K3dPolyObj *K3dConvex::CreatePolyFromVertexArray(const TVertexArray &_rvVertex)
00215 {
00216
00217 m_vVertex = _rvVertex;
00218
00219 m_pGameData->GetVertexWork()->DeleteDupVerts(m_vVertex);
00220 K3dPolyObj *pPoly = m_pGameData->GetPolyBuild()->CreateNewPolyObj();
00221
00222 for(size_t iV0=0; iV0<m_vVertex.size(); iV0++)
00223 {
00224
00225 K3dVertexObj *pVertex0 = m_vVertex[iV0];
00226 for(size_t iV1=iV0+1; iV1<m_vVertex.size(); iV1++)
00227 {
00228
00229 K3dVertexObj *pVertex1 = m_vVertex[iV1];
00230 for(size_t iV2=iV1+1; iV2<m_vVertex.size(); iV2++)
00231 {
00232
00233 K3dVertexObj *pVertex2 = m_vVertex[iV2];
00234
00235 CreateConvexPlane(*pVertex0->GetPosition(),*pVertex1->GetPosition(),*pVertex2->GetPosition());
00236
00237 CreateConvexPlane(*pVertex1->GetPosition(),*pVertex0->GetPosition(),*pVertex2->GetPosition());
00238 }
00239 }
00240 }
00241
00242 CreateEdgesFromVertexArray();
00243
00244 m_pGameData->GetLineWork()->DeleteDupLines(m_vEdge);
00245
00246
00247 CreateResultVertexArray();
00248
00249 pPoly->GetVertexArray() = m_vVertex;
00250 pPoly->GetEdgeArray() = m_vEdge;
00251 pPoly->GetPlaneArray() = m_vPolyPlane;
00252 m_pGameData->GetPolyWork()->CheckPoly(pPoly);
00253 m_pGameData->GetPolyWork()->VisiblePoly(pPoly);
00254 return pPoly;
00255 }
00256
00257