Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / occtdemo / HLR / HLR_Presentation.cpp
1 // HLR_Presentation.cpp: implementation of the HLR_Presentation class.
2 // Hidden lines removal
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "HLR_Presentation.h"
7
8 #include <Geom_ToroidalSurface.hxx>
9 #include <HLRBRep_Algo.hxx>
10 #include <BRepBuilderAPI_MakeShell.hxx>
11 #include <HLRAlgo_Projector.hxx>
12 #include <Prs3d_Projector.hxx>
13 #include <TopoDS_Shell.hxx>
14 #include <HLRBRep_HLRToShape.hxx>
15 #include <HLRBRep_PolyAlgo.hxx>
16 #include <HLRBRep_PolyHLRToShape.hxx>
17 #include <Prs3d_LineAspect.hxx>
18 #include <AIS_Drawer.hxx>
19 #include <BRepTools.hxx>
20 #include <BRepMesh.hxx>
21 #include <BRep_Builder.hxx>
22 #include <Geom_CylindricalSurface.hxx>
23 #include <Geom_RectangularTrimmedSurface.hxx>
24 #include <BRepPrimAPI_MakeCylinder.hxx>
25 #include <TopoDS_Solid.hxx>
26
27
28 // Initialization of global variable with an instance of this class
29 OCCDemo_Presentation* OCCDemo_Presentation::Current = new HLR_Presentation;
30
31 // Initialization of array of samples
32 const HLR_Presentation::PSampleFuncType HLR_Presentation::SampleFuncs[] =
33 {
34   &HLR_Presentation::sampleCylinder,
35   &HLR_Presentation::sampleTorus,
36   &HLR_Presentation::sampleBrepShape2,
37   &HLR_Presentation::sampleBrepShape1
38 };
39
40 #ifdef WNT
41  #define EOL "\r\n"
42 #else
43  #define EOL "\n"
44 #endif
45
46 #define SharpEdge_COLOR   Quantity_NOC_CYAN1
47 #define SmoothEdge_COLOR  Quantity_NOC_RED
48 #define SewnEdge_COLOR    Quantity_NOC_ORANGE1
49 #define OutLine_COLOR     Quantity_NOC_MAGENTA1
50 #define IsoLine_COLOR     Quantity_NOC_YELLOW
51
52 //////////////////////////////////////////////////////////////////////
53 // Construction/Destruction
54 //////////////////////////////////////////////////////////////////////
55
56 HLR_Presentation::HLR_Presentation()
57 {
58   FitMode = true;
59   myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
60   setName ("Hidden Lines Removal");
61 }
62
63 //////////////////////////////////////////////////////////////////////
64 // Sample execution
65 //////////////////////////////////////////////////////////////////////
66
67 void HLR_Presentation::DoSample()
68 {
69   getAISContext()->EraseAll();
70   setResultTitle("Hidden Lines Removal");
71   if (myIndex >=0 && myIndex < myNbSamples)
72     (this->*SampleFuncs[myIndex])();
73 }
74
75
76 //================================================================
77 // Function : Projector
78 // Purpose  : returns Prs3d_Projector object created corresponding 
79 //            to the current view parameters
80 //================================================================
81 Handle_Prs3d_Projector HLR_Presentation::Projector(gp_Trsf& theTrsf)
82 {
83   // retrieve view parameters for creation of Prs3d_Projector object
84   getViewer()->InitActiveViews();
85   Handle_V3d_View aView = getViewer()->ActiveView();
86   
87   Standard_Real aProjVecX, aProjVecY, aProjVecZ;
88   aView->Proj(aProjVecX, aProjVecY, aProjVecZ);
89   
90   Standard_Real aUpX, aUpY, aUpZ;
91   aView->Up(aUpX, aUpY, aUpZ);
92
93   Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;
94
95   // create a projector object
96   Handle_Prs3d_Projector aProjector = new Prs3d_Projector(Standard_False, 0.0, 
97     aProjVecX, aProjVecY, aProjVecZ, aPntX, aPntY, aPntZ, aUpX, aUpY, aUpZ);
98
99   gp_Pnt At (aPntX,aPntY,aPntZ);
100   gp_Dir Zpers (aProjVecX,aProjVecY,aProjVecZ);
101   gp_Dir Ypers (aUpX,aUpY,aUpZ);
102   gp_Dir Xpers = Ypers.Crossed(Zpers);
103   gp_Ax3 NewCoordSystem (At, Zpers, Xpers);
104   gp_Ax3 CurrentCoordSystem(gp_Pnt(0,0,0),gp_Dir(0,0,1));
105   gp_Trsf Trsf;
106   Trsf.SetDisplacement(CurrentCoordSystem, NewCoordSystem);
107
108   theTrsf = Trsf;
109
110   return aProjector;
111 }
112
113
114 //================================================================
115 // Function : HLR_Presentation::HLR
116 // Purpose  : creates and displays shape representation by HLRBRep_Algo class
117 //================================================================
118 Standard_Boolean HLR_Presentation::HLR(const TopoDS_Shape& aShape, const Standard_Integer nbIso)
119 {
120   setResultText(
121     "  TopoDS_Shape aShape;" EOL
122     "  // initializing aShape ..." EOL
123     "" EOL
124     "  // Build The algorithm object" EOL
125     "  Handle_HLRBRep_Algo myAlgo = new HLRBRep_Algo();" EOL
126     "  " EOL
127     "  // Add Shapes into the algorithm" EOL
128     "  Standard_Real nbIso = 5; // number of isolines to display" EOL
129     "  myAlgo->Add(aShape, nbIso);" EOL
130     "" EOL
131     "  // create a projector object" EOL
132     "  Handle_V3d_View aView;" EOL
133     "  // initializing V3d_View ..." EOL
134     "  " EOL
135     "  Standard_Real aProjVecX, aProjVecY, aProjVecZ;" EOL
136     "  aView->Proj(aProjVecX, aProjVecY, aProjVecZ);" EOL
137     "  " EOL
138     "  Standard_Real aUpX, aUpY, aUpZ;" EOL
139     "  aView->Up(aUpX, aUpY, aUpZ);" EOL
140     "" EOL
141     "  Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;" EOL
142     "" EOL
143     "  // create a projector object" EOL
144     "  Handle_Prs3d_Projector aProjector = new Prs3d_Projector(Standard_False, 0.0, " EOL
145     "    aProjVecX, aProjVecY, aProjVecZ, aPntX, aPntY, aPntZ, aUpX, aUpY, aUpZ);" EOL
146     "  " EOL
147     "  // Set The Projector" EOL
148     "  myAlgo->Projector(aProjector->Projector());" EOL
149     "  " EOL
150     "  // Launches calculation of outlines of the shape " EOL
151     "  myAlgo->Update();" EOL
152     "" EOL
153     "  // Computes the visible and hidden lines of the shape " EOL
154     "  myAlgo->Hide();" EOL
155     "  " EOL
156     "  // Build the extraction object :" EOL
157     "  HLRBRep_HLRToShape aHLRToShape(myAlgo);" EOL
158     "  " EOL
159     "  // extract the results of visible parts of the shape:" EOL
160     "  TopoDS_Shape VCompound = aHLRToShape.VCompound();" EOL
161     "  //Visible sharp edges, Sharp edges present a C0 continuity" EOL
162     "  " EOL
163     "  TopoDS_Shape Rg1LineVCompound = aHLRToShape.Rg1LineVCompound();" EOL
164     "  //Visible smooth edges, Smooth edges present a G1 continuity " EOL
165     "" EOL
166     "  TopoDS_Shape RgNLineVCompound = aHLRToShape.RgNLineVCompound();" EOL
167     "  //Visible sewn edges, Sewn edges present a C2 continuity" EOL
168     "" EOL
169     "  TopoDS_Shape OutLineVCompound = aHLRToShape.OutLineVCompound();" EOL
170     "  //Visible outline edges" EOL
171     "  " EOL
172     "  TopoDS_Shape IsoLineVCompound = aHLRToShape.IsoLineVCompound();" EOL
173     "  //Visible isoparameters" EOL
174     "  " EOL
175     "  // extract the results of hidden parts of the shape:" EOL
176     "  TopoDS_Shape HCompound = aHLRToShape.HCompound();" EOL
177     "  TopoDS_Shape Rg1LineHCompound = aHLRToShape.Rg1LineHCompound();" EOL
178     "  TopoDS_Shape RgNLineHCompound = aHLRToShape.RgNLineHCompound();" EOL
179     "  TopoDS_Shape OutLineHCompound = aHLRToShape.OutLineHCompound();" EOL
180     "  TopoDS_Shape IsoLineHCompound = aHLRToShape.IsoLineHCompound();" EOL
181     "  " EOL
182     "  " EOL
183     "  //===============================" EOL
184     "  Colors of results:" EOL
185     "  Outlines     - MAGENTA" EOL
186     "  Sharp Edges  - CYAN" EOL
187     "  Smooth Edges - RED" EOL
188     "  Sewn Edges   - ORANGE" EOL
189     "  Isolines     - YELLOW" EOL);
190
191   // Build The algorithm object
192   Handle_HLRBRep_Algo myAlgo = new HLRBRep_Algo();
193   
194   // Add Shapes into the algorithm
195   myAlgo->Add(aShape, nbIso);
196
197   // create a projector and transformation object
198   gp_Trsf Trsf;
199   HLRAlgo_Projector aProjector = Projector(Trsf)->Projector();
200   TopLoc_Location aLoc(Trsf);
201   
202   // Set The Projector
203   myAlgo->Projector(aProjector);
204   
205   // Launches calculation of outlines of the shape 
206   myAlgo->Update();
207
208   // Computes the visible and hidden lines of the shape 
209   myAlgo->Hide();
210   
211   // Build the extraction object :
212   HLRBRep_HLRToShape aHLRToShape(myAlgo);
213   
214   // extract the results of visible parts of the shape:
215   TopoDS_Shape VCompound = aHLRToShape.VCompound();
216   //Visible sharp edges, Sharp edges present a C0 continuity
217   
218   TopoDS_Shape Rg1LineVCompound = aHLRToShape.Rg1LineVCompound();
219   //Visible smooth edges, Smooth edges present a G1 continuity 
220
221   TopoDS_Shape RgNLineVCompound = aHLRToShape.RgNLineVCompound();
222   //Visible sewn edges, Sewn edges present a C2 continuity
223
224   TopoDS_Shape OutLineVCompound = aHLRToShape.OutLineVCompound();
225   //Visible outline edges
226   
227   TopoDS_Shape IsoLineVCompound = aHLRToShape.IsoLineVCompound();
228   //Visible isoparameters
229   
230   // extract the results of hidden parts of the shape:
231   TopoDS_Shape HCompound = aHLRToShape.HCompound();
232   TopoDS_Shape Rg1LineHCompound = aHLRToShape.Rg1LineHCompound();
233   TopoDS_Shape RgNLineHCompound = aHLRToShape.RgNLineHCompound();
234   TopoDS_Shape OutLineHCompound = aHLRToShape.OutLineHCompound();
235   TopoDS_Shape IsoLineHCompound = aHLRToShape.IsoLineHCompound();
236
237   Handle_AIS_Shape aShapeIO = drawShape(aShape);
238
239   if (WAIT_A_SECOND) return Standard_False;
240
241   getAISContext()->Erase(aShapeIO, Standard_False);
242
243   // Draw visible and hidden parts of shape.  
244   if (!drawHLR(OutLineVCompound, OutLineHCompound, aLoc, OutLine_COLOR)) 
245     return Standard_False;
246   if (!drawHLR(VCompound, HCompound, aLoc, SharpEdge_COLOR)) 
247     return Standard_False;
248   if (!drawHLR(Rg1LineVCompound, Rg1LineHCompound, aLoc, SmoothEdge_COLOR)) 
249     return Standard_False;
250   if (!drawHLR(RgNLineVCompound, RgNLineHCompound, aLoc, SewnEdge_COLOR)) 
251     return Standard_False;
252   if (!drawHLR(IsoLineVCompound, IsoLineHCompound, aLoc, IsoLine_COLOR)) 
253     return Standard_False;
254
255   return Standard_True;
256 }
257
258
259 //================================================================
260 // Function : HLR_Presentation::PolyHLR
261 // Purpose  : creates and displays shape representation by HLRBRep_PolyAlgo class
262 //================================================================
263 Standard_Boolean HLR_Presentation::PolyHLR(const TopoDS_Shape& aShape)
264 {
265   setResultText(
266     "  TopoDS_Shape aShape;" EOL
267     "  // initializing aShape ..." EOL
268     "  // triangulating aShape with BRepMesh::Mesh" EOL
269     "" EOL
270     "  // Build The algorithm object" EOL
271     "  Handle_HLRBRep_PolyAlgo myPolyAlgo = new HLRBRep_PolyAlgo();" EOL
272     "  " EOL
273     "  // Add Shapes into the algorithm" EOL
274     "  myPolyAlgo->Load(aShape);" EOL
275     "" EOL
276     "  // create a projector object" EOL
277     "  Handle_V3d_View aView;" EOL
278     "  // initializing V3d_View ..." EOL
279     "  " EOL
280     "  Standard_Real aProjVecX, aProjVecY, aProjVecZ;" EOL
281     "  aView->Proj(aProjVecX, aProjVecY, aProjVecZ);" EOL
282     "  " EOL
283     "  Standard_Real aUpX, aUpY, aUpZ;" EOL
284     "  aView->Up(aUpX, aUpY, aUpZ);" EOL
285     "" EOL
286     "  Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;" EOL
287     "" EOL
288     "  // create a projector object" EOL
289     "  Handle_Prs3d_Projector aProjector = new Prs3d_Projector(Standard_False, 0.0, " EOL
290     "    aProjVecX, aProjVecY, aProjVecZ, aPntX, aPntY, aPntZ, aUpX, aUpY, aUpZ);" EOL
291     "  " EOL
292     "  // Set The Projector" EOL
293     "  myPolyAlgo->Projector(aProjector);" EOL
294     "  " EOL
295     "  // Launches calculation of outlines of the shape " EOL
296     "  myPolyAlgo->Update();" EOL
297     "" EOL
298     "  // Build the extraction object :" EOL
299     "  HLRBRep_PolyHLRToShape aPolyHLRToShape;" EOL
300     "  aPolyHLRToShape.Update(myPolyAlgo);" EOL
301     "  " EOL
302     "  // extract the results of visible parts of the shape:" EOL
303     "  TopoDS_Shape VCompound = aPolyHLRToShape.VCompound();" EOL
304     "  //Visible sharp edges, Sharp edges present a C0 continuity" EOL
305     "  " EOL
306     "  TopoDS_Shape Rg1LineVCompound = aPolyHLRToShape.Rg1LineVCompound();" EOL
307     "  //Visible smooth edges, Smooth edges present a G1 continuity " EOL
308     "" EOL
309     "  TopoDS_Shape RgNLineVCompound = aPolyHLRToShape.RgNLineVCompound();" EOL
310     "  //Visible sewn edges, Sewn edges present a C2 continuity" EOL
311     "" EOL
312     "  TopoDS_Shape OutLineVCompound = aPolyHLRToShape.OutLineVCompound();" EOL
313     "  //Visible outline edges" EOL
314     "  " EOL
315     "  // extract the results of hidden parts of the shape:" EOL
316     "  TopoDS_Shape HCompound = aPolyHLRToShape.HCompound();" EOL
317     "  TopoDS_Shape Rg1LineHCompound = aPolyHLRToShape.Rg1LineHCompound();" EOL
318     "  TopoDS_Shape RgNLineHCompound = aPolyHLRToShape.RgNLineHCompound();" EOL
319     "  TopoDS_Shape OutLineHCompound = aPolyHLRToShape.OutLineHCompound();" EOL
320     "  " EOL
321     "  " EOL
322     "  //===============================" EOL
323     "  Colors of results:" EOL
324     "  Outlines     - MAGENTA" EOL
325     "  Sharp Edges  - CYAN" EOL
326     "  Smooth Edges - RED" EOL
327     "  Sewn Edges   - ORANGE" EOL
328     "  Isolines     - YELLOW" EOL);
329
330   // Build The algorithm object
331   Handle_HLRBRep_PolyAlgo myPolyAlgo = new HLRBRep_PolyAlgo();
332   
333   // Add Shapes into the algorithm
334   myPolyAlgo->Load(aShape);
335
336   // create a projector and transformation object
337   gp_Trsf Trsf;
338   HLRAlgo_Projector aProjector = Projector(Trsf)->Projector();
339   TopLoc_Location aLoc(Trsf);
340   
341   // Set The Projector
342   myPolyAlgo->Projector(aProjector);
343   
344   // Launches calculation of outlines of the shape 
345   myPolyAlgo->Update();
346
347   // Build the extraction object :
348   HLRBRep_PolyHLRToShape aPolyHLRToShape;
349   aPolyHLRToShape.Update(myPolyAlgo);
350   
351   // extract the results of visible parts of the shape:
352   TopoDS_Shape VCompound = aPolyHLRToShape.VCompound();
353   //Visible sharp edges, Sharp edges present a C0 continuity
354   
355   TopoDS_Shape Rg1LineVCompound = aPolyHLRToShape.Rg1LineVCompound();
356   //Visible smooth edges, Smooth edges present a G1 continuity 
357
358   TopoDS_Shape RgNLineVCompound = aPolyHLRToShape.RgNLineVCompound();
359   //Visible sewn edges, Sewn edges present a C2 continuity
360
361   TopoDS_Shape OutLineVCompound = aPolyHLRToShape.OutLineVCompound();
362   //Visible outline edges
363   
364   // extract the results of hidden parts of the shape:
365   TopoDS_Shape HCompound = aPolyHLRToShape.HCompound();
366   TopoDS_Shape Rg1LineHCompound = aPolyHLRToShape.Rg1LineHCompound();
367   TopoDS_Shape RgNLineHCompound = aPolyHLRToShape.RgNLineHCompound();
368   TopoDS_Shape OutLineHCompound = aPolyHLRToShape.OutLineHCompound();
369
370   Handle_AIS_Shape aShapeIO = drawShape(aShape);
371
372   if (WAIT_A_SECOND) return Standard_False;
373
374   getAISContext()->Erase(aShapeIO, Standard_False);
375
376   // Draw visible and hidden parts of shape.  
377   if (!drawHLR(OutLineVCompound, OutLineHCompound, aLoc, OutLine_COLOR)) 
378     return Standard_False;
379   if (!drawHLR(VCompound, HCompound, aLoc, SharpEdge_COLOR)) 
380     return Standard_False;
381   if (!drawHLR(Rg1LineVCompound, Rg1LineHCompound, aLoc, SmoothEdge_COLOR)) 
382     return Standard_False;
383   if (!drawHLR(RgNLineVCompound, RgNLineHCompound, aLoc, SewnEdge_COLOR)) 
384     return Standard_False;
385
386   return Standard_True;
387 }
388
389
390 //================================================================
391 // Function : HLR_Presentation::drawHLR
392 // Purpose  : moves a given shape to the given location, displays it.
393 //            for visible shape using Aspect_TOL_SOLID line type
394 //            for hidden shape using Aspect_TOL_DASH line type
395 //================================================================
396 Standard_Boolean HLR_Presentation::drawHLR(TopoDS_Shape& aVShape,
397                                            TopoDS_Shape& aHShape,
398                                            const TopLoc_Location& aLoc, 
399                                            const enum Quantity_NameOfColor aColor)
400 {
401   Standard_Boolean VShapeIsNull = aVShape.IsNull();
402   Standard_Boolean HShapeIsNull = aHShape.IsNull();
403
404   if (VShapeIsNull && HShapeIsNull) return Standard_True;
405
406   if (!VShapeIsNull)
407   {
408     // move the shape to an absolute location
409     aVShape.Location(aLoc);
410
411     // create a look for a line according to aColor and aLineType
412     Handle_AIS_Shape anIO = drawShape(aVShape, aColor, Standard_False);
413     anIO->Attributes()->WireAspect()->SetTypeOfLine(Aspect_TOL_SOLID);
414     
415     getAISContext()->Display(anIO, HShapeIsNull);
416   }
417   if (!HShapeIsNull)
418   {
419     // move the shape to an absolute location
420     aHShape.Location(aLoc);
421
422     // create a look for a line according to aColor and aLineType
423     Handle_AIS_Shape anIO = drawShape(aHShape, aColor, Standard_False);
424     anIO->Attributes()->WireAspect()->SetTypeOfLine(Aspect_TOL_DASH);
425
426     getAISContext()->Display(anIO);
427   }
428
429   return !WAIT_A_SECOND;
430 }
431
432 //////////////////////////////////////////////////////////////////////
433 // Sample functions
434 //////////////////////////////////////////////////////////////////////
435
436 //================================================================
437 // Function : HLR_Presentation::sampleCylinder
438 // Purpose  : 
439 //================================================================
440 void HLR_Presentation::sampleCylinder()
441 {
442   // define cylinder's radius and height
443   Standard_Real aRadius = 20;
444   Standard_Real aHeight = 50;
445
446   // make cylinder
447   TopoDS_Solid aShape = BRepPrimAPI_MakeCylinder(aRadius, aHeight);
448
449   Standard_Integer nbIso = 3;
450   if (!HLR(aShape, nbIso)) return;
451 }
452
453 //================================================================
454 // Function : HLR_Presentation::sampleTorus
455 // Purpose  : 
456 //================================================================
457 void HLR_Presentation::sampleTorus()
458 {
459   //define toroidal surface's axis
460   gp_Ax3 anAx3(gp_Pnt(0,0,0), gp_Dir(1,0,0));
461
462   //define two radiuses
463   Standard_Real MajorRadius = 20;
464   Standard_Real MinorRadius = 10;
465   
466   //make torus
467   Handle_Geom_ToroidalSurface aTorSurface = new Geom_ToroidalSurface(anAx3, MajorRadius, MinorRadius);
468   TopoDS_Shell aShape = BRepBuilderAPI_MakeShell(aTorSurface);
469
470   Standard_Integer nbIso = 3;
471   if (!HLR(aShape, nbIso)) return;
472 }
473
474
475 //================================================================
476 // Function : HLR_Presentation::sampleBox
477 // Purpose  : 
478 //================================================================
479 void HLR_Presentation::sampleBrepShape2()
480 {
481   // read a shape from shape2.brep file in DATA dir
482   TopoDS_Shape aShape;
483   BRep_Builder aBuilder;
484   TCollection_AsciiString aFileName(GetDataDir());
485   aFileName += "\\shape2.brep";
486   Standard_Boolean Result = BRepTools::Read(aShape, aFileName.ToCString(), aBuilder);
487
488   if (!Result)
489   {
490     aFileName += " was not found.  The sample can not be shown.";
491     setResultText(aFileName.ToCString());
492     return;
493   }
494
495   Standard_Integer nbIso = 0;
496   if (!HLR(aShape, nbIso)) return;
497 }
498
499
500 //================================================================
501 // Function : HLR_Presentation::sampleShape1
502 // Purpose  : 
503 //================================================================
504 void HLR_Presentation::sampleBrepShape1()
505 {
506   // read a shape from shape1.brep file in DATA dir
507   TopoDS_Shape aShape;
508   BRep_Builder aBuilder;
509   TCollection_AsciiString aFileName(GetDataDir());
510   aFileName += "\\shape1.brep";
511   Standard_Boolean Result = BRepTools::Read(aShape, aFileName.ToCString(), aBuilder);
512
513   if (!Result)
514   {
515     aFileName += " was not found.  The sample can not be shown.";
516     setResultText(aFileName.ToCString());
517     return;
518   }
519
520   // triangulating the shape
521   Standard_Real Deflection = 50;
522   BRepMesh::Mesh(aShape, Deflection);
523
524   if (!PolyHLR(aShape)) return;
525 }