625dbee8cb0466a59ac18b7afb4e569cc211b06f
[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_IncAllocator.hxx>
29 #include <NCollection_UBTreeFiller.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Vertex.hxx>
33
34 //
35 //
36 //
37 //
38 //
39 //=======================================================================
40 //function : 
41 //purpose  : 
42 //=======================================================================
43   BOPDS_SubIterator::BOPDS_SubIterator()
44 :
45   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
46   myList(myAllocator)
47 {
48   myDS=NULL; 
49 }
50 //=======================================================================
51 //function : 
52 //purpose  : 
53 //=======================================================================
54   BOPDS_SubIterator::BOPDS_SubIterator(const Handle(NCollection_BaseAllocator)& theAllocator)
55 :
56   myAllocator(theAllocator),
57   myList(myAllocator)
58 {
59   myDS=NULL; 
60 }
61 //=======================================================================
62 //function : ~
63 //purpose  : 
64 //=======================================================================
65   BOPDS_SubIterator::~BOPDS_SubIterator()
66 {
67 }
68 //=======================================================================
69 // function: SetDS
70 // purpose: 
71 //=======================================================================
72   void BOPDS_SubIterator::SetDS(const BOPDS_PDS& aDS)
73 {
74   myDS=aDS;
75 }
76 //=======================================================================
77 // function: DS
78 // purpose: 
79 //=======================================================================
80   const BOPDS_DS&  BOPDS_SubIterator::DS()const
81 {
82   return *myDS;
83 }
84 //=======================================================================
85 //function : SetSubSet1
86 //purpose  : 
87 //=======================================================================
88   void BOPDS_SubIterator::SetSubSet1(const BOPCol_ListOfInteger& theLI)
89 {
90   mySubSet1=(BOPCol_PListOfInteger)&theLI;
91 }
92 //=======================================================================
93 //function : SubSet1
94 //purpose  : 
95 //=======================================================================
96   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet1()const 
97 {
98   return *mySubSet1;
99 }
100 //=======================================================================
101 //function : SetSubSet2
102 //purpose  : 
103 //=======================================================================
104   void BOPDS_SubIterator::SetSubSet2(const BOPCol_ListOfInteger& theLI)
105 {
106   mySubSet2=(BOPCol_PListOfInteger)&theLI;
107 }
108 //=======================================================================
109 //function : SubSet2
110 //purpose  : 
111 //=======================================================================
112   const BOPCol_ListOfInteger& BOPDS_SubIterator::SubSet2()const 
113 {
114   return *mySubSet2;
115 }
116 //=======================================================================
117 // function: Initialize
118 // purpose: 
119 //=======================================================================
120   void BOPDS_SubIterator::Initialize()
121 {
122   myIterator.Initialize(myList);
123 }
124 //=======================================================================
125 // function: More
126 // purpose: 
127 //=======================================================================
128   Standard_Boolean BOPDS_SubIterator::More()const
129 {
130   return myIterator.More();
131 }
132 //=======================================================================
133 // function: Next
134 // purpose: 
135 //=======================================================================
136   void BOPDS_SubIterator::Next()
137 {
138   myIterator.Next();
139 }
140 //=======================================================================
141 // function: Value
142 // purpose: 
143 //=======================================================================
144   void BOPDS_SubIterator::Value(Standard_Integer& theI1,
145                                 Standard_Integer& theI2) const
146 {
147   Standard_Integer iT1, iT2, n1, n2;
148   //
149   const BOPDS_PassKeyBoolean& aPKB=myIterator.Value();
150   aPKB.Ids(n1, n2);
151   //
152   iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType());
153   iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType());
154   //
155   theI1=n1;
156   theI2=n2;
157   if (iT1<iT2) {
158     theI1=n2;
159     theI2=n1;
160   }
161 }
162 //=======================================================================
163 // function: Prepare
164 // purpose: 
165 //=======================================================================
166   void BOPDS_SubIterator::Prepare()
167 {
168   myList.Clear();
169   //
170   if (!myDS){
171     return;
172   }
173   if (!mySubSet1->Extent() || !mySubSet2->Extent()) {
174     return;
175   }
176   Intersect();
177 }
178 //=======================================================================
179 // function: Intersect
180 // purpose: 
181 //=======================================================================
182   void BOPDS_SubIterator::Intersect()
183 {
184   Standard_Integer i, aNbB, aNbSD, j, iDS, jB;
185   Handle(NCollection_IncAllocator) aAllocator;
186   BOPCol_ListIteratorOfListOfInteger aIt, aIt1, aIt2;
187   BOPDS_PassKeyBoolean aPKXB; 
188   //
189   //-----------------------------------------------------scope_1 f
190   aAllocator=new NCollection_IncAllocator();
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   aAllocator.Nullify();
258   //-----------------------------------------------------scope_1 t
259 }