62b004b05b0490ab0804d3dabe0bdcaca9100a60
[occt.git] / src / StdPrs / StdPrs_HLRPolyShape.cxx
1 // Created on: 1995-09-19
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <BRepMesh_IncrementalMesh.hxx>
19 #include <Graphic3d_ArrayOfPolylines.hxx>
20 #include <Graphic3d_Group.hxx>
21 #include <HLRAlgo_EdgeIterator.hxx>
22 #include <HLRAlgo_EdgeStatus.hxx>
23 #include <HLRBRep_BiPoint.hxx>
24 #include <HLRBRep_ListIteratorOfListOfBPoint.hxx>
25 #include <HLRBRep_ListOfBPoint.hxx>
26 #include <HLRBRep_PolyAlgo.hxx>
27 #include <Prs3d_LineAspect.hxx>
28 #include <Prs3d_Presentation.hxx>
29 #include <Prs3d_Projector.hxx>
30 #include <StdPrs_HLRPolyShape.hxx>
31 #include <StdPrs_WFShape.hxx>
32 #include <TColgp_SequenceOfPnt.hxx>
33 #include <TopAbs.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS_Shape.hxx>
36
37 #define PntX1 ((Standard_Real*)Coordinates)[0]
38 #define PntY1 ((Standard_Real*)Coordinates)[1]
39 #define PntZ1 ((Standard_Real*)Coordinates)[2]
40 #define PntX2 ((Standard_Real*)Coordinates)[3]
41 #define PntY2 ((Standard_Real*)Coordinates)[4]
42 #define PntZ2 ((Standard_Real*)Coordinates)[5]
43
44 //=======================================================================
45 //function : Add
46 //purpose  : 
47 //=======================================================================
48
49 void StdPrs_HLRPolyShape::Add(const Handle (Prs3d_Presentation)& aPresentation,
50                               const TopoDS_Shape&                 aShape,
51                               const Handle (Prs3d_Drawer)&        aDrawer,
52                               const Handle (Prs3d_Projector)&     aProjector)
53 {
54   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(aPresentation);
55
56   TopExp_Explorer ex;
57
58   // find vertices not under ancestors.
59   TopAbs_ShapeEnum E = aShape.ShapeType();
60   if (E == TopAbs_COMPOUND) {
61     // it is necessary to present isolated vertexes instead of hiding them.
62     for (ex.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); ex.More(); ex.Next()) {
63       StdPrs_WFShape::Add(aPresentation, ex.Current(), aDrawer);
64     }
65   }
66
67   TColgp_SequenceOfPnt HiddenPnts;
68   TColgp_SequenceOfPnt SeenPnts;
69
70   if (aDrawer->IsAutoTriangulation())
71   {
72     const Standard_Boolean aRel = aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE;
73     Standard_Real aDef = aRel ? aDrawer->HLRDeviationCoefficient() : aDrawer->MaximalChordialDeviation();
74     BRepMesh_IncrementalMesh mesh(aShape, aDef, aRel, aDrawer->HLRAngle());
75   }
76   
77   Handle(HLRBRep_PolyAlgo) hider = new HLRBRep_PolyAlgo(aShape);
78
79   hider->Projector(aProjector->Projector());
80   hider->Angle(aDrawer->HLRAngle());
81   hider->Update();
82   Standard_Real sta,end,dx,dy,dz;
83   Standard_ShortReal tolsta, tolend;
84   HLRAlgo_EdgeStatus status;
85   HLRAlgo_EdgeIterator It;
86   Standard_Boolean reg1,regn,outl, intl;
87   Standard_Address Coordinates;
88   TopoDS_Shape S;
89
90   HLRBRep_ListOfBPoint BiPntVis, BiPntHid;
91   
92   for (hider->InitHide(); hider->MoreHide(); hider->NextHide())
93   {
94     hider->Hide(Coordinates, status, S, reg1, regn, outl, intl);
95     
96     dx = PntX2 - PntX1;
97     dy = PntY2 - PntY1;
98     dz = PntZ2 - PntZ1;
99     
100     for (It.InitVisible(status); It.MoreVisible(); It.NextVisible())
101     {
102       It.Visible(sta,tolsta,end,tolend);
103       BiPntVis.Append
104         (HLRBRep_BiPoint
105            (PntX1 + sta * dx,PntY1 + sta * dy,PntZ1 + sta * dz,
106             PntX1 + end * dx,PntY1 + end * dy,PntZ1 + end * dz,
107             S,reg1,regn,outl,intl));
108     }
109     
110     for (It.InitHidden(status); It.MoreHidden(); It.NextHidden())
111     {
112       It.Hidden(sta,tolsta,end,tolend);
113       BiPntHid.Append
114         (HLRBRep_BiPoint
115            (PntX1 + sta * dx,PntY1 + sta * dy,PntZ1 + sta * dz,
116             PntX1 + end * dx,PntY1 + end * dy,PntZ1 + end * dz,
117             S,reg1,regn,outl,intl));    
118     }
119   }
120   
121   // storage in the group:
122   HLRBRep_ListIteratorOfListOfBPoint ItB;
123
124   if (aDrawer->DrawHiddenLine()) {
125     for (ItB.Initialize(BiPntHid); ItB.More(); ItB.Next()) {
126       const HLRBRep_BiPoint& BP = ItB.Value();
127       if (!BP.RgNLine() || BP.OutLine()) {
128         HiddenPnts.Append(BP.P1());
129         HiddenPnts.Append(BP.P2());
130       }
131     }
132   }
133
134   for (ItB.Initialize(BiPntVis); ItB.More(); ItB.Next()) {
135     const HLRBRep_BiPoint& BP = ItB.Value();
136     if (!BP.RgNLine() || BP.OutLine()) {
137       SeenPnts.Append(BP.P1());
138       SeenPnts.Append(BP.P2());
139     }
140   }
141
142   Standard_Integer nbVertices = HiddenPnts.Length();
143   if(nbVertices > 0) {
144     Handle(Graphic3d_ArrayOfPolylines) HiddenArray = new Graphic3d_ArrayOfPolylines(nbVertices, (Standard_Integer)nbVertices/2);
145     for(int i=1; i<=nbVertices; i+=2) {
146       HiddenArray->AddBound(2);
147       HiddenArray->AddVertex(HiddenPnts.Value(i));
148       HiddenArray->AddVertex(HiddenPnts.Value(i+1));
149     }
150     aGroup->SetPrimitivesAspect(aDrawer->HiddenLineAspect()->Aspect());
151     aGroup->AddPrimitiveArray(HiddenArray);
152   }
153   nbVertices = SeenPnts.Length();
154   if(nbVertices > 0) {
155     Handle(Graphic3d_ArrayOfPolylines) SeenArray = new Graphic3d_ArrayOfPolylines(nbVertices, (Standard_Integer)nbVertices/2);
156     for(int i=1; i<=nbVertices; i+=2) {
157       SeenArray->AddBound(2);
158       SeenArray->AddVertex(SeenPnts.Value(i));
159       SeenArray->AddVertex(SeenPnts.Value(i+1));
160     }
161     aGroup->SetPrimitivesAspect(aDrawer->SeenLineAspect()->Aspect());
162     aGroup->AddPrimitiveArray(SeenArray);
163   }
164 }