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