0026586: Eliminate compile warnings obtained by building occt with vc14: declaration...
[occt.git] / src / HLRBRep / HLRBRep_ShapeToHLR.cxx
1 // Created on: 1992-08-27
2 // Created by: Christophe MARION
3 // Copyright (c) 1992-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 #ifndef No_Exception
18 // #define No_Exception
19 #endif
20
21
22 #include <BRep_Tool.hxx>
23 #include <BRepTools.hxx>
24 #include <BRepTopAdaptor_MapOfShapeTool.hxx>
25 #include <HLRAlgo_Projector.hxx>
26 #include <HLRBRep.hxx>
27 #include <HLRBRep_Data.hxx>
28 #include <HLRBRep_ShapeToHLR.hxx>
29 #include <HLRTopoBRep_OutLiner.hxx>
30 #include <TopAbs.hxx>
31 #include <TopExp.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
42
43 //=======================================================================
44 // Function : Load
45 // Purpose  : 
46 //=======================================================================
47 Handle(HLRBRep_Data) 
48 HLRBRep_ShapeToHLR::Load(const Handle(HLRTopoBRep_OutLiner)& S,
49                          const HLRAlgo_Projector& P,
50                          BRepTopAdaptor_MapOfShapeTool& MST,
51                          const Standard_Integer nbIso)
52
53   S->Fill(P,MST,nbIso);
54
55   HLRTopoBRep_Data& TopDS = S->DataStructure();
56   TopTools_IndexedMapOfShape                FM;
57   TopTools_IndexedMapOfShape                EM;
58   TopTools_IndexedDataMapOfShapeListOfShape VerticesToEdges;
59   TopTools_IndexedDataMapOfShapeListOfShape EdgesToFaces;
60
61   TopExp_Explorer exshell,exface;
62
63   for (exshell.Init(S->OutLinedShape(), TopAbs_SHELL);
64        exshell.More(); 
65        exshell.Next()) {                             // faces in a shell
66     
67     for (exface.Init(exshell.Current(), TopAbs_FACE);
68          exface.More(); 
69          exface.Next()) {
70       if (!FM.Contains(exface.Current()))
71         FM.Add(exface.Current());
72     }
73   }
74
75   for (exface.Init(S->OutLinedShape(), TopAbs_FACE, TopAbs_SHELL);
76        exface.More(); 
77        exface.Next()) {                           // faces not in a shell
78     if (!FM.Contains(exface.Current()))
79       FM.Add(exface.Current());
80   }
81   
82   TopExp::MapShapes(S->OutLinedShape(),TopAbs_EDGE,EM);
83
84   Standard_Integer i;
85   Standard_Integer nbEdge = EM.Extent ();
86
87   for (i = 1; i <= nbEdge; i++)                 // vertices back to edges
88     TopExp::MapShapesAndAncestors
89       (EM(i), TopAbs_VERTEX, TopAbs_EDGE, VerticesToEdges);
90   
91   Standard_Integer nbVert = VerticesToEdges.Extent();
92   Standard_Integer nbFace = FM.Extent();
93
94   TopoDS_Vertex VF, VL;
95   TopTools_ListIteratorOfListOfShape itn;
96   Standard_Integer   i1, i2;
97   Standard_Boolean   o1, o2;
98   Standard_Boolean   c1, c2;
99   Standard_Real      pf, pl;
100   Standard_ShortReal tf, tl;
101
102   // Create the data structure
103   Handle(HLRBRep_Data) DS = new HLRBRep_Data (nbVert, nbEdge, nbFace);
104   HLRBRep_EdgeData* ed = NULL; 
105   if(nbEdge != 0) ed = &(DS->EDataArray().ChangeValue(1));
106 //  ed++;
107
108   for (i = 1; i <= nbFace; i++) { // test of Double edges
109     TopExp::MapShapesAndAncestors
110       (FM(i),TopAbs_EDGE, TopAbs_FACE, EdgesToFaces);
111   }
112
113   for (i = 1; i <= nbEdge; i++) {  // load the Edges
114     const TopoDS_Edge& Edg = TopoDS::Edge (EM(i));
115     TopExp::Vertices (Edg, VF, VL);
116     BRep_Tool::Range (Edg, pf, pl);
117     Standard_Boolean reg1 = Standard_False;
118     Standard_Boolean regn = Standard_False;
119     Standard_Integer inde = EdgesToFaces.FindIndex(Edg);
120     if (inde > 0) {
121       if (EdgesToFaces(inde).Extent() == 2) {
122         itn = EdgesToFaces(inde);
123         const TopoDS_Face& F1 = TopoDS::Face(itn.Value());
124         itn.Next();
125         const TopoDS_Face& F2 = TopoDS::Face(itn.Value());
126         GeomAbs_Shape rg = BRep_Tool::Continuity(Edg,F1,F2);
127         reg1 = rg >= GeomAbs_G1;
128         regn = rg >= GeomAbs_G2; 
129       }
130     }
131       
132     if (VF.IsNull()) {
133       i1 = 0;
134       o1 = Standard_False;
135       c1 = Standard_False;
136       pf = RealFirst();
137       tf = (Standard_ShortReal) Epsilon(pf);
138     }
139     else {
140       i1 = VerticesToEdges.FindIndex(VF);
141       o1 = TopDS.IsOutV(VF);
142       c1 = TopDS.IsIntV(VF);
143       tf = (Standard_ShortReal) BRep_Tool::Tolerance(VF);
144     }
145     
146     if (VL.IsNull()) {
147       i2 = 0;
148       o2 = Standard_False;
149       c2 = Standard_False;
150       pl = RealLast();
151       tl = (Standard_ShortReal) Epsilon (pl);
152     }
153     else {
154       i2 = VerticesToEdges.FindIndex(VL);
155       o2 = TopDS.IsOutV(VL);
156       c2 = TopDS.IsIntV(VL);
157       tl = (Standard_ShortReal) BRep_Tool::Tolerance(VL);
158     }
159     
160     ed->Set (reg1,regn, Edg, i1, i2, o1, o2, c1, c2, pf, tf, pl, tl);
161     DS->EdgeMap().Add(Edg);
162     ed++;
163   }
164   
165   ExploreShape(S,DS,FM,EM);
166   return DS;
167 }
168
169 //=======================================================================
170 // Function : ExploreFace
171 // Purpose  : 
172 //=======================================================================
173
174 void
175 HLRBRep_ShapeToHLR::ExploreFace(const Handle(HLRTopoBRep_OutLiner)& S,
176                                 const Handle(HLRBRep_Data)& DS,
177                                 const TopTools_IndexedMapOfShape& FM,
178                                 const TopTools_IndexedMapOfShape& EM,
179                                 Standard_Integer& i,
180                                 const TopoDS_Face& F,
181                                 const Standard_Boolean closed)
182 {
183   i++;
184   TopExp_Explorer Ex1,Ex2;
185   HLRTopoBRep_Data& TopDS = S->DataStructure();
186   TopAbs_Orientation orient = FM(i).Orientation();
187   TopoDS_Face theFace   = TopoDS::Face(FM(i));
188   theFace.Orientation (TopAbs_FORWARD);
189   HLRBRep_FaceData& fd = DS->FDataArray().ChangeValue(i);
190   
191   Standard_Integer nw = 0;
192   
193   for (Ex1.Init(theFace, TopAbs_WIRE); Ex1.More(); Ex1.Next())
194     nw++;
195
196   fd.Set (theFace, orient, closed, nw);
197   nw = 0;
198      
199   for (Ex1.Init(theFace, TopAbs_WIRE); Ex1.More(); Ex1.Next()) {
200     nw++;
201     Standard_Integer ne = 0;
202     
203     for (Ex2.Init(Ex1.Current(), TopAbs_EDGE); Ex2.More(); Ex2.Next())
204     {
205       const TopoDS_Edge& anEdge = TopoDS::Edge(Ex2.Current());
206       if (!BRep_Tool::Degenerated(anEdge))
207         ne++;
208     }
209     
210     fd.SetWire (nw, ne);
211     ne = 0;
212     
213     for (Ex2.Init(Ex1.Current(), TopAbs_EDGE);
214          Ex2.More();
215          Ex2.Next()) {
216       const TopoDS_Edge& E = TopoDS::Edge(Ex2.Current());
217       if (BRep_Tool::Degenerated(E))
218         continue;
219       ne++;
220       Standard_Integer ie   = EM.FindIndex(E);
221       TopAbs_Orientation anOrientE = E.Orientation();
222       Standard_Boolean Int  = TopDS.IsIntLFaceEdge(F,E);
223       Standard_Boolean Iso  = TopDS.IsIsoLFaceEdge(F,E);
224       Standard_Boolean Out  = TopDS.IsOutLFaceEdge(F,E);
225       Standard_Boolean Dbl  = BRepTools::IsReallyClosed(TopoDS::Edge(E),theFace);
226       fd.SetWEdge(nw, ne, ie, anOrientE, Out, Int, Dbl, Iso);
227     }
228   }
229   DS->FaceMap().Add(theFace);
230 }
231
232 //=======================================================================
233 //function : ExploreShape
234 //purpose  : 
235 //=======================================================================
236
237 void
238 HLRBRep_ShapeToHLR::ExploreShape (const Handle(HLRTopoBRep_OutLiner)& S,
239                                   const Handle(HLRBRep_Data)& DS,
240                                   const TopTools_IndexedMapOfShape& FM,
241                                   const TopTools_IndexedMapOfShape& EM)
242 {
243   TopTools_MapOfShape ShapeMap;
244   TopExp_Explorer exshell, exface, exedge;
245   Standard_Integer i = 0;
246
247   for (exshell.Init (S->OriginalShape(), TopAbs_SHELL);
248        exshell.More (); 
249        exshell.Next ()) {             // faces in a shell (open or close)
250
251     Standard_Boolean closed = exshell.Current().Closed();
252
253     if (!closed) {
254       Standard_Integer ie;
255       Standard_Integer nbEdge = EM.Extent ();
256       Standard_Integer *flag = new Standard_Integer[nbEdge + 1];
257
258       for(ie = 1; ie<=nbEdge; ie++)
259         flag[ie] = 0;
260       
261       for (exedge.Init(exshell.Current(), TopAbs_EDGE);
262            exedge.More(); 
263            exedge.Next()) {
264         const TopoDS_Edge& E = TopoDS::Edge(exedge.Current());
265         ie = EM.FindIndex(E);
266         TopAbs_Orientation orient = E.Orientation();
267         if (!BRep_Tool::Degenerated(E)) {
268           if      (orient == TopAbs_FORWARD ) flag[ie] += 1;
269           else if (orient == TopAbs_REVERSED) flag[ie] -= 1;
270         }
271       }
272       closed = Standard_True;
273
274       for (ie = 1; ie <= nbEdge && closed; ie++)
275         closed = (flag[ie] == 0); 
276       delete [] flag;
277       flag = NULL;
278     }
279       
280     for (exface.Init(exshell.Current(), TopAbs_FACE);
281          exface.More(); 
282          exface.Next()) {
283       if (ShapeMap.Add(exface.Current())) {
284         ExploreFace(S,DS,FM,EM,i,
285                     TopoDS::Face(exface.Current()),
286                     closed);
287       }
288     }
289   }
290   
291   for (exface.Init(S->OriginalShape(), TopAbs_FACE, TopAbs_SHELL);
292        exface.More(); 
293        exface.Next()) {                           // faces not in a shell
294     if (ShapeMap.Add(exface.Current())) {
295       ExploreFace(S,DS,FM,EM,i,
296                   TopoDS::Face(exface.Current()),
297                   Standard_False);
298     }
299   }
300 }