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