⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulatedoc.cpp

📁 这是关于飞机生存力计算软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// SimulateDoc.cpp : implementation of the CSimulateDoc class
//

#include "stdafx.h"
#include "Simulate.h"

#include "SimulateDoc.h"
#include "SimulateView.h"

#include "ToolsPanel.h"
#include "MainFrm.h"
#include "NProbability.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <Math.h>
#include <assert.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSimulateDoc

IMPLEMENT_DYNCREATE(CSimulateDoc, CDocument)

BEGIN_MESSAGE_MAP(CSimulateDoc, CDocument)
	//{{AFX_MSG_MAP(CSimulateDoc)
	ON_COMMAND(ID_FILE_IMPORT, OnFileImport)
	ON_COMMAND(ID_FILE_RESET, OnFileReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimulateDoc construction/destruction

CSimulateDoc::CSimulateDoc()
{
	// TODO: add one-time construction code here
	m_fGlobalXMin = 100000000.;
	m_fGlobalYMin = 100000000.;
	m_fGlobalZMin = 100000000.;
	m_fGlobalXMax = -100000000.;
	m_fGlobalYMax = -100000000.;
	m_fGlobalZMax = -100000000.;

	//Get the start directory
	GetCurrentDirectory(sizeof(m_szStartDirectory)/sizeof(TCHAR), m_szStartDirectory);
}

CSimulateDoc::~CSimulateDoc()
{
	int Index = GetNumTriMeshes();
	while(Index--)
	{
		if(GetTriMesh(Index) != NULL)
			delete GetTriMesh(Index);
	}
}

BOOL CSimulateDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CSimulateDoc serialization

void CSimulateDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSimulateDoc diagnostics

#ifdef _DEBUG
void CSimulateDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSimulateDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSimulateDoc commands

void CTriMesh::Show(BOOL bSmooth)
{
	int i;
	float fV1_X, fV1_Y, fV1_Z;
	float fV2_X, fV2_Y, fV2_Z;

//----------------------------------------------------------------
//	glEnable(GL_BLEND);
//	glDepthMask(GL_FALSE);
//	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//----------------------------------------------------------------

	if(bSmooth == TRUE)
	{
		CVector *pVertexNormal;
		pVertexNormal = new CVector [m_nVertices];
		int *pCounter = new int [m_nVertices];
		CVector Vector;

		for(i=0; i < m_nVertices; i++)
			pCounter[i] = 0;
	
		for(i=0; i < m_nFaces; i++)
		{
			fV1_X = m_pVertexList[m_pTriangleList[i].m_nB].m_fX -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fX;
	
			fV1_Y = m_pVertexList[m_pTriangleList[i].m_nB].m_fY -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fY;

			fV1_Z = m_pVertexList[m_pTriangleList[i].m_nB].m_fZ -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fZ;

			fV2_X = m_pVertexList[m_pTriangleList[i].m_nC].m_fX -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fX;

			fV2_Y = m_pVertexList[m_pTriangleList[i].m_nC].m_fY -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fY;

			fV2_Z = m_pVertexList[m_pTriangleList[i].m_nC].m_fZ -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fZ;

			Vector.m_fX = fV1_Y * fV2_Z - fV2_Y * fV1_Z;
			Vector.m_fY = fV1_Z * fV2_X - fV2_Z * fV1_X;
			Vector.m_fZ = fV1_X * fV2_Y - fV2_X * fV1_Y;
		
			pCounter[m_pTriangleList[i].m_nA]++;
			pCounter[m_pTriangleList[i].m_nB]++;
			pCounter[m_pTriangleList[i].m_nC]++;

			(pVertexNormal[m_pTriangleList[i].m_nA]).m_fX += Vector.m_fX;
			(pVertexNormal[m_pTriangleList[i].m_nA]).m_fY += Vector.m_fY;
			(pVertexNormal[m_pTriangleList[i].m_nA]).m_fZ += Vector.m_fZ;

			(pVertexNormal[m_pTriangleList[i].m_nB]).m_fX += Vector.m_fX;
			(pVertexNormal[m_pTriangleList[i].m_nB]).m_fY += Vector.m_fY;
			(pVertexNormal[m_pTriangleList[i].m_nB]).m_fZ += Vector.m_fZ;

			(pVertexNormal[m_pTriangleList[i].m_nC]).m_fX += Vector.m_fX;
			(pVertexNormal[m_pTriangleList[i].m_nC]).m_fY += Vector.m_fY;
			(pVertexNormal[m_pTriangleList[i].m_nC]).m_fZ += Vector.m_fZ;

		}

		for(i=0; i < m_nVertices; i++)
		{
			(pVertexNormal[i]).m_fX = (pVertexNormal[i]).m_fX / pCounter[i];
			(pVertexNormal[i]).m_fY = (pVertexNormal[i]).m_fY / pCounter[i];
			(pVertexNormal[i]).m_fZ = (pVertexNormal[i]).m_fZ / pCounter[i];
		}

		glBegin(GL_TRIANGLES); 
		for(i=0; i < m_nFaces; i++)
		{
			glNormal3f((pVertexNormal[m_pTriangleList[i].m_nA]).m_fX,
						(pVertexNormal[m_pTriangleList[i].m_nA]).m_fY,
						(pVertexNormal[m_pTriangleList[i].m_nA]).m_fZ);
			
//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nA].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nA].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nA].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nA].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nA].m_fZ);					

			glNormal3f((pVertexNormal[m_pTriangleList[i].m_nB]).m_fX,
						(pVertexNormal[m_pTriangleList[i].m_nB]).m_fY,
						(pVertexNormal[m_pTriangleList[i].m_nB]).m_fZ);

//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nB].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nB].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nB].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nB].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nB].m_fZ);					

			glNormal3f((pVertexNormal[m_pTriangleList[i].m_nC]).m_fX,
						(pVertexNormal[m_pTriangleList[i].m_nC]).m_fY,
						(pVertexNormal[m_pTriangleList[i].m_nC]).m_fZ);

//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nC].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nC].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nC].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nC].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nC].m_fZ);					

		}
		glEnd();

		delete [] pVertexNormal;
		delete [] pCounter;
	}
	else
	{
		glBegin(GL_TRIANGLES); 
		for(i=0; i < m_nFaces; i++)
		{
			fV1_X = m_pVertexList[m_pTriangleList[i].m_nB].m_fX -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fX;

			fV1_Y = m_pVertexList[m_pTriangleList[i].m_nB].m_fY -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fY;

			fV1_Z = m_pVertexList[m_pTriangleList[i].m_nB].m_fZ -
					m_pVertexList[m_pTriangleList[i].m_nA].m_fZ;

			fV2_X = m_pVertexList[m_pTriangleList[i].m_nC].m_fX -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fX;

			fV2_Y = m_pVertexList[m_pTriangleList[i].m_nC].m_fY -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fY;

			fV2_Z = m_pVertexList[m_pTriangleList[i].m_nC].m_fZ -
					m_pVertexList[m_pTriangleList[i].m_nB].m_fZ;

			glNormal3f(fV1_Y * fV2_Z - fV2_Y * fV1_Z,
						fV1_Z * fV2_X - fV2_Z * fV1_X,
						fV1_X * fV2_Y - fV2_X * fV1_Y);

//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nA].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nA].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nA].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nA].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nA].m_fZ);					

//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nB].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nB].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nB].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nB].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nB].m_fZ);					

//			glTexCoord2f((m_pVertexList[m_pTriangleList[i].m_nC].m_fX - m_fXMin)/(m_fXMax - m_fXMin),
//						(m_pVertexList[m_pTriangleList[i].m_nC].m_fY - m_fYMin)/(m_fYMax - m_fYMin));

			glVertex3f(m_pVertexList[m_pTriangleList[i].m_nC].m_fX,
						m_pVertexList[m_pTriangleList[i].m_nC].m_fY,					
						m_pVertexList[m_pTriangleList[i].m_nC].m_fZ);					
		}
		glEnd();
	}

//----------------------------------------------------------------
//	glDepthMask(GL_TRUE);
//	glDisable(GL_BLEND);
//----------------------------------------------------------------

}

void CTriMesh::CalculateBasicParameter(void)
{
	int i;
	CTriangle Triangle;
	float fTotalArea, fTriangleArea, fSumOfWeightX, fSumOfWeightY, fSumOfWeightZ;
	CVertex TriangleWeightCenterVertex;

	fTotalArea = 0.0;
	fSumOfWeightX = 0.0;
	fSumOfWeightY = 0.0;
	fSumOfWeightZ = 0.0;

	for(i=0; i < m_nFaces; i++)
	{
		fTriangleArea = 0.0;
		Triangle.m_VertexA = m_pVertexList[m_pTriangleList[i].m_nA];
		Triangle.m_VertexB = m_pVertexList[m_pTriangleList[i].m_nB];
		Triangle.m_VertexC = m_pVertexList[m_pTriangleList[i].m_nC];
		Triangle.GetArea(fTriangleArea);
		Triangle.GetWeightCenter(TriangleWeightCenterVertex);
		fTotalArea = fTotalArea + fTriangleArea;
		fSumOfWeightX = fSumOfWeightX + fTriangleArea * TriangleWeightCenterVertex.m_fX;
		fSumOfWeightY = fSumOfWeightY + fTriangleArea * TriangleWeightCenterVertex.m_fY;
		fSumOfWeightZ = fSumOfWeightZ + fTriangleArea * TriangleWeightCenterVertex.m_fZ;
	}
	
	m_fArea = fTotalArea;
	m_WeightCenter.m_fX = fSumOfWeightX/m_fArea;
	m_WeightCenter.m_fY = fSumOfWeightY/m_fArea;
	m_WeightCenter.m_fZ = fSumOfWeightZ/m_fArea;
}


BOOL CSimulateDoc::AddTriMesh(CTriMesh * TriMesh)
{
	CToolsPanel * pToolsPanel;
	pToolsPanel = (CToolsPanel *)GetToolsPanel();

	int Index = GetNumTriMeshes();
	while(Index--)
	{
		if(GetTriMesh(Index)->m_sName == TriMesh->m_sName)
			return FALSE;
	}

	m_TriMeshArray.Add(TriMesh);
	TriMesh->CalculateBasicParameter();

	if(TriMesh->m_fXMin < m_fGlobalXMin)m_fGlobalXMin = TriMesh->m_fXMin;
	if(TriMesh->m_fYMin < m_fGlobalYMin)m_fGlobalYMin = TriMesh->m_fYMin;
	if(TriMesh->m_fZMin < m_fGlobalZMin)m_fGlobalZMin = TriMesh->m_fZMin;
	if(TriMesh->m_fXMax > m_fGlobalXMax)m_fGlobalXMax = TriMesh->m_fXMax;
	if(TriMesh->m_fYMax > m_fGlobalYMax)m_fGlobalYMax = TriMesh->m_fYMax;
	if(TriMesh->m_fZMax > m_fGlobalZMax)m_fGlobalZMax = TriMesh->m_fZMax;

	Index = (((pToolsPanel->m_pTabTools)->m_pTabtoolOpenGL)->m_MeshesList).AddString(TriMesh->m_sName);
	Index = (((pToolsPanel->m_pTabTools)->m_pTabtoolSimulate)->m_MeshesList).AddString(TriMesh->m_sName);
	Index = (((pToolsPanel->m_pTabTools)->m_pTabtoolSimulate)->m_MeshesList1).AddString(TriMesh->m_sName);
	return TRUE;

}

CTriMesh * CSimulateDoc::GetTriMesh(int Index)
{
	if(Index < 0 || Index > m_TriMeshArray.GetUpperBound())
		return 0;

	return m_TriMeshArray.GetAt(Index);
}

int CSimulateDoc::GetNumTriMeshes()
{
	return m_TriMeshArray.GetSize();

}

void CSimulateDoc::OnFileImport() 
{
	// TODO: Add your command handler code here
	CString sMsg;
	CString sMsgTemp;
	FILE *AscFile;
	char AscChars[100];
	char TempStr[100];
	int VertexNumber, FaceNumber;
	int Vertices, Faces, i;
	int apTemp, bpTemp, cpTemp;
	float XMin, YMin, ZMin;
	float XMax, YMax, ZMax;
	CTriMesh* TriMesh = NULL;
	int nSmoothingNumber;
	
	ZMin = YMin = XMin = 100000000.;	 
	ZMax = YMax = XMax = -100000000.;	 
	CString sFilter = 
		"asc files(*.asc)|*.asc||";

	CFileDialog dlgOpen(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
		(LPCTSTR)sFilter, AfxGetApp()->m_pMainWnd);

	//Display the dialog modally and
	//see if the user canceled the dialog
	if(IDCANCEL == dlgOpen.DoModal())
		return;

	//  Pass the Named Object line
	AscFile  = fopen((LPCTSTR)dlgOpen.GetPathName(), "rt" );
	
	//  Pass the null line
	fgets(AscChars, 100, AscFile);

    //  Get the Name of the Object
	fscanf( AscFile, "Named Object: %s\n", TempStr);
	sMsgTemp.Format("%s", TempStr);

	sMsg = sMsgTemp.Right(sMsgTemp.GetLength() - 1);
	sMsgTemp = sMsg;
	sMsg = sMsgTemp.Left(sMsgTemp.GetLength() - 1);

	// Read the vertices and faces
    fscanf( AscFile, "Tri-mesh, Vertices: %d Faces: %d\n", &Vertices, &Faces);
	TriMesh =  new CTriMesh(Vertices, Faces);
	TriMesh->m_sName = sMsg;

	TriMesh->m_nVertices = Vertices;
	TriMesh->m_nFaces = Faces;

	//  Pass the Vertex list line
	fgets(AscChars, 100, AscFile);

	//  Now it's time to read the vertices!
	float fXTemp, fYTemp, fZTemp;
	for(i=0; i < Vertices; i++)
	{
	    fscanf( AscFile, "Vertex %d: X: %f Y: %f Z: %f\n",
			&VertexNumber,
			&((TriMesh->m_pVertexList[i]).m_fX),
			&((TriMesh->m_pVertexList[i]).m_fY),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -