7d3b5cc7fab802edded160cdfcd88758cdc77469
[occt.git] / src / BOPAlgo / BOPAlgo_Tools.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <BOPAlgo_Tools.hxx>
17 #include <BOPCol_IndexedMapOfInteger.hxx>
18 #include <BOPDS_CommonBlock.hxx>
19 #include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
20 #include <BOPDS_DS.hxx>
21 #include <BOPDS_IndexedMapOfPaveBlock.hxx>
22 #include <BOPDS_MapOfPaveBlock.hxx>
23 #include <BOPDS_PaveBlock.hxx>
24
25 //=======================================================================
26 //function : MakeBlocksCnx
27 //purpose  : 
28 //=======================================================================
29   void BOPAlgo_Tools::MakeBlocksCnx(const BOPCol_IndexedDataMapOfIntegerListOfInteger& aMILI,
30                                     BOPCol_DataMapOfIntegerListOfInteger& aMBlocks,
31                                     const Handle(NCollection_BaseAllocator)& aAllocator)
32 {
33   Standard_Integer aNbV, nV, aNbVS, nVP, nVx, aNbVP, aNbEC, k, i, j;
34   BOPCol_ListIteratorOfListOfInteger aItLI;
35   //
36   BOPCol_MapOfInteger aMVS(100, aAllocator);
37   BOPCol_IndexedMapOfInteger aMEC(100, aAllocator);
38   BOPCol_IndexedMapOfInteger aMVP(100, aAllocator);
39   BOPCol_IndexedMapOfInteger aMVAdd(100, aAllocator);
40   //
41   aNbV=aMILI.Extent();
42   //
43   for (k=0,i=1; i<=aNbV; ++i) {
44     aNbVS=aMVS.Extent();
45     if (aNbVS==aNbV) {
46       break;
47     }
48     //
49     nV = aMILI.FindKey(i);
50     if (aMVS.Contains(nV)){
51       continue;
52     }
53     aMVS.Add(nV);
54     //
55     aMEC.Clear();
56     aMVP.Clear();
57     aMVAdd.Clear();
58     //
59     aMVP.Add(nV);
60     for(;;) {
61       aNbVP=aMVP.Extent();
62       for (j=1; j<=aNbVP; ++j) {
63         nVP=aMVP(j);
64         const BOPCol_ListOfInteger& aLV=aMILI.FindFromKey(nVP);
65         aItLI.Initialize(aLV);
66         for (; aItLI.More(); aItLI.Next()) {
67           nVx=aItLI.Value();
68           if (aMEC.Contains(nVx)) {
69             continue;
70           }
71           //
72           aMVS.Add(nVx);
73           aMEC.Add(nVx);
74           aMVAdd.Add(nVx);
75         }
76       }
77       //
78       aNbVP=aMVAdd.Extent();
79       if (!aNbVP) {
80         break; // from while(1)
81       }
82       //
83       aMVP.Clear();
84       for (j=1; j<=aNbVP; ++j) {
85         aMVP.Add(aMVAdd(j));
86       }
87       aMVAdd.Clear();
88     }//while(1) {
89     //
90     BOPCol_ListOfInteger aLIx(aAllocator);
91     //
92     aNbEC = aMEC.Extent();
93     for (j=1; j<=aNbEC; ++j) {
94       nVx=aMEC(j);
95       aLIx.Append(nVx);
96     }
97     //
98     aMBlocks.Bind(k, aLIx);
99     ++k;
100   }//for (k=0,i=1; i<=aNbV; ++i)
101   aMVAdd.Clear();
102   aMVP.Clear();
103   aMEC.Clear();
104   aMVS.Clear();
105 }
106 //=======================================================================
107 //function : FillMap
108 //purpose  : 
109 //=======================================================================
110   void BOPAlgo_Tools::FillMap(const Standard_Integer n1,
111                               const Standard_Integer n2,
112                               BOPCol_IndexedDataMapOfIntegerListOfInteger& aMILI,
113                               const Handle(NCollection_BaseAllocator)& aAllocator)
114 {
115   if (aMILI.Contains(n1)) {
116     BOPCol_ListOfInteger& aLI=aMILI.ChangeFromKey(n1);
117     aLI.Append(n2);
118   }
119   else {
120     BOPCol_ListOfInteger aLI(aAllocator);
121     aLI.Append(n2);
122     aMILI.Add(n1, aLI);
123   }
124   if (aMILI.Contains(n2)) {
125     BOPCol_ListOfInteger& aLI=aMILI.ChangeFromKey(n2);
126     aLI.Append(n1);
127   }
128   else {
129     BOPCol_ListOfInteger aLI(aAllocator);
130     aLI.Append(n1);
131     aMILI.Add(n2, aLI);
132   }
133 }
134 //=======================================================================
135 //function : FillMap
136 //purpose  : 
137 //=======================================================================
138   void BOPAlgo_Tools::FillMap(const Handle(BOPDS_PaveBlock)& aPB1,
139                               const Handle(BOPDS_PaveBlock)& aPB2,
140                               BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock& aMPBLPB,
141                               const Handle(NCollection_BaseAllocator)& aAllocator)
142 {
143   if (aMPBLPB.Contains(aPB1)) {
144     BOPDS_ListOfPaveBlock& aLPB=aMPBLPB.ChangeFromKey(aPB1);
145     aLPB.Append(aPB2);
146   }
147   else {
148     BOPDS_ListOfPaveBlock aLPB(aAllocator);
149     aLPB.Append(aPB2);
150     aMPBLPB.Add(aPB1, aLPB);
151   }
152   if (aMPBLPB.Contains(aPB2)) {
153     BOPDS_ListOfPaveBlock& aLPB=aMPBLPB.ChangeFromKey(aPB2);
154     aLPB.Append(aPB1);
155   }
156   else {
157     BOPDS_ListOfPaveBlock aLPB(aAllocator);
158     aLPB.Append(aPB1);
159     aMPBLPB.Add(aPB2, aLPB);
160   }
161 }
162 //=======================================================================
163 //function : FillMap
164 //purpose  : 
165 //=======================================================================
166   void BOPAlgo_Tools::FillMap(const Handle(BOPDS_PaveBlock)& aPB,
167                               const Standard_Integer nF,
168                               BOPDS_IndexedDataMapOfPaveBlockListOfInteger& aMPBLI,
169                               const Handle(NCollection_BaseAllocator)& aAllocator)
170 {
171   if (aMPBLI.Contains(aPB)) {
172     BOPCol_ListOfInteger& aLI=aMPBLI.ChangeFromKey(aPB);
173     aLI.Append(nF);
174   }
175   else {
176     BOPCol_ListOfInteger aLI(aAllocator);
177     aLI.Append(nF);
178     aMPBLI.Add(aPB, aLI);
179   }
180 }
181 //=======================================================================
182 //function : MakeBlocks
183 //purpose  : 
184 //=======================================================================
185   void BOPAlgo_Tools::MakeBlocks(const BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock& aMILI,
186                                  BOPDS_DataMapOfIntegerListOfPaveBlock& aMBlocks,
187                                  const Handle(NCollection_BaseAllocator)& aAllocator)
188 {
189   Standard_Integer aNbV,  aNbVS, aNbVP, aNbEC, k, i, j;
190   BOPDS_ListIteratorOfListOfPaveBlock aItLI;
191   //
192   BOPDS_MapOfPaveBlock aMVS(100, aAllocator);
193   BOPDS_IndexedMapOfPaveBlock aMEC(100, aAllocator);
194   BOPDS_IndexedMapOfPaveBlock aMVP(100, aAllocator);
195   BOPDS_IndexedMapOfPaveBlock aMVAdd(100, aAllocator);
196   //
197   aNbV=aMILI.Extent();
198   //
199   for (k=0, i=1; i<=aNbV; ++i) {
200     aNbVS=aMVS.Extent();
201     if (aNbVS==aNbV) {
202       break;
203     }
204     //
205     const Handle(BOPDS_PaveBlock)& nV=aMILI.FindKey(i);
206     if (aMVS.Contains(nV)){
207       continue;
208     }
209     aMVS.Add(nV);
210     //
211     aMEC.Clear();
212     aMVP.Clear();
213     aMVAdd.Clear();
214     //
215     aMVP.Add(nV);
216     for(;;) {
217       aNbVP=aMVP.Extent();
218       for (j=1; j<=aNbVP; ++j) {
219         const Handle(BOPDS_PaveBlock)& nVP=aMVP(j);
220         const BOPDS_ListOfPaveBlock& aLV=aMILI.FindFromKey(nVP);
221         aItLI.Initialize(aLV);
222         for (; aItLI.More(); aItLI.Next()) {
223           const Handle(BOPDS_PaveBlock)& nVx=aItLI.Value();
224           if (aMEC.Contains(nVx)) {
225             continue;
226           }
227           //
228           aMVS.Add(nVx);
229           aMEC.Add(nVx);
230           aMVAdd.Add(nVx);
231         }
232       }
233       //
234       aNbVP=aMVAdd.Extent();
235       if (!aNbVP) {
236         break; // from while(1)
237       }
238       //
239       aMVP.Clear();
240       for (j=1; j<=aNbVP; ++j) {
241         aMVP.Add(aMVAdd(j));
242       }
243       aMVAdd.Clear();
244     }//while(1) {
245     //
246     BOPDS_ListOfPaveBlock aLIx(aAllocator);
247     //
248     aNbEC = aMEC.Extent();
249     for (j=1; j<=aNbEC; ++j) {
250       const Handle(BOPDS_PaveBlock)& nVx=aMEC(j);
251       aLIx.Append(nVx);
252     }
253     //
254     aMBlocks.Bind(k, aLIx);
255     ++k;
256   }//for (k=0, i=1; i<=aNbV; ++i)
257   aMVAdd.Clear();
258   aMVP.Clear();
259   aMEC.Clear();
260   aMVS.Clear();
261 }
262 //=======================================================================
263 //function : PerformCommonBlocks
264 //purpose  : 
265 //=======================================================================
266   void BOPAlgo_Tools::PerformCommonBlocks(BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock& aMPBLPB,
267                                           const Handle(NCollection_BaseAllocator)& aAllocator,
268                                           BOPDS_PDS& pDS)
269 {
270   Standard_Integer aNbCB;
271   //
272   aNbCB=aMPBLPB.Extent();
273   if (!aNbCB) {
274     return;
275   }
276   //
277   Standard_Integer aNbPB, aNbBlocks, k;
278   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
279   Handle(BOPDS_CommonBlock) aCB;
280   BOPDS_DataMapOfIntegerListOfPaveBlock aMBlocks(100, aAllocator);
281   //
282   BOPAlgo_Tools::MakeBlocks(aMPBLPB, aMBlocks, aAllocator);
283   //
284   aNbBlocks = aMBlocks.Extent();
285   for (k=0; k<aNbBlocks; ++k) {
286     const BOPDS_ListOfPaveBlock& aLPB=aMBlocks.Find(k);
287     aNbPB=aLPB.Extent();
288     if (aNbPB>1) {
289       aCB=new BOPDS_CommonBlock;
290       //
291       aItLPB.Initialize(aLPB);
292       for (; aItLPB.More(); aItLPB.Next()) {
293         const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
294         aCB->AddPaveBlock(aPBx);
295       }
296       //
297       aItLPB.Initialize(aLPB);
298       for (; aItLPB.More(); aItLPB.Next()) {
299         const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
300         pDS->SetCommonBlock(aPBx, aCB);
301       }
302     }//if (aNbPB>1) {
303   }
304 }
305 //=======================================================================
306 //function : PerformCommonBlocks
307 //purpose  : 
308 //=======================================================================
309   void BOPAlgo_Tools::PerformCommonBlocks(const BOPDS_IndexedDataMapOfPaveBlockListOfInteger& aMPBLI,
310                                           const Handle(NCollection_BaseAllocator)& ,//aAllocator
311                                           BOPDS_PDS& pDS)
312 {
313   Standard_Integer nF, i, aNb;
314   BOPCol_ListIteratorOfListOfInteger aItLI;
315   Handle(BOPDS_PaveBlock) aPB;
316   Handle(BOPDS_CommonBlock) aCB;
317   //
318   aNb=aMPBLI.Extent();
319   for (i=1; i<=aNb; ++i) {
320     aPB=aMPBLI.FindKey(i);
321     if (pDS->IsCommonBlock(aPB)) {
322       aCB=pDS->CommonBlock(aPB);
323     }
324     else {
325       aCB=new BOPDS_CommonBlock;
326       aCB->AddPaveBlock(aPB);
327     }
328     //
329     const BOPCol_ListOfInteger& aLI=aMPBLI.FindFromKey(aPB);
330     aItLI.Initialize(aLI);
331     for (; aItLI.More(); aItLI.Next()) {
332       nF=aItLI.Value();
333       aCB->AddFace(nF);
334     }
335     pDS->SetCommonBlock(aPB, aCB);
336   }
337 }