1 // Tesselate_Presentation.cpp: implementation of the Tesselate_Presentation class.
3 ////////////////////////////////////////////////////////////////////////////
6 #include "Tesselate_Presentation.h"
7 #include "TriangulationApp.h"
8 #include "TriangulationDoc.h"
10 #include <Precision.hxx>
12 #include <TopoDS_Edge.hxx>
13 #include <TopoDS_Face.hxx>
14 #include <TopoDS_Compound.hxx>
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>
23 #include <TopExp_Explorer.hxx>
24 #include <TopLoc_Location.hxx>
25 #include <TopTools_DataMapOfIntegerShape.hxx>
26 #include <TopTools_SequenceOfShape.hxx>
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>
38 // Initialization of global variable with an instance of this class
39 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Tesselate_Presentation;
41 // Initialization of array of samples
42 Standard_CString Tesselate_Presentation::myFileNames[] =
51 //////////////////////////////////////////////////////////////////////
52 // Construction/Destruction
53 //////////////////////////////////////////////////////////////////////
55 Tesselate_Presentation::Tesselate_Presentation()
57 for (myNbSamples = 0; myFileNames[myNbSamples]; myNbSamples++);
58 setName ("Tesselate shapes");
61 //////////////////////////////////////////////////////////////////////
63 //////////////////////////////////////////////////////////////////////
65 void Tesselate_Presentation::DoSample()
67 ((CTriangulationApp*) AfxGetApp())->SetSampleName (L"Tesselate");
68 ((CTriangulationApp*) AfxGetApp())->SetSamplePath (L"");
69 getAISContext()->EraseAll();
70 if (myIndex >=0 && myIndex < myNbSamples)
71 sample (myFileNames[myIndex]);
74 //////////////////////////////////////////////////////////////////////
76 //////////////////////////////////////////////////////////////////////
77 //================================================================
79 inline Standard_Integer _key(Standard_Integer n1,Standard_Integer n2)
82 Standard_Integer key =
83 (n2>n1)?(n1<<16)+n2:(n2<<16)+n1;
87 //DATA : [myIndex][{Deflection,NumberOfFace,NumberOfEdge}]
88 static const Standard_Real DATA [][3] =
90 {0.2,1,2},{0.5,6,2},{0.7,16,2},{1,1,2}
94 void Tesselate_Presentation::tesselateShape(const TopoDS_Shape& aShape)
96 // setResultTitle("Tesselate shape");
97 TCollection_AsciiString aText = (
98 "/////////////////////////////////////////////////////////////////" EOL
99 "// Tesselate shape." EOL
100 "/////////////////////////////////////////////////////////////////" EOL EOL
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];
108 "Standard_Real aDeflection;" EOL
109 "// aDeflection = ... ;" EOL EOL
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
115 "// adds a triangulation of the shape aShape with the deflection aDeflection:" EOL
116 "BRepMesh::Mesh(aShape,aDeflection);" EOL EOL
118 "TopExp_Explorer aExpFace,aExpEdge;" EOL
119 "for(aExpFace.Init(aShape,TopAbs_FACE);aExpFace.More();aExpFace.Next())" EOL
121 " TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());" EOL
122 " TopLoc_Location aLocation;" EOL EOL
124 " // takes the triangulation of the face aFace:" EOL
125 " Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);" EOL EOL
127 " if(!aTr.IsNull()) // if this triangulation is not NULL" 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
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
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
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
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
160 " if(!aEdge.IsNull()) // if this edge is not NULL" 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
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
174 "//==================================================" EOL EOL
177 aText += " Result with deflection = ";
178 aText += TCollection_AsciiString(aDeflection);
181 GetDocument()->PocessTextInDialog("Compute the triangulation on a shape", aText);
182 // setResultText(aText.ToCString());
184 //==========================================================================
186 BRepTools::Clean(aShape);
187 BRepMesh_IncrementalMesh(aShape,aDeflection);
189 BRep_Builder aBuilder,aBuild1,aBuild2;
190 TopoDS_Compound aCompound,aComp1,aComp2;
191 aBuilder.MakeCompound(aCompound);
192 aBuild1.MakeCompound(aComp1);
193 aBuild2.MakeCompound(aComp2);
195 TopTools_SequenceOfShape aVertices;
196 Standard_Integer aCount = 0;
197 Standard_Integer aNumOfNodes = 0;
198 Standard_Integer aNumOfTriangles = 0;
200 Handle(AIS_InteractiveObject) aShowEdge,aShowFace,aShowShape;
202 TopExp_Explorer aExpFace,aExpEdge;
204 for(aExpFace.Init(aShape,TopAbs_FACE);aExpFace.More();aExpFace.Next())
208 TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
209 TopLoc_Location aLocation;
211 Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);
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();
222 if(aCount == aNumOfFace)
224 Standard_Integer aNbOfNodesOfFace = aTr->NbNodes();
225 Standard_Integer aNbOfTrianglesOfFace = aTr->NbTriangles();
226 aExpEdge.Init(aFace,TopAbs_EDGE);
230 for( Standard_Integer i = 0; aExpEdge.More() && i < aNumOfEdge ; aExpEdge.Next(), i++)
231 aEdge = TopoDS::Edge(aExpEdge.Current());
235 Handle(Poly_PolygonOnTriangulation) aPol =
236 BRep_Tool::PolygonOnTriangulation(aEdge,aTr,aEdge.Location());
240 const TColStd_Array1OfInteger& aNodesOfPol = aPol->Nodes();
241 Standard_Integer aNbOfNodesOfEdge = aPol->NbNodes();
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());
252 Standard_Integer aLower = aNodesOfPol.Lower(), anUpper = aNodesOfPol.Upper();
253 for( int i = aLower; i < anUpper ; i++)
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);
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
264 aEdge = BRepBuilderAPI_MakeEdge (aVertex1,aVertex2);
265 aBuild2.Add(aComp2,aVertex1);
267 aBuild2.Add(aComp2,aEdge);
269 aBuild2.Add(aComp2,aVertex2);
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;
284 TopTools_DataMapOfIntegerShape aEdges;
285 TopTools_SequenceOfShape aVertices;
287 for( Standard_Integer i = 1; i < aNodes.Length()+1; i++)
289 gp_Pnt aPnt = aNodes(i).Transformed(aLocation);
290 TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(aPnt);
292 if(!aVertex.IsNull())
294 aBuilder.Add(aCompound,aVertex);
295 if(aCount == aNumOfFace )
296 aBuild1.Add(aComp1,aVertex);
297 aVertices.Append(aVertex);
301 Standard_Integer nnn = aTr->NbTriangles();
302 Standard_Integer nt,n1,n2,n3;
304 for( nt = 1 ; nt < nnn+1 ; nt++)
306 triangles(nt).Get(n1,n2,n3);
308 Standard_Integer key[3];
310 TopoDS_Vertex aV1,aV2;
311 key[0] = _key(n1, n2);
312 if(!aEdges.IsBound(key[0]))
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()))
319 TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);
322 aEdges.Bind(key[0], aEdge);
323 aBuilder.Add(aCompound,aEdges(key[0]));
324 if(aCount == aNumOfFace)
325 aBuild1.Add(aComp1,aEdges(key[0]));
330 key[1] = _key(n2,n3);
331 if(!aEdges.IsBound(key[1]))
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()))
338 TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);
341 aEdges.Bind(key[1],aEdge);
342 aBuilder.Add(aCompound,aEdges(key[1]));
343 if(aCount == aNumOfFace)
344 aBuild1.Add(aComp1,aEdges(key[1]));
349 key[2] = _key(n3,n1);
350 if(!aEdges.IsBound(key[2]))
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()))
357 TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge (aV1,aV2);
360 aEdges.Bind(key[2],aEdge);
361 aBuilder.Add(aCompound,aEdges(key[2]));
362 if(aCount == aNumOfFace)
363 aBuild1.Add(aComp1,aEdges(key[2]));
369 if(aCount == aNumOfFace)
371 aShowFace = drawShape(aComp1,Quantity_NOC_GREEN);
372 getAISContext()->Erase(aShowEdge);
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());
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());
391 if(WAIT_A_SECOND) return;
392 drawShape(aCompound,Quantity_NOC_GREEN);
393 getAISContext()->Erase(aShowFace);
397 void Tesselate_Presentation::sample(const Standard_CString aFileName)
399 CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir());
400 initfile += "\\..\\..\\..\\samples\\mfc\\standard\\Data\\";
401 initfile += aFileName;
406 if (aFileName == "wedge_ok.brep"){
407 SetViewCenter(6.3639597574916, 4.4907309380832);
408 SetViewScale(52.722555157077);
411 if (aFileName == "shell1.brep"){
412 SetViewCenter(60.457553053711, -20.351208944076);
413 SetViewScale(26.857478563027);
416 if (aFileName == "Pump_Nut.brep"){
417 SetViewCenter(248.77723166710, 77.249633819945);
418 SetViewScale(12.371719671833);
421 if (aFileName == "Pump_TopCover.brep"){
422 SetViewCenter(408.72474423160, 169.38361094986);
423 SetViewScale(2.1932732873087);
427 std::filebuf aFileBuf;
428 std::istream aStream (&aFileBuf);
429 if (!aFileBuf.open (initfile, ios::in))
431 initfile += L" was not found. The sample can not be shown.";
432 GetDocument()->PocessTextInDialog ("Compute the triangulation on a shape", initfile);
438 BRepTools::Read (aShape, aStream, aBld);
441 initfile += L" was not found. The sample can not be shown.";
442 GetDocument()->PocessTextInDialog ("Compute the triangulation on a shape", initfile);
446 tesselateShape (aShape);