Salome HOME
bf5075d38f5b0b7c51acd75428bb17a38c6f6a1c
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2023  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                                              const double theFuzzy)
132 {
133   build(theObjects, theTools, theFuzzy);
134 }
135
136 static void prepareShapes(const TopoDS_Shape&   theShape,
137                            TopTools_ListOfShape& theSimpleList)
138 {
139   if (theShape.ShapeType() != TopAbs_COMPOUND) {
140       theSimpleList.Append(theShape);
141     return;
142   }
143
144   // explode compound on simple shapes to allow their intersections
145   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
146   for (; It.More(); It.Next()) {
147     TopoDS_Shape curSh = It.Value();
148     prepareShapes(curSh, theSimpleList);
149   }
150 }
151
152 //=================================================================================================
153 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
154                                   const ListOfShape& theTools,
155                                   const double theFuzzy)
156 {
157   if (theObjects.empty()) {
158     return;
159   }
160
161   // Creating partition operation.
162   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
163   this->setImpl(anOperation);
164   this->setBuilderType(OCCT_BOPAlgo_Builder);
165
166   TopTools_MapOfShape ShapesMap;
167   // Getting objects.
168   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
169       anObjectsIt != theObjects.end();
170       anObjectsIt++)
171   {
172     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
173     // #2240: decompose compounds to get the valid result
174     TopTools_ListOfShape aSimpleShapes;
175     prepareShapes(aShape, aSimpleShapes);
176     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
177     for (; aSimpleIter.More(); aSimpleIter.Next()) {
178       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
179       if (ShapesMap.Add(aSimpleSh)) {
180         anOperation->AddArgument(aSimpleSh);
181       }
182     }
183   }
184
185   // Getting tools.
186   for (ListOfShape::const_iterator
187        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
188     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
189     // #2419: decompose compounds to get the valid result
190     TopTools_ListOfShape aSimpleShapes;
191     prepareShapes(aShape, aSimpleShapes);
192     TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
193     for (; aSimpleIter.More(); aSimpleIter.Next()) {
194       const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
195       if (ShapesMap.Add(aSimpleSh)) {
196         anOperation->AddTool(aSimpleSh);
197       }
198     }
199   }
200
201   // Set parallel processing mode (default is false)
202   Standard_Boolean bRunParallel = Standard_True;
203   anOperation->SetRunParallel(bRunParallel);
204
205   if (theFuzzy > 0) anOperation->SetFuzzyValue(theFuzzy);
206
207   // Building and getting result.
208   anOperation->Perform();
209   if (anOperation->HasErrors())
210     return;
211   TopoDS_Shape aResult = anOperation->Shape();
212
213   if(aResult.ShapeType() == TopAbs_COMPOUND) {
214     // Exclude faces and edges which are shared as another sub-shape.
215     NCollection_Vector<TopoDS_Shape> aFaces;
216     NCollection_Vector<TopoDS_Shape> anEdges;
217     TopoDS_Compound aTempCompound;
218     TopoDS_Builder aBuilder;
219     aBuilder.MakeCompound(aTempCompound);
220     for(TopoDS_Iterator anIt(aResult);
221         anIt.More();
222         anIt.Next()) {
223       const TopoDS_Shape& aSubShape = anIt.Value();
224       if (aSubShape.ShapeType() == TopAbs_FACE) {
225         aFaces.Append(aSubShape);
226       } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
227         anEdges.Append(aSubShape);
228       } else {
229         aBuilder.Add(aTempCompound, aSubShape);
230       }
231     }
232
233     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
234         anIt.More();
235         anIt.Next())
236     {
237       const TopoDS_Shape& aSubShape = anIt.Value();
238       if (!isSubShape(aTempCompound, aSubShape))
239       {
240         aBuilder.Add(aTempCompound, aSubShape);
241       }
242     }
243
244     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
245         anIt.More();
246         anIt.Next())
247     {
248       const TopoDS_Shape& aSubShape = anIt.Value();
249       if (!isSubShape(aTempCompound, aSubShape))
250       {
251         aBuilder.Add(aTempCompound, aSubShape);
252       }
253     }
254
255     aResult = aTempCompound;
256   }
257
258   if(aResult.ShapeType() == TopAbs_COMPOUND) {
259     // sort sub-shapes of compound before creation of a compsolid
260     sortCompound(aResult, anOperation);
261
262     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
263     aGeomShape->setImpl(new TopoDS_Shape(aResult));
264     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
265   }
266
267   // Setting result.
268   if(aResult.IsNull()) {
269     return;
270   }
271   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
272   aShape->setImpl(new TopoDS_Shape(aResult));
273   this->setShape(aShape);
274   this->setDone(true);
275 }