0021762: Integration of new Boolean Operation algorithm to OCCT.
[occt.git] / src / BOPDS / BOPDS_SubIterator.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19
20 #include <BOPDS_SubIterator.ixx>
21 //
22 #include <Bnd_Box.hxx>
23 //
24 #include <NCollection_BaseAllocator.hxx>
25 #include <NCollection_UBTreeFiller.hxx>
26 //
27 #include <TopoDS.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS_Shape.hxx>
30 //
31 #include <BOPCol_IndexedDataMapOfShapeBox.hxx>
32 #include <BOPCol_DataMapOfIntegerInteger.hxx>
33 #include <BOPCol_DataMapOfIntegerMapOfInteger.hxx>
34 #include <BOPCol_MapOfInteger.hxx>
35 //
36 #include <BOPDS_BoxBndTree.hxx>
37 #include <BOPDS_IndexRange.hxx>
38 #include <BOPDS_PassKeyBoolean.hxx>
39 #include <BOPDS_MapOfPassKeyBoolean.hxx>
40 #include <BOPDS_Tools.hxx>
41 #include <BOPDS_MapOfPassKeyBoolean.hxx>
42
43 //=======================================================================
44 //function : 
45 //purpose  : 
46 //=======================================================================
47   BOPDS_SubIterator::BOPDS_SubIterator()
48 :
49   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
50   myList(myAllocator)
51 {
52   myDS=NULL; 
53 }
54 //=======================================================================
55 //function : 
56 //purpose  : 
57 //=======================================================================
58   BOPDS_SubIterator::BOPDS_SubIterator(const Handle(NCollection_BaseAllocator)& theAllocator)
59 :
60   myAllocator(theAllocator),
61   myList(myAllocator)
62 {
63   myDS=NULL; 
64 }
65 //=======================================================================
66 //function : ~
67 //purpose  : 
68 //=======================================================================
69   BOPDS_SubIterator::~BOPDS_SubIterator()
70 {
71 }
72 //=======================================================================
73 // function: SetDS
74 // purpose: 
75 //=======================================================================
76   void BOPDS_SubIterator::SetDS(const BOPDS_PDS& aDS)
77 {
78   myDS=aDS;
79 }
80 //=======================================================================
81 // function: DS
82 // purpose: 
83 //=======================================================================
84   const BOPDS_DS&  BOPDS_SubIterator::DS()const
85 {
86   return *myDS;
87 }
88 //=======================================================================
89 //function : SetSubSet1
90 //purpose  : 
91 //=======================================================================
92   void BOPDS_SubIterator::SetSubSet1(const BOPCol_ListOfInteger& theLI)
93 {
94   mySubSet1=(BOPCol_PListOfInteger)&theLI;
95 }
96 //=======================================================================
97 //function : SubSet1
98 //purpose  : 
99 //=======================================================================
100   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet1()const 
101 {
102   return *mySubSet1;
103 }
104 //=======================================================================
105 //function : SetSubSet2
106 //purpose  : 
107 //=======================================================================
108   void BOPDS_SubIterator::SetSubSet2(const BOPCol_ListOfInteger& theLI)
109 {
110   mySubSet2=(BOPCol_PListOfInteger)&theLI;
111 }
112 //=======================================================================
113 //function : SubSet2
114 //purpose  : 
115 //=======================================================================
116   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet2()const 
117 {
118   return *mySubSet2;
119 }
120 //=======================================================================
121 // function: Initialize
122 // purpose: 
123 //=======================================================================
124   void BOPDS_SubIterator::Initialize()
125 {
126   myIterator.Initialize(myList);
127 }
128 //=======================================================================
129 // function: More
130 // purpose: 
131 //=======================================================================
132   Standard_Boolean BOPDS_SubIterator::More()const
133 {
134   return myIterator.More();
135 }
136 //=======================================================================
137 // function: Next
138 // purpose: 
139 //=======================================================================
140   void BOPDS_SubIterator::Next()
141 {
142   myIterator.Next();
143 }
144 //=======================================================================
145 // function: Value
146 // purpose: 
147 //=======================================================================
148   void BOPDS_SubIterator::Value(Standard_Integer& theI1,
149                                 Standard_Integer& theI2) const
150 {
151   Standard_Integer iT1, iT2, n1, n2;
152   //
153   const BOPDS_PassKeyBoolean& aPKB=myIterator.Value();
154   aPKB.Ids(n1, n2);
155   //
156   iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType());
157   iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType());
158   //
159   theI1=n1;
160   theI2=n2;
161   if (iT1<iT2) {
162     theI1=n2;
163     theI2=n1;
164   }
165 }
166 //=======================================================================
167 // function: Prepare
168 // purpose: 
169 //=======================================================================
170   void BOPDS_SubIterator::Prepare()
171 {
172   myList.Clear();
173   //
174   if (!myDS){
175     return;
176   }
177   if (!mySubSet1->Extent() || !mySubSet2->Extent()) {
178     return;
179   }
180   Intersect();
181 }
182 //=======================================================================
183 // function: Intersect
184 // purpose: 
185 //=======================================================================
186   void BOPDS_SubIterator::Intersect()
187 {
188   Standard_Integer i, aNbB, aNbSD, j, iDS, jB;
189   Handle(NCollection_IncAllocator) aAllocator;
190   BOPCol_ListIteratorOfListOfInteger aIt, aIt1, aIt2;
191   BOPDS_PassKeyBoolean aPKXB; 
192   //
193   //-----------------------------------------------------scope_1 f
194   aAllocator=new NCollection_IncAllocator();
195   //
196   BOPCol_DataMapOfShapeInteger aMSI(100, aAllocator);
197   BOPCol_DataMapOfIntegerInteger aMII(100, aAllocator);
198   BOPDS_MapOfPassKeyBoolean aMPKXB(100, aAllocator);
199   BOPCol_IndexedDataMapOfShapeBox aMSB(100, aAllocator);
200   //
201   BOPDS_BoxBndTreeSelector aSelector;
202   BOPDS_BoxBndTree aBBTree;
203   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
204   //
205   aIt1.Initialize(*mySubSet1);
206   for (; aIt1.More(); aIt1.Next()) {
207     i=aIt1.Value();
208     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
209     const TopoDS_Shape& aS=aSI.Shape();
210     const Bnd_Box& aBoxEx=aSI.Box();
211     aMSI.Bind(aS, i);
212     aMSB.Add(aS, aBoxEx);
213   }
214   //
215   aNbB=aMSB.Extent();
216   for (i=1; i<=aNbB; ++i) {
217     const TopoDS_Shape& aS=aMSB.FindKey(i);
218     const Bnd_Box& aBoxEx=aMSB(i);
219     //
220     aTreeFiller.Add(i, aBoxEx);
221     //
222     iDS=aMSI.Find(aS);
223     aMII.Bind(i, iDS);
224   }
225   //
226   aTreeFiller.Fill();
227   //
228   aIt2.Initialize(*mySubSet2);
229   for (; aIt2.More(); aIt2.Next()) {
230     i=aIt2.Value();
231     //
232     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
233     const TopoDS_Shape& aSi=aSI.Shape();
234     const Bnd_Box& aBoxEx=aSI.Box();
235     //
236     aSelector.Clear();
237     aSelector.SetBox(aBoxEx);
238     aNbSD=aBBTree.Select(aSelector);
239     if (!aNbSD){
240       continue;
241     }
242     //const Bnd_Box& aBoxi=myDS->ShapeInfo(i).Box();
243     //
244     const BOPCol_ListOfInteger& aLI=aSelector.Indices();
245     aIt.Initialize(aLI);
246     for (; aIt.More(); aIt.Next()) {
247       jB=aIt.Value();  // box index in MII
248       j=aMII.Find(jB); // DS index
249       //
250       aPKXB.SetIds(i, j);
251       if (aMPKXB.Add(aPKXB)) {
252         myList.Append(aPKXB);
253       }// if (aMPKXB.Add(aPKXB)) {
254     }// for (; aIt.More(); aIt.Next()) {
255   }
256   //
257   aMSI.Clear();
258   aMII.Clear();
259   aMPKXB.Clear();
260   aMSB.Clear();
261   //
262   aAllocator.Nullify();
263   //-----------------------------------------------------scope_1 t
264 }