7fd59977 |
1 | // TriangulationDoc.cpp : implementation of the CTriangulationDoc class |
2 | // |
3 | |
4 | #include "stdafx.h" |
5 | |
6 | #include "TriangulationDoc.h" |
7 | |
8 | #include "TriangulationApp.h" |
9 | |
10 | #include "OCCDemo_Presentation.h" |
11 | |
12 | #include <OCC_3dView.h> |
13 | |
14 | #include "..\res\resource.h" |
15 | |
16 | #include <AIS_ListOfInteractive.hxx> |
17 | #include <AIS_ListIteratorOfListOfInteractive.hxx> |
18 | |
19 | ///////////////////////////////////////////////////////////////////////////// |
20 | // CTriangulationDoc |
21 | |
22 | IMPLEMENT_DYNCREATE(CTriangulationDoc, CDocument) |
23 | |
24 | BEGIN_MESSAGE_MAP(CTriangulationDoc, OCC_3dBaseDoc) |
25 | //{{AFX_MSG_MAP(CTriangulationDoc) |
26 | ON_COMMAND(ID_TRIANGU, OnTriangu) |
27 | ON_COMMAND(ID_Clear, OnClear) |
28 | ON_COMMAND(ID_Visu, OnVisu) |
29 | ON_COMMAND(ID_BUTTONNext, OnBUTTONNext) |
30 | ON_COMMAND(ID_BUTTONStart, OnBUTTONStart) |
31 | ON_COMMAND(ID_BUTTONRepeat, OnBUTTONRepeat) |
32 | ON_COMMAND(ID_BUTTONPrev, OnBUTTONPrev) |
33 | ON_COMMAND(ID_BUTTONEnd, OnBUTTONEnd) |
34 | ON_UPDATE_COMMAND_UI(ID_BUTTONNext, OnUpdateBUTTONNext) |
35 | ON_UPDATE_COMMAND_UI(ID_BUTTONPrev, OnUpdateBUTTONPrev) |
36 | ON_COMMAND(ID_FILE_NEW, OnFileNew) |
37 | ON_COMMAND(ID_DUMP_VIEW, OnDumpView) |
38 | //}}AFX_MSG_MAP |
39 | END_MESSAGE_MAP() |
40 | |
41 | ///////////////////////////////////////////////////////////////////////////// |
42 | // CTriangulationDoc construction/destruction |
43 | |
44 | CTriangulationDoc::CTriangulationDoc() |
45 | { |
46 | myPresentation = OCCDemo_Presentation::Current; |
47 | myPresentation->SetDocument(this); |
48 | |
49 | strcpy_s(myDataDir, "Data"); |
50 | strcpy_s(myLastPath, "."); |
51 | } |
52 | |
53 | CTriangulationDoc::~CTriangulationDoc() |
54 | { |
55 | } |
56 | |
57 | ///////////////////////////////////////////////////////////////////////////// |
58 | // CTriangulationDoc diagnostics |
59 | |
60 | #ifdef _DEBUG |
61 | void CTriangulationDoc::AssertValid() const |
62 | { |
63 | CDocument::AssertValid(); |
64 | } |
65 | |
66 | void CTriangulationDoc::Dump(CDumpContext& dc) const |
67 | { |
68 | CDocument::Dump(dc); |
69 | } |
70 | #endif //_DEBUG |
71 | |
72 | void CTriangulationDoc::OnTriangu() |
73 | { |
74 | AIS_ListOfInteractive aList; |
75 | myAISContext->DisplayedObjects(aList); |
76 | AIS_ListIteratorOfListOfInteractive aListIterator; |
77 | for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){ |
78 | myAISContext->Remove(aListIterator.Value()); |
79 | } |
80 | |
81 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); |
82 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); |
83 | TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); |
84 | BRepMesh::Mesh(ShapeFused,1); |
85 | |
86 | Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused); |
87 | myAISContext->SetDisplayMode(aSection,1); |
88 | myAISContext->SetColor(aSection,Quantity_NOC_RED); |
89 | myAISContext->SetMaterial(aSection,Graphic3d_NOM_GOLD); |
90 | myAISContext->Display(aSection); |
91 | |
92 | Standard_Integer result(0); |
93 | |
94 | for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { |
95 | TopoDS_Face F =TopoDS::Face(ex.Current()); |
96 | TopLoc_Location L; |
97 | Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); |
98 | result = result + facing->NbTriangles(); |
99 | } |
100 | Fit(); |
101 | |
102 | TCollection_AsciiString Message ("\ |
103 | \n\ |
104 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\ |
105 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\ |
106 | \n\ |
107 | TopoDS_Shape ShapeFused = BRepBuilderAPI_Fuse(theSphere,theBox); \n\ |
108 | \n\ |
109 | BRepMesh::Mesh(ShapeFused,1); \n\ |
110 | \n\ |
111 | Standard_Integer result(0); \n\ |
112 | \n\ |
113 | for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { \n\ |
114 | TopoDS_Face F =TopoDS::Face(ex.Current()); \n\ |
115 | TopLoc_Location L; \n\ |
116 | Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); \n\ |
117 | result = result + facing->NbTriangles(); \n\ |
118 | } \n\ |
119 | \n\ |
120 | --- Number of created triangles ---\n"); |
121 | TCollection_AsciiString nombre(result); |
122 | Message += nombre; |
123 | Message +=("\ |
124 | \n\ |
125 | \n"); |
126 | PocessTextInDialog("Compute the triangulation on a shape", Message); |
127 | } |
128 | |
129 | void CTriangulationDoc::OnVisu() |
130 | { |
131 | |
132 | AIS_ListOfInteractive aList; |
133 | myAISContext->DisplayedObjects(aList); |
134 | AIS_ListIteratorOfListOfInteractive aListIterator; |
135 | for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){ |
136 | myAISContext->Remove(aListIterator.Value()); |
137 | } |
138 | |
139 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); |
140 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); |
141 | TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); |
142 | BRepMesh::Mesh(ShapeFused,1); |
143 | |
144 | Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused); |
145 | myAISContext->SetDisplayMode(aSection,1); |
146 | myAISContext->SetColor(aSection,Quantity_NOC_RED); |
147 | myAISContext->SetMaterial(aSection,Graphic3d_NOM_GOLD); |
148 | myAISContext->SetTransparency(aSection,0.1); |
149 | myAISContext->Display(aSection); |
150 | |
151 | BRep_Builder builder; |
152 | TopoDS_Compound Comp; |
153 | builder.MakeCompound(Comp); |
154 | |
155 | for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { |
156 | |
157 | TopoDS_Face F =TopoDS::Face(ex.Current()); |
158 | TopLoc_Location L; |
159 | Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); |
160 | TColgp_Array1OfPnt tab(1,(facing->NbNodes())); |
161 | tab = facing->Nodes(); |
162 | Poly_Array1OfTriangle tri(1,facing->NbTriangles()); |
163 | tri = facing->Triangles(); |
164 | |
165 | for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) { |
166 | Poly_Triangle trian = tri.Value(i); |
167 | Standard_Integer index1,index2,index3,M,N; |
168 | trian.Get(index1,index2,index3); |
169 | |
170 | for (Standard_Integer j=1;j<=3;j++) { |
171 | switch (j) { |
172 | case 1 : |
173 | M = index1; |
174 | N = index2; |
175 | break; |
176 | case 2 : |
177 | N = index3; |
178 | break; |
179 | case 3 : |
180 | M = index2; |
181 | } |
182 | |
183 | BRepBuilderAPI_MakeEdge ME(tab.Value(M),tab.Value(N)); |
184 | if (ME.IsDone()) { |
185 | builder.Add(Comp,ME.Edge()); |
186 | } |
187 | } |
188 | } |
189 | } |
190 | Handle (AIS_Shape) atriangulation = new AIS_Shape(Comp); |
191 | myAISContext->SetDisplayMode(atriangulation,0); |
192 | myAISContext->SetColor(atriangulation,Quantity_NOC_WHITE); |
193 | myAISContext->Display(atriangulation); |
194 | |
195 | Fit(); |
196 | |
197 | TCollection_AsciiString Message ("\ |
198 | \n\ |
199 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\ |
200 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\ |
201 | TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\ |
202 | BRepMesh::Mesh(ShapeFused,1); \n\ |
203 | \n\ |
204 | BRep_Builder builder; \n\ |
205 | TopoDS_Compound Comp; \n\ |
206 | builder.MakeCompound(Comp); \n\ |
207 | \n\ |
208 | for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { \n\ |
209 | \n\ |
210 | TopoDS_Face F =TopoDS::Face(ex.Current()); \n\ |
211 | TopLoc_Location L; \n\ |
212 | Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); \n\ |
213 | TColgp_Array1OfPnt tab(1,(facing->NbNodes())); \n\ |
214 | tab = facing->Nodes(); \n\ |
215 | Poly_Array1OfTriangle tri(1,facing->NbTriangles()); \n\ |
216 | tri = facing->Triangles(); \n\ |
217 | \n\ |
218 | for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) { \n\ |
219 | Poly_Triangle trian = tri.Value(i); \n\ |
220 | Standard_Integer index1,index2,index3,M,N; \n\ |
221 | trian.Get(index1,index2,index3); \n\ |
222 | \n\ |
223 | for (Standard_Integer j=1;j<=3;j++) { \n\ |
224 | switch (j) { \n\ |
225 | case 1 : \n\ |
226 | M = index1; \n\ |
227 | N = index2; \n\ |
228 | break; \n\ |
229 | case 2 : \n\ |
230 | N = index3; \n\ |
231 | break; \n\ |
232 | case 3 : \n\ |
233 | M = index2; \n\ |
234 | } \n\ |
235 | \n\ |
236 | BRepBuilderAPI_MakeEdge ME(tab.Value(M),tab.Value(N)); \n\ |
237 | if (ME.IsDone()) { \n\ |
238 | builder.Add(Comp,ME.Edge()); \n\ |
239 | } \n\ |
240 | } \n\ |
241 | } \n\ |
242 | } \n\ |
243 | \n\ |
244 | Warning : The visualisation of the mesh is not optimised.\n\ |
245 | The shared edges between triangles are dispayed twice.\n\ |
246 | The purpose here is only to show how to decode the data structure of triangulation.\n\ |
247 | \n"); |
248 | PocessTextInDialog("Visualize the triangulation on a shape", Message); |
249 | |
250 | } |
251 | |
252 | |
253 | |
254 | |
255 | void CTriangulationDoc::OnClear() |
256 | { |
257 | AIS_ListOfInteractive aList; |
258 | myAISContext->DisplayedObjects(aList); |
259 | AIS_ListIteratorOfListOfInteractive aListIterator; |
260 | for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){ |
261 | myAISContext->Remove(aListIterator.Value()); |
262 | } |
263 | |
264 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); |
265 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); |
266 | TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); |
267 | BRepMesh::Mesh(ShapeFused,1); |
268 | |
269 | |
270 | Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused); |
271 | myAISContext->SetDisplayMode(aSection,1); |
272 | myAISContext->SetColor(aSection,Quantity_NOC_RED); |
273 | myAISContext->SetMaterial(aSection,Graphic3d_NOM_GOLD); |
274 | myAISContext->Display(aSection); |
275 | |
276 | BRepTools::Clean(ShapeFused); |
277 | |
278 | TCollection_AsciiString test; |
279 | if (!BRepTools::Triangulation(ShapeFused,1)) { |
280 | test = ("In fact the triangulation has been removed\n"); |
281 | } |
282 | |
283 | Fit(); |
284 | TCollection_AsciiString Message ("\ |
285 | \n\ |
286 | TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\ |
287 | TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\ |
288 | TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\ |
289 | BRepMesh::Mesh(ShapeFused,1); \n\ |
290 | \n\ |
291 | BRepTools::Clean(ShapeFused); \n\ |
292 | \n\ |
293 | if (!BRepTools::Triangulation(ShapeFused,1)) { \n\ |
294 | TCollection_AsciiString test(<In fact the triangulation has been removed>); \n\ |
295 | } \n\ |
296 | \n\ |
297 | --- Result ---\n"); |
298 | |
299 | Message += test; |
300 | Message +=("\ |
301 | \n\ |
302 | \n"); |
303 | |
304 | PocessTextInDialog("Remove the triangulation", Message); |
305 | |
306 | } |
307 | |
308 | /******************************************************************************* |
309 | ********************* T E S S E L A T E ************************************** |
310 | *******************************************************************************/ |
311 | |
312 | void CTriangulationDoc::Start() |
313 | { |
314 | myPresentation->Init(); |
315 | OnBUTTONStart(); |
316 | } |
317 | |
318 | void CTriangulationDoc::OnFileNew() |
319 | { |
320 | OnNewDocument(); |
321 | Start(); |
322 | } |
323 | |
324 | void CTriangulationDoc::InitViewButtons() |
325 | { |
326 | POSITION pos = GetFirstViewPosition(); |
327 | /* LLS |
328 | while (pos != NULL) |
329 | { |
330 | COCCDemoView* pView = (COCCDemoView*) GetNextView(pos); |
331 | pView->InitButtons(); |
332 | } |
333 | */ |
334 | } |
335 | |
336 | void CTriangulationDoc::DoSample() |
337 | { |
338 | InitViewButtons(); |
339 | |
340 | HCURSOR hOldCursor = ::GetCursor(); |
341 | HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING); |
342 | |
343 | SetCursor(hNewCursor); |
344 | { |
345 | try |
346 | { |
347 | myPresentation->DoSample(); |
348 | } |
349 | catch (Standard_Failure) |
350 | { |
351 | Standard_SStream aSStream; |
352 | aSStream << "An exception was caught: " << Standard_Failure::Caught() << ends; |
353 | Standard_CString aMsg = aSStream.str().c_str(); |
354 | // aSStream.rdbuf()->freeze(0); // allow deletion of dynamic array |
355 | AfxMessageBox (aMsg); |
356 | } |
357 | } |
358 | SetCursor(hOldCursor); |
359 | } |
360 | |
361 | void CTriangulationDoc::OnBUTTONStart() |
362 | { |
eb4320f2 |
363 | myAISContext->EraseAll(); |
7fd59977 |
364 | myPresentation->FirstSample(); |
365 | DoSample(); |
366 | } |
367 | |
368 | void CTriangulationDoc::OnBUTTONEnd() |
369 | { |
eb4320f2 |
370 | myAISContext->EraseAll(); |
7fd59977 |
371 | myPresentation->LastSample(); |
372 | DoSample(); |
373 | } |
374 | |
375 | void CTriangulationDoc::OnBUTTONRepeat() |
376 | { |
377 | DoSample(); |
378 | } |
379 | |
380 | void CTriangulationDoc::OnBUTTONNext() |
381 | { |
382 | if (!myPresentation->AtLastSample()) |
383 | { |
384 | myPresentation->NextSample(); |
385 | DoSample(); |
386 | } |
387 | } |
388 | |
389 | void CTriangulationDoc::OnBUTTONPrev() |
390 | { |
391 | if (!myPresentation->AtFirstSample()) |
392 | { |
393 | myPresentation->PrevSample(); |
394 | DoSample(); |
395 | } |
396 | } |
397 | |
398 | void CTriangulationDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) |
399 | { |
400 | pCmdUI->Enable (!myPresentation->AtLastSample()); |
401 | } |
402 | |
403 | void CTriangulationDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) |
404 | { |
405 | pCmdUI->Enable (!myPresentation->AtFirstSample()); |
406 | } |
407 | |
408 | void CTriangulationDoc::OnDumpView() |
409 | { |
5c1f974e |
410 | CFileDialog aDlg (false, "gif", "OCCView.gif", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, |
411 | "GIF Files (*.GIF)|*.gif|" |
412 | "BMP Files (*.BMP)|*.bmp|" |
413 | "PNG Files (*.PNG)|*.png|" |
414 | "JPEG Files (*.JPEG)|*.jpeg|" |
415 | "PPM Files (*.PPM)|*.ppm|" |
416 | "TIFF Files (*.TIFF)|*.tiff|" |
417 | "TGA Files (*.TGA)|*.tga|" |
418 | "EXR Files (*.EXR)|*.exr||", NULL); |
419 | if (aDlg.DoModal() != IDOK) |
7fd59977 |
420 | { |
5c1f974e |
421 | return; |
422 | } |
7fd59977 |
423 | |
5c1f974e |
424 | for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;) |
425 | { |
426 | OCC_3dView* pView = (OCC_3dView* )GetNextView (aPos); |
427 | pView->UpdateWindow(); |
7fd59977 |
428 | } |
5c1f974e |
429 | myViewer->InitActiveViews(); |
430 | Handle(V3d_View) aView = myViewer->ActiveView(); |
431 | aView->Dump (aDlg.GetPathName()); |
7fd59977 |
432 | } |
433 | |
434 | void CTriangulationDoc::Fit() |
435 | { |
436 | CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; |
437 | CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame(); |
438 | OCC_3dView *pView = (OCC_3dView*)pChild->GetActiveView(); |
439 | pView->FitAll(); |
440 | } |