0022627: Change OCCT memory management defaults
[occt.git] / src / BOP / BOP_ShellSolidHistoryCollector.cxx
1 #include <BOP_ShellSolidHistoryCollector.ixx>
2
3 #include <TopAbs.hxx>
4 #include <TopExp_Explorer.hxx>
5 #include <BooleanOperations_ShapesDataStructure.hxx>
6 #include <BooleanOperations_IndexedDataMapOfShapeInteger.hxx>
7 #include <BOPTools_SSInterference.hxx>
8 #include <BOPTools_InterferencePool.hxx>
9 #include <BOPTools_CArray1OfSSInterference.hxx>
10 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
11 #include <BOPTools_Curve.hxx>
12 #include <BOPTools_PaveBlock.hxx>
13 #include <BOPTools_DSFiller.hxx>
14 #include <BOPTools_PaveFiller.hxx>
15 #include <BOPTools_CommonBlock.hxx>
16 #include <BOPTools_ListOfCommonBlock.hxx>
17 #include <BOPTools_ListIteratorOfListOfCommonBlock.hxx>
18 #include <BOPTools_ListIteratorOfListOfPave.hxx>
19 #include <BOP_BuilderTools.hxx>
20 #include <TopTools_IndexedMapOfShape.hxx>
21 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
22 #include <TopExp.hxx>
23 #include <TColStd_ListIteratorOfListOfInteger.hxx>
24 #include <TopTools_ListIteratorOfListOfShape.hxx>
25
26 // ====================================================================================================
27 // function: Constructor
28 // purpose:
29 // ====================================================================================================
30 BOP_ShellSolidHistoryCollector::BOP_ShellSolidHistoryCollector(const TopoDS_Shape& theShape1,
31                                                                const TopoDS_Shape& theShape2,
32                                                                const BOP_Operation theOperation) :
33 BOP_HistoryCollector(theShape1, theShape2, theOperation)
34 {
35 }
36
37 // ====================================================================================================
38 // function: AddNewFace
39 // purpose:
40 // ====================================================================================================
41 void BOP_ShellSolidHistoryCollector::AddNewFace(const TopoDS_Shape&       theOldShape,
42                                                 const TopoDS_Shape&       theNewShape,
43                                                 const BOPTools_PDSFiller& theDSFiller) 
44 {
45   Standard_Boolean bAddAsGenerated = Standard_True;
46
47   if(theOldShape.ShapeType() != theNewShape.ShapeType())
48     return;
49
50   const BooleanOperations_IndexedDataMapOfShapeInteger& aMap = theDSFiller->DS().ShapeIndexMap(1);
51   Standard_Boolean bIsObject = (myS1.ShapeType() == TopAbs_SHELL) ? aMap.Contains(theOldShape) : !aMap.Contains(theOldShape);
52
53   if(bIsObject) {
54     bAddAsGenerated = Standard_False;
55   }
56
57   TopTools_DataMapOfShapeListOfShape& aHistoryMap = (bAddAsGenerated) ? myGenMap : myModifMap;
58
59   if(aHistoryMap.IsBound(theOldShape)) {
60     aHistoryMap.ChangeFind(theOldShape).Append(theNewShape);
61   }
62   else {
63     TopTools_ListOfShape aShapeList;
64     aShapeList.Append(theNewShape);
65     aHistoryMap.Bind(theOldShape, aShapeList);
66   }
67 }
68
69 // ====================================================================================================
70 // function: SetResult
71 // purpose:
72 // ====================================================================================================
73 void BOP_ShellSolidHistoryCollector::SetResult(const TopoDS_Shape&       theResult,
74                                                const BOPTools_PDSFiller& theDSFiller) 
75 {
76   myResult = theResult;
77   FillSection(theDSFiller);
78   FillEdgeHistory(theDSFiller);
79
80   myHasDeleted = Standard_False;
81   Standard_Integer i = 0;
82
83   TopTools_IndexedMapOfShape aFreeBoundaryMap;
84   TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
85
86   if(myS1.ShapeType() == TopAbs_SHELL) {
87     TopExp::MapShapesAndAncestors(myS1, TopAbs_EDGE, TopAbs_FACE, aEFMap);
88   }
89   else {
90     TopExp::MapShapesAndAncestors(myS2, TopAbs_EDGE, TopAbs_FACE, aEFMap);
91   }
92
93   for(i = 1; i <= aEFMap.Extent(); i++) {
94     if(aEFMap.FindFromIndex(i).Extent() < 2)
95       aFreeBoundaryMap.Add(aEFMap.FindKey(i));
96   }
97   aEFMap.Clear();
98   
99   for(i = (Standard_Integer)TopAbs_FACE; !myHasDeleted && (i < (Standard_Integer)TopAbs_EDGE); i++) {
100     if(i == (Standard_Integer)TopAbs_WIRE)
101       continue;
102
103     TopAbs_ShapeEnum aType = (TopAbs_ShapeEnum) i;
104     TopTools_IndexedMapOfShape aMap;
105     TopExp::MapShapes(myResult, aType, aMap);
106
107     TopExp_Explorer anExpObj(myS1, aType);
108
109     for(; anExpObj.More(); anExpObj.Next()) {
110       const TopoDS_Shape& aS = anExpObj.Current();
111
112       if((i == (Standard_Integer)TopAbs_EDGE) && !aFreeBoundaryMap.Contains(aS))
113         continue;
114
115       if(!aMap.Contains(aS)) {
116
117         if((!myModifMap.IsBound(aS) || myModifMap(aS).IsEmpty()) &&
118            (!myGenMap.IsBound(aS) || myGenMap(aS).IsEmpty())) {
119           myHasDeleted = Standard_True;
120           break;
121         }
122       }
123     }
124     TopExp_Explorer anExpTool(myS2, aType);
125
126     for(; anExpTool.More(); anExpTool.Next()) {
127       const TopoDS_Shape& aS = anExpTool.Current();
128
129       if(!aMap.Contains(aS)) {
130         if((!myModifMap.IsBound(aS) || myModifMap(aS).IsEmpty()) &&
131            (!myGenMap.IsBound(aS) || myGenMap(aS).IsEmpty())) {
132           myHasDeleted = Standard_True;
133           break;
134         }
135       }
136     }
137   }
138 }
139
140 // ====================================================================================================
141 // function: FillSection
142 // purpose:
143 // ====================================================================================================
144 void BOP_ShellSolidHistoryCollector::FillSection(const BOPTools_PDSFiller& theDSFiller) 
145 {
146   BOPTools_InterferencePool* pIntrPool = (BOPTools_InterferencePool*)&theDSFiller->InterfPool();
147   BOPTools_CArray1OfSSInterference& aFFs = pIntrPool->SSInterferences();
148   Standard_Integer i = 0;
149   TopTools_IndexedDataMapOfShapeListOfShape aResultMap;
150
151   if(!myResult.IsNull()) {
152     TopExp::MapShapesAndAncestors(myResult, TopAbs_EDGE, TopAbs_FACE, aResultMap);
153   }
154
155   for(i = 1; i <= aFFs.Extent(); i++) {
156     BOPTools_SSInterference& aFF = aFFs(i);
157     Standard_Integer nF1 = aFF.Index1();
158     Standard_Integer nF2 = aFF.Index2();
159
160     if(aFF.IsTangentFaces())
161       continue;
162     TopoDS_Shape aF1 = theDSFiller->DS().Shape(nF1);
163     TopoDS_Shape aF2 = theDSFiller->DS().Shape(nF2);
164
165
166     BOPTools_SequenceOfCurves& aSeqOfCurves = aFF.Curves();
167     Standard_Integer j = 0;
168
169     for(j = 1; j <= aSeqOfCurves.Length(); j++) {
170       const BOPTools_Curve& aCurve = aSeqOfCurves.Value(j);
171
172       const BOPTools_ListOfPaveBlock& aPBList = aCurve.NewPaveBlocks();
173       BOPTools_ListIteratorOfListOfPaveBlock anIt(aPBList);
174
175       for (; anIt.More();anIt.Next()) {
176         const BOPTools_PaveBlock& aPB = anIt.Value();
177         Standard_Integer anindex = aPB.Edge();
178         const TopoDS_Shape& aS = theDSFiller->DS().GetShape(anindex);
179
180         if(aResultMap.Contains(aS)) {
181           TopTools_ListOfShape thelist;
182           if(!myGenMap.IsBound(aF1))
183             myGenMap.Bind(aF1, thelist);
184           myGenMap.ChangeFind(aF1).Append(aS);
185
186           if(!myGenMap.IsBound(aF2))
187             myGenMap.Bind(aF2, thelist);
188           myGenMap.ChangeFind(aF2).Append(aS);
189         }
190       }
191     }
192
193     // Old Section Edges
194     const BOPTools_ListOfPaveBlock& aSectList = aFF.PaveBlocks();
195     BOPTools_ListIteratorOfListOfPaveBlock anIt(aSectList);
196
197     for (; anIt.More();anIt.Next()) {
198       const BOPTools_PaveBlock& aPB=anIt.Value();
199       Standard_Integer nSect = aPB.Edge();
200       const TopoDS_Shape& aS = theDSFiller->DS().GetShape(nSect);
201
202       if(aResultMap.Contains(aS)) {
203         if(aResultMap.FindFromKey(aS).Extent() >= 2)
204           continue;
205
206         Standard_Boolean add1 = Standard_True;  
207         TopTools_ListOfShape thelist1;
208         if(!myGenMap.IsBound(aF1))
209           myGenMap.Bind(aF1, thelist1);
210         else {
211           TopTools_ListOfShape& aList = myGenMap.ChangeFind(aF1);
212           TopTools_ListIteratorOfListOfShape anItF1(aList);
213
214           for(; anItF1.More(); anItF1.Next()) {
215             if(aS.IsSame(anItF1.Value())) {
216               add1 = Standard_False;
217               break;
218             }
219           }
220         }
221
222         if(add1) {
223           myGenMap.ChangeFind(aF1).Append(aS);
224         }
225         Standard_Boolean add2 = Standard_True;  
226         TopTools_ListOfShape thelist2;
227         if(!myGenMap.IsBound(aF2))
228           myGenMap.Bind(aF2, thelist2);
229         else {
230           TopTools_ListOfShape& aList = myGenMap.ChangeFind(aF2);
231           TopTools_ListIteratorOfListOfShape anItF2(aList);
232
233           for(; anItF2.More(); anItF2.Next()) {
234             if(aS.IsSame(anItF2.Value())) {
235               add2 = Standard_False;
236               break;
237             }
238           }
239         }
240
241         if(add2) {
242           myGenMap.ChangeFind(aF2).Append(aS);
243         }
244       }
245     }
246   }
247   aResultMap.Clear();
248   TopTools_IndexedMapOfShape aResultMapV;
249
250   if(!myResult.IsNull()) {
251     TopExp::MapShapes(myResult, TopAbs_VERTEX, aResultMapV);
252   }
253
254   for(i = 1; i <= aFFs.Extent(); i++) {
255     BOPTools_SSInterference& aFF = aFFs(i);
256     Standard_Integer nF1 = aFF.Index1();
257     Standard_Integer nF2 = aFF.Index2();
258
259     if(aFF.IsTangentFaces())
260       continue;
261     TopoDS_Shape aF1 = theDSFiller->DS().Shape(nF1);
262     TopoDS_Shape aF2 = theDSFiller->DS().Shape(nF2);
263
264     TColStd_ListIteratorOfListOfInteger anIt(aFF.AloneVertices());
265
266     for(; anIt.More(); anIt.Next()) {
267       TopoDS_Shape aNewVertex = theDSFiller->DS().Shape(anIt.Value());
268
269       if(aResultMapV.Contains(aNewVertex)) {
270         TopTools_ListOfShape thelist3;
271         if(!myGenMap.IsBound(aF1))
272           myGenMap.Bind(aF1, thelist3);
273         myGenMap.ChangeFind(aF1).Append(aNewVertex);
274
275         if(!myGenMap.IsBound(aF2))
276           myGenMap.Bind(aF2, thelist3);
277         myGenMap.ChangeFind(aF2).Append(aNewVertex);
278       }
279     }
280   }
281 }
282
283 // ====================================================================================================
284 // function: FillEdgeHistory
285 // purpose:  
286 // ====================================================================================================
287 void BOP_ShellSolidHistoryCollector::FillEdgeHistory(const BOPTools_PDSFiller& theDSFiller) 
288 {
289   const BooleanOperations_ShapesDataStructure& aDS = theDSFiller->DS();
290   const BOPTools_PaveFiller& aPaveFiller           = theDSFiller->PaveFiller();
291   const BOPTools_SplitShapesPool& aSplitShapesPool = aPaveFiller.SplitShapesPool();
292   TopTools_IndexedMapOfShape aResultMap, aFreeBoundaryMap;
293
294   if(!myResult.IsNull()) {
295     TopExp::MapShapes(myResult, TopAbs_EDGE, aResultMap);
296
297     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
298
299     if(myS1.ShapeType() == TopAbs_SHELL) {
300       TopExp::MapShapesAndAncestors(myS1, TopAbs_EDGE, TopAbs_FACE, aEFMap);
301     }
302     else {
303       TopExp::MapShapesAndAncestors(myS2, TopAbs_EDGE, TopAbs_FACE, aEFMap);
304     }
305     Standard_Integer i = 0;
306
307     for(i = 1; i <= aEFMap.Extent(); i++) {
308       if(aEFMap.FindFromIndex(i).Extent() < 2)
309         aFreeBoundaryMap.Add(aEFMap.FindKey(i));
310     }
311   }
312   
313   Standard_Integer iRank = 1;
314
315   for(; iRank <= 2; iRank++) {
316     BooleanOperations_StateOfShape aStateCmp, aState;
317     aStateCmp = BOP_BuilderTools::StateToCompare(iRank, myOp);
318     Standard_Integer i = 0;
319     Standard_Integer nb = aDS.NumberOfShapesOfTheObject();
320     nb = (iRank == 1) ? nb : (nb + aDS.NumberOfShapesOfTheTool());
321     Standard_Integer startindex = (iRank == 1) ? 1 : (aDS.NumberOfShapesOfTheObject() + 1);
322
323     for(i = startindex; i <= nb; i++) {
324       if(aDS.GetShapeType(i) != TopAbs_EDGE)
325         continue;
326       const BOPTools_ListOfPaveBlock& aLPB = aSplitShapesPool(aDS.RefEdge(i));
327       const TopoDS_Shape& anOldShape = aDS.Shape(i);
328
329       if(!aFreeBoundaryMap.Contains(anOldShape)) {
330         continue;
331       }
332
333       if(!aLPB.IsEmpty()) {
334         BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aLPB);
335
336         for(; aPBIt.More(); aPBIt.Next()) {
337           const BOPTools_PaveBlock& aPB = aPBIt.Value();
338           Standard_Integer nSp = aPB.Edge();
339
340           if(nSp == i)
341             continue;
342           aState=aDS.GetState(nSp);
343
344           if (aState == aStateCmp) {
345             const TopoDS_Shape& aNewShape = aDS.Shape(nSp);
346
347             if(aResultMap.Contains(aNewShape)) {
348               TopTools_DataMapOfShapeListOfShape& aHistoryMap = myModifMap;
349
350               if(aHistoryMap.IsBound(anOldShape)) {
351                 aHistoryMap.ChangeFind(anOldShape).Append(aNewShape);
352               }
353               else {
354                 TopTools_ListOfShape aShapeList;
355                 aShapeList.Append(aNewShape);
356                 aHistoryMap.Bind(anOldShape, aShapeList);
357               }
358             }
359           }
360         }
361       }
362       //end if(!aLPB.IsEmpty...
363
364       const BOPTools_CommonBlockPool& aCBPool = aPaveFiller.CommonBlockPool();
365       const BOPTools_ListOfCommonBlock& aLCB = aCBPool(aDS.RefEdge(i));
366       BOPTools_ListIteratorOfListOfCommonBlock anItCB(aLCB);
367
368       for (; anItCB.More(); anItCB.Next()) {
369         const BOPTools_CommonBlock& aCB = anItCB.Value();
370         const BOPTools_PaveBlock& aPB = aCB.PaveBlock1();
371         Standard_Integer nSp = aPB.Edge();
372         TopoDS_Shape aNewShape = aDS.Shape(nSp);
373
374         if(aResultMap.Contains(aNewShape)) {
375           TopTools_DataMapOfShapeListOfShape& aHistoryMap = myModifMap;
376
377           if(aHistoryMap.IsBound(anOldShape)) {
378             aHistoryMap.ChangeFind(anOldShape).Append(aNewShape);
379           }
380           else {
381             TopTools_ListOfShape aShapeList;
382             aShapeList.Append(aNewShape);
383             aHistoryMap.Bind(anOldShape, aShapeList);
384           }
385         }
386       }
387       // end for (; anItCB.More...
388     }
389   }
390 }
391