]> SALOME platform Git repositories - modules/geom.git/blob - src/NMTDS/NMTDS_Iterator.cxx
Salome HOME
d186736c06789c715c33fc4c1bad3a310a2daca2
[modules/geom.git] / src / NMTDS / NMTDS_Iterator.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        NMTDS_Iterator.cxx
21 // Created:     Sun May 07 15:04:41 2006
22 // Author:      Peter KURNEV
23 //              <peter@PREFEX>
24
25
26 #include <NMTDS_Iterator.ixx>
27 #include <NMTDS_CArray1OfIndexRange.hxx>
28 #include <NMTDS_IndexRange.hxx>
29 #include <Bnd_Box.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TColStd_ListOfInteger.hxx>
32 #include <TColStd_Array1OfListOfInteger.hxx>
33 #include <TColStd_ListIteratorOfListOfInteger.hxx>
34 #include <TColStd_MapOfInteger.hxx>
35 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
36 #include <NMTDS_PassKeyBoolean.hxx>
37 #include <NMTDS_MapOfPassKeyBoolean.hxx>
38 #include <NMTDS_IndexedDataMapOfShapeBox.hxx>
39 #include <NMTDS_IndexedDataMapOfIntegerShape.hxx>
40 #include <Bnd_HArray1OfBox.hxx>
41 #include <Bnd_BoundSortBox.hxx>
42 #include <TColStd_ListOfInteger.hxx>
43 #include <TColStd_DataMapOfIntegerInteger.hxx>
44 #include <TopTools_DataMapOfShapeInteger.hxx>
45 #include <TColStd_DataMapOfIntegerListOfInteger.hxx>
46 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
47 #include <TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger.hxx>
48 #include <TColStd_MapOfInteger.hxx>
49
50 #include <NMTDS_BoxBndTree.hxx>
51 #include <NCollection_UBTreeFiller.hxx>
52
53 static 
54   void ComputeBoxEx(const Standard_Integer aIx,
55                     NMTDS_ShapesDataStructure* pDS,
56                     Bnd_Box& aBoxEx);
57
58 static inline
59   Standard_Boolean IsValidType(const TopAbs_ShapeEnum aT);
60
61 static
62   Standard_Integer TypeToInteger(const TopAbs_ShapeEnum aType1,
63                                  const TopAbs_ShapeEnum aType2);
64
65
66 //=======================================================================
67 //function : NMTDS_Iterator
68 //purpose  : 
69 //=======================================================================
70   NMTDS_Iterator::NMTDS_Iterator()
71 {
72   myPDS=NULL; 
73   myLength=0;
74 }
75 //=======================================================================
76 //function : ~NMTDS_Iterator
77 //purpose  : 
78 //=======================================================================
79   NMTDS_Iterator::~NMTDS_Iterator()
80 {
81 }
82 //=======================================================================
83 // function: SetDS
84 // purpose: 
85 //=======================================================================
86   void NMTDS_Iterator::SetDS(const NMTDS_PShapesDataStructure& aDS)
87 {
88   myPDS=aDS;
89 }
90 //=======================================================================
91 // function: DS
92 // purpose: 
93 //=======================================================================
94   const NMTDS_ShapesDataStructure&  NMTDS_Iterator::DS()const
95 {
96   return *myPDS;
97 }
98 //=======================================================================
99 // function: ExpectedLength
100 // purpose: 
101 //=======================================================================
102   Standard_Integer NMTDS_Iterator::ExpectedLength() const
103 {
104   return myLength;
105 }
106 //=======================================================================
107 // function: Initialize
108 // purpose: 
109 //=======================================================================
110   void NMTDS_Iterator::Initialize(const TopAbs_ShapeEnum aType1,
111                                   const TopAbs_ShapeEnum aType2)
112 {
113   Standard_Integer iX;
114   //
115   iX=TypeToInteger(aType1, aType2);
116   if (iX>=0) {
117     myIterator.Initialize(myLists[iX]);
118     myLength=myLists[iX].Extent();
119   }
120   else {
121     myIterator.Initialize(myEmptyList);
122     myLength=0;
123   }
124 }
125 //=======================================================================
126 // function: More
127 // purpose: 
128 //=======================================================================
129   Standard_Boolean NMTDS_Iterator::More()const
130 {
131   return myIterator.More();
132 }
133 //=======================================================================
134 // function: Next
135 // purpose: 
136 //=======================================================================
137   void NMTDS_Iterator::Next()
138 {
139   myIterator.Next();
140 }
141 //=======================================================================
142 // function: Current
143 // purpose: 
144 //=======================================================================
145   void NMTDS_Iterator::Current(Standard_Integer& aIndex1,
146                                Standard_Integer& aIndex2,
147                                Standard_Boolean& aWithSubShape) const
148 {
149   const NMTDS_PassKeyBoolean& aPKB=myIterator.Value();
150   aPKB.Ids(aIndex1, aIndex2);
151   aWithSubShape=aPKB.Flag();
152 }
153 //=======================================================================
154 // function: SDVertices
155 // purpose: 
156 //=======================================================================
157   const TColStd_DataMapOfIntegerListOfInteger& NMTDS_Iterator::SDVertices()const
158 {
159   return myMVSD;
160 }
161
162 //=======================================================================
163 // function: Prepare
164 // purpose: 
165 //=======================================================================
166   void NMTDS_Iterator::Prepare()
167 {
168   Standard_Boolean bFlag;
169   Standard_Integer aNb, i, aNbB, aNbR;
170   Standard_Integer i1, i2, aNbSD, iX, j, iDS, jB, iR, k, aNbLV;
171   TColStd_ListIteratorOfListOfInteger aIt;
172   TColStd_DataMapOfIntegerInteger aMII;
173   TColStd_MapOfInteger aMFence;
174   TopTools_DataMapOfShapeInteger aMSI;
175   TopAbs_ShapeEnum aTi, aTj;
176   NMTDS_PassKeyBoolean aPKXB; 
177   NMTDS_MapOfPassKeyBoolean aMPKXB;
178   NMTDS_IndexedDataMapOfShapeBox aMSB;
179   Handle(Bnd_HArray1OfBox) aHAB;
180   Bnd_BoundSortBox aBSB;
181   //
182   NMTDS_BoxBndTreeSelector aSelector;
183   NMTDS_BoxBndTree aBBTree;
184   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
185   //
186   if (myPDS==NULL){
187     return;
188   }
189   //
190   myLength=0;
191   for (i=0; i<6; ++i) {
192     myLists[i].Clear();
193   }
194   myMVSD.Clear();
195   //
196   const NMTDS_CArray1OfIndexRange& aRanges=myPDS->Ranges();
197   aNbR=aRanges.Extent();
198   //
199   aNb=myPDS->NumberOfShapesOfTheObject();
200   for (i=1; i<=aNb; ++i) {
201     const TopoDS_Shape& aS=myPDS->Shape(i);
202     aTi=aS.ShapeType();
203     if (IsValidType(aTi)) {
204       Bnd_Box aBoxEx;
205       //
206       ComputeBoxEx(i, myPDS, aBoxEx);
207       aMSI.Bind(aS, i);
208       aMSB.Add(aS, aBoxEx);
209     }
210   }
211   //
212   aNbB=aMSB.Extent();
213   //
214   for (i=1; i<=aNbB; ++i) {
215     const TopoDS_Shape& aS=aMSB.FindKey(i);
216     const Bnd_Box& aBoxEx=aMSB(i);
217     //
218     aTreeFiller.Add(i, aBoxEx);
219     //
220     iDS=aMSI.Find(aS);
221     aMII.Bind(i, iDS);
222   }
223   //
224   aTreeFiller.Fill();
225   //
226   for (iR=1; iR<aNbR; ++iR) {
227     const NMTDS_IndexRange& aR=aRanges(iR);
228     i1=aR.First();
229     i2=aR.Last();
230     for (i=i1; i<=i2; ++i) {
231       const TopoDS_Shape& aSi=myPDS->Shape(i);
232       aTi=aSi.ShapeType();
233       if (!IsValidType(aTi)){
234         continue;
235       }
236       const Bnd_Box& aBoxEx=aMSB.FindFromKey(aSi);
237       aSelector.Clear();
238       aSelector.SetBox(aBoxEx);
239       //
240       aNbSD=aBBTree.Select(aSelector);
241       //
242       if (!aNbSD){
243         continue;
244       }
245       //
246       const TColStd_ListOfInteger& aLI=aSelector.Indices();
247       //
248       k=0;
249       TColStd_ListOfInteger aLV;
250       //
251       aIt.Initialize(aLI);
252       for (; aIt.More(); aIt.Next()) {
253         jB=aIt.Value();  // box index in MII
254         j=aMII.Find(jB); // DS index
255         if (j>=i1 && j<=i2) {
256           continue;// same range
257         }
258         //
259         aPKXB.SetIds(i, j);
260         if (aMPKXB.Add(aPKXB)) {
261           bFlag=Standard_False;// Bounding boxes are intersected
262           const Bnd_Box& aBoxi=myPDS->GetBoundingBox(i);
263           const Bnd_Box& aBoxj=myPDS->GetBoundingBox(j);
264           if (aBoxi.IsOut(aBoxj)) {
265             bFlag=!bFlag; //Bounding boxes of Sub-shapes are intersected
266           }
267           const TopoDS_Shape& aSj=myPDS->Shape(j);
268           aTj=aSj.ShapeType();
269           iX=TypeToInteger(aTi, aTj);
270           
271           aPKXB.SetFlag(bFlag);
272           myLists[iX].Append(aPKXB);
273           //
274           // VSD prepare
275           if (iX==5) { //VV
276             if (aMFence.Add(j)) {
277               aLV.Append(j);
278             }
279           }
280         }// if (aMPKXB.Add(aPKXB)) {
281       }// for (; aIt.More(); aIt.Next()) {
282       //
283       // VSD treatment
284       aNbLV=aLV.Extent();
285       if (aNbLV) {
286         myMVSD.Bind(i, aLV);
287       }
288     }//for (i=i1; i<=i2; ++i) {
289   }//for (iR=1; iR<aNbR; ++iR) {
290 }
291
292 //=======================================================================
293 // function: IsValidType
294 // purpose: 
295 //=======================================================================
296 Standard_Boolean IsValidType(const TopAbs_ShapeEnum aTi)
297 {
298   return (aTi==TopAbs_VERTEX || aTi==TopAbs_EDGE || aTi==TopAbs_FACE);
299 }
300 //=======================================================================
301 // function: ComputeBoxEx
302 // purpose: 
303 //=======================================================================
304   void ComputeBoxEx(const Standard_Integer aIx,
305                     NMTDS_ShapesDataStructure* pDS,
306                     Bnd_Box& aBoxEx)
307 {
308   Standard_Integer i, aNbS, iS;
309   //
310   const Bnd_Box& aBox=pDS->GetBoundingBox(aIx);
311   aBoxEx.Add(aBox);
312   //
313   aNbS=pDS->NumberOfSuccessors(aIx);
314   for (i=1; i<=aNbS; ++i) {
315     Bnd_Box aBoxS;
316     iS=pDS->GetSuccessor(aIx, i);
317     ComputeBoxEx(iS, pDS, aBoxS);
318     aBoxEx.Add(aBoxS);
319   }
320 }
321
322 //=======================================================================
323 // function: TypeToInteger
324 // purpose: 
325 //=======================================================================
326 Standard_Integer TypeToInteger(const TopAbs_ShapeEnum aType1,
327                                const TopAbs_ShapeEnum aType2)
328 {
329   Standard_Integer iRet, iT1, iT2, iX;
330   //
331   iRet=-1;
332   iT1=(Standard_Integer)aType1;
333   iT2=(Standard_Integer)aType2;
334   //
335   iX=iT2*10+iT1;
336   switch (iX) {
337     case 77:
338       iRet=5; // VV
339       break;
340     case 76:
341     case 67:
342       iRet=4; // VE
343       break;
344     case 74:
345     case 47:
346       iRet=2; // VF
347       break;
348     case 66:
349       iRet=3; // EE
350       break;
351     case 64:
352     case 46:
353       iRet=1; // EF
354       break;
355     case 44:
356       iRet=0; // FF
357       break;
358     default:
359       break;
360   }
361   return iRet; 
362 }
363
364 /*
365 //
366   // check
367   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger aIt1;
368   TColStd_DataMapIteratorOfDataMapOfIntegerInteger aIt2;
369   //
370   printf(" \n");
371   printf(" aMVLV.Extent()=%d\n", aMVLV.Extent());
372   aIt1.Initialize(aMVLV);
373   for (; aIt1.More(); aIt1.Next()) {
374     i=aIt1.Key();
375     printf(" i=%d (", i);
376     const TColStd_ListOfInteger& aLV=aIt1.Value();
377     aIt.Initialize(aLV);
378     for (; aIt.More(); aIt.Next()) {
379       j=aIt.Value();
380       printf(" %d", j);
381     }
382     printf(")\n");
383   }
384   //
385   printf(" \n");
386   printf(" aMVV.Extent()=%d\n", aMVV.Extent());
387   aIt2.Initialize(aMVV);
388   for (; aIt2.More(); aIt2.Next()) {
389     i=aIt2.Key();
390     j=aIt2.Value();
391     printf(" (%d, %d)\n", i, j);
392   }
393   printf("\n");
394 */