0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / TopOpeBRepTool / TopOpeBRepTool_BoxSort.cxx
1 // File:        TopOpeBRepTool_BoxSort.cxx
2 // Created:     Mon Jul 12 16:33:26 1993
3 // Author:      Jean Yves LEBEY
4 //              <jyl@zerox>
5
6 #include <TopOpeBRepTool_BoxSort.ixx>
7
8 #include <TopExp_Explorer.hxx>
9 #include <TopExp.hxx>
10 #include <BRep_Tool.hxx>
11 #include <TopoDS.hxx>
12 #include <TopoDS_Vertex.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Face.hxx>
15 #include <GeomAdaptor_Surface.hxx>
16 #include <Geom_Surface.hxx>
17 #include <GeomAbs_SurfaceType.hxx>
18 #include <gp_Pln.hxx>
19 #include <TopOpeBRepTool_box.hxx>
20 #include <TopOpeBRepTool_define.hxx>
21 #include <Standard_ProgramError.hxx>
22
23 #ifdef DEB
24 #define TBOX TopOpeBRepTool_GettraceBOX()
25 #endif
26
27 #define MTOhbt Handle(TopOpeBRepTool_HBoxTool)
28 #define MTClioloi TColStd_ListIteratorOfListOfInteger
29
30 //=======================================================================
31 //function : TopOpeBRepTool_BoxSort
32 //purpose  : 
33 //=======================================================================
34 TopOpeBRepTool_BoxSort::TopOpeBRepTool_BoxSort()
35 {
36 }
37
38 //=======================================================================
39 //function : TopOpeBRepTool_BoxSort
40 //purpose  : 
41 //=======================================================================
42 TopOpeBRepTool_BoxSort::TopOpeBRepTool_BoxSort(const MTOhbt& HBT)
43 {
44   SetHBoxTool(HBT);
45 }
46
47 //modified by NIZNHY-PKV Mon Dec 16 10:26:00 2002 f
48 //=======================================================================
49 //function : Destroy
50 //purpose  : alias ~TopOpeBRepTool_BoxSort
51 //=======================================================================
52 void TopOpeBRepTool_BoxSort::Destroy()
53 {
54   if (!myHBT.IsNull()) {
55     myHBT->Clear();
56   }
57 }
58 //modified by NIZNHY-PKV Mon Dec 16 10:26:02 2002 t
59
60 //=======================================================================
61 //function : SetHBoxTool
62 //purpose  : 
63 //=======================================================================
64 void TopOpeBRepTool_BoxSort::SetHBoxTool(const MTOhbt& HBT)
65 {
66   myHBT = HBT;
67 }
68
69 //=======================================================================
70 //function : HBoxTool
71 //purpose  : 
72 //=======================================================================
73 const MTOhbt& TopOpeBRepTool_BoxSort::HBoxTool() const
74 {
75   return myHBT;
76 }
77
78 //=======================================================================
79 //function : Clear
80 //purpose  : 
81 //=======================================================================
82 void TopOpeBRepTool_BoxSort::Clear()
83 {
84   myCOB.SetVoid();
85 //  myHAB.Nullify();
86 //  myHAI.Nullify();
87 }
88
89 //=======================================================================
90 //function : AddBoxes
91 //purpose  : 
92 //=======================================================================
93 void TopOpeBRepTool_BoxSort::AddBoxes(const TopoDS_Shape& S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA)
94 {
95   if (myHBT.IsNull()) myHBT = new TopOpeBRepTool_HBoxTool();
96   myHBT->AddBoxes(S,TS,TA);
97 }  
98
99 //=======================================================================
100 //function : MakeHAB
101 //purpose  : 
102 //=======================================================================
103 void TopOpeBRepTool_BoxSort::MakeHAB(const TopoDS_Shape& S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA)
104 {
105 #ifdef DEB
106   TopAbs_ShapeEnum t =
107 #endif
108                        S.ShapeType();
109   Standard_Integer n = 0; TopExp_Explorer ex;
110   for (ex.Init(S,TS,TA);ex.More();ex.Next()) n++;
111
112   myHAB = new Bnd_HArray1OfBox(0,n);
113   Bnd_Array1OfBox& AB = myHAB->ChangeArray1();
114   myHAI = new TColStd_HArray1OfInteger(0,n);
115   TColStd_Array1OfInteger& AI = myHAI->ChangeArray1();
116   
117   Standard_Integer i = 0;
118   for (ex.Init(S,TS,TA);ex.More();ex.Next()) {
119     i++;
120     const TopoDS_Shape& ss = ex.Current();    
121     Standard_Boolean hb = myHBT->HasBox(ss);
122     if (!hb) myHBT->AddBox(ss);
123     Standard_Integer im = myHBT->Index(ss);
124     const Bnd_Box& B = myHBT->Box(ss);
125     AI.ChangeValue(i) = im;
126     AB.ChangeValue(i) = B;
127   }
128
129 #ifdef DEB
130   if (TBOX) {
131     cout<<"# BS::MakeHAB : ";TopAbs::Print(t,cout);cout<<" : "<<n<<"\n";
132     cout.flush();
133   }
134 #endif
135
136 }
137
138 //=======================================================================
139 //function : HAB
140 //purpose  : 
141 //=======================================================================
142 const Handle(Bnd_HArray1OfBox)& TopOpeBRepTool_BoxSort::HAB() const
143 {
144   return myHAB;
145 }
146
147 //=======================================================================
148 //function : MakeHABCOB
149 //purpose  : 
150 //=======================================================================
151 void TopOpeBRepTool_BoxSort::MakeHABCOB(const Handle(Bnd_HArray1OfBox)& HAB,
152                                         Bnd_Box& COB)
153 {
154   COB.SetVoid();
155   Standard_Integer n = HAB->Upper();
156   const Bnd_Array1OfBox& AB = HAB->Array1();
157   for (Standard_Integer i = 1; i <= n; i++) {
158     const Bnd_Box& B = AB(i);
159     COB.Add(B);
160   }
161 }
162
163 //=======================================================================
164 //function : HABShape
165 //purpose  : 
166 //=======================================================================
167 const TopoDS_Shape& TopOpeBRepTool_BoxSort::HABShape(const Standard_Integer I) const
168 {
169   Standard_Integer iu = myHAI->Upper();
170   Standard_Boolean b = (I >= 1 && I <= iu);
171   if (!b) {
172     Standard_ProgramError::Raise("BS::Box3");
173   }
174   Standard_Integer im = myHAI->Value(I);
175   const TopoDS_Shape& S = myHBT->Shape(im);
176   return S;
177 }
178
179 //=======================================================================
180 //function : MakeCOB
181 //purpose  : 
182 //=======================================================================
183 void TopOpeBRepTool_BoxSort::MakeCOB(const TopoDS_Shape& S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA)
184 {
185   MakeHAB(S,TS,TA);
186   MakeHABCOB(myHAB,myCOB);
187   myBSB.Initialize(myCOB,myHAB);
188 #ifdef DEB
189   if (TBOX) {myHBT->DumpB(myCOB);cout<<";# BS::MakeCOB"<<endl;}
190 #endif
191 }
192
193 //=======================================================================
194 //function : AddBoxesMakeCOB
195 //purpose  : 
196 //=======================================================================
197 void TopOpeBRepTool_BoxSort::AddBoxesMakeCOB(const TopoDS_Shape& S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA)
198 {
199   AddBoxes(S,TS,TA);
200   MakeCOB(S,TS,TA);
201 }
202
203 //=======================================================================
204 //function : Compare
205 //purpose  : 
206 //=======================================================================
207 const MTClioloi& TopOpeBRepTool_BoxSort::Compare(const TopoDS_Shape &S)
208 {
209   if ( myHBT.IsNull() ) myHBT = new TopOpeBRepTool_HBoxTool();
210   
211   gp_Pln P;
212   Standard_Boolean isPlane = Standard_False;
213   TopAbs_ShapeEnum t = S.ShapeType();
214   Standard_Boolean hasb = myHBT->HasBox(S);
215   if (!hasb) myHBT->AddBox(S);
216
217   myLastCompareShape = S;
218   myLastCompareShapeBox.SetVoid();
219   
220   if ( t == TopAbs_FACE) {
221     const TopoDS_Face& F = TopoDS::Face(S);
222     Standard_Boolean natu = BRep_Tool::NaturalRestriction(F);
223     if (natu) {
224       Handle(Geom_Surface) surf = BRep_Tool::Surface(F);
225       GeomAdaptor_Surface GAS(surf); 
226       GeomAbs_SurfaceType suty = GAS.GetType();
227       isPlane = (suty == GeomAbs_Plane);
228       if (isPlane) P = GAS.Plane();
229       else {
230         myLastCompareShapeBox = myHBT->Box(F);
231       }
232     }
233     else {
234       myLastCompareShapeBox = myHBT->Box(F);
235     }
236   }
237   else if (t == TopAbs_EDGE) {
238     const TopoDS_Edge& E = TopoDS::Edge(S);
239     TopoDS_Vertex V1,V2; TopExp::Vertices(E,V1,V2);
240     Standard_Boolean perso = (V1.IsNull() || V2.IsNull());
241     if (perso) {
242       myHBT->ComputeBoxOnVertices(E,myLastCompareShapeBox);
243     } 
244     else {
245       myLastCompareShapeBox = myHBT->Box(E);
246     }
247   }
248
249   const TColStd_ListOfInteger* L;
250   if (isPlane) L = &myBSB.Compare(P);
251   else L = &myBSB.Compare(myLastCompareShapeBox);
252   myIterator.Initialize(*L);
253   
254 #ifdef DEB
255   if (TBOX) {
256     Standard_Integer nl = (*L).Extent();
257     cout<<"#------------------------"<<endl;
258     myHBT->DumpB(myLastCompareShapeBox);cout<<"; # BS::Compare"<<endl;
259     cout<<"# touche "<<nl<<" boites ";cout.flush();
260     Standard_Integer il;
261     for (MTClioloi idd((*L));idd.More();idd.Next()) {
262       il=idd.Value();cout<<il<<" ";cout.flush();
263     }
264     cout<<endl<<"#------------------------"<<endl;
265   }
266 #endif
267  
268  return myIterator;
269 }
270         
271 //=======================================================================
272 //function : TouchedShape
273 //purpose  : 
274 //=======================================================================
275 const TopoDS_Shape& TopOpeBRepTool_BoxSort::TouchedShape(const MTClioloi& LI) const
276 {
277   Standard_Integer icur = LI.Value();
278   const TopoDS_Shape& Scur = HABShape(icur);
279   return Scur;
280 }
281
282 //=======================================================================
283 //function : Box
284 //purpose  : 
285 //=======================================================================
286 const Bnd_Box& TopOpeBRepTool_BoxSort::Box(const TopoDS_Shape& S) const
287 {
288   if ( myHBT.IsNull() ) {
289     *((MTOhbt*)&myHBT) = new TopOpeBRepTool_HBoxTool();
290   }
291
292   if ( myHBT->HasBox(S) ) {
293     const Bnd_Box& B = myHBT->Box(S);
294     return B;
295   }
296   else if ( !myLastCompareShape.IsNull() ) {
297     if ( S.IsEqual(myLastCompareShape) ) {
298       if ( !myLastCompareShapeBox.IsVoid() ) {
299         return myLastCompareShapeBox;
300       }
301     }
302   }
303   
304   const Bnd_Box& B = myHBT->Box(S);
305   return B;
306 }