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