Salome HOME
1ae7a1b2e641b3f0b031343c84eabed0e326e02b
[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
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
125   HYDROData_DataMapOfShapeListOfString aDM3;
126   if(!anInputGroupList.isEmpty()) {     // Old edge ==> List_Of_Names
127     QStringList aListOfNames;
128     for (int i=0;i < anInputGroupList.size() ;i++) {
129       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;   
130       aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);        
131         }
132   }
133   // Step 1. Prepare Partition structures.
134   Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
135   BOPAlgo_PaveFiller* aPaveFiller      = new BOPAlgo_PaveFiller(pA1);
136   BOPAlgo_Builder* aBuilder            = new BOPAlgo_Builder(pA2);
137   BOPCol_ListOfShape aLS;  
138   QStringList aListOfNames;
139   for (int i=0;i < anInputSplitDataList.size() ;i++) {
140         const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;     
141         aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
142     aLS.Append(aSh);    
143   }
144   aPaveFiller->SetArguments(aLS);
145   aPaveFiller->Perform();
146   Standard_Integer anErr = aPaveFiller->ErrorStatus();
147   if(anErr) 
148     return anOutputSplitDataList;
149   BOPDS_PDS pDS = aPaveFiller->PDS();
150   if (!pDS) 
151     return anOutputSplitDataList;
152   aBuilder->Clear();
153
154   // Step 2. Split faces 
155   BOPCol_ListIteratorOfListOfShape anIt(aPaveFiller->Arguments());
156   for (; anIt.More(); anIt.Next()) {
157     const TopoDS_Shape& aS = anIt.Value();
158     aBuilder->AddArgument(aS);
159   }
160   aBuilder->PerformWithFiller(*aPaveFiller);
161   anErr = aBuilder->ErrorStatus();
162   if(anErr) 
163     return anOutputSplitDataList;
164   const TopoDS_Shape& aResult = aBuilder->Shape();
165   if (aResult.IsNull()) 
166     return anOutputSplitDataList;
167   BRepCheck_Analyzer aCheck (aResult);
168   if(!aCheck.IsValid()) {
169 #ifdef DEB_SPLIT_TO_ZONES       
170     cout << "result is not valid" <<endl;        
171     BRepTools::Write(aResult, "SplitFacesNV.brep");  
172 #endif
173     return anOutputSplitDataList;
174   }
175 #ifdef DEB_SPLIT_TO_ZONES       
176   //BRepTools::Write(aResult, "SplitFacesV.brep");
177 #endif
178   
179   // Step 3. Collect history  
180   HYDROData_DataMapOfShapeListOfShape aDM1;
181   anIt.Init(aLS); 
182   //TCollection_AsciiString aNamM ("EdgM_");
183   //TCollection_AsciiString aNamG ("EdgG_");
184   for (int i =1;anIt.More();anIt.Next(),i++) {
185         const TopTools_ListOfShape& aListOfNew = aBuilder->Modified(anIt.Value());      
186     TopTools_ListOfShape aList;
187         TopTools_ListIteratorOfListOfShape it(aListOfNew);
188         for(;it.More();it.Next())       
189           aList.Append(it.Value());
190         // Bug in History: partition should give only modified entities! => temporary solution is used
191     const TopTools_ListOfShape& aListOfGen = aBuilder->Generated(anIt.Value());
192         it.Initialize(aListOfGen);    
193         for(;it.More();it.Next())     
194           aList.Append(it.Value());
195
196         aDM1.Bind(anIt.Value(), aList);
197         //TCollection_AsciiString aName;
198         if(!anInputGroupList.isEmpty()) { /* 1 */
199       aList.Clear();
200           TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
201           for (int j =1;exp.More();exp.Next(),j++) {
202             const TopTools_ListOfShape& aListM = aBuilder->Modified(exp.Current());     
203                 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
204                 it.Initialize(aListM);    
205                 for(int k=1;it.More();it.Next(),k++) {    
206               aList.Append(it.Value());
207                   //aName = aNamM + i + j +k +".brep";
208                   //BRepTools::Write(it.Value(),aName.ToCString());
209                 }
210                 const TopTools_ListOfShape& aListG = aBuilder->Generated(exp.Current());
211                 it.Initialize(aListG);    
212                 for(int k=1;it.More();it.Next(),k++)   {  
213               aList.Append(it.Value());
214                   //aName = aNamG + i + j +k +".brep";
215                  //BRepTools::Write(it.Value(),aName.ToCString());
216                 }
217                 //cout << "NB_EDGE = " << aList.Extent() <<endl;
218                 aDM1.Bind(exp.Current(), aList);
219           }      
220         }
221   }
222
223   // aDM2: NewShape ==> ListOfOldShapes
224   HYDROData_DataMapOfShapeListOfShape aDM2;
225   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
226   for(;aMIt.More();aMIt.Next()) {
227         const TopoDS_Shape& aKey = aMIt.Key();
228         TopTools_ListOfShape aList;
229         aList.Append(aKey);
230         const TopTools_ListOfShape& aListOfNew = aMIt.Value();
231         TopTools_ListIteratorOfListOfShape it(aListOfNew);
232         for(;it.More();it.Next()) {
233           if(!aDM2.IsBound(it.Value()))
234             aDM2.Bind(it.Value(), aList);
235           else {
236                 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
237                 aList.Prepend(aKey);
238           }
239         }
240   }
241   //cout << "DM2 Ext = " <<aDM2.Extent() <<endl;
242   // Step 4. Fill output structure.
243   aMIt.Initialize(aDM2);
244   for(int i =1;aMIt.More();aMIt.Next(),i++) {
245     SplitData aDestSplitData;
246         const TopoDS_Shape& aKey = aMIt.Key(); //new
247         aDestSplitData.Shape = aKey;
248         if(aKey.ShapeType() == TopAbs_FACE)
249           aDestSplitData.Type = SplitData::Data_Zone;
250         else
251       aDestSplitData.Type = SplitData::Data_Edge;
252
253         QStringList aListOfNames; // names processing
254     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
255     TopTools_ListIteratorOfListOfShape it(aListOfOld);
256         for(;it.More();it.Next()) {     
257           const TopoDS_Shape& aSh = it.Value(); //old
258           if(aDM3.IsBound(aSh)) {
259             const QStringList& ObjectNames = aDM3.Find(aSh);
260                 aListOfNames.append(ObjectNames);
261           }      
262         }
263 /*      
264         if(aKey.ShapeType() == TopAbs_EDGE) {
265           QString aStr("EDGE_Of_Face_");
266           aStr = aStr.append(TCollection_AsciiString(i).ToCString());
267           const QStringList& ObjectNames = QStringList( aStr);
268           cout << "EDGE ==> " << ObjectNames.size() <<" " << aStr.toStdString()<<endl;
269           aListOfNames.append(ObjectNames);
270         }*/
271         aDestSplitData.ObjectNames = aListOfNames;  
272         anOutputSplitDataList.append(aDestSplitData);
273   }
274   //cout << "anOutputSplitDataList = " <<anOutputSplitDataList.size() <<endl;
275   return anOutputSplitDataList;
276
277   /*
278   SplitDataListIterator anInputIter( anInputSplitDataList );
279   while( anInputIter.hasNext() )
280   {
281     const SplitData& anInputSplitData = anInputIter.next();
282     if( anOutputSplitDataList.isEmpty() )
283       anOutputSplitDataList.append( anInputSplitData );
284     else
285     {
286       SplitDataList aSplitDataList;
287
288       SplitDataList aSrcSplitDataList;
289       aSrcSplitDataList.append( anInputSplitData );
290
291       SplitDataList aDestSplitDataList = anOutputSplitDataList;
292       anOutputSplitDataList.clear();
293
294       while( !aDestSplitDataList.isEmpty() )
295       {
296         SplitData aSrcSplitData = aSrcSplitDataList.last();
297
298         SplitData aDestSplitData = aDestSplitDataList.first();
299         aDestSplitDataList.pop_front();
300
301         SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
302         if( SplitTwoData( aSrcSplitData, aDestSplitData,
303                           aData1Subtracted, aData2Subtracted, aDataIntersected ) )
304           anOutputSplitDataList.append( aDataIntersected );
305         anOutputSplitDataList.append( aData2Subtracted );
306         aSrcSplitDataList.append( aData1Subtracted );
307       }
308
309       if( !aSrcSplitDataList.isEmpty() )
310         anOutputSplitDataList.append( aSrcSplitDataList.last() );
311     }
312   }
313
314   // Step 2. Take into account the boundary polyline.
315   if( !thePolyline.IsNull() )
316   {
317     TopoDS_Wire aWire = TopoDS::Wire( thePolyline->GetShape() );
318     if( !aWire.IsNull() )
319     {
320       BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
321       aMakeFace.Build();
322       if( aMakeFace.IsDone() )
323       {
324         SplitData aBoundarySplitData( SplitData::Data_Zone, aMakeFace.Face(), "" );
325
326         SplitDataList aCutSplitDataList;
327         SplitDataListIterator anOutputIter( anOutputSplitDataList );
328         while( anOutputIter.hasNext() )
329         {
330           const SplitData& anOutputSplitData = anOutputIter.next();
331
332           SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
333           if( SplitTwoData( anOutputSplitData, aBoundarySplitData,
334                             aData1Subtracted, aData2Subtracted, aDataIntersected ) )
335             aCutSplitDataList.append( aDataIntersected );
336         }
337         anOutputSplitDataList = aCutSplitDataList;
338       }
339     }
340   }
341
342   // Step 3. Extract the separate regions.
343   SplitDataList anExtractedSplitDataList;
344   SplitDataListIterator anOutputIter( anOutputSplitDataList );
345   while( anOutputIter.hasNext() )
346   {
347     const SplitData& anOutputSplitData = anOutputIter.next();
348     anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
349   }
350 */
351  // return anExtractedSplitDataList;
352 }
353
354 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
355                                                const SplitData& theData2,
356                                                SplitData& theData1Subtracted,
357                                                SplitData& theData2Subtracted,
358                                                SplitData& theDataIntersected )
359 {
360   const TopoDS_Shape& aShape1 = theData1.Shape;
361   const TopoDS_Shape& aShape2 = theData2.Shape;
362
363   const QStringList& anObjectNames1 = theData1.ObjectNames;
364   const QStringList& anObjectNames2 = theData2.ObjectNames;
365
366   BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
367   TopoDS_Shape aCommonShape = aCommon.Shape();
368   if( aCommonShape.IsNull() )
369   {
370     theData1Subtracted = theData1;
371     theData2Subtracted = theData2;
372     return false;
373   }
374
375   BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
376   TopoDS_Shape aCut1Shape = aCut1.Shape();
377
378   BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
379   TopoDS_Shape aCut2Shape = aCut2.Shape();
380
381   theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
382   theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
383   theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
384
385   return true;
386 }
387
388 HYDROData_SplitToZonesTool::SplitDataList
389 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
390 {
391   SplitDataList aSplitDataList;
392   TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
393   for( ; anExp.More(); anExp.Next() )
394   {
395     TopoDS_Shape aShape = anExp.Current();
396     if( aShape.ShapeType() == TopAbs_FACE )
397     {
398       TopoDS_Face aFace = TopoDS::Face( aShape );
399       if( !aFace.IsNull() )
400       {
401         SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
402         aSplitDataList.append( aSplitData );
403       }
404     }
405   }
406   return aSplitDataList;
407 }