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