0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / BOPDS / BOPDS_Iterator.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18
19 #include <Bnd_Box.hxx>
20 #include <BOPCol_BoxBndTree.hxx>
21 #include <BOPCol_NCVector.hxx>
22 #include <BOPCol_Parallel.hxx>
23 #include <BOPDS_DS.hxx>
24 #include <BOPDS_IndexRange.hxx>
25 #include <BOPDS_Iterator.hxx>
26 #include <BOPDS_MapOfPassKeyBoolean.hxx>
27 #include <BOPDS_PassKeyBoolean.hxx>
28 #include <BOPDS_Tools.hxx>
29 #include <NCollection_IncAllocator.hxx>
30 #include <NCollection_UBTreeFiller.hxx>
31 #include <TopoDS_Shape.hxx>
32
33 //
34 //
35 //
36 //
37 //
38 /////////////////////////////////////////////////////////////////////////
39 //=======================================================================
40 //class    : BOPDS_TreeSelector
41 //purpose  : 
42 //=======================================================================
43 class BOPDS_TSR : public BOPCol_BoxBndTreeSelector{
44  public:
45   BOPDS_TSR() : 
46     BOPCol_BoxBndTreeSelector(), 
47     myHasBRep(Standard_False), 
48     myTree(NULL) {
49   }
50   //
51   virtual ~BOPDS_TSR() {
52   }
53   //
54   void SetHasBRep(const Standard_Boolean bFlag) {
55     myHasBRep=bFlag;
56   }
57   //
58   void SetTree(BOPCol_BoxBndTree& aTree) {
59     myTree=&aTree;
60   }
61   //
62   void Perform() {
63     if (myHasBRep) {
64       myTree->Select(*this);
65     }
66   }
67   //
68  protected:
69   Standard_Boolean myHasBRep;
70   BOPCol_BoxBndTree *myTree;
71 };
72 //
73 //=======================================================================
74 typedef BOPCol_NCVector <BOPDS_TSR> BOPDS_VectorOfTSR; 
75 typedef BOPCol_Functor <BOPDS_TSR,BOPDS_VectorOfTSR> BOPDS_TSRFunctor;
76 typedef BOPCol_Cnt <BOPDS_TSRFunctor, BOPDS_VectorOfTSR> BOPDS_TSRCnt;
77 /////////////////////////////////////////////////////////////////////////
78
79
80 //=======================================================================
81 //function : 
82 //purpose  : 
83 //=======================================================================
84 BOPDS_Iterator::BOPDS_Iterator()
85 :
86   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
87   myRunParallel(Standard_False)
88 {
89   Standard_Integer i, aNb;
90   //
91   myDS=NULL; 
92   myLength=0;
93   //
94   aNb=BOPDS_DS::NbInterfTypes();
95   myLists.SetIncrement(aNb);
96   for (i=0; i<aNb; ++i) {
97     myLists.Append1();
98   }
99 }
100 //=======================================================================
101 //function : 
102 //purpose  : 
103 //=======================================================================
104 BOPDS_Iterator::BOPDS_Iterator
105   (const Handle(NCollection_BaseAllocator)& theAllocator)
106 :
107   myAllocator(theAllocator),
108   myLists(0, theAllocator),
109   myRunParallel(Standard_False)
110 {
111   Standard_Integer i, aNb;
112   //
113   myDS=NULL; 
114   myLength=0;
115   //
116   aNb=BOPDS_DS::NbInterfTypes();
117   myLists.SetIncrement(aNb);
118   for (i=0; i<aNb; ++i) {
119     myLists.Append1();
120   }
121 }
122 //=======================================================================
123 //function : ~
124 //purpose  : 
125 //=======================================================================
126 BOPDS_Iterator::~BOPDS_Iterator()
127 {
128 }
129 //=======================================================================
130 //function : SetRunParallel
131 //purpose  : 
132 //=======================================================================
133 void BOPDS_Iterator::SetRunParallel(const Standard_Boolean theFlag)
134 {
135   myRunParallel=theFlag;
136 }
137 //=======================================================================
138 //function : RunParallel
139 //purpose  : 
140 //=======================================================================
141 Standard_Boolean BOPDS_Iterator::RunParallel()const
142 {
143   return myRunParallel;
144 }
145 //=======================================================================
146 // function: SetDS
147 // purpose: 
148 //=======================================================================
149 void BOPDS_Iterator::SetDS(const BOPDS_PDS& aDS)
150 {
151   myDS=aDS;
152 }
153 //=======================================================================
154 // function: DS
155 // purpose: 
156 //=======================================================================
157 const BOPDS_DS&  BOPDS_Iterator::DS()const
158 {
159   return *myDS;
160 }
161 //=======================================================================
162 // function: ExpectedLength
163 // purpose: 
164 //=======================================================================
165 Standard_Integer BOPDS_Iterator::ExpectedLength() const
166 {
167   return myLength;
168 }
169 //=======================================================================
170 // function: BlockLength
171 // purpose: 
172 //=======================================================================
173 Standard_Integer BOPDS_Iterator::BlockLength() const
174 {
175   Standard_Integer aNbIIs;
176   Standard_Real aCfPredict=.5;
177   
178   aNbIIs=ExpectedLength();
179   
180   if (aNbIIs<=1) {
181     return 1;
182   }
183   //
184   aNbIIs=(Standard_Integer) (aCfPredict*(Standard_Real)aNbIIs);
185   return aNbIIs;
186 }
187 //=======================================================================
188 // function: Initialize
189 // purpose: 
190 //=======================================================================
191 void BOPDS_Iterator::Initialize(const TopAbs_ShapeEnum aType1,
192     const TopAbs_ShapeEnum aType2)
193 {
194   Standard_Integer iX;
195   //
196   myLength=0;
197   iX=BOPDS_Tools::TypeToInteger(aType1, aType2);
198   if (iX>=0) {
199     myIterator.Initialize(myLists(iX));
200     myLength=myLists(iX).Extent();
201   }
202 }
203 //=======================================================================
204 // function: More
205 // purpose: 
206 //=======================================================================
207 Standard_Boolean BOPDS_Iterator::More()const
208 {
209   return myIterator.More();
210 }
211 //=======================================================================
212 // function: Next
213 // purpose: 
214 //=======================================================================
215 void BOPDS_Iterator::Next()
216 {
217   myIterator.Next();
218 }
219 //=======================================================================
220 // function: Value
221 // purpose: 
222 //=======================================================================
223 void BOPDS_Iterator::Value
224   (Standard_Integer& theI1,
225    Standard_Integer& theI2,
226    Standard_Boolean& theWithSubShape) const
227 {
228   Standard_Integer iT1, iT2, n1, n2;
229   //
230   const BOPDS_PassKeyBoolean& aPKB=myIterator.Value();
231   aPKB.Ids(n1, n2);
232   //
233   iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType());
234   iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType());
235   //
236   theI1=n1;
237   theI2=n2;
238   if (iT1<iT2) {
239     theI1=n2;
240     theI2=n1;
241   }
242   //
243   theWithSubShape=aPKB.Flag();
244 }
245 //=======================================================================
246 // function: Prepare
247 // purpose: 
248 //=======================================================================
249 void BOPDS_Iterator::Prepare()
250 {
251   Standard_Integer i, aNbInterfTypes;
252   //
253   aNbInterfTypes=BOPDS_DS::NbInterfTypes();
254   myLength=0;
255   for (i=0; i<aNbInterfTypes; ++i) {
256     myLists(i).Clear();
257   }
258   //
259   if (myDS==NULL){
260     return;
261   }
262   Intersect();
263 }
264 //
265 //=======================================================================
266 // function: Intersect
267 // purpose: 
268 //=======================================================================
269 void BOPDS_Iterator::Intersect()
270 {
271   Standard_Boolean bFlag;
272   Standard_Integer aNb, i, aNbR, iTi, iTj;
273   Standard_Integer i1, i2, aNbSD, iX, j, iR;
274   TopAbs_ShapeEnum aTi, aTj;
275   Handle(NCollection_IncAllocator) aAllocator;
276   BOPCol_ListIteratorOfListOfInteger aIt;
277   //
278   //-----------------------------------------------------scope_1 f
279   aAllocator=new NCollection_IncAllocator();
280   //
281   BOPDS_MapOfPassKeyBoolean aMPKXB(100, aAllocator);
282   BOPDS_PassKeyBoolean aPKXB; 
283   //
284   BOPCol_BoxBndTreeSelector aSelector;
285   BOPCol_BoxBndTree aBBTree;
286   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
287   //
288   aNb=myDS->NbSourceShapes();
289   BOPDS_VectorOfTSR aVTSR(aNb, aAllocator);
290   //
291   for (i=0; i<aNb; ++i) {
292     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
293     bFlag=aSI.IsInterfering();
294     //
295     BOPDS_TSR& aTSR=aVTSR.Append1();
296     //
297     aTSR.SetHasBRep(bFlag);
298     if (!bFlag) {
299       continue;
300     }
301     //
302     const Bnd_Box& aBoxEx=aSI.Box();
303     aTSR.SetTree(aBBTree);
304     aTSR.SetBox(aBoxEx);
305     //
306     aTreeFiller.Add(i, aBoxEx);
307   }
308   //
309   aTreeFiller.Fill();
310   //
311   //===========================================
312   BOPDS_TSRCnt::Perform(myRunParallel, aVTSR);
313   //===========================================
314   //
315   aNbR=myDS->NbRanges()-1;
316   for (iR=0; iR<aNbR; ++iR) {
317     const BOPDS_IndexRange& aR=myDS->Range(iR);
318     i1=aR.First();
319     i2=aR.Last();
320     for (i=i1; i<=i2; ++i) {
321       const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
322       //
323       if (!aSI.IsInterfering()){
324         continue;
325       }
326       //
327       aTi=aSI.ShapeType();
328       const Bnd_Box& aBoxi=aSI.Box();
329       //
330       BOPDS_TSR& aTSRi=aVTSR(i);
331       const BOPCol_ListOfInteger& aLI=aTSRi.Indices();
332       aNbSD=aLI.Extent();
333       if (!aNbSD){
334         continue;
335       }
336       //
337       aIt.Initialize(aLI);
338       for (; aIt.More(); aIt.Next()) {
339         j=aIt.Value(); // DS index
340         if (j>=i1 && j<=i2) {
341           continue;// same range
342         }
343         //
344         const BOPDS_ShapeInfo& aSIj=myDS->ShapeInfo(j);
345         aTj=aSIj.ShapeType();
346         iTi=BOPDS_Tools::TypeToInteger(aTi);
347         iTj=BOPDS_Tools::TypeToInteger(aTj);
348         //
349         bFlag=Standard_False;
350         if (iTi<iTj) {
351           bFlag=aSI.HasSubShape(j);
352         } 
353         else if (iTj<iTi) {
354           bFlag=aSIj.HasSubShape(i);
355         }
356         if (bFlag) {
357           continue; 
358         }
359         //
360         aPKXB.SetIds(i, j);
361         if (aMPKXB.Add(aPKXB)) {
362           bFlag=Standard_False;// Bounding boxes are intersected
363           const Bnd_Box& aBoxj=aSIj.Box();
364           if (aBoxi.IsOut(aBoxj)) {
365             bFlag=!bFlag; //Bounding boxes of Sub-shapes are intersected
366           }
367           //
368           iX=BOPDS_Tools::TypeToInteger(aTi, aTj);
369           aPKXB.SetFlag(bFlag);
370           myLists(iX).Append(aPKXB);
371         }// if (aMPKXB.Add(aPKXB)) {
372       }// for (; aIt.More(); aIt.Next()) {
373     }//for (i=i1; i<=i2; ++i) {
374   }//for (iR=1; iR<aNbR; ++iR) {
375   //
376   aMPKXB.Clear();
377   aVTSR.Clear();
378   aAllocator.Nullify();
379   //-----------------------------------------------------scope_1 t
380 }