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