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