Salome HOME
CMake porting.
[modules/geom.git] / src / NMTDS / NMTDS_Iterator.cxx
1 // Copyright (C) 2007-2013  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.hxx>
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 #include <NMTDS_ShapesDataStructure.hxx>
57
58 //=======================================================================
59 //function : NMTDS_Iterator
60 //purpose  : 
61 //=======================================================================
62 NMTDS_Iterator::NMTDS_Iterator()
63 {
64   myDS=NULL; 
65   myLength=0;
66 }
67 //=======================================================================
68 //function : ~NMTDS_Iterator
69 //purpose  : 
70 //=======================================================================
71 NMTDS_Iterator::~NMTDS_Iterator()
72 {
73 }
74 //=======================================================================
75 // function: SetDS
76 // purpose: 
77 //=======================================================================
78 void NMTDS_Iterator::SetDS(const NMTDS_PShapesDataStructure& aDS)
79 {
80   myDS=aDS;
81 }
82 //=======================================================================
83 // function: DS
84 // purpose: 
85 //=======================================================================
86 const NMTDS_ShapesDataStructure&  NMTDS_Iterator::DS()const
87 {
88   return *myDS;
89 }
90 //=======================================================================
91 // function: ExpectedLength
92 // purpose: 
93 //=======================================================================
94 Standard_Integer NMTDS_Iterator::ExpectedLength() const
95 {
96   return myLength;
97 }
98 //=======================================================================
99 // function: BlockLength
100 // purpose: 
101 //=======================================================================
102 Standard_Integer NMTDS_Iterator::BlockLength() const
103 {
104   Standard_Integer aNbIIs, iTresh;
105   Standard_Real aCfPredict=.5;
106   
107   aNbIIs=ExpectedLength();
108   
109   if (aNbIIs<=1) {
110     return 1;
111   }
112   //modified by NIZNHY-PKV Mon Dec 12 08:50:50 2011f
113   iTresh=1000;
114   if (aNbIIs>iTresh) {
115     aNbIIs=iTresh;
116     return aNbIIs;
117   }
118   //modified by NIZNHY-PKV Mon Dec 12 08:50:54 2011t
119   //
120   aNbIIs=(Standard_Integer) (aCfPredict*(Standard_Real)aNbIIs);
121   return aNbIIs;
122 }
123 //=======================================================================
124 // function: Initialize
125 // purpose: 
126 //=======================================================================
127 void NMTDS_Iterator::Initialize(const TopAbs_ShapeEnum aType1,
128                                 const TopAbs_ShapeEnum aType2)
129 {
130   Standard_Integer iX;
131   //
132   iX=NMTDS_Tools::TypeToInteger(aType1, aType2);
133   if (iX>=0) {
134     myIterator.Initialize(myLists[iX]);
135     myLength=myLists[iX].Extent();
136   }
137   else {
138     myIterator.Initialize(myEmptyList);
139     myLength=0;
140   }
141 }
142 //=======================================================================
143 // function: More
144 // purpose: 
145 //=======================================================================
146 Standard_Boolean NMTDS_Iterator::More()const
147 {
148   return myIterator.More();
149 }
150 //=======================================================================
151 // function: Next
152 // purpose: 
153 //=======================================================================
154 void NMTDS_Iterator::Next()
155 {
156   myIterator.Next();
157 }
158 //=======================================================================
159 // function: Current
160 // purpose: 
161 //=======================================================================
162 void NMTDS_Iterator::Current(Standard_Integer& aIndex1,
163                              Standard_Integer& aIndex2,
164                              Standard_Boolean& aWithSubShape) const
165 {
166   const NMTDS_PairBoolean& aPKB=myIterator.Value();
167   aPKB.Ids(aIndex1, aIndex2);
168   aWithSubShape=aPKB.Flag();
169 }
170 //=======================================================================
171 // function: SDVertices
172 // purpose: 
173 //=======================================================================
174 const TColStd_DataMapOfIntegerListOfInteger& NMTDS_Iterator::SDVertices()const
175 {
176   return myMVSD;
177 }
178 //=======================================================================
179 // function: Prepare
180 // purpose: 
181 //=======================================================================
182 void NMTDS_Iterator::Prepare()
183 {
184   Standard_Integer i;
185   //
186   myLength=0;
187   for (i=0; i<6; ++i) {
188     myLists[i].Clear();
189   }
190   myMVSD.Clear();
191   //
192   if (myDS==NULL){
193     return;
194   }
195   Intersect();
196 }
197 //=======================================================================
198 // function: Intersect
199 // purpose: 
200 //=======================================================================
201 void NMTDS_Iterator::Intersect()
202 {
203   Standard_Boolean bFlag;
204   Standard_Integer aNb, i, aNbB, aNbR, iFlag;
205   Standard_Integer i1, i2, aNbSD, iX, j, iDS, jB, iR, k, aNbLV, aNbLV1;
206   TColStd_ListIteratorOfListOfInteger aIt;
207   TColStd_DataMapOfIntegerInteger aMII;
208   TColStd_DataMapOfIntegerListOfInteger aMVSD;
209   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItVSD;
210   TopTools_DataMapOfShapeInteger aMSI;
211   TopAbs_ShapeEnum aTi, aTj;
212   NMTDS_PairBoolean aPKXB; 
213   NMTDS_MapOfPairBoolean aMPKXB;
214   NMTDS_IndexedDataMapOfShapeBox aMSB;
215   //
216   NMTDS_BoxBndTreeSelector aSelector;
217   NMTDS_BoxBndTree aBBTree;
218   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
219   //
220   const NMTDS_CArray1OfIndexRange& aRanges=myDS->Ranges();
221   aNbR=aRanges.Extent();
222   //
223   aNb=myDS->NumberOfShapesOfTheObject();
224   for (i=1; i<=aNb; ++i) {
225     const TopoDS_Shape& aS=myDS->Shape(i);
226     aTi=aS.ShapeType();
227     if (NMTDS_Tools::HasBRep(aTi)) {
228       Bnd_Box aBoxEx;
229       //
230       myDS->ComputeBoxEx(i, aBoxEx);
231       aMSI.Bind(aS, i);
232       aMSB.Add(aS, aBoxEx);
233     }
234   }
235   //
236   aNbB=aMSB.Extent();
237   //
238   for (i=1; i<=aNbB; ++i) {
239     const TopoDS_Shape& aS=aMSB.FindKey(i);
240     const Bnd_Box& aBoxEx=aMSB(i);
241     //
242     aTreeFiller.Add(i, aBoxEx);
243     //
244     iDS=aMSI.Find(aS);
245     aMII.Bind(i, iDS);
246   }
247   //
248   aTreeFiller.Fill();
249   //
250   for (iR=1; iR<aNbR; ++iR) {
251     const NMTDS_IndexRange& aR=aRanges(iR);
252     i1=aR.First();
253     i2=aR.Last();
254     for (i=i1; i<=i2; ++i) {
255       const TopoDS_Shape& aSi=myDS->Shape(i);
256       aTi=aSi.ShapeType();
257       if (!NMTDS_Tools::HasBRep(aTi)){
258         continue;
259       }
260       const Bnd_Box& aBoxEx=aMSB.FindFromKey(aSi);
261       aSelector.Clear();
262       aSelector.SetBox(aBoxEx);
263       //
264       aNbSD=aBBTree.Select(aSelector);
265       //
266       if (!aNbSD){
267         continue;
268       }
269       //
270       const TColStd_ListOfInteger& aLI=aSelector.Indices();
271       //
272       k=0;
273       TColStd_ListOfInteger aLV;
274       //
275       aIt.Initialize(aLI);
276       for (; aIt.More(); aIt.Next()) {
277         jB=aIt.Value();  // box index in MII
278         j=aMII.Find(jB); // DS index
279         if (j>=i1 && j<=i2) {
280           continue;// same range
281         }
282         //
283         aPKXB.SetIds(i, j);
284         //
285         if (aMPKXB.Add(aPKXB)) {
286           bFlag=Standard_False;// Bounding boxes are intersected
287           const Bnd_Box& aBoxi=myDS->GetBoundingBox(i);
288           const Bnd_Box& aBoxj=myDS->GetBoundingBox(j);
289           if (aBoxi.IsOut(aBoxj)) {
290             bFlag=!bFlag; //Bounding boxes of Sub-shapes are intersected
291           }
292           const TopoDS_Shape& aSj=myDS->Shape(j);
293           aTj=aSj.ShapeType();
294           iX=NMTDS_Tools::TypeToInteger(aTi, aTj);
295           //bFlag=(iStatus==2);
296           aPKXB.SetFlag(bFlag);
297           myLists[iX].Append(aPKXB);
298           //
299           // VSD prepare
300           if (iX==5) { //VV
301             aLV.Append(j);
302           }
303         }// if (aMPKXB.Add(aPKXB)) {
304       }// for (; aIt.More(); aIt.Next()) {
305       //
306       // VSD treatment
307       aNbLV=aLV.Extent();
308       if (aNbLV) {
309         TColStd_ListOfInteger aLV1;
310         //
311         const TopoDS_Vertex& aVi=TopoDS::Vertex(aSi);
312         aIt.Initialize(aLV);
313         for (; aIt.More(); aIt.Next()) {
314           j=aIt.Value();  
315           const TopoDS_Shape&  aSj=myDS->Shape(j);
316           const TopoDS_Vertex& aVj=TopoDS::Vertex(aSj);
317           iFlag=NMTDS_Tools::ComputeVV(aVi, aVj);
318           if (!iFlag) {
319             aLV1.Append(j);
320           }
321           else {
322             aPKXB.SetIds(i, j);
323             aMPKXB.Remove(aPKXB);
324           }
325         }
326         //
327         aNbLV1=aLV1.Extent();
328         if (aNbLV1) {
329           aMVSD.Bind(i, aLV1);
330         }
331       }
332     }//for (i=i1; i<=i2; ++i) {
333   }//for (iR=1; iR<aNbR; ++iR) {
334   //
335   //
336   // Chains
337   //=================
338   myMVSD.Clear();
339   NMTDS_Iterator::FillMVSD(aMVSD, myMVSD);
340   
341   //modified by NIZNHY-PKV Mon Dec 12 09:51:29 2011f
342   aMPKXB.Clear();
343   Standard::Purge();
344   //modified by NIZNHY-PKV Mon Dec 12 09:51:33 2011t
345 }
346 //=======================================================================
347 //function : FillMVSD
348 //purpose  : 
349 //=======================================================================
350 void NMTDS_Iterator::FillMVSD(const TColStd_DataMapOfIntegerListOfInteger& aMVSD,
351                               TColStd_DataMapOfIntegerListOfInteger& bMVSD)
352 {
353   Standard_Boolean bFound;
354   Standard_Integer aNbVSD, iCnt, i, j, k;
355   TColStd_ListOfInteger aLV;
356   TColStd_ListIteratorOfListOfInteger aIt;
357   TColStd_MapOfInteger aMF;
358   TColStd_MapIteratorOfMapOfInteger aItMI;
359   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItVSD;
360   NMTDS_DataMapOfIntegerMapOfInteger aDMIMI;
361   NMTDS_DataMapIteratorOfDataMapOfIntegerMapOfInteger aIti, aItj;
362   //
363   aNbVSD=aMVSD.Extent();
364   if (!aNbVSD) {
365     return;
366   }
367   //
368   aItVSD.Initialize(aMVSD);
369   for (; aItVSD.More(); aItVSD.Next()) {
370     TColStd_MapOfInteger aMI;
371     //
372     i=aItVSD.Key();
373     aMI.Add(i);
374     const TColStd_ListOfInteger& aLVSD=aItVSD.Value();
375     aIt.Initialize(aLVSD);
376     for (; aIt.More(); aIt.Next()) {
377       j=aIt.Value();
378       aMI.Add(j);
379     }
380     aDMIMI.Bind(i, aMI);
381   }
382   // i
383   aIti.Initialize(aDMIMI);
384   for (; aIti.More(); aIti.Next()) {
385     i=aIti.Key();
386     if (aMF.Contains(i)) {
387       continue;
388     }
389     aMF.Add(i);
390     //
391     //TColStd_MapOfInteger& aMIi=aDMIMI.ChangeFind(i);
392     TColStd_MapOfInteger *pMIi=(TColStd_MapOfInteger *)&aIti.Value();
393     TColStd_MapOfInteger& aMIi=*pMIi;
394     //  j
395     while (1) {
396       iCnt=0;
397       aItj.Initialize(aDMIMI);
398       for (; aItj.More(); aItj.Next()) {
399         j=aItj.Key();
400         if (aMF.Contains(j)) {
401           continue;
402         }
403         //
404         //TColStd_MapOfInteger& aMIj=aDMIMI.ChangeFind(j);
405         TColStd_MapOfInteger *pMj=(TColStd_MapOfInteger *)&aItj.Value();
406         TColStd_MapOfInteger& aMIj=*pMj;
407         //
408         aItMI.Initialize(aMIj);
409         for (; aItMI.More(); aItMI.Next()) {
410           k=aItMI.Key();
411           bFound=aMIi.Contains(k);
412           if (bFound) {
413             break;
414           }
415         }
416         if (!bFound) {
417           continue;
418         }
419         //
420         aItMI.Initialize(aMIj);
421         for (; aItMI.More(); aItMI.Next()) {
422           k=aItMI.Key();
423           aMIi.Add(k);
424         }
425         //
426         if (aMF.Add(j)) {
427           ++iCnt;
428         }
429       } //for (; aItj.More(); aItj.Next()) {
430       if (!iCnt) {
431         break;
432       }
433     } // while (1) {
434     //
435     aLV.Clear();
436     aItMI.Initialize(aMIi);
437     for (; aItMI.More(); aItMI.Next()) {
438       k=aItMI.Key();
439         if (k!=i) {
440           aLV.Append(k);
441         }
442     }
443     bMVSD.Bind(i, aLV);
444   }// for (; aIti.More(); aIti.Next()) {
445 }
446
447   /*  
448   {
449     // check
450     TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aItX;
451     //
452     printf(" \n");
453     printf(" myMVSD.Extent()=%d\n", myMVSD.Extent());
454     aItX.Initialize(myMVSD);
455     for (; aItX.More(); aItX.Next()) {
456       i=aItX.Key();
457       printf(" i=%d (", i);
458       const TColStd_ListOfInteger& aLV=aItX.Value();
459       aIt.Initialize(aLV);
460       for (; aIt.More(); aIt.Next()) {
461         j=aIt.Value();
462         printf(" %d", j);
463       }
464       printf(")\n");
465     }
466   }
467 */
468