0033040: Coding - get rid of unused headers [Storage to TopOpeBRepTool]
[occt.git] / src / TopOpeBRep / TopOpeBRep_ProcessSectionEdges.cxx
1 // Created on: 1997-06-12
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1997-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 <BRep_Tool.hxx>
19 #include <gp_Pnt.hxx>
20 #include <TColStd_ListOfInteger.hxx>
21 #include <TopExp.hxx>
22 #include <TopoDS.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <TopOpeBRep_define.hxx>
25 #include <TopOpeBRep_FacesFiller.hxx>
26 #include <TopOpeBRep_FacesIntersector.hxx>
27 #include <TopOpeBRep_FFDumper.hxx>
28 #include <TopOpeBRep_LineInter.hxx>
29 #include <TopOpeBRep_PointClassifier.hxx>
30 #include <TopOpeBRep_VPointInter.hxx>
31 #include <TopOpeBRep_VPointInterClassifier.hxx>
32 #include <TopOpeBRep_VPointInterIterator.hxx>
33 #include <TopOpeBRepDS_DataStructure.hxx>
34 #include <TopOpeBRepTool_EXPORT.hxx>
35 #include <TopOpeBRepTool_ShapeTool.hxx>
36 #include <TopOpeBRepTool_TOOL.hxx>
37 #include <TopTools_MapOfShape.hxx>
38
39 Standard_EXPORT Standard_Boolean FUN_EqualponR(const TopOpeBRep_LineInter& Lrest,
40                                   const TopOpeBRep_VPointInter& VP1,
41                                   const TopOpeBRep_VPointInter& VP2);
42 Standard_EXPORT Standard_Boolean FUN_EqualPonR(const TopOpeBRep_LineInter& Lrest,
43                                   const TopOpeBRep_VPointInter& VP1,
44                                   const TopOpeBRep_VPointInter& VP2);
45
46 //=======================================================================
47 //function : GetESL
48 //purpose  : Get list <LES> of restriction edges from the current faces 
49 //           intersector having part IN one of the 2 faces.
50 //=======================================================================
51 void TopOpeBRep_FacesFiller::GetESL(TopTools_ListOfShape& LES)
52 {
53
54 #ifdef OCCT_DEBUG
55   Standard_Boolean trRL=Standard_False;
56 #endif
57
58   TopTools_MapOfShape mapES;
59
60   // !! : do NOT use  myFacesIntersector->Restrictions()
61   // the same map is filled for all couple of faces.
62   
63   myFacesIntersector->InitLine();
64   for (; myFacesIntersector->MoreLine(); myFacesIntersector->NextLine()) {
65     const TopOpeBRep_LineInter& L = myFacesIntersector->CurrentLine();
66     TopOpeBRep_TypeLineCurve t = L.TypeLineCurve();
67     Standard_Boolean isrest = (t == TopOpeBRep_RESTRICTION);
68     
69     if (isrest) {
70       const TopoDS_Edge& E = TopoDS::Edge(L.Arc());
71       
72 #ifdef OCCT_DEBUG
73       if (trRL) {
74         TopOpeBRep_VPointInterIterator VPI;VPI.Init(L);
75         std::cout<<std::endl<<"------------ Dump Rline  --------------------"<<std::endl;
76         for (; VPI.More(); VPI.Next()) myHFFD->DumpVP(VPI.CurrentVP());
77       }
78 #endif
79       
80       Standard_Boolean add = !mapES.Contains(E);
81       if (add) {
82         Standard_Boolean checkkeep = Standard_False;
83         add = KeepRLine(L,checkkeep);
84       }
85       if (add) {
86         mapES.Add(E);
87         LES.Append(E);
88       }
89     }
90   } // loop on lines
91 }
92
93 //=======================================================================
94 //function : KeepRLine
95 //purpose  :
96 //=======================================================================
97 Standard_Boolean TopOpeBRep_FacesFiller::KeepRLine
98 (const TopOpeBRep_LineInter& L,const Standard_Boolean checkkeep) const
99
100   TopOpeBRep_TypeLineCurve t = L.TypeLineCurve();
101   Standard_Boolean isrest = (t == TopOpeBRep_RESTRICTION);
102   if (!isrest) return Standard_False;
103   const TopoDS_Edge& EL = TopoDS::Edge(L.Arc());
104   Standard_Boolean isdg = BRep_Tool::Degenerated(EL);
105   if (isdg) return Standard_False;
106
107   // look for a vpoint with transition IN/OUT or OUT/IN
108   TopOpeBRep_VPointInterIterator VPI; VPI.Init(L,checkkeep);
109   
110   // With LineConstructor, each RLine restricted by its vpbounds
111   // has its restrictions IN or ON the two faces
112   Standard_Boolean keeprline;
113   Standard_Boolean isedge1 = L.ArcIsEdge(1);
114   if (!VPI.More()) return Standard_False;
115   
116   Standard_Boolean samevp = Standard_True;
117   const TopOpeBRep_VPointInter& vpf = VPI.CurrentVP();
118   
119   TopOpeBRep_VPointInter vpl;
120   VPI.Init(L,checkkeep);
121   if (VPI.More()) VPI.Next();
122
123   Standard_Boolean middle = Standard_False; // xpu011098 : cto012U1
124   TopoDS_Vertex vv; Standard_Boolean closedE = TopOpeBRepTool_TOOL::ClosedE(EL,vv);
125   if (closedE) {
126     Standard_Real parf,parl; FUN_tool_bounds(EL,parf,parl);
127     for (; VPI.More(); VPI.Next()) {
128       vpl = VPI.CurrentVP();
129       Standard_Real pf = VPParamOnER(vpl,L);
130       Standard_Boolean middlept = (parf<pf) && (pf<parl);
131       if (middlept) {middle = Standard_True; samevp=Standard_False; break;}
132     }
133     if (middle) {
134       VPI.Init(L,checkkeep);
135       for (; VPI.More(); VPI.Next()) {
136         vpl = VPI.CurrentVP();  
137         Standard_Boolean samept = FUN_EqualPonR(L,vpf,vpl);
138         if (samept) continue;
139         else break;
140       }
141     }
142   }  
143   if (!middle) {
144     VPI.Init(L,checkkeep);
145     if (VPI.More()) VPI.Next();
146     for (; VPI.More(); VPI.Next()) {
147       vpl = VPI.CurrentVP();
148       samevp = FUN_EqualponR(L,vpf,vpl);
149       if (!samevp) break;
150     }
151   }
152
153   if (!samevp) {
154     // xpu151098 : cto 904 C8 : modif done tol2d > 0 => found restriction shared 
155     //   by circle/line
156     Standard_Boolean samept = FUN_EqualPonR(L,vpf,vpl);
157     if (samept) {
158       TopoDS_Vertex vclo; Standard_Boolean closedEL = TopOpeBRepTool_TOOL::ClosedE(EL,vclo);
159       if (closedEL) {
160         Standard_Real tolvclo = BRep_Tool::Tolerance(vclo);
161 //      Standard_Real tolvclo = BRep_Tool::Tolerance(TopoDS::Vertex(vclo));
162         gp_Pnt ptclo = BRep_Tool::Pnt(vclo);
163 //      gp_Pnt ptclo = BRep_Tool::Pnt(TopoDS::Vertex(vclo));
164         Standard_Real tolf = vpf.Tolerance(); gp_Pnt ptf = vpf.Value();
165         Standard_Real d = ptf.Distance(ptclo);
166         Standard_Boolean sameclo = (d < Max(tolvclo,tolf));
167         if (!sameclo) return Standard_False;
168       }
169       else return Standard_False;
170     }
171   }
172
173
174   Standard_Boolean out = Standard_False;
175   if (samevp) {
176     Standard_Boolean isper = TopOpeBRepTool_ShapeTool::BASISCURVE(EL)->IsPeriodic();
177
178     Standard_Integer f,l,n; L.VPBounds(f,l,n);
179     if (isper && n == 2) {
180       const TopOpeBRep_VPointInter& vpf1 = L.VPoint(f);
181       const TopOpeBRep_VPointInter& vpl1 = L.VPoint(l);
182       Standard_Integer ioo = (isedge1) ? 2 : 1;
183       TopAbs_State sf = vpf1.State(ioo), sl = vpl1.State(ioo);
184       Standard_Boolean bfl = Standard_True;
185       // xpu120898 : when projection fails we get unknown status
186       //             recall VP are same. (CTS21182,restriction edge 6)
187       Standard_Boolean bf = (sf == TopAbs_IN || sf == TopAbs_ON);
188       Standard_Boolean bl = (sl == TopAbs_IN || sl == TopAbs_ON);
189 //      bfl = bf && bl;
190       if((sf == TopAbs_UNKNOWN)||(sl == TopAbs_UNKNOWN)) bfl = bf || bl;
191       else    bfl = bf && bl;
192
193       if ( bfl ) {
194         out = Standard_False;
195       } 
196       else {
197         out = Standard_True;
198       }
199     }
200     else {
201       out = Standard_True;
202     }
203   }
204   if (out) {
205     return Standard_False;
206   }
207   
208   TopAbs_State stVPbip = StBipVPonF(vpf,vpl,L,isedge1);
209   keeprline = (stVPbip == TopAbs_IN);
210   keeprline = keeprline||(stVPbip==TopAbs_ON); // REST1
211 #ifdef OCCT_DEBUG
212 //  if (trc) {
213 //    std::cout<<" bip("<<vpf.Index()<<","<<vpl.Index()<<") of line restriction ";
214 //    std::cout<<L.Index()<<" is ";TopAbs::Print(stVPbip,std::cout);
215 //    if (keeprline) std::cout<<" : edge restriction kept"<<std::endl;
216 //    else std::cout<<" : edge restriction kept not kept"<<std::endl; 
217 //  }
218 #endif  
219     
220   return keeprline;
221 }
222
223 Standard_EXPORT Standard_Boolean FUN_brep_sdmRE(const TopoDS_Edge& E1, const TopoDS_Edge& E2) 
224 { // prequesitory : E1, E2 are restriction edges of opposite rank
225   //                found in the same FacesFiller
226   Standard_Boolean ok = Standard_False;
227   BRepAdaptor_Curve BAC;
228   TopoDS_Vertex v1,v2;TopExp::Vertices(E1,v1,v2);
229   TopoDS_Vertex v3,v4;TopExp::Vertices(E2,v3,v4);
230   if (!ok) {
231     BAC.Initialize(E1);
232     Standard_Real tol1 = BRep_Tool::Tolerance(E1);
233     Standard_Real tol2 = BRep_Tool::Tolerance(v3);
234     Standard_Real tol3 = BRep_Tool::Tolerance(v4);
235     Standard_Real tol4 = Max(tol1,Max(tol2,tol3));
236     if (!ok) {
237       const gp_Pnt& P3 = BRep_Tool::Pnt(v3);
238       ok = FUN_tool_PinC(P3,BAC,tol4);
239     }
240     if (!ok) {
241       const gp_Pnt& P4 = BRep_Tool::Pnt(v4);
242       ok = FUN_tool_PinC(P4,BAC,tol4);
243     }
244   }
245   if (!ok) {
246     BAC.Initialize(E2);
247     Standard_Real tol1 = BRep_Tool::Tolerance(E2);
248     Standard_Real tol2 = BRep_Tool::Tolerance(v1);
249     Standard_Real tol3 = BRep_Tool::Tolerance(v2);
250     Standard_Real tol4 = Max(tol1,Max(tol2,tol3));
251     if (!ok) {
252       const gp_Pnt& P1 = BRep_Tool::Pnt(v1);
253       ok = FUN_tool_PinC(P1,BAC,tol4);
254     }
255     if (!ok) {
256       const gp_Pnt& P2 = BRep_Tool::Pnt(v2);
257       ok = FUN_tool_PinC(P2,BAC,tol4);
258     }
259   }
260   return ok;
261 }
262
263 //=======================================================================
264 //function : ProcessSectionEdges
265 //purpose  : 
266 //=======================================================================
267 void TopOpeBRep_FacesFiller::ProcessSectionEdges()
268 {
269   // recuperation des aretes d'intersection mapES
270   // MSV: replace map with list to achieve predictable order of edges
271   TopTools_ListOfShape LES;
272   GetESL(LES);
273
274   // add LES edges as section edges in the DS.
275   TopTools_ListIteratorOfListOfShape itLES;
276   for (itLES.Initialize(LES); itLES.More(); itLES.Next()) {
277     const TopoDS_Edge& E = TopoDS::Edge(itLES.Value());
278
279     Standard_Boolean isdg = BRep_Tool::Degenerated(E); //xpu290698
280     if (isdg) continue;                   //xpu290698
281
282     myDS->AddSectionEdge(E);
283     myDS->Shape(E);
284     myDS->AncestorRank(E);
285   }
286
287   TColStd_ListOfInteger LOI; TColStd_ListIteratorOfListOfInteger itLOI;
288
289   // LOI = liste des rank (1 ou 2 ) des aretes de section (liste LES)
290   for( itLES.Initialize(LES); itLES.More(); itLES.Next()) {
291     const TopoDS_Edge& ELES = TopoDS::Edge(itLES.Value());
292     Standard_Boolean is1 = Standard_False;
293     Standard_Boolean is2 = Standard_False;
294     myFacesIntersector->InitLine();
295     TopoDS_Edge ELI;
296     for(;myFacesIntersector->MoreLine();myFacesIntersector->NextLine()){
297       TopOpeBRep_LineInter& L = myFacesIntersector->CurrentLine();
298       if (L.TypeLineCurve() != TopOpeBRep_RESTRICTION) continue;
299       ELI = TopoDS::Edge(L.Arc());
300       if ( ELI.IsEqual(ELES) ) {
301         is1 = L.ArcIsEdge(1);
302         is2 = L.ArcIsEdge(2);
303         break;
304       }
305     }
306     Standard_Real toappend = Standard_True;
307     if (toappend) {
308       if      (is1) LOI.Append(1);
309       else if (is2) LOI.Append(2);
310     }
311   }
312   
313   // ajout des aretes de section dans la DS de shape,connaissant leur rank
314   for (itLES.Initialize(LES),itLOI.Initialize(LOI);
315        itLES.More(),itLOI.More();
316        itLES.Next(),itLOI.Next()) {
317     const TopoDS_Shape& E1 = itLES.Value();
318     Standard_Integer rE1 = itLOI.Value();
319     myDS->AddShape(E1,rE1);
320   }
321   
322   // determination des aretes SameDomain en 3d pur
323   // mapELE(arete(1)) -> {arete(2)}
324   // mapELE(arete(2)) -> {arete(1)}
325   TopTools_DataMapOfShapeListOfShape mapELE;
326   for( itLES.Initialize(LES); itLES.More(); itLES.Next()) {
327     const TopoDS_Edge& E1 = TopoDS::Edge(itLES.Value());
328     Standard_Integer iE1 = myDS->Shape(E1);
329     Standard_Integer rE1 = myDS->AncestorRank(iE1);
330     if (rE1 != 1) continue;
331     TopTools_ListOfShape thelist;
332     mapELE.Bind(E1, thelist);
333
334     TopTools_ListIteratorOfListOfShape itLES2;
335     for (itLES2.Initialize(LES); itLES2.More(); itLES2.Next()) {
336       const TopoDS_Edge& E2 = TopoDS::Edge(itLES2.Value());
337       Standard_Integer iE2 = myDS->Shape(E2);
338       Standard_Integer rE2 = myDS->AncestorRank(iE2);
339       if ( rE2 == 0 || iE1 == iE2 || rE2 == rE1 ) continue;
340
341       Standard_Boolean toappend = FUN_brep_sdmRE(E1,E2);
342       if (toappend) { 
343         mapELE.ChangeFind(E1).Append(E2);
344       }
345     }
346   }
347
348   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmapELE;
349
350   for (itmapELE.Initialize(mapELE); itmapELE.More(); itmapELE.Next()) {
351     const TopoDS_Edge& E1 = TopoDS::Edge(itmapELE.Key());
352     Standard_Integer iE1 = myDS->Shape(E1);
353     Standard_Integer rE1 = myDS->AncestorRank(iE1);
354     const TopoDS_Face& aFace1 = TopoDS::Face(myFacesIntersector->Face(rE1));
355     Standard_Boolean isClosing1 = BRep_Tool::IsClosed(E1,aFace1);
356     TopTools_ListIteratorOfListOfShape itL(itmapELE.Value());
357     for (; itL.More(); itL.Next()) {
358       const TopoDS_Edge& E2 = TopoDS::Edge(itL.Value());
359       Standard_Integer iE2 = myDS->Shape(E2);
360       Standard_Integer rE2 = myDS->AncestorRank(iE2);
361       const TopoDS_Face& aFace2 = TopoDS::Face(myFacesIntersector->Face(rE2));
362       Standard_Boolean isClosing2 = BRep_Tool::IsClosed(E2,aFace2);
363       Standard_Boolean refFirst = isClosing1 || !isClosing2;
364       myDS->FillShapesSameDomain(E1,E2,TopOpeBRepDS_UNSHGEOMETRY,
365                                  TopOpeBRepDS_UNSHGEOMETRY, refFirst);
366     }
367   }
368 }