Salome HOME
IPAL22903: TC650: Store/Restore last GUI state does not work for Isos
[modules/geom.git] / src / NMTDS / NMTDS_Iterator.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        NMTDS_Iterator.cxx
24 // Author:      Peter KURNEV
25
26 #include <NMTDS_Iterator.ixx>
27 //
28 #include <Bnd_Box.hxx>
29 //
30 #include <TColStd_ListOfInteger.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <TColStd_MapOfInteger.hxx>
33 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
34 #include <TColStd_DataMapOfIntegerInteger.hxx>
35 #include <TColStd_DataMapOfIntegerListOfInteger.hxx>
36 #include <TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger.hxx>
37 #include <TColStd_MapOfInteger.hxx>
38 //
39 #include <TopoDS.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Shape.hxx>
42 //
43 #include <TopTools_DataMapOfShapeInteger.hxx>
44 //
45 #include <NMTDS_BoxBndTree.hxx>
46 #include <NCollection_UBTreeFiller.hxx>
47 #include <NMTDS_CArray1OfIndexRange.hxx>
48 #include <NMTDS_IndexRange.hxx>
49 #include <NMTDS_PairBoolean.hxx>
50 #include <NMTDS_MapOfPairBoolean.hxx>
51 #include <NMTDS_IndexedDataMapOfShapeBox.hxx>
52 #include <NMTDS_IndexedDataMapOfIntegerShape.hxx>
53 #include <NMTDS_Tools.hxx>
54 #include <NMTDS_DataMapOfIntegerMapOfInteger.hxx>
55 #include <NMTDS_DataMapIteratorOfDataMapOfIntegerMapOfInteger.hxx>
56
57 //=======================================================================
58 //function : NMTDS_Iterator
59 //purpose  : 
60 //=======================================================================
61 NMTDS_Iterator::NMTDS_Iterator()
62 {
63   myDS=NULL; 
64   myLength=0;
65 }
66 //=======================================================================
67 //function : ~NMTDS_Iterator
68 //purpose  : 
69 //=======================================================================
70 NMTDS_Iterator::~NMTDS_Iterator()
71 {
72 }
73 //=======================================================================
74 // function: SetDS
75 // purpose: 
76 //=======================================================================
77 void NMTDS_Iterator::SetDS(const NMTDS_PShapesDataStructure& aDS)
78 {
79   myDS=aDS;
80 }
81 //=======================================================================
82 // function: DS
83 // purpose: 
84 //=======================================================================
85 const NMTDS_ShapesDataStructure&  NMTDS_Iterator::DS()const
86 {
87   return *myDS;
88 }
89 //=======================================================================
90 // function: ExpectedLength
91 // purpose: 
92 //=======================================================================
93 Standard_Integer NMTDS_Iterator::ExpectedLength() const
94 {
95   return myLength;
96 }
97 //=======================================================================
98 // function: BlockLength
99 // purpose: 
100 //=======================================================================
101 Standard_Integer NMTDS_Iterator::BlockLength() const
102 {
103   Standard_Integer aNbIIs, iTresh;
104   Standard_Real aCfPredict=.5;
105   
106   aNbIIs=ExpectedLength();
107   
108   if (aNbIIs<=1) {
109     return 1;
110   }
111   //modified by NIZNHY-PKV Mon Dec 12 08:50:50 2011f
112   iTresh=1000;
113   if (aNbIIs>iTresh) {
114     aNbIIs=iTresh;
115     return aNbIIs;
116   }
117   //modified by NIZNHY-PKV Mon Dec 12 08:50:54 2011t
118   //
119   aNbIIs=(Standard_Integer) (aCfPredict*(Standard_Real)aNbIIs);
120   return aNbIIs;
121 }
122 //=======================================================================
123 // function: Initialize
124 // purpose: 
125 //=======================================================================
126 void NMTDS_Iterator::Initialize(const TopAbs_ShapeEnum aType1,
127                                 const TopAbs_ShapeEnum aType2)
128 {
129   Standard_Integer iX;
130   //
131   iX=NMTDS_Tools::TypeToInteger(aType1, aType2);
132   if (iX>=0) {
133     myIterator.Initialize(myLists[iX]);
134     myLength=myLists[iX].Extent();
135   }
136   else {
137     myIterator.Initialize(myEmptyList);
138     myLength=0;
139   }
140 }
141 //=======================================================================
142 // function: More
143 // purpose: 
144 //=======================================================================
145 Standard_Boolean NMTDS_Iterator::More()const
146 {
147   return myIterator.More();
148 }
149 //=======================================================================
150 // function: Next
151 // purpose: 
152 //=======================================================================
153 void NMTDS_Iterator::Next()
154 {
155   myIterator.Next();
156 }
157 //=======================================================================
158 // function: Current
159 // purpose: 
160 //=======================================================================
161 void NMTDS_Iterator::Current(Standard_Integer& aIndex1,
162                              Standard_Integer& aIndex2,
163                              Standard_Boolean& aWithSubShape) const
164 {
165   const NMTDS_PairBoolean& aPKB=myIterator.Value();
166   aPKB.Ids(aIndex1, aIndex2);
167   aWithSubShape=aPKB.Flag();
168 }
169 //=======================================================================
170 // function: SDVertices
171 // purpose: 
172 //=======================================================================
173 const TColStd_DataMapOfIntegerListOfInteger& NMTDS_Iterator::SDVertices()const
174 {
175   return myMVSD;
176 }
177 //=======================================================================
178 // function: Prepare
179 // purpose: 
180 //=======================================================================
181 void NMTDS_Iterator::Prepare()
182 {
183   Standard_Integer i;
184   //
185   myLength=0;
186   for (i=0; i<6; ++i) {
187     myLists[i].Clear();
188   }
189   myMVSD.Clear();
190   //
191   if (myDS==NULL){
192     return;
193   }
194   Intersect();
195 }
196 //=======================================================================
197 // function: Intersect
198 // purpose: 
199 //=======================================================================
200 void NMTDS_Iterator::Intersect()
201 {
202   Standard_Boolean bFlag;
203   Standard_Integer aNb, i, aNbB, aNbR, iFlag;
204   Standard_Integer i1, i2, aNbSD, iX, j, iDS, jB, iR, k, aNbLV, aNbLV1;
205   TColStd_ListIteratorOfListOfInteger aIt;
206   TColStd_DataMapOfIntegerInteger aMII;
207   TColStd_DataMapOfIntegerListOfInteger aMVSD;
208   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItVSD;
209   TopTools_DataMapOfShapeInteger aMSI;
210   TopAbs_ShapeEnum aTi, aTj;
211   NMTDS_PairBoolean aPKXB; 
212   NMTDS_MapOfPairBoolean aMPKXB;
213   NMTDS_IndexedDataMapOfShapeBox aMSB;
214   //
215   NMTDS_BoxBndTreeSelector aSelector;
216   NMTDS_BoxBndTree aBBTree;
217   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
218   //
219   const NMTDS_CArray1OfIndexRange& aRanges=myDS->Ranges();
220   aNbR=aRanges.Extent();
221   //
222   aNb=myDS->NumberOfShapesOfTheObject();
223   for (i=1; i<=aNb; ++i) {
224     const TopoDS_Shape& aS=myDS->Shape(i);
225     aTi=aS.ShapeType();
226     if (NMTDS_Tools::HasBRep(aTi)) {
227       Bnd_Box aBoxEx;
228       //
229       myDS->ComputeBoxEx(i, aBoxEx);
230       aMSI.Bind(aS, i);
231       aMSB.Add(aS, aBoxEx);
232     }
233   }
234   //
235   aNbB=aMSB.Extent();
236   //
237   for (i=1; i<=aNbB; ++i) {
238     const TopoDS_Shape& aS=aMSB.FindKey(i);
239     const Bnd_Box& aBoxEx=aMSB(i);
240     //
241     aTreeFiller.Add(i, aBoxEx);
242     //
243     iDS=aMSI.Find(aS);
244     aMII.Bind(i, iDS);
245   }
246   //
247   aTreeFiller.Fill();
248   //
249   for (iR=1; iR<aNbR; ++iR) {
250     const NMTDS_IndexRange& aR=aRanges(iR);
251     i1=aR.First();
252     i2=aR.Last();
253     for (i=i1; i<=i2; ++i) {
254       const TopoDS_Shape& aSi=myDS->Shape(i);
255       aTi=aSi.ShapeType();
256       if (!NMTDS_Tools::HasBRep(aTi)){
257         continue;
258       }
259       const Bnd_Box& aBoxEx=aMSB.FindFromKey(aSi);
260       aSelector.Clear();
261       aSelector.SetBox(aBoxEx);
262       //
263       aNbSD=aBBTree.Select(aSelector);
264       //
265       if (!aNbSD){
266         continue;
267       }
268       //
269       const TColStd_ListOfInteger& aLI=aSelector.Indices();
270       //
271       k=0;
272       TColStd_ListOfInteger aLV;
273       //
274       aIt.Initialize(aLI);
275       for (; aIt.More(); aIt.Next()) {
276         jB=aIt.Value();  // box index in MII
277         j=aMII.Find(jB); // DS index
278         if (j>=i1 && j<=i2) {
279           continue;// same range
280         }
281         //
282         aPKXB.SetIds(i, j);
283         //
284         if (aMPKXB.Add(aPKXB)) {
285           bFlag=Standard_False;// Bounding boxes are intersected
286           const Bnd_Box& aBoxi=myDS->GetBoundingBox(i);
287           const Bnd_Box& aBoxj=myDS->GetBoundingBox(j);
288           if (aBoxi.IsOut(aBoxj)) {
289             bFlag=!bFlag; //Bounding boxes of Sub-shapes are intersected
290           }
291           const TopoDS_Shape& aSj=myDS->Shape(j);
292           aTj=aSj.ShapeType();
293           iX=NMTDS_Tools::TypeToInteger(aTi, aTj);
294           //bFlag=(iStatus==2);
295           aPKXB.SetFlag(bFlag);
296           myLists[iX].Append(aPKXB);
297           //
298           // VSD prepare
299           if (iX==5) { //VV
300             aLV.Append(j);
301           }
302         }// if (aMPKXB.Add(aPKXB)) {
303       }// for (; aIt.More(); aIt.Next()) {
304       //
305       // VSD treatment
306       aNbLV=aLV.Extent();
307       if (aNbLV) {
308         TColStd_ListOfInteger aLV1;
309         //
310         const TopoDS_Vertex& aVi=TopoDS::Vertex(aSi);
311         aIt.Initialize(aLV);
312         for (; aIt.More(); aIt.Next()) {
313           j=aIt.Value();  
314           const TopoDS_Shape&  aSj=myDS->Shape(j);
315           const TopoDS_Vertex& aVj=TopoDS::Vertex(aSj);
316           iFlag=NMTDS_Tools::ComputeVV(aVi, aVj);
317           if (!iFlag) {
318             aLV1.Append(j);
319           }
320           else {
321             aPKXB.SetIds(i, j);
322             aMPKXB.Remove(aPKXB);
323           }
324         }
325         //
326         aNbLV1=aLV1.Extent();
327         if (aNbLV1) {
328           aMVSD.Bind(i, aLV1);
329         }
330       }
331     }//for (i=i1; i<=i2; ++i) {
332   }//for (iR=1; iR<aNbR; ++iR) {
333   //
334   //
335   // Chains
336   //=================
337   myMVSD.Clear();
338   NMTDS_Iterator::FillMVSD(aMVSD, myMVSD);
339   
340   //modified by NIZNHY-PKV Mon Dec 12 09:51:29 2011f
341   aMPKXB.Clear();
342   Standard::Purge();
343   //modified by NIZNHY-PKV Mon Dec 12 09:51:33 2011t
344 }
345 //=======================================================================
346 //function : FillMVSD
347 //purpose  : 
348 //=======================================================================
349 void NMTDS_Iterator::FillMVSD(const TColStd_DataMapOfIntegerListOfInteger& aMVSD,
350                               TColStd_DataMapOfIntegerListOfInteger& bMVSD)
351 {
352   Standard_Boolean bFound;
353   Standard_Integer aNbVSD, iCnt, i, j, k;
354   TColStd_ListOfInteger aLV;
355   TColStd_ListIteratorOfListOfInteger aIt;
356   TColStd_MapOfInteger aMF;
357   TColStd_MapIteratorOfMapOfInteger aItMI;
358   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItVSD;
359   NMTDS_DataMapOfIntegerMapOfInteger aDMIMI;
360   NMTDS_DataMapIteratorOfDataMapOfIntegerMapOfInteger aIti, aItj;
361   //
362   aNbVSD=aMVSD.Extent();
363   if (!aNbVSD) {
364     return;
365   }
366   //
367   aItVSD.Initialize(aMVSD);
368   for (; aItVSD.More(); aItVSD.Next()) {
369     TColStd_MapOfInteger aMI;
370     //
371     i=aItVSD.Key();
372     aMI.Add(i);
373     const TColStd_ListOfInteger& aLVSD=aItVSD.Value();
374     aIt.Initialize(aLVSD);
375     for (; aIt.More(); aIt.Next()) {
376       j=aIt.Value();
377       aMI.Add(j);
378     }
379     aDMIMI.Bind(i, aMI);
380   }
381   // i
382   aIti.Initialize(aDMIMI);
383   for (; aIti.More(); aIti.Next()) {
384     i=aIti.Key();
385     if (aMF.Contains(i)) {
386       continue;
387     }
388     aMF.Add(i);
389     //
390     //TColStd_MapOfInteger& aMIi=aDMIMI.ChangeFind(i);
391     TColStd_MapOfInteger *pMIi=(TColStd_MapOfInteger *)&aIti.Value();
392     TColStd_MapOfInteger& aMIi=*pMIi;
393     //  j
394     while (1) {
395       iCnt=0;
396       aItj.Initialize(aDMIMI);
397       for (; aItj.More(); aItj.Next()) {
398         j=aItj.Key();
399         if (aMF.Contains(j)) {
400           continue;
401         }
402         //
403         //TColStd_MapOfInteger& aMIj=aDMIMI.ChangeFind(j);
404         TColStd_MapOfInteger *pMj=(TColStd_MapOfInteger *)&aItj.Value();
405         TColStd_MapOfInteger& aMIj=*pMj;
406         //
407         aItMI.Initialize(aMIj);
408         for (; aItMI.More(); aItMI.Next()) {
409           k=aItMI.Key();
410           bFound=aMIi.Contains(k);
411           if (bFound) {
412             break;
413           }
414         }
415         if (!bFound) {
416           continue;
417         }
418         //
419         aItMI.Initialize(aMIj);
420         for (; aItMI.More(); aItMI.Next()) {
421           k=aItMI.Key();
422           aMIi.Add(k);
423         }
424         //
425         if (aMF.Add(j)) {
426           ++iCnt;
427         }
428       } //for (; aItj.More(); aItj.Next()) {
429       if (!iCnt) {
430         break;
431       }
432     } // while (1) {
433     //
434     aLV.Clear();
435     aItMI.Initialize(aMIi);
436     for (; aItMI.More(); aItMI.Next()) {
437       k=aItMI.Key();
438         if (k!=i) {
439           aLV.Append(k);
440         }
441     }
442     bMVSD.Bind(i, aLV);
443   }// for (; aIti.More(); aIti.Next()) {
444 }
445
446   /*  
447   {
448     // check
449     TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItX;
450     //
451     printf(" \n");
452     printf(" myMVSD.Extent()=%d\n", myMVSD.Extent());
453     aItX.Initialize(myMVSD);
454     for (; aItX.More(); aItX.Next()) {
455       i=aItX.Key();
456       printf(" i=%d (", i);
457       const TColStd_ListOfInteger& aLV=aItX.Value();
458       aIt.Initialize(aLV);
459       for (; aIt.More(); aIt.Next()) {
460         j=aIt.Value();
461         printf(" %d", j);
462       }
463       printf(")\n");
464     }
465   }
466 */
467