0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / TopOpeBRepDS / TopOpeBRepDS_connex.cxx
1 // Created on: 1997-11-25
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 #include <TopOpeBRepDS_connex.hxx>
18
19 #include <TopExp_Explorer.hxx>
20 #include <TopoDS.hxx>
21
22 static TopTools_DataMapOfShapeListOfShape *GLOBAL_elf1 = NULL; // NYI to CDLize as a tool of DS
23 static TopTools_DataMapOfShapeListOfShape *GLOBAL_elf2 = NULL; // NYI to CDLize as a tool of DS
24 static TopTools_DataMapOfShapeListOfShape *GLOBAL_fle = NULL;  // NYI to CDLize as a tool of DS
25 static TopTools_ListOfShape *GLOBAL_los = NULL;  // NYI to CDLize as a tool of DS
26 static Standard_Boolean GLOBAL_FDSCNX_prepared = Standard_False;
27
28 //modified by NIZNHY-PKV Sun Dec 15 17:41:43 2002 f
29 //=======================================================================
30 //function : FDSCNX_Close
31 //purpose  : 
32 //=======================================================================
33 void FDSCNX_Close()
34 {
35   if (GLOBAL_elf1) {
36     delete GLOBAL_elf1;
37     GLOBAL_elf1=NULL;
38   }
39   //
40   if (GLOBAL_elf2) {
41     delete GLOBAL_elf2;
42     GLOBAL_elf2=NULL;
43   }
44   //
45   if (GLOBAL_fle) {
46     delete GLOBAL_fle;
47     GLOBAL_fle=NULL;
48   }
49   //
50   if (GLOBAL_los) {
51     delete GLOBAL_los;
52     GLOBAL_los=NULL;
53   }
54   //
55   GLOBAL_FDSCNX_prepared = Standard_False;
56 }
57 //modified by NIZNHY-PKV Sun Dec 15 17:41:40 2002 t
58
59 Standard_EXPORT const TopTools_ListOfShape& FDSCNX_EdgeConnexityShapeIndex(const TopoDS_Shape& E,const Handle(TopOpeBRepDS_HDataStructure)& HDS,const Standard_Integer SI) 
60 {
61   if (HDS.IsNull()) return *GLOBAL_los;
62   if (!GLOBAL_FDSCNX_prepared) return *GLOBAL_los;
63   if (SI != 1 && SI != 2) return *GLOBAL_los;
64   const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
65   TopAbs_ShapeEnum t = E.ShapeType(); if (t != TopAbs_EDGE) return *GLOBAL_los;
66   Standard_Boolean has = FDSCNX_HasConnexFace(E,HDS); if (!has) return *GLOBAL_los;
67   Standard_Integer re = BDS.AncestorRank(E); if (re == 0) return *GLOBAL_los;
68   TopTools_DataMapOfShapeListOfShape* pelf = (SI == 1) ? GLOBAL_elf1 : GLOBAL_elf2;
69   TopTools_DataMapOfShapeListOfShape& elf = *pelf;
70   const TopTools_ListOfShape& lof = elf.Find(E);
71   return lof;
72 }
73
74 // S = edge --> liste de faces connexes par S
75 // S = face --> liste d'edges E de S qui ont au moins une autre face connexe
76 Standard_EXPORT const TopTools_ListOfShape& FDSCNX_EdgeConnexitySameShape(const TopoDS_Shape& S,const Handle(TopOpeBRepDS_HDataStructure)& HDS) 
77 {
78   TopAbs_ShapeEnum t = S.ShapeType();
79   if      (t == TopAbs_EDGE) {
80     Standard_Integer si = HDS->DS().AncestorRank(S);
81     const TopTools_ListOfShape& lf = FDSCNX_EdgeConnexityShapeIndex(S,HDS,si);
82     return lf;
83   }
84   else if (t == TopAbs_FACE) {
85     TopTools_DataMapOfShapeListOfShape& fle = *GLOBAL_fle;
86     if (fle.IsBound(S)) {
87       const TopTools_ListOfShape& le = fle.Find(S);
88       return le;
89     }
90   }
91   return *GLOBAL_los;
92 }
93
94 Standard_EXPORT void FDSCNX_Prepare(const TopoDS_Shape& /*S1*/,
95                                     const TopoDS_Shape& /*S2*/,
96                                     const Handle(TopOpeBRepDS_HDataStructure)& HDS)
97 {
98   if (HDS.IsNull()) {
99     GLOBAL_FDSCNX_prepared = Standard_False;
100     return;
101   }
102   const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
103   if (GLOBAL_elf1 == NULL) GLOBAL_elf1 = (TopTools_DataMapOfShapeListOfShape*) new TopTools_DataMapOfShapeListOfShape();
104   if (GLOBAL_elf2 == NULL) GLOBAL_elf2 = (TopTools_DataMapOfShapeListOfShape*) new TopTools_DataMapOfShapeListOfShape();
105   if (GLOBAL_fle == NULL) GLOBAL_fle = (TopTools_DataMapOfShapeListOfShape*) new TopTools_DataMapOfShapeListOfShape();
106   if (GLOBAL_los == NULL) GLOBAL_los = (TopTools_ListOfShape*) new TopTools_ListOfShape();
107   GLOBAL_elf1->Clear();
108   GLOBAL_elf2->Clear();
109   GLOBAL_fle->Clear();
110   GLOBAL_los->Clear();
111
112   Standard_Integer i=0,n=BDS.NbShapes();
113   for (i=1;i<=n;i++) {
114     const TopoDS_Shape& f = BDS.Shape(i); if (f.ShapeType() != TopAbs_FACE) continue;
115     Standard_Integer rf = BDS.AncestorRank(f); if (rf == 0) continue;
116 //    BDS.Shape(f);
117     TopTools_DataMapOfShapeListOfShape& fle = *GLOBAL_fle;
118     TopTools_DataMapOfShapeListOfShape& elf = (rf == 1) ? *GLOBAL_elf1 : *GLOBAL_elf2; 
119     TopExp_Explorer exe;
120     for (exe.Init(f,TopAbs_EDGE);exe.More();exe.Next()) {
121 //    for (TopExp_Explorer exe(f,TopAbs_EDGE);exe.More();exe.Next()) {
122       const TopoDS_Shape& e = exe.Current();
123 //               BDS.Shape(e);
124 //      Standard_Boolean se = BDS.IsSectionEdge(TopoDS::Edge(e)); if (!se) continue;
125       Standard_Boolean hs = BDS.HasShape(TopoDS::Edge(e)); if (!hs) continue;
126       TopTools_ListOfShape thelist, thelist1;
127       if (!fle.IsBound(f)) fle.Bind(f, thelist); fle.ChangeFind(f).Append(e);
128       if (!elf.IsBound(e)) elf.Bind(e, thelist1); elf.ChangeFind(e).Append(f);
129     }
130   }
131   GLOBAL_FDSCNX_prepared = Standard_True;
132 }
133
134 Standard_EXPORT Standard_Boolean FDSCNX_HasConnexFace(const TopoDS_Shape& S,const Handle(TopOpeBRepDS_HDataStructure)& HDS) 
135 {
136   if (HDS.IsNull()) return Standard_False; const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
137   const TopAbs_ShapeEnum t = S.ShapeType(); if (t != TopAbs_FACE && t != TopAbs_EDGE) return Standard_False;
138   const Standard_Integer rs = BDS.AncestorRank(S); if (rs == 0) return Standard_False;
139   TopTools_DataMapOfShapeListOfShape* pelf = (rs == 1) ? GLOBAL_elf1 : GLOBAL_elf2; if (pelf == NULL) return Standard_False;
140   TopTools_DataMapOfShapeListOfShape& fle = *GLOBAL_fle; 
141   TopTools_DataMapOfShapeListOfShape& elf = *pelf;
142   Standard_Boolean has = Standard_False;
143   if      (t == TopAbs_EDGE) has = elf.IsBound(S);
144   else if (t == TopAbs_FACE) has = fle.IsBound(S);
145   return has;
146 }
147
148 Standard_EXPORT void FDSCNX_FaceEdgeConnexFaces(const TopoDS_Shape& F,const TopoDS_Shape& E,const Handle(TopOpeBRepDS_HDataStructure)& HDS,TopTools_ListOfShape& LF) 
149 {
150   LF.Clear();
151 #ifdef OCCT_DEBUG
152 //  Standard_Integer Fi =
153 #endif
154 //           HDS->Shape(F);
155 #ifdef OCCT_DEBUG
156 //  Standard_Integer Ei =
157 #endif
158 //           HDS->Shape(E);
159
160   // verifier que E est une arete de connexite de F
161   Standard_Boolean EofF = Standard_False;
162   const TopTools_ListOfShape& loe = FDSCNX_EdgeConnexitySameShape(F,HDS); if (loe.IsEmpty()) return;
163   for (TopTools_ListIteratorOfListOfShape i(loe);i.More();i.Next()) {
164     const TopoDS_Shape& e = i.Value();
165 //             HDS->Shape(e);
166     Standard_Boolean b = e.IsSame(E);
167     if (b) {
168       EofF=Standard_True;
169       break;
170     }
171   }
172   if (!EofF) return;
173   
174   const TopTools_ListOfShape& lof = FDSCNX_EdgeConnexitySameShape(E,HDS); if (lof.IsEmpty()) return;
175   for (TopTools_ListIteratorOfListOfShape it(lof);it.More();it.Next()) {
176     const TopoDS_Shape& f = it.Value();
177 #ifdef OCCT_DEBUG
178 //    Standard_Integer fi =
179 #endif
180 //             HDS->Shape(f);
181     Standard_Boolean b = f.IsSame(F);
182     if (!b) {
183       LF.Append(f);
184     }
185   }
186 }
187
188 Standard_EXPORT void FDSCNX_DumpIndex(const Handle(TopOpeBRepDS_HDataStructure)& HDS,const Standard_Integer I)
189 {
190   if (HDS.IsNull()) return; const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
191   Standard_Integer ns=BDS.NbShapes();
192   if (I<1 || I>ns) return;
193   Standard_Integer i=I;
194   const TopoDS_Shape& s=BDS.Shape(i);
195 #ifdef OCCT_DEBUG
196 //  Standard_Integer is=
197 #endif
198 //         BDS.Shape(s);
199   TopAbs_ShapeEnum ts=s.ShapeType();
200   const TopTools_ListOfShape& ls=FDSCNX_EdgeConnexitySameShape(s,HDS);
201   if      (ts == TopAbs_EDGE) {
202     TopTools_ListIteratorOfListOfShape ils(ls);if(!ils.More())return;
203     for(;ils.More();ils.Next())cout<<BDS.Shape(ils.Value())<<" ";
204   }
205   else if (ts == TopAbs_FACE) {
206     TopTools_ListIteratorOfListOfShape ils(ls);if(!ils.More())return;
207     for(;ils.More();ils.Next()) {
208       const TopoDS_Shape& e=ils.Value();
209 #ifdef OCCT_DEBUG
210 //      Standard_Integer ie=BDS.Shape(e);
211 //      TopAbs_ShapeEnum te=e.ShapeType();
212 #endif
213       TopTools_ListOfShape lf;FDSCNX_FaceEdgeConnexFaces(s,e,HDS,lf);
214       TopTools_ListIteratorOfListOfShape ilf(lf);if(!ilf.More())continue;
215       for(;ilf.More();ilf.Next())cout<<BDS.Shape(ilf.Value())<<" ";
216     }
217   }
218 }
219
220 Standard_EXPORT void FDSCNX_Dump(const Handle(TopOpeBRepDS_HDataStructure)& HDS,const Standard_Integer I)
221 {
222   if (HDS.IsNull()) return; const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
223   Standard_Integer ns=BDS.NbShapes();
224   if (I<1 || I>ns) return;
225   Standard_Integer i=I;
226   const TopoDS_Shape& s=BDS.Shape(i);Standard_Integer is=BDS.Shape(s);TopAbs_ShapeEnum ts=s.ShapeType();
227   const TopTools_ListOfShape& ls=FDSCNX_EdgeConnexitySameShape(s,HDS);
228   if      (ts == TopAbs_EDGE) {
229     TopTools_ListIteratorOfListOfShape ils(ls);if(!ils.More())return;
230     cout<<"clear;";
231     for(;ils.More();ils.Next())cout<<"tsee f "<<BDS.Shape(ils.Value())<<";";
232     cout<<"tsee e "<<is<<";### edge "<<is<<" connexity"<<endl;
233   }
234   else if (ts == TopAbs_FACE) {
235     TopTools_ListIteratorOfListOfShape ils(ls);if(!ils.More())return;
236     for(;ils.More();ils.Next()) {
237       const TopoDS_Shape& e=ils.Value();Standard_Integer ie=BDS.Shape(e);
238 #ifdef OCCT_DEBUG
239 //      TopAbs_ShapeEnum te=e.ShapeType();
240 #endif
241       TopTools_ListOfShape lf;FDSCNX_FaceEdgeConnexFaces(s,e,HDS,lf);
242       TopTools_ListIteratorOfListOfShape ilf(lf);if(!ilf.More())continue;
243       cout<<"clear;";cout<<"tsee f "<<is<<";";
244       for(;ilf.More();ilf.Next())cout<<"tsee f "<<BDS.Shape(ilf.Value())<<";";
245       cout<<"tsee e "<<ie<<";### face "<<is<<" connexity"<<endl;
246     }
247   }
248 }
249
250 Standard_EXPORT void FDSCNX_Dump(const Handle(TopOpeBRepDS_HDataStructure)& HDS)
251 {
252   if (HDS.IsNull()) return; const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
253   Standard_Integer i=1,ns=BDS.NbShapes();
254   for (i=1;i<=ns;i++) FDSCNX_Dump(HDS,i);
255 }