Salome HOME
20.12.2013.Update to fix case with one input shape: no split.
[modules/hydro.git] / src / HYDROData / HYDROData_SplitToZonesTool.cxx
1
2 #include "HYDROData_SplitToZonesTool.h"
3
4 #include "HYDROData_PolylineXY.h"
5 #include "HYDROData_ShapesGroup.h"
6
7 #include <BRepAlgoAPI_Common.hxx>
8 #include <BRepAlgoAPI_Cut.hxx>
9 #include <BRepBuilderAPI_MakeFace.hxx>
10
11 #include <TopExp_Explorer.hxx>
12 #include <TopoDS.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Iterator.hxx>
15 #include <TopoDS_Wire.hxx>
16 #include <TopoDS_Iterator.hxx>
17 #include <BRepCheck_Analyzer.hxx>
18 #include <NCollection_IncAllocator.hxx>
19 #include <BOPAlgo_Builder.hxx>
20 #include <BOPAlgo_PaveFiller.hxx>
21 #include <NCollection_DataMap.hxx>
22 #include <TopTools_ListOfShape.hxx>
23 #include <TopTools_ListIteratorOfListOfShape.hxx>
24 #include <TopTools_ShapeMapHasher.hxx>
25 typedef NCollection_DataMap<TopoDS_Shape, TopTools_ListOfShape, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfShape;
26 typedef HYDROData_DataMapOfShapeListOfShape::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape;
27 typedef NCollection_DataMap<TopoDS_Shape,  QStringList, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfString;
28 typedef HYDROData_DataMapOfShapeListOfString::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfString;
29 #undef _NCollection_MapHasher
30
31 #define DEB_SPLIT_TO_ZONES 1
32 #ifdef DEB_SPLIT_TO_ZONES
33 #include <BRepTools.hxx>
34 #endif
35 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
36 {
37   TopoDS_Face aResFace;
38
39   if( !Shape.IsNull() )
40   {
41     if ( Shape.ShapeType() == TopAbs_FACE )
42     {
43       aResFace = TopoDS::Face( Shape );
44     }
45     else if ( Shape.ShapeType() == TopAbs_WIRE )
46     {
47       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
48       aMakeFace.Build();
49       if( aMakeFace.IsDone() )
50         aResFace = aMakeFace.Face();
51     }
52   }
53
54   return aResFace;
55 }
56
57 HYDROData_SplitToZonesTool::SplitDataList
58   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects&  theObjectList,
59                                      const HYDROData_SequenceOfObjects&  theGroupsList,
60                                      const Handle(HYDROData_PolylineXY)& thePolyline )
61 {
62   SplitDataList anOutputSplitDataList;
63   if(theObjectList.IsEmpty()) return anOutputSplitDataList;
64   // Preparation. 
65   // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
66   SplitDataList anInputSplitDataList;
67   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
68   {
69     Handle(HYDROData_Object) aGeomObj = 
70       Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
71     if( aGeomObj.IsNull() )
72       continue;
73
74     TopoDS_Shape aShape = aGeomObj->GetTopShape();
75     if ( aShape.IsNull() )
76       continue;  
77
78     if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
79       // Create split data for each face contained in the compound
80       TopExp_Explorer anExp( aShape, TopAbs_FACE );
81       for ( ; anExp.More(); anExp.Next() ) {
82         const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
83         if ( !aFace.IsNull() ) {
84           SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
85           anInputSplitDataList.append( aSplitData );
86         }
87       }
88     } else {
89       SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
90       anInputSplitDataList.append( aSplitData );
91     }
92   }
93   //
94   SplitDataList anInputGroupList;
95   for( int anIndex = 1; anIndex <= theGroupsList.Length(); anIndex++ )
96   {
97     Handle(HYDROData_ShapesGroup) aGeomGroup = 
98       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
99     if( aGeomGroup.IsNull() )
100       continue;
101       
102     TopTools_SequenceOfShape aGroupShapes;
103     aGeomGroup->GetShapes( aGroupShapes );
104     for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
105       const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
106       if ( aGroupShape.IsNull() )
107         continue;  
108
109       if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {    
110         TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
111         for ( ; anExp.More(); anExp.Next() ) {
112           const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
113           if ( !anEdge.IsNull() ) {
114             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
115             anInputGroupList.append( aSplitData );
116           }
117           }
118       } else {
119         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
120         anInputGroupList.append( aSplitData );
121       }
122     }
123   }
124   // If only one shape is given we don't split it 
125   // algorithm just returns the unpacked input data
126   if(theObjectList.Size() == 1)  {
127         anOutputSplitDataList.append(anInputSplitDataList); 
128         if(!theGroupsList.IsEmpty() ) 
129           anOutputSplitDataList.append(anInputGroupList);       
130     return anOutputSplitDataList;
131   }
132  
133   HYDROData_DataMapOfShapeListOfString aDM3;
134   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
135     QStringList aListOfNames;
136     for (int i=0;i < anInputGroupList.size() ;i++) {
137       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
138       aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
139     }
140   }
141   // Step 1. Prepare Partition structures.
142   Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
143   BOPAlgo_PaveFiller* aPaveFiller      = new BOPAlgo_PaveFiller(pA1);
144   BOPAlgo_Builder* aBuilder            = new BOPAlgo_Builder(pA2);
145   BOPCol_ListOfShape aLS;  
146   QStringList aListOfNames;
147   for (int i=0;i < anInputSplitDataList.size() ;i++) {
148     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
149     aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
150     aLS.Append(aSh);
151   }
152   aPaveFiller->SetArguments(aLS);
153   aPaveFiller->Perform();
154   Standard_Integer anErr = aPaveFiller->ErrorStatus();
155   if(anErr) 
156     return anOutputSplitDataList;
157   BOPDS_PDS pDS = aPaveFiller->PDS();
158   if (!pDS) 
159     return anOutputSplitDataList;
160   aBuilder->Clear();
161
162   // Step 2. Split faces 
163   BOPCol_ListIteratorOfListOfShape anIt(aPaveFiller->Arguments());
164   for (; anIt.More(); anIt.Next()) {
165     const TopoDS_Shape& aS = anIt.Value();
166     aBuilder->AddArgument(aS);
167   }
168   aBuilder->PerformWithFiller(*aPaveFiller);
169   anErr = aBuilder->ErrorStatus();
170   if(anErr) 
171     return anOutputSplitDataList;
172   const TopoDS_Shape& aResult = aBuilder->Shape();
173   if (aResult.IsNull()) 
174     return anOutputSplitDataList;
175   BRepCheck_Analyzer aCheck (aResult);
176   if(!aCheck.IsValid()) {
177 #ifdef DEB_SPLIT_TO_ZONES
178     cout << "result is not valid" <<endl;
179     BRepTools::Write(aResult, "SplitFacesNV.brep");  
180 #endif
181     return anOutputSplitDataList;
182   }
183 #ifdef DEB_SPLIT_TO_ZONES
184   //BRepTools::Write(aResult, "SplitFacesV.brep");
185 #endif
186   
187   // Step 3. Collect history  
188   HYDROData_DataMapOfShapeListOfShape aDM1;
189   anIt.Init(aLS); 
190   //TCollection_AsciiString aNamM ("EdgM_");
191   //TCollection_AsciiString aNamG ("EdgG_");
192   for (int i =1;anIt.More();anIt.Next(),i++) {
193     const TopTools_ListOfShape& aListOfNew = aBuilder->Modified(anIt.Value());
194     TopTools_ListOfShape aList;
195     TopTools_ListIteratorOfListOfShape it(aListOfNew);
196     for(;it.More();it.Next())       
197       aList.Append(it.Value());
198     // Bug in History: partition should give only modified entities! => temporary solution is used
199     const TopTools_ListOfShape& aListOfGen = aBuilder->Generated(anIt.Value());
200         it.Initialize(aListOfGen);    
201         for(;it.More();it.Next())     
202           aList.Append(it.Value());
203
204         aDM1.Bind(anIt.Value(), aList);
205         //TCollection_AsciiString aName;
206         if(!anInputGroupList.isEmpty() ) { 
207       aList.Clear();
208           TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
209           for (int j =1;exp.More();exp.Next(),j++) {
210             const TopTools_ListOfShape& aListM = aBuilder->Modified(exp.Current());     
211                 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
212                 it.Initialize(aListM);    
213                 for(int k=1;it.More();it.Next(),k++) {    
214               aList.Append(it.Value());
215                   //aName = aNamM + i + j +k +".brep";
216                   //BRepTools::Write(it.Value(),aName.ToCString());
217                 }
218                 const TopTools_ListOfShape& aListG = aBuilder->Generated(exp.Current());
219                 it.Initialize(aListG);    
220                 for(int k=1;it.More();it.Next(),k++)   {  
221               aList.Append(it.Value());
222                   //aName = aNamG + i + j +k +".brep";
223                   //BRepTools::Write(it.Value(),aName.ToCString());
224                 }
225                 //cout << "NB_EDGE = " << aList.Extent() <<endl;
226                 aDM1.Bind(exp.Current(), aList);
227           }      
228         }
229   }
230
231   // aDM2: NewShape ==> ListOfOldShapes
232   HYDROData_DataMapOfShapeListOfShape aDM2;
233   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
234   for(;aMIt.More();aMIt.Next()) {
235     const TopoDS_Shape& aKey = aMIt.Key();
236     TopTools_ListOfShape aList;
237     aList.Append(aKey);
238     const TopTools_ListOfShape& aListOfNew = aMIt.Value();
239     TopTools_ListIteratorOfListOfShape it(aListOfNew);
240     for(;it.More();it.Next()) {
241       if(!aDM2.IsBound(it.Value()))
242         aDM2.Bind(it.Value(), aList);
243       else {
244         TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
245         aList.Prepend(aKey);
246       }
247     }
248   }
249   //cout << "DM2 Ext = " <<aDM2.Extent() <<endl;
250   // Step 4. Fill output structure.
251   aMIt.Initialize(aDM2);
252   for(int i =1;aMIt.More();aMIt.Next(),i++) {
253     SplitData aDestSplitData;
254     const TopoDS_Shape& aKey = aMIt.Key(); //new
255     aDestSplitData.Shape = aKey;
256     if(aKey.ShapeType() == TopAbs_FACE)
257       aDestSplitData.Type = SplitData::Data_Zone;
258     else
259       aDestSplitData.Type = SplitData::Data_Edge;
260
261     QStringList aListOfNames; // names processing
262     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
263     TopTools_ListIteratorOfListOfShape it(aListOfOld);
264         for(;it.More();it.Next()) {     
265           const TopoDS_Shape& aSh = it.Value(); //old
266           if(aDM3.IsBound(aSh)) {
267             const QStringList& ObjectNames = aDM3.Find(aSh);
268                 aListOfNames.append(ObjectNames);
269           }      
270         }
271
272         aDestSplitData.ObjectNames = aListOfNames;  
273         anOutputSplitDataList.append(aDestSplitData);
274   }
275   //cout << "anOutputSplitDataList = " <<anOutputSplitDataList.size() <<endl;
276   return anOutputSplitDataList;
277
278 /* 
279   // Step 3. Extract the separate regions.
280   SplitDataList anExtractedSplitDataList;
281   SplitDataListIterator anOutputIter( anOutputSplitDataList );
282   while( anOutputIter.hasNext() )
283   {
284     const SplitData& anOutputSplitData = anOutputIter.next();
285     anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
286   }
287 */
288  // return anExtractedSplitDataList;
289 }
290
291 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
292                                                const SplitData& theData2,
293                                                SplitData& theData1Subtracted,
294                                                SplitData& theData2Subtracted,
295                                                SplitData& theDataIntersected )
296 {
297   const TopoDS_Shape& aShape1 = theData1.Shape;
298   const TopoDS_Shape& aShape2 = theData2.Shape;
299
300   const QStringList& anObjectNames1 = theData1.ObjectNames;
301   const QStringList& anObjectNames2 = theData2.ObjectNames;
302
303   BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
304   TopoDS_Shape aCommonShape = aCommon.Shape();
305   if( aCommonShape.IsNull() )
306   {
307     theData1Subtracted = theData1;
308     theData2Subtracted = theData2;
309     return false;
310   }
311
312   BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
313   TopoDS_Shape aCut1Shape = aCut1.Shape();
314
315   BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
316   TopoDS_Shape aCut2Shape = aCut2.Shape();
317
318   theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
319   theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
320   theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
321
322   return true;
323 }
324
325 HYDROData_SplitToZonesTool::SplitDataList
326 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
327 {
328   SplitDataList aSplitDataList;
329   TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
330   for( ; anExp.More(); anExp.Next() )
331   {
332     TopoDS_Shape aShape = anExp.Current();
333     if( aShape.ShapeType() == TopAbs_FACE )
334     {
335       TopoDS_Face aFace = TopoDS::Face( aShape );
336       if( !aFace.IsNull() )
337       {
338         SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
339         aSplitDataList.append( aSplitData );
340       }
341     }
342   }
343   return aSplitDataList;
344 }