Salome HOME
04e212a7f2deb0e7ee07a0fd4c676192a6930317
[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   //TCollection_AsciiString aNam("Faces_");
68   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
69   {
70     Handle(HYDROData_Object) aGeomObj = 
71       Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
72     if( aGeomObj.IsNull() )
73       continue;
74
75     TopoDS_Shape aShape = aGeomObj->GetTopShape();
76     if ( aShape.IsNull() )
77       continue;  
78 //TCollection_AsciiString aName = aNam + anIndex + ".brep";
79 //BRepTools::Write(aShape, aName.ToCString());
80
81     if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
82       // Create split data for each face contained in the compound
83       TopExp_Explorer anExp( aShape, TopAbs_FACE );
84       for ( ; anExp.More(); anExp.Next() ) {
85         const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
86         if ( !aFace.IsNull() ) {
87           SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
88           anInputSplitDataList.append( aSplitData );
89         }
90       }
91     } else {
92       SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
93       anInputSplitDataList.append( aSplitData );
94     }
95   }
96   //
97   SplitDataList anInputGroupList;
98   for( int anIndex = 1; anIndex <= theGroupsList.Length(); anIndex++ )
99   {
100     Handle(HYDROData_ShapesGroup) aGeomGroup = 
101       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
102     if( aGeomGroup.IsNull() )
103       continue;
104       
105     TopTools_SequenceOfShape aGroupShapes;
106     aGeomGroup->GetShapes( aGroupShapes );
107     for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
108       const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
109       if ( aGroupShape.IsNull() )
110         continue;  
111
112       if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {    
113         TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
114         for ( ; anExp.More(); anExp.Next() ) {
115           const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
116           if ( !anEdge.IsNull() ) {
117             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
118             anInputGroupList.append( aSplitData );
119           }
120           }
121       } else {
122         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
123         anInputGroupList.append( aSplitData );
124       }
125     }
126   }
127   // If only one shape is given we don't split it 
128   // algorithm just returns the unpacked input data
129   if(theObjectList.Size() == 1)  {
130         anOutputSplitDataList.append(anInputSplitDataList); 
131         if(!theGroupsList.IsEmpty() ) 
132           anOutputSplitDataList.append(anInputGroupList);       
133     return anOutputSplitDataList;
134   }
135  
136   HYDROData_DataMapOfShapeListOfString aDM3;
137   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
138     QStringList aListOfNames;
139     for (int i=0;i < anInputGroupList.size() ;i++) {
140       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
141       aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
142     }
143   }
144   // Step 1. Prepare Partition structures.
145   Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
146   BOPAlgo_PaveFiller* aPaveFiller      = new BOPAlgo_PaveFiller(pA1);
147   BOPAlgo_Builder* aBuilder            = new BOPAlgo_Builder(pA2);
148   BOPCol_ListOfShape aLS;  
149   QStringList aListOfNames;
150   for (int i=0;i < anInputSplitDataList.size() ;i++) {
151     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
152     aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
153     aLS.Append(aSh);
154   }
155   aPaveFiller->SetArguments(aLS);
156   aPaveFiller->Perform();
157   Standard_Integer anErr = aPaveFiller->ErrorStatus();
158   if(anErr) 
159     return anOutputSplitDataList;
160   BOPDS_PDS pDS = aPaveFiller->PDS();
161   if (!pDS) 
162     return anOutputSplitDataList;
163   aBuilder->Clear();
164
165   // Step 2. Split faces 
166   BOPCol_ListIteratorOfListOfShape anIt(aPaveFiller->Arguments());
167   for (; anIt.More(); anIt.Next()) {
168     const TopoDS_Shape& aS = anIt.Value();
169     aBuilder->AddArgument(aS);
170   }
171   aBuilder->PerformWithFiller(*aPaveFiller);
172   anErr = aBuilder->ErrorStatus();
173   if(anErr) 
174     return anOutputSplitDataList;
175   const TopoDS_Shape& aResult = aBuilder->Shape();
176   if (aResult.IsNull()) 
177     return anOutputSplitDataList;
178   BRepCheck_Analyzer aCheck (aResult);
179   if(!aCheck.IsValid()) {
180 #ifdef DEB_SPLIT_TO_ZONES
181     cout << "result is not valid" <<endl;
182     BRepTools::Write(aResult, "SplitFacesNV.brep");  
183 #endif
184     return anOutputSplitDataList;
185   }
186 #ifdef DEB_SPLIT_TO_ZONES
187   //BRepTools::Write(aResult, "SplitFacesV.brep");
188 #endif
189   
190   // Step 3. Collect history  
191   HYDROData_DataMapOfShapeListOfShape aDM1;
192   anIt.Init(aLS); 
193   //TCollection_AsciiString aNamM ("EdgM_");
194   //TCollection_AsciiString aNamG ("EdgG_");
195   for (int i =1;anIt.More();anIt.Next(),i++) {
196         Standard_Boolean foundF(Standard_False);
197     const TopTools_ListOfShape& aListOfNew = aBuilder->Modified(anIt.Value());
198         if(!aListOfNew.IsEmpty())
199           foundF = Standard_True;
200     TopTools_ListOfShape aList;
201     TopTools_ListIteratorOfListOfShape it(aListOfNew);
202     for(;it.More();it.Next())       
203       aList.Append(it.Value());
204     // Bug in History: partition should give only modified entities! => temporary solution is used
205     const TopTools_ListOfShape& aListOfGen = aBuilder->Generated(anIt.Value());
206         if(!aListOfGen.IsEmpty())
207           foundF = Standard_True;
208         it.Initialize(aListOfGen);    
209         for(;it.More();it.Next())     
210           aList.Append(it.Value());
211     if(!foundF) // face is not modified
212           aList.Append (anIt.Value());
213         aDM1.Bind(anIt.Value(), aList);
214         //TCollection_AsciiString aName;
215         if(!anInputGroupList.isEmpty() ) { // 1
216       aList.Clear();
217           TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
218           for (int j =1;exp.More();exp.Next(),j++) {
219             Standard_Boolean foundE(Standard_False);
220             const TopTools_ListOfShape& aListM = aBuilder->Modified(exp.Current());     
221                 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
222                 if(aListM.Extent()) foundE = Standard_True;
223                 it.Initialize(aListM);    
224                 for(int k=1;it.More();it.Next(),k++) {    
225               aList.Append(it.Value());
226                   //aName = aNamM + i + j +k +".brep";
227                   //BRepTools::Write(it.Value(),aName.ToCString());
228                 }
229                 const TopTools_ListOfShape& aListG = aBuilder->Generated(exp.Current());
230                 if(aListG.Extent()) foundE = Standard_True;
231                 it.Initialize(aListG);    
232                 for(int k=1;it.More();it.Next(),k++)   {  
233               aList.Append(it.Value());
234                   //aName = aNamG + i + j +k +".brep";
235                   //BRepTools::Write(it.Value(),aName.ToCString());
236                 }
237                 //cout << "NB_EDGE = " << aList.Extent() <<endl;
238                 if(!foundE) //{
239                   aList.Append (exp.Current());
240                 //aName = aNamG + i + j +".brep";
241                 //BRepTools::Write(exp.Current(),aName.ToCString());
242                 //}
243                 aDM1.Bind(exp.Current(), aList);
244           }      
245         }
246   }
247
248   // aDM2: NewShape ==> ListOfOldShapes
249   HYDROData_DataMapOfShapeListOfShape aDM2;
250   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
251   for(;aMIt.More();aMIt.Next()) {
252     const TopoDS_Shape& aKey = aMIt.Key();
253     TopTools_ListOfShape aList;
254     aList.Append(aKey);
255     const TopTools_ListOfShape& aListOfNew = aMIt.Value();
256     TopTools_ListIteratorOfListOfShape it(aListOfNew);
257     for(;it.More();it.Next()) {
258       if(!aDM2.IsBound(it.Value()))
259         aDM2.Bind(it.Value(), aList);
260       else {
261         TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
262         aList.Prepend(aKey);
263       }
264     }
265   }
266   //cout << "DM2 Ext = " <<aDM2.Extent() <<endl;
267   // Step 4. Fill output structure.
268   aMIt.Initialize(aDM2);
269   for(int i =1;aMIt.More();aMIt.Next(),i++) {
270     SplitData aDestSplitData;
271     const TopoDS_Shape& aKey = aMIt.Key(); //new
272     aDestSplitData.Shape = aKey;
273     if(aKey.ShapeType() == TopAbs_FACE)
274       aDestSplitData.Type = SplitData::Data_Zone;
275     else
276       aDestSplitData.Type = SplitData::Data_Edge;
277
278     QStringList aListOfNames; // names processing
279     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
280     TopTools_ListIteratorOfListOfShape it(aListOfOld);
281         for(;it.More();it.Next()) {     
282           const TopoDS_Shape& aSh = it.Value(); //old
283           if(aDM3.IsBound(aSh)) {
284             const QStringList& ObjectNames = aDM3.Find(aSh);
285                 aListOfNames.append(ObjectNames);
286           }      
287         }
288
289         aDestSplitData.ObjectNames = aListOfNames;  
290         anOutputSplitDataList.append(aDestSplitData);
291   }
292   //cout << "anOutputSplitDataList = " <<anOutputSplitDataList.size() <<endl;
293   return anOutputSplitDataList;
294
295 /* 
296   // Step 3. Extract the separate regions.
297   SplitDataList anExtractedSplitDataList;
298   SplitDataListIterator anOutputIter( anOutputSplitDataList );
299   while( anOutputIter.hasNext() )
300   {
301     const SplitData& anOutputSplitData = anOutputIter.next();
302     anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
303   }
304 */
305  // return anExtractedSplitDataList;
306 }
307
308 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
309                                                const SplitData& theData2,
310                                                SplitData& theData1Subtracted,
311                                                SplitData& theData2Subtracted,
312                                                SplitData& theDataIntersected )
313 {
314   const TopoDS_Shape& aShape1 = theData1.Shape;
315   const TopoDS_Shape& aShape2 = theData2.Shape;
316
317   const QStringList& anObjectNames1 = theData1.ObjectNames;
318   const QStringList& anObjectNames2 = theData2.ObjectNames;
319
320   BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
321   TopoDS_Shape aCommonShape = aCommon.Shape();
322   if( aCommonShape.IsNull() )
323   {
324     theData1Subtracted = theData1;
325     theData2Subtracted = theData2;
326     return false;
327   }
328
329   BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
330   TopoDS_Shape aCut1Shape = aCut1.Shape();
331
332   BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
333   TopoDS_Shape aCut2Shape = aCut2.Shape();
334
335   theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
336   theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
337   theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
338
339   return true;
340 }
341
342 HYDROData_SplitToZonesTool::SplitDataList
343 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
344 {
345   SplitDataList aSplitDataList;
346   TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
347   for( ; anExp.More(); anExp.Next() )
348   {
349     TopoDS_Shape aShape = anExp.Current();
350     if( aShape.ShapeType() == TopAbs_FACE )
351     {
352       TopoDS_Face aFace = TopoDS::Face( aShape );
353       if( !aFace.IsNull() )
354       {
355         SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
356         aSplitDataList.append( aSplitData );
357       }
358     }
359   }
360   return aSplitDataList;
361 }