www.pudn.com > MakeVessel直.rar > MakeVesselDlg.cpp


// MakeVesselDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MakeVessel.h" 
#include "MakeVesselDlg.h" 
#include "ModelContext.h" 
#include "math.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
static struct Vertex vertex[VERTEX_MAX]; 
static struct Facet facet[FACET_MAX]; 
 
int totalvertex; 
int circlenum; 
int arcnum; 
int totalnormal; 
int totalsurface; 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMakeVesselDlg dialog 
 
CMakeVesselDlg::CMakeVesselDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMakeVesselDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMakeVesselDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMakeVesselDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMakeVesselDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMakeVesselDlg, CDialog) 
	//{{AFX_MSG_MAP(CMakeVesselDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_MakeVessel, OnMakeVessel) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMakeVesselDlg message handlers 
 
BOOL CMakeVesselDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CMakeVesselDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CMakeVesselDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CMakeVesselDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMakeVesselDlg::OnMakeVessel()  
{ 
	// TODO: Add your control notification handler code here 
	FILE *fptr; 
 
	DrawColumn(0.5f, 2.0f, 5.0f); 
	fptr = fopen("test.tri","w"); 
	Write2file(fptr); 
	fclose(fptr); 
	AfxMessageBox("ok"); 
	 
	 
} 
 
//////////////////////////////////////////////// 
//画圆柱形部分 
void CMakeVesselDlg::DrawColumn(float radius, float length, float columntop) 
{ 
	float z; 
	float pai = 3.1415926f; 
 
	int i = 0; 
 
 
/////////////////////////////////////////////// 
////生成顶点 
	circlenum = 0; 
	for(z = columntop; z >= columntop-length; z -= 0.1f) 
	{ 
		arcnum = 0; 
		for(float u = 0.0f; u <= pai * 2 ; u += 0.15707963f) 
		{ 
			vertex[i].x = radius * sin(u); 
		    vertex[i].y = radius * cos(u); 
			vertex[i].z = z; 
			vertex[i].nx = -sin(u); 
			vertex[i].ny = -cos(u); 
			vertex[i].nz = 0; 
			vertex[i].angleIndex = arcnum; 
			i++; 
			arcnum++; 
 
		} 
		circlenum++; 
	} 
//////////////////////////////////////////////////////////// 
	//统计 
	totalvertex = i; 
	totalnormal = totalvertex; 
 
/////////////////////////////////////////////////////////// 
//生成面 
	i = 0; 
	int j = 0; 
	for(int m = 0; m < circlenum -1; m++) 
	{ 
 
		for(int n = 0 ; n < arcnum -1; n++) 
		{ 
			facet[i].a = j; 
			facet[i].b = j + 1; 
			facet[i].c = j + arcnum; 
			facet[i+1].a = j + 1; 
			facet[i+1].b = j + arcnum; 
			facet[i+1].c = j + arcnum + 1;  
			j++; 
			i += 2;			 
		} 
		facet[i].a = j; 
		facet[i].b = j + 1 - 40; 
		facet[i].c = j + arcnum; 
		facet[i+1].a = j + 1 - 40; 
		facet[i+1].b = j + arcnum; 
		facet[i+1].c = j + arcnum + 1 -40; 
		i +=2; 
		j++; 
	} 
	totalsurface = i ; 
 
 
 
} 
 
void CMakeVesselDlg::Write2file(FILE *file) 
{ 
	int i; 
 
	fprintf(file, "V %d\n", totalvertex); 
 
	 
	 
	for(i = 0; i < totalvertex; i++) 
	{ 
		 fprintf(file, "%.4f %.4f %.4f\n",  
		 vertex[i].x,  
		 vertex[i].y,  
		 vertex[i].z); 
	} 
	 
 
	fprintf(file, "N %d\n", totalnormal); 
	for(i = 0; i < totalnormal; i++) 
	{ 
		fprintf(file, "%.2f %.2f %.2f\n", 
			vertex[i].nx, 
			vertex[i].ny, 
			vertex[i].nz); 
	} 
	fprintf(file, "F %d\n", totalsurface); 
	for(i = 0; i < totalsurface; i++) 
	{ 
		fprintf(file, "%d %d %d\n", 
		    facet[i].a, 
    		facet[i].b, 
    		facet[i].c); 
					 
	} 
	 
}