1 // HLR_Presentation.cpp: implementation of the HLR_Presentation class.
2 // Hidden lines removal
3 //////////////////////////////////////////////////////////////////////
6 #include "HLR_Presentation.h"
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>
28 // Initialization of global variable with an instance of this class
29 OCCDemo_Presentation* OCCDemo_Presentation::Current = new HLR_Presentation;
31 // Initialization of array of samples
32 const HLR_Presentation::PSampleFuncType HLR_Presentation::SampleFuncs[] =
34 &HLR_Presentation::sampleCylinder,
35 &HLR_Presentation::sampleTorus,
36 &HLR_Presentation::sampleBrepShape2,
37 &HLR_Presentation::sampleBrepShape1
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
52 //////////////////////////////////////////////////////////////////////
53 // Construction/Destruction
54 //////////////////////////////////////////////////////////////////////
56 HLR_Presentation::HLR_Presentation()
59 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
60 setName ("Hidden Lines Removal");
63 //////////////////////////////////////////////////////////////////////
65 //////////////////////////////////////////////////////////////////////
67 void HLR_Presentation::DoSample()
69 getAISContext()->EraseAll();
70 setResultTitle("Hidden Lines Removal");
71 if (myIndex >=0 && myIndex < myNbSamples)
72 (this->*SampleFuncs[myIndex])();
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)
83 // retrieve view parameters for creation of Prs3d_Projector object
84 getViewer()->InitActiveViews();
85 Handle_V3d_View aView = getViewer()->ActiveView();
87 Standard_Real aProjVecX, aProjVecY, aProjVecZ;
88 aView->Proj(aProjVecX, aProjVecY, aProjVecZ);
90 Standard_Real aUpX, aUpY, aUpZ;
91 aView->Up(aUpX, aUpY, aUpZ);
93 Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;
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);
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));
106 Trsf.SetDisplacement(CurrentCoordSystem, NewCoordSystem);
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)
121 " TopoDS_Shape aShape;" EOL
122 " // initializing aShape ..." EOL
124 " // Build The algorithm object" EOL
125 " Handle_HLRBRep_Algo myAlgo = new HLRBRep_Algo();" 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
131 " // create a projector object" EOL
132 " Handle_V3d_View aView;" EOL
133 " // initializing V3d_View ..." EOL
135 " Standard_Real aProjVecX, aProjVecY, aProjVecZ;" EOL
136 " aView->Proj(aProjVecX, aProjVecY, aProjVecZ);" EOL
138 " Standard_Real aUpX, aUpY, aUpZ;" EOL
139 " aView->Up(aUpX, aUpY, aUpZ);" EOL
141 " Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;" 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
147 " // Set The Projector" EOL
148 " myAlgo->Projector(aProjector->Projector());" EOL
150 " // Launches calculation of outlines of the shape " EOL
151 " myAlgo->Update();" EOL
153 " // Computes the visible and hidden lines of the shape " EOL
154 " myAlgo->Hide();" EOL
156 " // Build the extraction object :" EOL
157 " HLRBRep_HLRToShape aHLRToShape(myAlgo);" 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
163 " TopoDS_Shape Rg1LineVCompound = aHLRToShape.Rg1LineVCompound();" EOL
164 " //Visible smooth edges, Smooth edges present a G1 continuity " EOL
166 " TopoDS_Shape RgNLineVCompound = aHLRToShape.RgNLineVCompound();" EOL
167 " //Visible sewn edges, Sewn edges present a C2 continuity" EOL
169 " TopoDS_Shape OutLineVCompound = aHLRToShape.OutLineVCompound();" EOL
170 " //Visible outline edges" EOL
172 " TopoDS_Shape IsoLineVCompound = aHLRToShape.IsoLineVCompound();" EOL
173 " //Visible isoparameters" 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
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);
191 // Build The algorithm object
192 Handle_HLRBRep_Algo myAlgo = new HLRBRep_Algo();
194 // Add Shapes into the algorithm
195 myAlgo->Add(aShape, nbIso);
197 // create a projector and transformation object
199 HLRAlgo_Projector aProjector = Projector(Trsf)->Projector();
200 TopLoc_Location aLoc(Trsf);
203 myAlgo->Projector(aProjector);
205 // Launches calculation of outlines of the shape
208 // Computes the visible and hidden lines of the shape
211 // Build the extraction object :
212 HLRBRep_HLRToShape aHLRToShape(myAlgo);
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
218 TopoDS_Shape Rg1LineVCompound = aHLRToShape.Rg1LineVCompound();
219 //Visible smooth edges, Smooth edges present a G1 continuity
221 TopoDS_Shape RgNLineVCompound = aHLRToShape.RgNLineVCompound();
222 //Visible sewn edges, Sewn edges present a C2 continuity
224 TopoDS_Shape OutLineVCompound = aHLRToShape.OutLineVCompound();
225 //Visible outline edges
227 TopoDS_Shape IsoLineVCompound = aHLRToShape.IsoLineVCompound();
228 //Visible isoparameters
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();
237 Handle_AIS_Shape aShapeIO = drawShape(aShape);
239 if (WAIT_A_SECOND) return Standard_False;
241 getAISContext()->Erase(aShapeIO, Standard_False);
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;
255 return Standard_True;
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)
266 " TopoDS_Shape aShape;" EOL
267 " // initializing aShape ..." EOL
268 " // triangulating aShape with BRepMesh::Mesh" EOL
270 " // Build The algorithm object" EOL
271 " Handle_HLRBRep_PolyAlgo myPolyAlgo = new HLRBRep_PolyAlgo();" EOL
273 " // Add Shapes into the algorithm" EOL
274 " myPolyAlgo->Load(aShape);" EOL
276 " // create a projector object" EOL
277 " Handle_V3d_View aView;" EOL
278 " // initializing V3d_View ..." EOL
280 " Standard_Real aProjVecX, aProjVecY, aProjVecZ;" EOL
281 " aView->Proj(aProjVecX, aProjVecY, aProjVecZ);" EOL
283 " Standard_Real aUpX, aUpY, aUpZ;" EOL
284 " aView->Up(aUpX, aUpY, aUpZ);" EOL
286 " Standard_Real aPntX = 0.0, aPntY = 0.0, aPntZ = 0.0;" 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
292 " // Set The Projector" EOL
293 " myPolyAlgo->Projector(aProjector);" EOL
295 " // Launches calculation of outlines of the shape " EOL
296 " myPolyAlgo->Update();" EOL
298 " // Build the extraction object :" EOL
299 " HLRBRep_PolyHLRToShape aPolyHLRToShape;" EOL
300 " aPolyHLRToShape.Update(myPolyAlgo);" 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
306 " TopoDS_Shape Rg1LineVCompound = aPolyHLRToShape.Rg1LineVCompound();" EOL
307 " //Visible smooth edges, Smooth edges present a G1 continuity " EOL
309 " TopoDS_Shape RgNLineVCompound = aPolyHLRToShape.RgNLineVCompound();" EOL
310 " //Visible sewn edges, Sewn edges present a C2 continuity" EOL
312 " TopoDS_Shape OutLineVCompound = aPolyHLRToShape.OutLineVCompound();" EOL
313 " //Visible outline edges" 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
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);
330 // Build The algorithm object
331 Handle_HLRBRep_PolyAlgo myPolyAlgo = new HLRBRep_PolyAlgo();
333 // Add Shapes into the algorithm
334 myPolyAlgo->Load(aShape);
336 // create a projector and transformation object
338 HLRAlgo_Projector aProjector = Projector(Trsf)->Projector();
339 TopLoc_Location aLoc(Trsf);
342 myPolyAlgo->Projector(aProjector);
344 // Launches calculation of outlines of the shape
345 myPolyAlgo->Update();
347 // Build the extraction object :
348 HLRBRep_PolyHLRToShape aPolyHLRToShape;
349 aPolyHLRToShape.Update(myPolyAlgo);
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
355 TopoDS_Shape Rg1LineVCompound = aPolyHLRToShape.Rg1LineVCompound();
356 //Visible smooth edges, Smooth edges present a G1 continuity
358 TopoDS_Shape RgNLineVCompound = aPolyHLRToShape.RgNLineVCompound();
359 //Visible sewn edges, Sewn edges present a C2 continuity
361 TopoDS_Shape OutLineVCompound = aPolyHLRToShape.OutLineVCompound();
362 //Visible outline edges
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();
370 Handle_AIS_Shape aShapeIO = drawShape(aShape);
372 if (WAIT_A_SECOND) return Standard_False;
374 getAISContext()->Erase(aShapeIO, Standard_False);
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;
386 return Standard_True;
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)
401 Standard_Boolean VShapeIsNull = aVShape.IsNull();
402 Standard_Boolean HShapeIsNull = aHShape.IsNull();
404 if (VShapeIsNull && HShapeIsNull) return Standard_True;
408 // move the shape to an absolute location
409 aVShape.Location(aLoc);
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);
415 getAISContext()->Display(anIO, HShapeIsNull);
419 // move the shape to an absolute location
420 aHShape.Location(aLoc);
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);
426 getAISContext()->Display(anIO);
429 return !WAIT_A_SECOND;
432 //////////////////////////////////////////////////////////////////////
434 //////////////////////////////////////////////////////////////////////
436 //================================================================
437 // Function : HLR_Presentation::sampleCylinder
439 //================================================================
440 void HLR_Presentation::sampleCylinder()
442 // define cylinder's radius and height
443 Standard_Real aRadius = 20;
444 Standard_Real aHeight = 50;
447 TopoDS_Solid aShape = BRepPrimAPI_MakeCylinder(aRadius, aHeight);
449 Standard_Integer nbIso = 3;
450 if (!HLR(aShape, nbIso)) return;
453 //================================================================
454 // Function : HLR_Presentation::sampleTorus
456 //================================================================
457 void HLR_Presentation::sampleTorus()
459 //define toroidal surface's axis
460 gp_Ax3 anAx3(gp_Pnt(0,0,0), gp_Dir(1,0,0));
462 //define two radiuses
463 Standard_Real MajorRadius = 20;
464 Standard_Real MinorRadius = 10;
467 Handle_Geom_ToroidalSurface aTorSurface = new Geom_ToroidalSurface(anAx3, MajorRadius, MinorRadius);
468 TopoDS_Shell aShape = BRepBuilderAPI_MakeShell(aTorSurface);
470 Standard_Integer nbIso = 3;
471 if (!HLR(aShape, nbIso)) return;
475 //================================================================
476 // Function : HLR_Presentation::sampleBox
478 //================================================================
479 void HLR_Presentation::sampleBrepShape2()
481 // read a shape from shape2.brep file in DATA dir
483 BRep_Builder aBuilder;
484 TCollection_AsciiString aFileName(GetDataDir());
485 aFileName += "\\shape2.brep";
486 Standard_Boolean Result = BRepTools::Read(aShape, aFileName.ToCString(), aBuilder);
490 aFileName += " was not found. The sample can not be shown.";
491 setResultText(aFileName.ToCString());
495 Standard_Integer nbIso = 0;
496 if (!HLR(aShape, nbIso)) return;
500 //================================================================
501 // Function : HLR_Presentation::sampleShape1
503 //================================================================
504 void HLR_Presentation::sampleBrepShape1()
506 // read a shape from shape1.brep file in DATA dir
508 BRep_Builder aBuilder;
509 TCollection_AsciiString aFileName(GetDataDir());
510 aFileName += "\\shape1.brep";
511 Standard_Boolean Result = BRepTools::Read(aShape, aFileName.ToCString(), aBuilder);
515 aFileName += " was not found. The sample can not be shown.";
516 setResultText(aFileName.ToCString());
520 // triangulating the shape
521 Standard_Real Deflection = 50;
522 BRepMesh::Mesh(aShape, Deflection);
524 if (!PolyHLR(aShape)) return;