Salome HOME
Fix for the issue #2517 . Stabilization of the order of results of partition based...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Partition.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <GeomAlgoAPI_SortListOfShapes.h>
26
27 #include <GEOMAlgo_Splitter.hxx>
28
29 #include <NCollection_Vector.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TopoDS_Builder.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <BRepTools_History.hxx>
34
35
36 //=================================================================================================
37 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
38 {
39   for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
40     if(theSubShape.IsSame(anExp.Current())) {
41       return true;
42     }
43   }
44
45   return false;
46 }
47
48 //=================================================================================================
49 void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) {
50   if (BRepTools_History::IsSupportedType(theShape)) {
51     theResult.Append(theShape);
52   } else {
53     for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) {
54       getHistorySupportedType(aSubIter.Value(), theResult);
55     }
56   }
57 }
58
59 //=================================================================================================
60 // Operation is used for production of ordered sorting: generated/modified from the first argument
61 // must be located in hte result first, etc. THis is for the issue #2517
62 static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation)
63 {
64   TopoDS_Compound aResCompound;
65   TopoDS_Builder aBuilder;
66   aBuilder.MakeCompound(aResCompound);
67
68   TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments
69
70   bool aNotProduced = true;// flag to add to result also results that were not produced by any argument
71   TopTools_ListOfShape::Iterator anArgs(theOperation->Arguments());
72   while(aNotProduced || anArgs.More()) {
73     // collect shapes that were produced from the current argument
74     TopTools_MapOfShape aProducedByArg;
75     if (anArgs.More()) {
76       TopTools_ListOfShape allArgs;
77       getHistorySupportedType(anArgs.Value(), allArgs);
78       for (TopTools_ListOfShape::Iterator argsIter(allArgs); argsIter.More(); argsIter.Next()) {
79         aProducedByArg.Add(argsIter.Value()); // if argument was not modified, it is fully in the result
80         const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
81         for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
82           aProducedByArg.Add(aModIter.Value());
83         }
84         const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
85         for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next()) {
86           aProducedByArg.Add(aGenIter.Value());
87         }
88       }
89       anArgs.Next();
90     }
91     else {
92       aNotProduced = false;
93     }
94
95     ListOfShape aCombiningShapes;
96     for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
97       bool aProducedContains = false;
98       if (aNotProduced) { // collect all supported type-shapes of result
99         TopTools_ListOfShape allRes;
100         getHistorySupportedType(anIt.Value(), allRes);
101         for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
102           if (aProducedByArg.Contains(aResIter.Value())) {
103             aProducedContains = true;
104             break;
105           }
106         }
107       }
108       if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
109         GeomShapePtr aSub(new GeomAPI_Shape);
110         aSub->setImpl(new TopoDS_Shape(anIt.Value()));
111         aCombiningShapes.push_back(aSub);
112       }
113     }
114
115     // sort sub-shapes of compound to stabilize the sequence of the Partition's results
116     GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
117
118     for (ListOfShape::iterator anIt = aCombiningShapes.begin();
119       anIt != aCombiningShapes.end(); ++anIt)
120       aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
121   }
122
123   theCompound = aResCompound;
124 }
125
126 //=================================================================================================
127 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
128                                                            const ListOfShape& theTools)
129 {
130   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
131   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
132     return aPartitionAlgo.shape();
133   }
134   return std::shared_ptr<GeomAPI_Shape>();
135 }
136
137 //=================================================================================================
138 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
139                                              const ListOfShape& theTools)
140 {
141   build(theObjects, theTools);
142 }
143
144 static void prepareShapes(const TopoDS_Shape&   theShape,
145                            TopTools_ListOfShape& theSimpleList)
146 {
147   if (theShape.ShapeType() != TopAbs_COMPOUND) {
148       theSimpleList.Append(theShape);
149     return;
150   }
151
152   // explode compound on simple shapes to allow their intersections
153   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
154   for (; It.More(); It.Next()) {
155     TopoDS_Shape curSh = It.Value();
156     prepareShapes(curSh, theSimpleList);
157   }
158 }
159
160 //=================================================================================================
161 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
162                                   const ListOfShape& theTools)
163 {
164   if (theObjects.empty()) {
165     return;
166   }
167
168   // Creating partition operation.
169   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
170   this->setImpl(anOperation);
171   this->setBuilderType(OCCT_BOPAlgo_Builder);
172
173   TopTools_MapOfShape ShapesMap;
174   // Getting objects.
175   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
176       anObjectsIt != theObjects.end();
177       anObjectsIt++)
178   {
179     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
180     // #2240: decompose compounds to get the valid result
181     TopTools_ListOfShape aSimpleShapes;
182     prepareShapes(aShape, aSimpleShapes);
183     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
184     for (; aSimpleIter.More(); aSimpleIter.Next()) {
185       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
186       if (ShapesMap.Add(aSimpleSh)) {
187         anOperation->AddArgument(aSimpleSh);
188       }
189     }
190   }
191
192   // Getting tools.
193   for (ListOfShape::const_iterator
194        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
195     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
196     // #2419: decompose compounds to get the valid result
197     TopTools_ListOfShape aSimpleShapes;
198     prepareShapes(aShape, aSimpleShapes);
199     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
200     for (; aSimpleIter.More(); aSimpleIter.Next()) {
201       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
202       if (ShapesMap.Add(aSimpleSh)) {
203         anOperation->AddTool(aSimpleSh);
204       }
205     }
206   }
207
208   // Building and getting result.
209   anOperation->Perform();
210   if (anOperation->HasErrors())
211     return;
212   TopoDS_Shape aResult = anOperation->Shape();
213
214   if(aResult.ShapeType() == TopAbs_COMPOUND) {
215     // Exclude faces and edges which are shared as another sub-shape.
216     NCollection_Vector<TopoDS_Shape> aFaces;
217     NCollection_Vector<TopoDS_Shape> anEdges;
218     TopoDS_Compound aTempCompound;
219     TopoDS_Builder aBuilder;
220     aBuilder.MakeCompound(aTempCompound);
221     for(TopoDS_Iterator anIt(aResult);
222         anIt.More();
223         anIt.Next()) {
224       const TopoDS_Shape& aSubShape = anIt.Value();
225       if (aSubShape.ShapeType() == TopAbs_FACE) {
226         aFaces.Append(aSubShape);
227       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
228         anEdges.Append(aSubShape);
229       } else {
230         aBuilder.Add(aTempCompound, aSubShape);
231       }
232     }
233
234     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
235         anIt.More();
236         anIt.Next())
237     {
238       const TopoDS_Shape& aSubShape = anIt.Value();
239       if (!isSubShape(aTempCompound, aSubShape))
240       {
241         aBuilder.Add(aTempCompound, aSubShape);
242       }
243     }
244
245     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
246         anIt.More();
247         anIt.Next())
248     {
249       const TopoDS_Shape& aSubShape = anIt.Value();
250       if (!isSubShape(aTempCompound, aSubShape))
251       {
252         aBuilder.Add(aTempCompound, aSubShape);
253       }
254     }
255
256     aResult = aTempCompound;
257   }
258
259   if(aResult.ShapeType() == TopAbs_COMPOUND) {
260     // sort sub-shapes of compound before creation of a compsolid
261     sortCompound(aResult, anOperation);
262
263     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
264     aGeomShape->setImpl(new TopoDS_Shape(aResult));
265     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
266   }
267
268   // Setting result.
269   if(aResult.IsNull()) {
270     return;
271   }
272   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
273   aShape->setImpl(new TopoDS_Shape(aResult));
274   this->setShape(aShape);
275   this->setDone(true);
276 }