📄 tabtoolsimulate.cpp
字号:
float fAiv;
float fP;
float fP2;
float fSumOfAiv = 0.0;
float fSumOfP = 0.0;
float fSumOfP2 = 0.0;
FILE * OutPutFile;
BOOL bIsOK = TRUE;
int nReturnCode;
int nDetonationInTimes = 0;
for(i=0; i < DialogOfSingleMissle.m_nTimes; i++)
{
//Simulate DialogOfSingleMissle.m_nTimes times
//First let's get the detonation point
NProbability.GetTwoValues(dX, dY);
NProbability.GetTwoValues(dZ, dTemp);
DetonationVertex.m_fX = (float)dX;
DetonationVertex.m_fY = (float)dY;
DetonationVertex.m_fZ = (float)dZ;
sMsg.Format("touying01.din");
if(EllipseSphere.bCheckVerexIn(DetonationVertex))
{
nDetonationInTimes = nDetonationInTimes + 1;
continue;
}
fLengthOfDPToWC = sqrt((TriMeshesWeightCenterVertex.m_fX - DetonationVertex.m_fX) * (TriMeshesWeightCenterVertex.m_fX - DetonationVertex.m_fX) +
(TriMeshesWeightCenterVertex.m_fY - DetonationVertex.m_fY) * (TriMeshesWeightCenterVertex.m_fY - DetonationVertex.m_fY) +
(TriMeshesWeightCenterVertex.m_fZ - DetonationVertex.m_fZ) * (TriMeshesWeightCenterVertex.m_fZ - DetonationVertex.m_fZ));
fLengthOfDetonation = fLengthOfDPToWC * tan(fDetonationConeAngle/2.0);
//Produce the data file
nReturnCode = Simulate(DetonationVertex,
TriMeshesWeightCenterVertex,
fLengthOfDetonation,
fLengthOfDPToWC,
m_nYScanLinesSM,
sMsg,
pUnimportantMeshNameArray,
nUnimportantMeshesItems,
pImportantMeshNameArray,
nImportantMeshesItems);
if(nReturnCode == -1)
{
//some error has rise
bIsOK = FALSE;
break;
}
if(nReturnCode == 3)
{
//This missile must miss the fighter
continue;
}
ExeFile = fopen("vul01.exe", "rb" );
if(ExeFile == NULL)
{
AfxMessageBox("未找到导弹仿真分析程序vul01.exe!");
bIsOK = FALSE;
break;
}
else
fclose(ExeFile);
memset(&StartupInfo, 0, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
fSuccess = ::CreateProcess(NULL,
"vul01.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInfo);
if(fSuccess)
{
hProcess = ProcessInfo.hProcess;
//Close the thread handle as soon as it is no longer needed!
::CloseHandle(ProcessInfo.hThread);
if(WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED)
{
//The process terminated
::GetExitCodeProcess(hProcess, &dwExitCode);
}
//Close the process handle as soon as it is no longer needed
CloseHandle(hProcess);
OutPutFile = fopen("final01.dat", "rt" );
if(OutPutFile == NULL)
{
AfxMessageBox("找不到数据文件final01.dat!");
bIsOK = FALSE;
break;
}
fscanf(OutPutFile, "%f %f %f", &fAiv, &fP, &fP2);
fSumOfAiv = fSumOfAiv + fAiv;
fSumOfP = fSumOfP + fP;
fSumOfP2 = fSumOfP2 + fP2;
fclose(OutPutFile);
}
else
{
AfxMessageBox("创建子进程失败!");
bIsOK = FALSE;
break;
}
}
if(nDetonationInTimes == DialogOfSingleMissle.m_nTimes)
{
sMsg.Format("所有的爆炸均为机内爆炸,故P=1, P2=1");
AfxMessageBox(sMsg);
}
else if(bIsOK == TRUE)
{
fAiv = fSumOfAiv/(DialogOfSingleMissle.m_nTimes - nDetonationInTimes);
fSumOfP = fSumOfP + nDetonationInTimes * 1.0;
fSumOfP2 = fSumOfP2 + nDetonationInTimes * 1.0;
fP = fSumOfP/(DialogOfSingleMissle.m_nTimes);
fP2 = fSumOfP2/(DialogOfSingleMissle.m_nTimes);
OutPutFile = fopen("final01_1.dat", "wt" );
fprintf(OutPutFile, "模拟次数: %d\n", DialogOfSingleMissle.m_nTimes);
fprintf(OutPutFile, "模拟结果: Aiv = %f, P = %f, P2 = %f\n", fAiv, fP, fP2);
fclose(OutPutFile);
sMsg.Format("模拟结束,结果请查看数据文件final01_1.dat");
AfxMessageBox(sMsg);
}
SetCurrentDirectory(szCurDir);
if(pUnimportantMeshNameArray != NULL)
delete [] pUnimportantMeshNameArray;
delete [] pImportantMeshNameArray;
}
}
void CTabtoolSimulate::OnTabtoolSimulateAlldirections()
{
// TODO: Add your control notification handler code here
CSimulateDoc* pDoc = ((CSimulateView *)GetRenderView())->GetDocument();
ASSERT_VALID(pDoc);
CString sMsg;
CString sMsg1;
float fPI = 3.1415926535;
int i;
int nMeshesWantedItems;
nMeshesWantedItems = m_MeshesWantedList.GetCount();
if(nMeshesWantedItems == LB_ERR)
{
sMsg.Format("计算形心列表框操作出错!");
AfxMessageBox(sMsg, MB_ICONSTOP | MB_OK);
return;
}
if(nMeshesWantedItems == 0)
{
sMsg.Format("未选择进行形心计算的部件!");
AfxMessageBox(sMsg ,MB_ICONEXCLAMATION | MB_OK);
return;
}
int nImportantMeshesItems = m_ImportantMeshesList.GetCount();
int nUnimportantMeshesItems = m_MeshesList1.GetCount();
if(nImportantMeshesItems == LB_ERR | nUnimportantMeshesItems == LB_ERR)
{
sMsg.Format("重要部件列表框操作出错!");
AfxMessageBox(sMsg, MB_ICONSTOP | MB_OK);
return;
}
if(nImportantMeshesItems == 0)
{
sMsg.Format("未选择用于分析的重要部件!");
AfxMessageBox(sMsg ,MB_ICONEXCLAMATION | MB_OK);
return;
}
CString * pImportantMeshNameArray = new CString [nImportantMeshesItems];
for(i = 0; i < nImportantMeshesItems; i++)
{
m_ImportantMeshesList.GetText(i, pImportantMeshNameArray[i]);
}
//Now it's the big time to show the dialog for input messages!
CDialogOfAllDirections DialogOfAllDirections;
DialogOfAllDirections.m_fAlfa1 = m_fAlfa1AD;
DialogOfAllDirections.m_fAlfa2 = m_fAlfa2AD;
DialogOfAllDirections.m_fCita = m_fCitaAD;
DialogOfAllDirections.m_fVf = m_fVfAD;
DialogOfAllDirections.m_fVm = m_fVmAD;
DialogOfAllDirections.m_fVt = m_fVtAD;
DialogOfAllDirections.m_nYScanLines = m_nYScanLinesAD;
DialogOfAllDirections.m_nIntervals = m_nIntervalsAD;
DialogOfAllDirections.m_fMinDistance = m_fMinDistanceAD;
DialogOfAllDirections.m_fMaxDistance = m_fMaxDistanceAD;
if(DialogOfAllDirections.DoModal() == IDOK)
{
if(DialogOfAllDirections.m_fMinDistance > DialogOfAllDirections.m_fMaxDistance)
{
//m_fMinDistance can not be greater than m_fMaxDistance
AfxMessageBox("最小距离不能大于最大距离!");
delete [] pImportantMeshNameArray;
return;
}
//If the input values are checked! let's prepare the data for calculation!
CVertex DetonationVertex;
float fDetonationConeAngle;
float fAlfa1 = DialogOfAllDirections.m_fAlfa1;
float fAlfa2 = DialogOfAllDirections.m_fAlfa2;
float fCita = DialogOfAllDirections.m_fCita;
float fVf = DialogOfAllDirections.m_fVf;
float fVm = DialogOfAllDirections.m_fVm;
float fVt = DialogOfAllDirections.m_fVt;
float fFai1;
float fFai2;
m_fAlfa1AD = DialogOfAllDirections.m_fAlfa1;
m_fAlfa2AD = DialogOfAllDirections.m_fAlfa2;
m_fCitaAD = DialogOfAllDirections.m_fCita;
m_fVfAD = DialogOfAllDirections.m_fVf;
m_fVmAD = DialogOfAllDirections.m_fVm;
m_fVtAD = DialogOfAllDirections.m_fVt;
m_nYScanLinesAD = DialogOfAllDirections.m_nYScanLines;
m_nIntervalsAD = DialogOfAllDirections.m_nIntervals;
m_fMinDistanceAD = DialogOfAllDirections.m_fMinDistance;
m_fMaxDistanceAD = DialogOfAllDirections.m_fMaxDistance;
fFai1 = atan((fVm * sin(fCita*fPI/180.) + fVf * sin(fCita*fPI/180. + fAlfa1*fPI/180.))/
(fVm * cos(fCita*fPI/180.) + fVf * cos(fCita*fPI/180. + fAlfa1*fPI/180.) - fVt)) - fCita*fPI/180.;
fFai2 = atan((fVm * sin(fCita*fPI/180.) + fVf * sin(fCita*fPI/180. + fAlfa2*fPI/180.))/
(fVm * cos(fCita*fPI/180.) + fVf * cos(fCita*fPI/180. + fAlfa2*fPI/180.) - fVt)) - fCita*fPI/180.;
if(fFai1 < 0.)fFai1 = fPI + fFai1;
if(fFai2 < 0.)fFai2 = fPI + fFai2;
fDetonationConeAngle = fFai2 - fFai1;
CEllipseSphere EllipseSphere;
(EllipseSphere.m_VertexCenter).m_fX = (pDoc->m_fGlobalXMin +
pDoc->m_fGlobalXMax)/2.0;
(EllipseSphere.m_VertexCenter).m_fY = (pDoc->m_fGlobalYMin +
pDoc->m_fGlobalYMax)/2.0;
(EllipseSphere.m_VertexCenter).m_fZ = (pDoc->m_fGlobalZMin +
pDoc->m_fGlobalZMax)/2.0;
EllipseSphere.m_fA = (pDoc->m_fGlobalXMax -
pDoc->m_fGlobalXMin)/2.0;
EllipseSphere.m_fB = (pDoc->m_fGlobalYMax -
pDoc->m_fGlobalYMin)/2.0;
EllipseSphere.m_fC = (pDoc->m_fGlobalZMax -
pDoc->m_fGlobalZMin)/2.0;
//now let's calculate the Weight Center of the selected TriMeshes
float fTotalArea, fSumOfWeightX, fSumOfWeightY, fSumOfWeightZ;
CVertex TriMeshesWeightCenterVertex;
int Index, nIndex;
CString sMeshName;
fTotalArea = 0.0;
fSumOfWeightX = 0.0;
fSumOfWeightY = 0.0;
fSumOfWeightZ = 0.0;
for(nIndex = 0; nIndex < nMeshesWantedItems; nIndex++)
{
m_MeshesWantedList.GetText(nIndex, sMeshName);
Index = pDoc->GetNumTriMeshes();
while(Index--)
{
if((pDoc->GetTriMesh(Index))->m_sName == sMeshName)
{
//We have found the mesh
//So just get the m_fArea and the m_WeightCenter
fTotalArea = fTotalArea + (pDoc->GetTriMesh(Index))->m_fArea;
fSumOfWeightX = fSumOfWeightX + (pDoc->GetTriMesh(Index))->m_fArea * ((pDoc->GetTriMesh(Index))->m_WeightCenter).m_fX;
fSumOfWeightY = fSumOfWeightY + (pDoc->GetTriMesh(Index))->m_fArea * ((pDoc->GetTriMesh(Index))->m_WeightCenter).m_fY;
fSumOfWeightZ = fSumOfWeightZ + (pDoc->GetTriMesh(Index))->m_fArea * ((pDoc->GetTriMesh(Index))->m_WeightCenter).m_fZ;
break;
}
}
}
TriMeshesWeightCenterVertex.m_fX = fSumOfWeightX/fTotalArea;
TriMeshesWeightCenterVertex.m_fY = fSumOfWeightY/fTotalArea;
TriMeshesWeightCenterVertex.m_fZ = fSumOfWeightZ/fTotalArea;
float fLengthOfDPToWC; //The length of the detonation point to the weight center
float fLengthOfDetonation; //The half length of the detonation boundary
CString * pUnimportantMeshNameArray;
if(nUnimportantMeshesItems == 0)
{
//There is not exist any unimportant mesh
//so let's process this situation
pUnimportantMeshNameArray = NULL;
}
else
{
//There must exist some unimportant meshes
//so we should store them to an array
pUnimportantMeshNameArray = new CString [nUnimportantMeshesItems];
for(i = 0; i < nUnimportantMeshesItems; i++)
{
m_MeshesList1.GetText(i, pUnimportantMeshNameArray[i]);
}
}
CSimulateView *pSimulateView;
pSimulateView = (CSimulateView *)GetRenderView();
TCHAR szCurDir[260];
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
DWORD dwExitCode;
BOOL fSuccess;
FILE * ExeFile;
HANDLE hProcess;
//Get the current directory
GetCurrentDirectory(sizeof(szCurDir)/sizeof(TCHAR), szCurDir);
//Change the current directory to the start directory
SetCurrentDirectory((pSimulateView->GetDocument())->m_szStartDirectory);
//Create the directory for single missile simulation
float fAiv;
float fP;
float fP2;
float fSumOfAiv = 0.0;
float fSumOfP = 0.0;
float fSumOfP2 = 0.0;
FILE * OutPutFile;
FILE * OutPutFile1;
BOOL bIsOK = TRUE;
float fStepOfDistance;
float fDistance;
int nSphereCita;
int nSphereFi;
int nReturnCode;
fStepOfDistance = (m_fMaxDistanceAD - m_fMinDistanceAD)/m_nIntervalsAD;
for(i = 0; i <= m_nIntervalsAD; i++)
{
fDistance = m_fMinDistanceAD + i * fStepOfDistance;
sMsg.Format("final26_%d.dat", i + 1);
sMsg1.Format("touying01.din");
OutPutFile = fopen(sMsg, "wt");
fprintf(OutPutFile, "%f,\n", fDistance);
for(nSphereCita = 0; nSphereCita < 180; nSphereCita = nSphereCita + 45)
{
for(nSphereFi = 0; nSphereFi < 360; nSphereFi = nSphereFi + 45)
{
//Exclude the duplicate angles
if(nSphereCita != 0 && nSphereFi == 0)
continue;
if(nSphereCita != 0 && nSphereFi == 180)
continue;
fprintf(OutPutFile, "%d, %d,\n", nSphereCita, nSphereFi);
DetonationVertex.m_fX = TriMeshesWeightCenterVertex.m_fX +
fDistance *
sin(((float)nSphereFi) * 3.1415926535 / 180.) *
cos(((float)nSphereCita) * 3.1415926535 / 180.);
DetonationVertex.m_fY = TriMeshesWeightCenterVertex.m_fY +
fDistance *
sin(((float)nSphereFi) * 3.1415926535 / 180.) *
sin(((float)nSphereCita) * 3.1415926535 / 180.);
DetonationVertex.m_fZ = TriMeshesWeightCenterVertex.m_fZ +
fDistance *
cos(((float)nSphereFi) * 3.1415926535 / 180.);
if(EllipseSphere.bCheckVerexIn(DetonationVertex))
{
//if the detonation take place in the fighter
fprintf(OutPutFile, "-1.0 1.0 1.0\n");
continue;
}
fLengthOfDPToWC = fDistance;
fLengthOfDetonation = fLengthOfDPToWC * tan(fDetonationConeAngle/2.0);
//Produce the data file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -