/
SubdivisionDoc.cpp
334 lines (256 loc) · 6.61 KB
/
SubdivisionDoc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SubdivisionDoc.cpp : implementation of the CSubdivisionDoc class
//
#include "stdafx.h"
#include "Subdivision.h"
#include "SubdivisionDoc.h"
#include ".\subdivisiondoc.h"
#include "MeshSubdivision.h"
#include "CreateSea.h"
#include "StopWatch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CSubdivisionDoc
IMPLEMENT_DYNCREATE(CSubdivisionDoc, CDocument)
BEGIN_MESSAGE_MAP(CSubdivisionDoc, CDocument)
ON_COMMAND(ID_IMPORT, OnImport)
ON_COMMAND(ID_EXPORT, OnExport)
ON_COMMAND(ID_SUBDIVISION_DOO, OnSubdivisionDoo)
ON_COMMAND(ID_SUBDIVISION_CATMULL, OnSubdivisionCatmull)
ON_UPDATE_COMMAND_UI(ID_SUBDIVISION_DOO, OnUpdateSubdivisionDoo)
ON_UPDATE_COMMAND_UI(ID_SUBDIVISION_CATMULL, OnUpdateSubdivisionCatmull)
ON_UPDATE_COMMAND_UI(ID_SEA, OnUpdateSea)////
ON_COMMAND(ID_OPERATION_NEW, &CSubdivisionDoc::OnOperationNew)
END_MESSAGE_MAP()
// CSubdivisionDoc construction/destruction
CSubdivisionDoc::CSubdivisionDoc()
{
// TODO: add one-time construction code here
m_pmesh = new Mesh3D;
}
CSubdivisionDoc::~CSubdivisionDoc()
{
if (m_pmesh) {
delete m_pmesh;
m_pmesh = NULL;
}
}
//BOOL CSubdivisionDoc::OnNewDocument()
//{
//
// if (!CDocument::OnNewDocument())
// return FALSE;
//
// // TODO: add reinitialization code here
// // (SDI documents will reuse this document)
//
// return TRUE;
//}
// CSubdivisionDoc serialization
void CSubdivisionDoc::Serialize(CArchive& ar)
{
//if (ar.IsStoring())
//{
// // TODO: add storing code here
//}
//else
//{
// // TODO: add loading code here
//}
}
// CSubdivisionDoc diagnostics
#ifdef _DEBUG
void CSubdivisionDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CSubdivisionDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CSubdivisionDoc commands
BOOL CSubdivisionDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
// get extension
//CString file = lpszPathName;
//CString extension = lpszPathName;
//extension = extension.Right(extension.GetLength()-extension.ReverseFind('.'));
//extension.MakeLower();
//CString path = lpszPathName;
//path = path.Left(path.ReverseFind('\\'));
//SetCurrentDirectory(path);
//if(extension == ".off")
//{
// std::ifstream stream(lpszPathName);
// if (stream.fail()) {
// return FALSE;
// }
//
// if (m_pmesh==NULL) {
// m_pmesh = new Mesh3D;
// }
//
// bool suc = m_pmesh->load_off(lpszPathName);
// stream.close();
// return suc == true;
//}
return FALSE;
}
BOOL CSubdivisionDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
if (m_pmesh==NULL || (!m_pmesh->isvalid())) {
return FALSE;
}
//// Extension-based checking
//CString file = lpszPathName;
//// Extension
//CString extension = lpszPathName;
//extension = extension.Right(extension.GetLength()-extension.ReverseFind('.'));
//extension.MakeLower();
//// Path "c:\path\file.wrl" -> c:\path
//CString path = lpszPathName;
//path = path.Left(path.ReverseFind('\\'));
//// Current path
//SetCurrentDirectory(path);
//
//if(extension == ".off")
//{
// // OFF extension
// std::ofstream stream(lpszPathName);
// if (stream.fail()) {
// return FALSE;
// }
// m_pmesh->write_off(stream);
// stream.close();
// return TRUE;
//}
return FALSE;
}
void CSubdivisionDoc::OnImport()
{
CString lpszFilter = _T("Obj files (*.obj)|*.obj|Off files (*.off)|*.off||");
CFileDialog dlg(true, NULL, NULL, OFN_READONLY, lpszFilter);
dlg.m_ofn.lpstrTitle = _T("Import 3D model"); ////
dlg.m_ofn.lpstrDefExt = _T("obj");
if(dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName();
// delete m_pmesh, m_pmesh = NULL;
if (m_pmesh==NULL) {
m_pmesh = new Mesh3D;
}
CString extension = strFile;
extension = extension.Right(extension.GetLength()-extension.ReverseFind('.'));
extension.MakeLower();
if (extension == ".off")
{
CStringA ss(strFile);
m_pmesh->load_off((LPCSTR)(ss));
}
else if (extension == ".obj")
{
//const char * ss = CString2constchar(strFile);
CStringA ss(strFile);
m_pmesh->load_obj((LPCSTR)(ss));
}
m_pmesh->update_mesh();
}
UpdateAllViews(NULL);
}
void CSubdivisionDoc::OnExport()
{
if(m_pmesh == NULL)
return;
// TODO: Add your command handler code here
CString lpszFilter = _T("Obj files (*.obj)|*.obj|Off files (*.off)|*.off||");
CFileDialog dlg(true, NULL, NULL, OFN_READONLY, lpszFilter);
dlg.m_ofn.lpstrTitle = _T("Export 3D model"); ////
dlg.m_ofn.lpstrDefExt = _T("obj");
if(dlg.DoModal() == IDOK && m_pmesh)
{
//CString strFile = dlg.GetPathName();
//std::ofstream fp (strFile);
//if (fp.fail()) {
// return;
//}
//m_pmesh->write_data(fp);
//fp.close();
CString strFile = dlg.GetPathName();
// delete m_pmesh, m_pmesh = NULL;
CString extension = strFile;
extension = extension.Right(extension.GetLength()-extension.ReverseFind('.'));
extension.MakeLower();
if (extension == ".off")
{
CStringA ss(strFile);
m_pmesh->write_off((LPCSTR)ss);
}
else if (extension == ".obj")
{
CStringA ss(strFile);
m_pmesh->write_obj((LPCSTR)ss);
}
}
}
void CSubdivisionDoc::OnSubdivisionDoo()
{
// TODO: Add your command handler code here
MeshSubdivision m_sub(m_pmesh);
m_pmesh = m_sub.Doo_Sabin();
UpdateAllViews(NULL);
}
void CSubdivisionDoc::OnSubdivisionCatmull()
{
// TODO: Add your command handler code here
MeshSubdivision m_sub(m_pmesh);
m_pmesh = m_sub.Catmull_Clark();
UpdateAllViews(NULL);
}
void CSubdivisionDoc::OnUpdateSubdivisionDoo(CCmdUI *pCmdUI)
{
// TODO: Add your command update UI handler code here
//pCmdUI->Enable(m_pmesh&&m_pmesh->isclosed() && m_pmesh->isvalid());
pCmdUI->Enable(m_pmesh && m_pmesh->isvalid());
//pCmdUI->Enable(m_pmesh&&m_pmesh->isvalid()&&m_pmesh->isclosed());
}
void CSubdivisionDoc::OnUpdateSubdivisionCatmull(CCmdUI *pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_pmesh&&m_pmesh->isvalid());
}
void CSubdivisionDoc::OnOperationNew()
{
// TODO: Add your command handler code here
MeshSubdivision m_sub(m_pmesh);
m_pmesh = m_sub.Doo_Sabin();
UpdateAllViews(NULL);
}
void CSubdivisionDoc::OnUpdateSea(CCmdUI *pCmdUI){
pCmdUI->Enable(m_pmesh&&m_pmesh->isvalid());
}
//void CSubdivisionView::OnTimer(UINT_PTR nIDEvent)
//{
// // TODO: 在此添加消息处理程序代码和/或调用默认值
// if (nIDEvent == 1){
// CSubdivisionDoc::loop = true;
// }
//
//
// CView::OnTimer(nIDEvent);
//}
// void CSubdivisionDoc::LoopDraw(){
// static CStopWatch Timer;
//
// delete m_pmesh;
// CopyTo(m_pmesh_origin, m_pmesh);
//
// CreateSea createSea(m_pmesh_origin, Timer.GetElapsedSeconds());
// m_pmesh = createSea.OnCreateSea();
//
// UpdateAllViews(NULL);
// }
//
// void CSubdivisionDoc::ThreadFunc(CSubdivisionDoc* p){
// p->LoopDraw();
// }