0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / samples / mfc / standard / 07_Triangulation / src / Tesselate_Presentation.cpp
1 // Tesselate_Presentation.cpp: implementation of the Tesselate_Presentation class.
2 // Tesselate shapes.
3 ////////////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "Tesselate_Presentation.h" 
7 #include "TriangulationApp.h" 
8 #include "TriangulationDoc.h" 
9
10 #include <Precision.hxx>
11 #include <TopoDS.hxx>
12 #include <TopoDS_Edge.hxx>
13 #include <TopoDS_Face.hxx>
14 #include <TopoDS_Compound.hxx>
15
16 #include <BRep_Builder.hxx>
17 #include <BRepTools.hxx>
18 #include <BRep_Tool.hxx>
19 #include <BRepBuilderAPI_MakeEdge.hxx>
20 #include <BRepBuilderAPI_MakeVertex.hxx>
21 #include <BRepMesh_IncrementalMesh.hxx>
22
23 #include <TopExp_Explorer.hxx>
24 #include <TopLoc_Location.hxx>
25 #include <TopTools_DataMapOfIntegerShape.hxx>
26 #include <TopTools_SequenceOfShape.hxx>
27
28 #include <Poly_Triangulation.hxx>
29 #include <Poly_PolygonOnTriangulation.hxx>
30 #include <Poly_Array1OfTriangle.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <TColStd_Array1OfInteger.hxx>
33
34 #include <gp_Pnt.hxx>
35
36 #define EOL "\r\n"
37
38 // Initialization of global variable with an instance of this class
39 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Tesselate_Presentation;
40
41 // Initialization of array of samples
42 Standard_CString Tesselate_Presentation::myFileNames[] =
43 {
44   "wedge_ok.brep",
45   "shell1.brep",
46   "Pump_Nut.brep",
47   "Pump_TopCover.brep",
48   0
49 };
50
51 //////////////////////////////////////////////////////////////////////
52 // Construction/Destruction
53 //////////////////////////////////////////////////////////////////////
54
55 Tesselate_Presentation::Tesselate_Presentation()
56 {
57   for (myNbSamples = 0; myFileNames[myNbSamples]; myNbSamples++);
58   setName ("Tesselate shapes");
59 }
60
61 //////////////////////////////////////////////////////////////////////
62 // Sample execution
63 //////////////////////////////////////////////////////////////////////
64
65 void Tesselate_Presentation::DoSample()
66 {
67         ((CTriangulationApp*) AfxGetApp())->SetSampleName (L"Tesselate");
68         ((CTriangulationApp*) AfxGetApp())->SetSamplePath (L"");
69         getAISContext()->EraseAll();
70         if (myIndex >=0 && myIndex < myNbSamples)
71     sample (myFileNames[myIndex]);
72 }
73
74 //////////////////////////////////////////////////////////////////////
75 // Sample functions
76 //////////////////////////////////////////////////////////////////////
77 //================================================================
78
79 inline Standard_Integer _key(Standard_Integer n1,Standard_Integer n2)
80 {
81
82   Standard_Integer key = 
83     (n2>n1)?(n1<<16)+n2:(n2<<16)+n1;
84   return key;
85 }
86
87 //DATA : [myIndex][{Deflection,NumberOfFace,NumberOfEdge}]
88 static const Standard_Real DATA [][3] = 
89 {
90   {0.2,1,2},{0.5,6,2},{0.7,16,2},{1,1,2}
91 };
92
93
94 void Tesselate_Presentation::tesselateShape(const TopoDS_Shape& aShape)
95 {
96 //  setResultTitle("Tesselate shape");
97   TCollection_AsciiString aText = (
98     "/////////////////////////////////////////////////////////////////" EOL
99     "// Tesselate shape." EOL
100     "/////////////////////////////////////////////////////////////////" EOL EOL
101     ) ;
102
103   Standard_Real aDeflection = DATA[myIndex][0];
104   Standard_Integer aNumOfFace = (Standard_Integer)DATA[myIndex][1];
105   Standard_Integer aNumOfEdge = (Standard_Integer)DATA[myIndex][2];
106
107   aText +=
108     "Standard_Real aDeflection;" EOL
109     "// aDeflection = ... ;" EOL EOL
110
111     "// removes all the triangulations of the faces ," EOL
112     "//and all the polygons on the triangulations of the edges:" EOL
113     "BRepTools::Clean(aShape);" EOL EOL
114
115     "// adds a triangulation of the shape aShape with the deflection aDeflection:" EOL
116     "BRepMesh::Mesh(aShape,aDeflection);" EOL EOL
117
118     "TopExp_Explorer aExpFace,aExpEdge;" EOL
119     "for(aExpFace.Init(aShape,TopAbs_FACE);aExpFace.More();aExpFace.Next())" EOL
120     "{  " EOL
121     "  TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());" EOL
122     "  TopLoc_Location aLocation;" EOL EOL
123     
124     "  // takes the triangulation of the face aFace:" EOL
125     "  Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);" EOL EOL
126     
127     "  if(!aTr.IsNull()) // if this triangulation is not NULL" EOL
128     "  { " EOL
129     "    // takes the array of nodes for this triangulation:" EOL
130     "    const TColgp_Array1OfPnt& aNodes = aTr->Nodes();" EOL
131     "    // takes the array of triangles for this triangulation:" EOL
132     "    const Poly_Array1OfTriangle& triangles = aTr->Triangles();" EOL EOL
133     
134     "    // create array of node points in absolute coordinate system" EOL
135     "    TColgp_Array1OfPnt aPoints(1, aNodes.Length());" EOL
136     "    for( Standard_Integer i = 1; i < aNodes.Length()+1; i++)" EOL
137     "      aPoints(i) = aNodes(i).Transformed(aLocation);" EOL EOL
138
139     "    // Takes the node points of each triangle of this triangulation." EOL
140     "    // takes a number of triangles:" EOL
141     "    Standard_Integer nnn = aTr->NbTriangles();" EOL
142     "    Standard_Integer nt,n1,n2,n3;" EOL
143     "    for( nt = 1 ; nt < nnn+1 ; nt++)" EOL
144     "    {" EOL
145     "      // takes the node indices of each triangle in n1,n2,n3:" EOL
146     "      triangles(nt).Get(n1,n2,n3);" EOL
147     "      // takes the node points:" EOL
148     "      gp_Pnt aPnt1 = aPoints(n1);" EOL
149     "      gp_Pnt aPnt2 = aPoints(n2);" EOL
150     "      gp_Pnt aPnt3 = aPoints(n3);" EOL
151     "    } " EOL EOL
152     
153     "    // Takes the polygon associated to an edge." EOL
154     "    aExpEdge.Init(aFace,TopAbs_EDGE);" EOL
155     "    TopoDS_Edge aEdge;" EOL
156     "    // for example,working with the first edge:" EOL
157     "    if(aExpEdge.More())" EOL
158     "      aEdge = TopoDS::Edge(aExpEdge.Current());" EOL EOL
159     
160     "    if(!aEdge.IsNull()) // if this edge is not NULL" EOL
161     "    {" EOL
162     "      // takes the polygon associated to the edge aEdge:" EOL
163     "      Handle(Poly_PolygonOnTriangulation) aPol = " EOL
164     "        BRep_Tool::PolygonOnTriangulation(aEdge,aTr,aEdge.Location());" EOL EOL
165     
166     "      if(!aPol.IsNull()) // if this polygon is not NULL" EOL
167     "        // takes the array of nodes for this polygon" EOL
168     "        // (indexes in the array of nodes for triangulation of theFace):" EOL
169     "        const TColStd_Array1OfInteger& aNodesOfPol = aPol->Nodes();" EOL
170     "    }" EOL
171     "  }" EOL
172     "}" EOL EOL
173     
174     "//==================================================" EOL EOL
175     
176       ;
177    aText += "  Result with deflection = ";
178    aText += TCollection_AsciiString(aDeflection);
179    aText += " :" EOL;
180
181    GetDocument()->PocessTextInDialog("Compute the triangulation on a shape", aText);
182 //   setResultText(aText.ToCString());  
183
184 //==========================================================================
185
186   BRepTools::Clean(aShape);
187   BRepMesh_IncrementalMesh(aShape,aDeflection);
188
189   BRep_Builder aBuilder,aBuild1,aBuild2;
190   TopoDS_Compound aCompound,aComp1,aComp2;
191   aBuilder.MakeCompound(aCompound);
192   aBuild1.MakeCompound(aComp1);
193   aBuild2.MakeCompound(aComp2);
194
195   TopTools_SequenceOfShape aVertices;
196   Standard_Integer aCount = 0;
197   Standard_Integer aNumOfNodes = 0;
198   Standard_Integer aNumOfTriangles = 0;
199   
200   Handle(AIS_InteractiveObject) aShowEdge,aShowFace,aShowShape;
201   
202   TopExp_Explorer aExpFace,aExpEdge;
203
204   for(aExpFace.Init(aShape,TopAbs_FACE);aExpFace.More();aExpFace.Next())
205   {  
206     aCount++;
207   
208     TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
209     TopLoc_Location aLocation;
210
211     Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);
212
213     if(!aTr.IsNull())
214     { 
215       const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
216       aNumOfNodes += aTr->NbNodes();
217       //Standard_Integer aLower = aNodes.Lower();
218       //Standard_Integer anUpper = aNodes.Upper();
219       const Poly_Array1OfTriangle& triangles = aTr->Triangles();
220       aNumOfTriangles += aTr->NbTriangles();
221
222       if(aCount == aNumOfFace)
223       {
224         Standard_Integer aNbOfNodesOfFace = aTr->NbNodes();
225         Standard_Integer aNbOfTrianglesOfFace = aTr->NbTriangles();
226         aExpEdge.Init(aFace,TopAbs_EDGE);
227
228         TopoDS_Edge aEdge;
229
230         for( Standard_Integer i = 0; aExpEdge.More() && i < aNumOfEdge ; aExpEdge.Next(), i++)
231           aEdge = TopoDS::Edge(aExpEdge.Current());
232
233         if(!aEdge.IsNull())
234         {
235           Handle(Poly_PolygonOnTriangulation) aPol = 
236             BRep_Tool::PolygonOnTriangulation(aEdge,aTr,aEdge.Location());
237
238           if(!aPol.IsNull())
239           {
240             const TColStd_Array1OfInteger& aNodesOfPol = aPol->Nodes();
241             Standard_Integer aNbOfNodesOfEdge = aPol->NbNodes();
242
243             aText += "Number of nodes of the edge = ";
244             aText += TCollection_AsciiString(aNbOfNodesOfEdge) + EOL;
245             aText += "Number of nodes of the face = ";
246             aText += TCollection_AsciiString(aNbOfNodesOfFace) + EOL;
247             aText += "Number of triangles of the face = ";
248             aText += TCollection_AsciiString(aNbOfTrianglesOfFace) + EOL;
249                         GetDocument()->PocessTextInDialog("Compute the triangulation on a shape", aText);
250 //            setResultText(aText.ToCString());  
251
252             Standard_Integer aLower = aNodesOfPol.Lower(), anUpper = aNodesOfPol.Upper();
253             for( int i = aLower; i < anUpper ; i++)
254             {
255               gp_Pnt aPnt1 = aNodes(aNodesOfPol(i)).Transformed(aLocation);
256               gp_Pnt aPnt2 = aNodes(aNodesOfPol(i+1)).Transformed(aLocation);
257               TopoDS_Vertex aVertex1 = BRepBuilderAPI_MakeVertex (aPnt1);
258               TopoDS_Vertex aVertex2 = BRepBuilderAPI_MakeVertex (aPnt2);
259
260               if(!aVertex1.IsNull() && !aVertex2.IsNull() && // if vertices are "alive"
261                 !BRep_Tool::Pnt(aVertex1).IsEqual(
262                 BRep_Tool::Pnt(aVertex2),Precision::Confusion())) // if they are different
263               {
264                 aEdge = BRepBuilderAPI_MakeEdge (aVertex1,aVertex2);
265                 aBuild2.Add(aComp2,aVertex1);
266                 if(!aEdge.IsNull())
267                   aBuild2.Add(aComp2,aEdge);
268                 if(i == anUpper-1)
269                   aBuild2.Add(aComp2,aVertex2);
270               }
271             }
272       
273             getAISContext()->EraseAll();
274             aShowShape = drawShape(aShape);
275             if(WAIT_A_SECOND) return;
276             aShowEdge = drawShape(aComp2,Quantity_NOC_GREEN);
277             getAISContext()->Erase(aShowShape);
278             if(WAIT_A_SECOND) return;
279           }
280         }
281       }
282     
283
284       TopTools_DataMapOfIntegerShape aEdges;
285       TopTools_SequenceOfShape aVertices;
286
287       for( Standard_Integer i = 1; i < aNodes.Length()+1; i++)
288       {
289         gp_Pnt aPnt = aNodes(i).Transformed(aLocation);
290         TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(aPnt);
291
292         if(!aVertex.IsNull())
293         {
294           aBuilder.Add(aCompound,aVertex);
295           if(aCount == aNumOfFace ) 
296             aBuild1.Add(aComp1,aVertex);
297           aVertices.Append(aVertex);
298         }
299       }
300
301       Standard_Integer nnn = aTr->NbTriangles();
302       Standard_Integer nt,n1,n2,n3;
303
304       for( nt = 1 ; nt < nnn+1 ; nt++)
305       {     
306         triangles(nt).Get(n1,n2,n3);
307
308         Standard_Integer key[3];
309         
310         TopoDS_Vertex aV1,aV2;
311         key[0] = _key(n1, n2);
312         if(!aEdges.IsBound(key[0]))
313         {
314           aV1 = TopoDS::Vertex(aVertices(n1));
315           aV2 = TopoDS::Vertex(aVertices(n2));
316           if(!aV1.IsNull() && !aV2.IsNull() &&
317             !BRep_Tool::Pnt(aV1).IsEqual(BRep_Tool::Pnt(aV2),Precision::Confusion()))
318           {
319             TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);  
320             if(!aEdge.IsNull())
321             {
322               aEdges.Bind(key[0], aEdge);
323               aBuilder.Add(aCompound,aEdges(key[0]));
324               if(aCount == aNumOfFace)
325                 aBuild1.Add(aComp1,aEdges(key[0]));
326             } 
327           }
328         }
329         
330         key[1] = _key(n2,n3);
331         if(!aEdges.IsBound(key[1])) 
332         { 
333           aV1 = TopoDS::Vertex(aVertices(n2));
334           aV2 = TopoDS::Vertex(aVertices(n3));
335           if(!aV1.IsNull() && !aV2.IsNull() &&
336             !BRep_Tool::Pnt(aV1).IsEqual(BRep_Tool::Pnt(aV2),Precision::Confusion()))
337           {
338             TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);  
339             if(!aEdge.IsNull())
340             {
341               aEdges.Bind(key[1],aEdge);
342               aBuilder.Add(aCompound,aEdges(key[1]));
343               if(aCount == aNumOfFace) 
344                 aBuild1.Add(aComp1,aEdges(key[1]));
345             } 
346           } 
347         } 
348  
349         key[2] = _key(n3,n1);
350         if(!aEdges.IsBound(key[2])) 
351         { 
352           aV1 = TopoDS::Vertex(aVertices(n3));
353           aV2 = TopoDS::Vertex(aVertices(n1));
354           if(!aV1.IsNull() && !aV2.IsNull() &&
355             !BRep_Tool::Pnt(aV1).IsEqual(BRep_Tool::Pnt(aV2),Precision::Confusion()))
356           { 
357             TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);  
358             if(!aEdge.IsNull())
359             { 
360               aEdges.Bind(key[2],aEdge);
361               aBuilder.Add(aCompound,aEdges(key[2]));
362               if(aCount == aNumOfFace) 
363                 aBuild1.Add(aComp1,aEdges(key[2]));
364             } 
365           } 
366         } 
367       } 
368       
369       if(aCount == aNumOfFace)
370       {
371         aShowFace = drawShape(aComp1,Quantity_NOC_GREEN);
372         getAISContext()->Erase(aShowEdge);
373       }
374     }
375     else
376     {
377       aText += "Can't compute a triangulation on face ";
378       aText += TCollection_AsciiString(aCount) + EOL;
379           GetDocument()->PocessTextInDialog("Compute the triangulation on a shape", aText);
380 //      setResultText(aText.ToCString());
381     }
382   }
383   
384   aText += "Number of nodes of the shape = ";
385   aText += TCollection_AsciiString(aNumOfNodes) + EOL;
386   aText += "Number of triangles of the shape = ";
387   aText += TCollection_AsciiString(aNumOfTriangles) + EOL EOL;
388   GetDocument()->PocessTextInDialog("Compute the triangulation on a shape", aText);
389 //  setResultText(aText.ToCString());
390
391   if(WAIT_A_SECOND) return;
392   drawShape(aCompound,Quantity_NOC_GREEN);
393   getAISContext()->Erase(aShowFace);
394   
395 }
396
397 void Tesselate_Presentation::sample(const Standard_CString aFileName)
398 {
399   CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir());
400   initfile += "\\..\\..\\..\\samples\\mfc\\standard\\Data\\";
401   initfile += aFileName;
402
403 /*  
404   ResetView();
405   
406   if (aFileName == "wedge_ok.brep"){
407         SetViewCenter(6.3639597574916, 4.4907309380832);
408         SetViewScale(52.722555157077); 
409   }
410
411   if (aFileName == "shell1.brep"){
412         SetViewCenter(60.457553053711, -20.351208944076);
413         SetViewScale(26.857478563027); 
414   }
415
416   if (aFileName == "Pump_Nut.brep"){
417         SetViewCenter(248.77723166710, 77.249633819945);
418         SetViewScale(12.371719671833); 
419   }
420
421   if (aFileName == "Pump_TopCover.brep"){
422         SetViewCenter(408.72474423160, 169.38361094986);
423         SetViewScale(2.1932732873087); 
424   }
425 */
426
427   std::filebuf aFileBuf;
428   std::istream aStream (&aFileBuf);
429   if (!aFileBuf.open (initfile, ios::in))
430   {
431     initfile += L" was not found. The sample can not be shown.";
432     GetDocument()->PocessTextInDialog ("Compute the triangulation on a shape", initfile);
433     return;
434   }
435
436   TopoDS_Shape aShape;
437   BRep_Builder aBld;
438   BRepTools::Read (aShape, aStream, aBld);
439   if (aShape.IsNull())
440   {
441     initfile += L" was not found. The sample can not be shown.";
442     GetDocument()->PocessTextInDialog ("Compute the triangulation on a shape", initfile);
443     return;
444   }
445
446   tesselateShape (aShape);
447 }