0024187: Wrong result of COMMON operation.
[occt.git] / src / BOPAlgo / BOPAlgo_Builder_4.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2012 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // The content of this file is subject to the Open CASCADE Technology Public
8 // License Version 6.5 (the "License"). You may not use the content of this file
9 // except in compliance with the License. Please obtain a copy of the License
10 // at http://www.opencascade.org and read it completely before using this file.
11 //
12 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
13 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 //
15 // The Original Code and all software distributed under the License is
16 // distributed on an "AS IS" basis, without warranty of any kind, and the
17 // Initial Developer hereby disclaims all such warranties, including without
18 // limitation, any warranties of merchantability, fitness for a particular
19 // purpose or non-infringement. Please see the License for the specific terms
20 // and conditions governing the rights and limitations under the License.
21
22
23 #include <BOPAlgo_Builder.ixx>
24
25 #include <TopoDS_Iterator.hxx>
26
27 #include <BOPCol_ListOfShape.hxx>
28 #include <BOPCol_MapOfShape.hxx>
29
30 #include <BOPTools_AlgoTools.hxx>
31 #include <BOPTools.hxx>
32
33 //=======================================================================
34 //function : Generated
35 //purpose  : 
36 //=======================================================================
37   const TopTools_ListOfShape& BOPAlgo_Builder::Generated(const TopoDS_Shape& theS)
38 {
39   Standard_Boolean bHasImage, bToReverse;
40   TopAbs_ShapeEnum aType;
41   BOPCol_ListIteratorOfListOfShape aIt;
42   //
43   myHistShapes.Clear();
44   //
45   if (theS.IsNull()) {
46     return myHistShapes;
47   }
48   //
49   bHasImage=myImages.IsBound(theS);
50   if (!bHasImage) {
51     return myHistShapes;
52   }
53   //
54   aType=theS.ShapeType();
55   //
56   if (!(aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
57       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
58     return myHistShapes;
59   } 
60   //
61   //PrepareHistory();
62   //
63   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
64   aIt.Initialize(aLSp);
65   for (; aIt.More(); aIt.Next()) {
66     const TopoDS_Shape& aSp=aIt.Value();
67     if (myShapesSD.IsBound(aSp)) {
68       if (myMapShape.Contains(aSp)) {
69         TopoDS_Shape aSpR=myShapesSD.Find(aSp);
70         //
71         if (aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
72           aSpR.Orientation(theS.Orientation());
73         }
74         else {
75           bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aSpR, theS, myContext);
76           if (bToReverse) {
77             aSpR.Reverse();
78           }
79         }
80         //
81           myHistShapes.Append(aSpR);
82       }
83     }
84   }
85   //
86   return myHistShapes;
87 }
88 //=======================================================================
89 //function : Modified
90 //purpose  : 
91 //=======================================================================
92   const TopTools_ListOfShape& BOPAlgo_Builder::Modified(const TopoDS_Shape& theS)
93 {
94   Standard_Boolean bHasImage, bToReverse;
95   TopAbs_ShapeEnum aType;
96   BOPCol_ListIteratorOfListOfShape aIt;
97   //
98   myHistShapes.Clear();
99   //
100   if (theS.IsNull()) {
101     return myHistShapes;
102   }
103   //
104   bHasImage=myImages.IsBound(theS);
105   if (!bHasImage) {
106     return myHistShapes;
107   }
108   //
109   aType=theS.ShapeType();
110   //
111   if (!(aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
112       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
113     return myHistShapes;
114   } 
115   //
116   //PrepareHistory();
117   //
118   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
119   aIt.Initialize(aLSp);
120   for (; aIt.More(); aIt.Next()) {
121     TopoDS_Shape aSp=aIt.Value();
122     if (!myShapesSD.IsBound(aSp)) {
123       if (myMapShape.Contains(aSp)) {
124         //
125         if (aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
126           aSp.Orientation(theS.Orientation());
127         }
128         else {
129           bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aSp, theS, myContext);
130           if (bToReverse) {
131             aSp.Reverse();
132           }
133         }
134         //
135         myHistShapes.Append(aSp);
136       }
137     }
138   }
139   //
140   return myHistShapes;
141 }
142 //=======================================================================
143 //function : IsDeleted
144 //purpose  : 
145 //=======================================================================
146   Standard_Boolean BOPAlgo_Builder::IsDeleted(const TopoDS_Shape& theS)
147 {
148   Standard_Boolean bRet, bHasImage, bContains;
149   TopAbs_ShapeEnum aType;
150   BOPCol_ListIteratorOfListOfShape aIt;
151   //
152   bRet=Standard_False;
153   //
154   if (theS.IsNull()) {
155     return !bRet; //true
156   }
157   //
158   aType=theS.ShapeType();
159   if (!(aType==TopAbs_EDGE || aType==TopAbs_FACE || 
160       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
161     return !bRet;
162   }
163   //
164   bHasImage=myImages.IsBound(theS);
165   if (!bHasImage) {
166     return !bRet; //true
167   }
168   //
169   //PrepareHistory();
170   //
171   bContains=myMapShape.Contains(theS);
172   if (bContains) {
173     return bRet; //false
174   }
175   //
176   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
177   aIt.Initialize(aLSp);
178   for (; aIt.More(); aIt.Next()) {
179     TopoDS_Shape aSp=aIt.Value();
180     //
181     if (!myShapesSD.IsBound(aSp)) {
182       if (myMapShape.Contains(aSp)) {
183         return bRet; //false
184       }
185     }
186     else {
187       TopoDS_Shape aSpR=myShapesSD.Find(aSp);
188       if (myMapShape.Contains(aSpR)) {
189         return bRet; //false
190       }
191     }
192   }
193   return !bRet; // true
194 }
195 //=======================================================================
196 //function : PrepareHistory
197 //purpose  : 
198 //=======================================================================
199   void BOPAlgo_Builder::PrepareHistory()
200 {
201   if (!myFlagHistory) {
202     return;
203   }
204   //
205   if(myShape.IsNull()) {
206     return;
207   }
208   //
209   Standard_Boolean bHasImage, bContainsSD;
210   TopAbs_ShapeEnum aType;
211   BOPCol_MapOfShape aMS;
212   BOPCol_ListIteratorOfListOfShape aIt;
213   BOPCol_MapIteratorOfMapOfShape aItM;
214   //
215   // 1. Clearing 
216   BOPAlgo_BuilderShape::PrepareHistory();
217   //
218   // 2. myMapShape - all shapes of result with theirs sub-shapes 
219   BOPTools::MapShapes(myShape, myMapShape);
220   //
221   // 3. MS - all argument shapes with theirs sub-shapes
222   aIt.Initialize(myArguments);
223   for (; aIt.More(); aIt.Next()) {
224     const TopoDS_Shape& aSx=aIt.Value();
225     BOPTools::MapShapes(aSx, aMS);
226   }
227   //
228   // 4. Treatment
229   aItM.Initialize(aMS);
230   for (; aItM.More(); aItM.Next()) {
231     const TopoDS_Shape& aSx=aItM.Key();
232     aType=aSx.ShapeType();
233     //
234     // 4.1 .myImagesResult
235     bHasImage=myImages.IsBound(aSx); 
236     //
237     BOPCol_ListOfShape aLSx;
238     if (!bHasImage) {
239       if (myMapShape.Contains(aSx)) {
240         aLSx.Append(aSx);
241         myImagesResult.Add(aSx, aLSx);
242       }
243     }
244     else {
245       const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
246       aIt.Initialize(aLSp);
247       for (; aIt.More(); aIt.Next()) {
248         const TopoDS_Shape& aSp=aIt.Value();
249         if (myMapShape.Contains(aSp)) {
250           aLSx.Append(aSp);
251         }
252       }
253       myImagesResult.Add(aSx, aLSx);
254     }
255     // <- A
256     //
257     // 4.2 As it was 
258     if (!myHasDeleted) {
259       myHasDeleted=IsDeleted(aSx);
260     }
261     //
262     if (!myHasGenerated || !myHasModified) {
263       if (aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
264           aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
265         if (bHasImage) {
266           const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
267           aIt.Initialize(aLSp);
268           for (; aIt.More(); aIt.Next()) {
269             const TopoDS_Shape& aSp=aIt.Value();
270             //
271             if (myMapShape.Contains(aSp)) {
272               bContainsSD=myShapesSD.IsBound(aSp);
273               //
274               if (!myHasGenerated) {
275                 if (bContainsSD) {
276                   myHasGenerated=Standard_True;
277                 }
278               }
279               if (!myHasModified) {
280                 if (!bContainsSD) {
281                   myHasModified=Standard_True;
282                 }
283               }
284             } // if (myMapShape.Contains(aSp))
285           }
286         }
287       } 
288     }
289   }
290   myFlagHistory=Standard_True;
291 }
292
293 // <- A
294     /*
295     BOPCol_ListOfShape aLSx;
296     if (!bHasImage) {
297       if (myMapShape.Contains(aSx)) {
298         aLSx.Append(aSx);
299         myImagesResult.Add(aSx, aLSx);
300       }
301     }
302     else {
303       const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
304       aIt.Initialize(aLSp);
305       for (; aIt.More(); aIt.Next()) {
306         const TopoDS_Shape& aSp=aIt.Value();
307         if (myMapShape.Contains(aSp)) {
308           aLSx.Append(aSp);
309         }
310       }
311       myImagesResult.Add(aSx, aLSx);
312     }
313     */