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
7 // This file is part of Open CASCADE Technology software library.
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.
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
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_Pair.hxx>
27 #include <BOPDS_MapOfPair.hxx>
28 #include <BOPDS_Tools.hxx>
29 #include <NCollection_UBTreeFiller.hxx>
30 #include <TopoDS_Shape.hxx>
33 /////////////////////////////////////////////////////////////////////////
34 //=======================================================================
35 //class : BOPDS_TreeSelector
37 //=======================================================================
38 class BOPDS_TSR : public BOPCol_BoxBndTreeSelector{
41 BOPCol_BoxBndTreeSelector(),
42 myHasBRep(Standard_False),
46 virtual ~BOPDS_TSR() {
49 void SetHasBRep(const Standard_Boolean bFlag) {
53 void SetTree(BOPCol_BoxBndTree& aTree) {
59 myTree->Select(*this);
64 Standard_Boolean myHasBRep;
65 BOPCol_BoxBndTree *myTree;
68 //=======================================================================
69 typedef BOPCol_NCVector <BOPDS_TSR> BOPDS_VectorOfTSR;
70 typedef BOPCol_Functor <BOPDS_TSR,BOPDS_VectorOfTSR> BOPDS_TSRFunctor;
71 typedef BOPCol_Cnt <BOPDS_TSRFunctor, BOPDS_VectorOfTSR> BOPDS_TSRCnt;
72 /////////////////////////////////////////////////////////////////////////
75 //=======================================================================
78 //=======================================================================
79 BOPDS_Iterator::BOPDS_Iterator()
81 myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
82 myRunParallel(Standard_False)
84 Standard_Integer i, aNb;
89 aNb=BOPDS_DS::NbInterfTypes();
90 myLists.SetIncrement(aNb);
91 for (i=0; i<aNb; ++i) {
95 //=======================================================================
98 //=======================================================================
99 BOPDS_Iterator::BOPDS_Iterator
100 (const Handle(NCollection_BaseAllocator)& theAllocator)
102 myAllocator(theAllocator),
103 myLists(0, theAllocator),
104 myRunParallel(Standard_False)
106 Standard_Integer i, aNb;
111 aNb=BOPDS_DS::NbInterfTypes();
112 myLists.SetIncrement(aNb);
113 for (i=0; i<aNb; ++i) {
117 //=======================================================================
120 //=======================================================================
121 BOPDS_Iterator::~BOPDS_Iterator()
124 //=======================================================================
125 //function : SetRunParallel
127 //=======================================================================
128 void BOPDS_Iterator::SetRunParallel(const Standard_Boolean theFlag)
130 myRunParallel=theFlag;
132 //=======================================================================
133 //function : RunParallel
135 //=======================================================================
136 Standard_Boolean BOPDS_Iterator::RunParallel()const
138 return myRunParallel;
140 //=======================================================================
143 //=======================================================================
144 void BOPDS_Iterator::SetDS(const BOPDS_PDS& aDS)
148 //=======================================================================
151 //=======================================================================
152 const BOPDS_DS& BOPDS_Iterator::DS()const
156 //=======================================================================
157 // function: ExpectedLength
159 //=======================================================================
160 Standard_Integer BOPDS_Iterator::ExpectedLength() const
164 //=======================================================================
165 // function: BlockLength
167 //=======================================================================
168 Standard_Integer BOPDS_Iterator::BlockLength() const
170 Standard_Integer aNbIIs;
171 Standard_Real aCfPredict=.5;
173 aNbIIs=ExpectedLength();
179 aNbIIs=(Standard_Integer) (aCfPredict*(Standard_Real)aNbIIs);
182 //=======================================================================
183 // function: Initialize
185 //=======================================================================
186 void BOPDS_Iterator::Initialize(const TopAbs_ShapeEnum aType1,
187 const TopAbs_ShapeEnum aType2)
192 iX=BOPDS_Tools::TypeToInteger(aType1, aType2);
194 // sort interfering pairs for constant order of intersection
195 std::stable_sort(myLists(iX).begin(), myLists(iX).end());
196 // initialize iterator to access the pairs
197 myIterator.Init(myLists(iX));
198 myLength = myLists(iX).Extent();
201 //=======================================================================
204 //=======================================================================
205 Standard_Boolean BOPDS_Iterator::More()const
207 return myIterator.More();
209 //=======================================================================
212 //=======================================================================
213 void BOPDS_Iterator::Next()
217 //=======================================================================
220 //=======================================================================
221 void BOPDS_Iterator::Value(Standard_Integer& theI1,
222 Standard_Integer& theI2) const
224 Standard_Integer iT1, iT2, n1, n2;
226 const BOPDS_Pair& aPair = myIterator.Value();
227 aPair.Indices(n1, n2);
229 iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType());
230 iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType());
239 //=======================================================================
242 //=======================================================================
243 void BOPDS_Iterator::Prepare()
245 Standard_Integer i, aNbInterfTypes;
247 aNbInterfTypes=BOPDS_DS::NbInterfTypes();
249 for (i=0; i<aNbInterfTypes; ++i) {
259 //=======================================================================
260 // function: Intersect
262 //=======================================================================
263 void BOPDS_Iterator::Intersect()
265 Standard_Integer i, j, iX, i1, i2, iR, aNb, aNbR;
266 Standard_Integer iTi, iTj;
267 TopAbs_ShapeEnum aTi, aTj;
269 BOPCol_BoxBndTree aBBTree;
270 NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
272 aNb = myDS->NbSourceShapes();
273 BOPDS_VectorOfTSR aVTSR(aNb);
275 for (i=0; i<aNb; ++i) {
276 const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
277 Standard_Boolean bHasBrep = aSI.IsInterfering() && !(aSI.ShapeType() == TopAbs_SOLID);
279 BOPDS_TSR& aTSR=aVTSR.Append1();
281 aTSR.SetHasBRep(bHasBrep);
286 const Bnd_Box& aBoxEx=aSI.Box();
287 aTSR.SetTree(aBBTree);
290 aTreeFiller.Add(i, aBoxEx);
295 //===========================================
296 BOPDS_TSRCnt::Perform(myRunParallel, aVTSR);
297 //===========================================
299 BOPDS_MapOfPair aMPFence;
301 aNbR = myDS->NbRanges() - 1;
302 for (iR = 0; iR < aNbR; ++iR) {
303 const BOPDS_IndexRange& aR = myDS->Range(iR);
306 for (i = i1; i <= i2; ++i) {
307 const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(i);
309 if (!aSI.IsInterfering() || (aSI.ShapeType() == TopAbs_SOLID)) {
313 BOPDS_TSR& aTSRi = aVTSR(i);
314 const BOPCol_ListOfInteger& aLI = aTSRi.Indices();
315 Standard_Integer aNbSD = aLI.Extent();
320 aTi = aSI.ShapeType();
321 iTi = BOPDS_Tools::TypeToInteger(aTi);
323 BOPCol_ListIteratorOfListOfInteger aIt(aLI);
324 for (; aIt.More(); aIt.Next()) {
325 j = aIt.Value(); // DS index
326 if (j >= i1 && j <= i2) {
327 continue;// same range
330 const BOPDS_ShapeInfo& aSJ = myDS->ShapeInfo(j);
331 aTj = aSJ.ShapeType();
332 iTj = BOPDS_Tools::TypeToInteger(aTj);
334 // avoid interfering of the same shapes and shape with its sub-shapes
335 if (((iTi < iTj) && aSI.HasSubShape(j)) ||
336 ((iTi > iTj) && aSJ.HasSubShape(i))) {
340 BOPDS_Pair aPair(i, j);
341 if (aMPFence.Add(aPair)) {
342 iX = BOPDS_Tools::TypeToInteger(aTi, aTj);
343 myLists(iX).Append(aPair);
344 }// if (aMPFence.Add(aPair)) {
345 }// for (; aIt.More(); aIt.Next()) {
346 }//for (i=i1; i<=i2; ++i) {
347 }//for (iR=1; iR<aNbR; ++iR) {
351 //-----------------------------------------------------scope_1 t