Salome HOME
Copyright update 2020
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Partition.h"
21
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24 #include <GeomAlgoAPI_SortListOfShapes.h>
25
26 #include <GEOMAlgo_Splitter.hxx>
27
28 #include <NCollection_Vector.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopoDS_Builder.hxx>
31 #include <TopTools_MapOfShape.hxx>
32 #include <BRepTools_History.hxx>
33
34
35 //=================================================================================================
36 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
37 {
38   for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
39     if(theSubShape.IsSame(anExp.Current())) {
40       return true;
41     }
42   }
43
44   return false;
45 }
46
47 //=================================================================================================
48 void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) {
49   if (BRepTools_History::IsSupportedType(theShape)) {
50     theResult.Append(theShape);
51   } else {
52     for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) {
53       getHistorySupportedType(aSubIter.Value(), theResult);
54     }
55   }
56 }
57
58 //=================================================================================================
59 // Operation is used for production of ordered sorting: generated/modified from the first argument
60 // must be located in hte result first, etc. THis is for the issue #2517
61 static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation)
62 {
63   TopoDS_Compound aResCompound;
64   TopoDS_Builder aBuilder;
65   aBuilder.MakeCompound(aResCompound);
66
67   TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments
68
69   // flag to add to result also results which were not produced by any argument
70   bool aNotProduced = true;
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         // if argument was not modified, it is fully in the result
80         aProducedByArg.Add(argsIter.Value());
81         const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
82         for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
83           aProducedByArg.Add(aModIter.Value());
84         }
85         const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
86         for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next())
87         {
88           aProducedByArg.Add(aGenIter.Value());
89         }
90       }
91       anArgs.Next();
92     }
93     else {
94       aNotProduced = false;
95     }
96
97     ListOfShape aCombiningShapes;
98     for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
99       bool aProducedContains = false;
100       if (aNotProduced) { // collect all supported type-shapes of result
101         TopTools_ListOfShape allRes;
102         getHistorySupportedType(anIt.Value(), allRes);
103         for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
104           if (aProducedByArg.Contains(aResIter.Value())) {
105             aProducedContains = true;
106             break;
107           }
108         }
109       }
110       if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
111         GeomShapePtr aSub(new GeomAPI_Shape);
112         aSub->setImpl(new TopoDS_Shape(anIt.Value()));
113         aCombiningShapes.push_back(aSub);
114       }
115     }
116
117     // sort sub-shapes of compound to stabilize the sequence of the Partition's results
118     GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
119
120     for (ListOfShape::iterator anIt = aCombiningShapes.begin();
121       anIt != aCombiningShapes.end(); ++anIt)
122       aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
123   }
124
125   theCompound = aResCompound;
126 }
127
128 //=================================================================================================
129 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
130                                              const ListOfShape& theTools)
131 {
132   build(theObjects, theTools);
133 }
134
135 static void prepareShapes(const TopoDS_Shape&   theShape,
136                            TopTools_ListOfShape& theSimpleList)
137 {
138   if (theShape.ShapeType() != TopAbs_COMPOUND) {
139       theSimpleList.Append(theShape);
140     return;
141   }
142
143   // explode compound on simple shapes to allow their intersections
144   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
145   for (; It.More(); It.Next()) {
146     TopoDS_Shape curSh = It.Value();
147     prepareShapes(curSh, theSimpleList);
148   }
149 }
150
151 //=================================================================================================
152 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
153                                   const ListOfShape& theTools)
154 {
155   if (theObjects.empty()) {
156     return;
157   }
158
159   // Creating partition operation.
160   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
161   this->setImpl(anOperation);
162   this->setBuilderType(OCCT_BOPAlgo_Builder);
163
164   TopTools_MapOfShape ShapesMap;
165   // Getting objects.
166   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
167       anObjectsIt != theObjects.end();
168       anObjectsIt++)
169   {
170     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
171     // #2240: decompose compounds to get the valid result
172     TopTools_ListOfShape aSimpleShapes;
173     prepareShapes(aShape, aSimpleShapes);
174     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
175     for (; aSimpleIter.More(); aSimpleIter.Next()) {
176       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
177       if (ShapesMap.Add(aSimpleSh)) {
178         anOperation->AddArgument(aSimpleSh);
179       }
180     }
181   }
182
183   // Getting tools.
184   for (ListOfShape::const_iterator
185        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
186     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
187     // #2419: decompose compounds to get the valid result
188     TopTools_ListOfShape aSimpleShapes;
189     prepareShapes(aShape, aSimpleShapes);
190     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
191     for (; aSimpleIter.More(); aSimpleIter.Next()) {
192       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
193       if (ShapesMap.Add(aSimpleSh)) {
194         anOperation->AddTool(aSimpleSh);
195       }
196     }
197   }
198
199   // Set parallel processing mode (default is false)
200   Standard_Boolean bRunParallel = Standard_True;
201   anOperation->SetRunParallel(bRunParallel);
202
203   // Building and getting result.
204   anOperation->Perform();
205   if (anOperation->HasErrors())
206     return;
207   TopoDS_Shape aResult = anOperation->Shape();
208
209   if(aResult.ShapeType() == TopAbs_COMPOUND) {
210     // Exclude faces and edges which are shared as another sub-shape.
211     NCollection_Vector<TopoDS_Shape> aFaces;
212     NCollection_Vector<TopoDS_Shape> anEdges;
213     TopoDS_Compound aTempCompound;
214     TopoDS_Builder aBuilder;
215     aBuilder.MakeCompound(aTempCompound);
216     for(TopoDS_Iterator anIt(aResult);
217         anIt.More();
218         anIt.Next()) {
219       const TopoDS_Shape& aSubShape = anIt.Value();
220       if (aSubShape.ShapeType() == TopAbs_FACE) {
221         aFaces.Append(aSubShape);
222       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
223         anEdges.Append(aSubShape);
224       } else {
225         aBuilder.Add(aTempCompound, aSubShape);
226       }
227     }
228
229     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
230         anIt.More();
231         anIt.Next())
232     {
233       const TopoDS_Shape& aSubShape = anIt.Value();
234       if (!isSubShape(aTempCompound, aSubShape))
235       {
236         aBuilder.Add(aTempCompound, aSubShape);
237       }
238     }
239
240     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
241         anIt.More();
242         anIt.Next())
243     {
244       const TopoDS_Shape& aSubShape = anIt.Value();
245       if (!isSubShape(aTempCompound, aSubShape))
246       {
247         aBuilder.Add(aTempCompound, aSubShape);
248       }
249     }
250
251     aResult = aTempCompound;
252   }
253
254   if(aResult.ShapeType() == TopAbs_COMPOUND) {
255     // sort sub-shapes of compound before creation of a compsolid
256     sortCompound(aResult, anOperation);
257
258     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
259     aGeomShape->setImpl(new TopoDS_Shape(aResult));
260     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
261   }
262
263   // Setting result.
264   if(aResult.IsNull()) {
265     return;
266   }
267   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
268   aShape->setImpl(new TopoDS_Shape(aResult));
269   this->setShape(aShape);
270   this->setDone(true);
271 }