0026515: Exponential memory usage problems with BOPDS_IndexedMapOfPaveBlock and NColl...
[occt.git] / src / BOPDS / BOPDS_SubIterator.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Bnd_Box.hxx>
17 #include <BOPCol_BoxBndTree.hxx>
18 #include <BOPCol_DataMapOfIntegerInteger.hxx>
19 #include <BOPCol_DataMapOfIntegerMapOfInteger.hxx>
20 #include <BOPCol_IndexedDataMapOfShapeBox.hxx>
21 #include <BOPCol_MapOfInteger.hxx>
22 #include <BOPDS_DS.hxx>
23 #include <BOPDS_IndexRange.hxx>
24 #include <BOPDS_MapOfPassKeyBoolean.hxx>
25 #include <BOPDS_PassKeyBoolean.hxx>
26 #include <BOPDS_SubIterator.hxx>
27 #include <BOPDS_Tools.hxx>
28 #include <NCollection_UBTreeFiller.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Vertex.hxx>
32
33 //
34 //
35 //
36 //
37 //
38 //=======================================================================
39 //function : 
40 //purpose  : 
41 //=======================================================================
42   BOPDS_SubIterator::BOPDS_SubIterator()
43 :
44   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
45   myList(myAllocator)
46 {
47   myDS=NULL; 
48 }
49 //=======================================================================
50 //function : 
51 //purpose  : 
52 //=======================================================================
53   BOPDS_SubIterator::BOPDS_SubIterator(const Handle(NCollection_BaseAllocator)& theAllocator)
54 :
55   myAllocator(theAllocator),
56   myList(myAllocator)
57 {
58   myDS=NULL; 
59 }
60 //=======================================================================
61 //function : ~
62 //purpose  : 
63 //=======================================================================
64   BOPDS_SubIterator::~BOPDS_SubIterator()
65 {
66 }
67 //=======================================================================
68 // function: SetDS
69 // purpose: 
70 //=======================================================================
71   void BOPDS_SubIterator::SetDS(const BOPDS_PDS& aDS)
72 {
73   myDS=aDS;
74 }
75 //=======================================================================
76 // function: DS
77 // purpose: 
78 //=======================================================================
79   const BOPDS_DS&  BOPDS_SubIterator::DS()const
80 {
81   return *myDS;
82 }
83 //=======================================================================
84 //function : SetSubSet1
85 //purpose  : 
86 //=======================================================================
87   void BOPDS_SubIterator::SetSubSet1(const BOPCol_ListOfInteger& theLI)
88 {
89   mySubSet1=(BOPCol_PListOfInteger)&theLI;
90 }
91 //=======================================================================
92 //function : SubSet1
93 //purpose  : 
94 //=======================================================================
95   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet1()const 
96 {
97   return *mySubSet1;
98 }
99 //=======================================================================
100 //function : SetSubSet2
101 //purpose  : 
102 //=======================================================================
103   void BOPDS_SubIterator::SetSubSet2(const BOPCol_ListOfInteger& theLI)
104 {
105   mySubSet2=(BOPCol_PListOfInteger)&theLI;
106 }
107 //=======================================================================
108 //function : SubSet2
109 //purpose  : 
110 //=======================================================================
111   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet2()const 
112 {
113   return *mySubSet2;
114 }
115 //=======================================================================
116 // function: Initialize
117 // purpose: 
118 //=======================================================================
119   void BOPDS_SubIterator::Initialize()
120 {
121   myIterator.Initialize(myList);
122 }
123 //=======================================================================
124 // function: More
125 // purpose: 
126 //=======================================================================
127   Standard_Boolean BOPDS_SubIterator::More()const
128 {
129   return myIterator.More();
130 }
131 //=======================================================================
132 // function: Next
133 // purpose: 
134 //=======================================================================
135   void BOPDS_SubIterator::Next()
136 {
137   myIterator.Next();
138 }
139 //=======================================================================
140 // function: Value
141 // purpose: 
142 //=======================================================================
143   void BOPDS_SubIterator::Value(Standard_Integer& theI1,
144                                 Standard_Integer& theI2) const
145 {
146   Standard_Integer iT1, iT2, n1, n2;
147   //
148   const BOPDS_PassKeyBoolean& aPKB=myIterator.Value();
149   aPKB.Ids(n1, n2);
150   //
151   iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType());
152   iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType());
153   //
154   theI1=n1;
155   theI2=n2;
156   if (iT1<iT2) {
157     theI1=n2;
158     theI2=n1;
159   }
160 }
161 //=======================================================================
162 // function: Prepare
163 // purpose: 
164 //=======================================================================
165   void BOPDS_SubIterator::Prepare()
166 {
167   myList.Clear();
168   //
169   if (!myDS){
170     return;
171   }
172   if (!mySubSet1->Extent() || !mySubSet2->Extent()) {
173     return;
174   }
175   Intersect();
176 }
177 //=======================================================================
178 // function: Intersect
179 // purpose: 
180 //=======================================================================
181   void BOPDS_SubIterator::Intersect()
182 {
183   Standard_Integer i, aNbB, aNbSD, j, iDS, jB;
184   Handle(NCollection_BaseAllocator) aAllocator;
185   BOPCol_ListIteratorOfListOfInteger aIt, aIt1, aIt2;
186   BOPDS_PassKeyBoolean aPKXB; 
187   //
188   //-----------------------------------------------------scope_1 f
189   aAllocator=
190     NCollection_BaseAllocator::CommonBaseAllocator();
191   //
192   BOPCol_DataMapOfShapeInteger aMSI(100, aAllocator);
193   BOPCol_DataMapOfIntegerInteger aMII(100, aAllocator);
194   BOPDS_MapOfPassKeyBoolean aMPKXB(100, aAllocator);
195   BOPCol_IndexedDataMapOfShapeBox aMSB(100, aAllocator);
196   //
197   BOPCol_BoxBndTreeSelector aSelector;
198   BOPCol_BoxBndTree aBBTree;
199   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
200   //
201   aIt1.Initialize(*mySubSet1);
202   for (; aIt1.More(); aIt1.Next()) {
203     i=aIt1.Value();
204     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
205     const TopoDS_Shape& aS=aSI.Shape();
206     const Bnd_Box& aBoxEx=aSI.Box();
207     aMSI.Bind(aS, i);
208     aMSB.Add(aS, aBoxEx);
209   }
210   //
211   aNbB=aMSB.Extent();
212   for (i=1; i<=aNbB; ++i) {
213     const TopoDS_Shape& aS=aMSB.FindKey(i);
214     const Bnd_Box& aBoxEx=aMSB(i);
215     //
216     aTreeFiller.Add(i, aBoxEx);
217     //
218     iDS=aMSI.Find(aS);
219     aMII.Bind(i, iDS);
220   }
221   //
222   aTreeFiller.Fill();
223   //
224   aIt2.Initialize(*mySubSet2);
225   for (; aIt2.More(); aIt2.Next()) {
226     i=aIt2.Value();
227     //
228     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
229     const Bnd_Box& aBoxEx=aSI.Box();
230     //
231     aSelector.Clear();
232     aSelector.SetBox(aBoxEx);
233     aNbSD=aBBTree.Select(aSelector);
234     if (!aNbSD){
235       continue;
236     }
237     //const Bnd_Box& aBoxi=myDS->ShapeInfo(i).Box();
238     //
239     const BOPCol_ListOfInteger& aLI=aSelector.Indices();
240     aIt.Initialize(aLI);
241     for (; aIt.More(); aIt.Next()) {
242       jB=aIt.Value();  // box index in MII
243       j=aMII.Find(jB); // DS index
244       //
245       aPKXB.SetIds(i, j);
246       if (aMPKXB.Add(aPKXB)) {
247         myList.Append(aPKXB);
248       }// if (aMPKXB.Add(aPKXB)) {
249     }// for (; aIt.More(); aIt.Next()) {
250   }
251   //
252   aMSI.Clear();
253   aMII.Clear();
254   aMPKXB.Clear();
255   aMSB.Clear();
256   //
257   //-----------------------------------------------------scope_1 t
258 }