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