Salome HOME
1af26fdb902f4fd4c63970e07c81268dd3400deb
[modules/hydro.git] / src / HYDROData / HYDROData_LandCoverMap.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_LandCoverMap.h>
20 #include <HYDROData_Object.h>
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Tool.h>
23
24 #include <BOPAlgo_BOP.hxx>
25 #include <BOPAlgo_Builder.hxx>
26 #include <BOPAlgo_PaveFiller.hxx>
27 #include <BOPCol_ListOfShape.hxx>
28 #include <BRep_Builder.hxx>
29 #include <BRepAlgoAPI_Fuse.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <NCollection_IndexedMap.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Iterator.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopTools_ListIteratorOfListOfShape.hxx>
38 #include <BOPAlgo_PaveFiller.hxx>
39 #include <BRepTools.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <ShapeUpgrade_UnifySameDomain.hxx>
42
43 #include <QString>
44
45 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
46 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
47
48 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
49 {
50 };
51
52 /**
53   Constructor
54   @param theMap the land cover map to iterate through
55 */
56 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
57 {
58   Init( theMap );
59 }
60
61 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
62 {
63   if( theMap.IsNull() )
64   {
65     myIterator = 0;
66     myIndex = -1;
67   }
68   else
69     Init( *theMap );
70 }
71
72 /**
73   Initialize the iterator
74   @param theMap the land cover map to iterate through
75 */
76 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
77 {
78   TopoDS_Shape aShape = theMap.GetShape();
79   if( aShape.IsNull() )
80     myIterator = 0;
81   else
82     myIterator = new TopoDS_Iterator( aShape );
83   myIndex = 0;
84   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
85 }
86
87 /**
88   Destructor
89 */
90 HYDROData_LandCoverMap::Iterator::~Iterator()
91 {
92   delete myIterator;
93 }
94
95 /**
96   Return if the iterator has more elements
97   @return if the iterator has more elements
98 */
99 bool HYDROData_LandCoverMap::Iterator::More() const
100 {
101   return !myArray.IsNull() && myIterator && myIterator->More();
102 }
103
104 /**
105   Move iterator to the next element
106 */
107 void HYDROData_LandCoverMap::Iterator::Next()
108 {
109   if( myIterator )
110   {
111     myIterator->Next();
112     myIndex++;
113   }
114 }
115
116 /**
117   Get the current land cover (face)
118   @return the land cover's face
119 */
120 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
121 {
122   if( myIterator )
123     return TopoDS::Face( myIterator->Value() );
124   else
125     return TopoDS_Face();
126 }
127
128 /**
129   Get the current land cover's Strickler type 
130   @return the land cover's Strickler type 
131 */
132 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
133 {
134   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
135     return "";
136   else
137     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
138 }
139
140 /**
141   Constructor
142 */
143 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
144 {
145 }
146
147 /**
148   Destructor
149 */
150 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
151 {
152 }
153
154 /**
155   Get object's kind
156   @return object's kind
157 */
158 const ObjectKind HYDROData_LandCoverMap::GetKind() const
159 {
160   return KIND_LAND_COVER_MAP;
161 }
162
163 /**
164   Import the land cover map from QGIS
165   @param theFileName the name of file
166   @return if the import is successful
167 */
168 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
169 {
170   //TODO
171   return false;
172 }
173
174 /**
175   Export the land cover map to QGIS
176   @param theFileName the name of file
177   @return if the export is successful
178 */
179 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
180 {
181   //TODO
182   return false;
183 }
184
185 /**
186   Export the land cover map for the solver (Telemac)
187   @param theFileName the name of file
188   @return if the export is successful
189 */
190 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
191 {
192   //TODO
193   return false;
194 }
195
196 /**
197   Add a new object as land cover
198   @param theObject the object to add as land cover
199   @param theType the Strickler type for the new land cover
200   @return if the addition is successful
201 */
202 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
203 {
204   if( theObject.IsNull() )
205     return false;
206
207   TopoDS_Shape aShape = theObject->GetTopShape();
208   if( aShape.ShapeType()!=TopAbs_FACE )
209     return false;
210
211   TopoDS_Face aFace = TopoDS::Face( aShape );
212   return LocalPartition( aFace, theType );
213 }
214
215 /**
216   Add a new polyline as land cover
217   @param thePolyline the polyline to add as land cover
218   @param theType the Strickler type for the new land cover
219   @return if the addition is successful
220 */
221 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
222 {
223   if( thePolyline.IsNull() )
224     return false;
225
226   TopoDS_Shape aShape = thePolyline->GetShape();
227   if( aShape.ShapeType()!=TopAbs_WIRE )
228     return false;
229
230   TopoDS_Wire aWire = TopoDS::Wire( aShape );
231   if( !aWire.Closed() )
232     return false;
233
234   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
235   return LocalPartition( aFace, theType );
236 }
237
238 /**
239   Remove the given face from land cover map
240   @param theFace the face to be removed
241   @return if the removing is successful
242 */
243 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
244 {
245   TopTools_ListOfShape aList;
246   aList.Append( theFace );
247   return Remove( aList );
248 }
249
250 /**
251   Remove the given faces from land cover map
252   @param theFacesToRemove the face list to be removed
253   @return if the removing is successful
254 */
255 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
256 {
257   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
258   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
259   for( ; aFIt.More(); aFIt.Next() )
260   {
261     TopoDS_Shape aShape = aFIt.Value();
262     if( aShape.ShapeType()==TopAbs_FACE )
263       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
264   }
265
266   Iterator anIt( *this );
267   for( ; anIt.More(); anIt.Next() )
268     if( !aFacesToRemove.Contains( anIt.Face() ) )
269       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
270
271   StoreLandCovers( aNewFaces );
272   return true;
273 }
274
275 /**
276   Split the land cover map by the given polyline
277   @param thePolyline the tool polyline to split the land cover map
278   @return if the removing is successful
279 */
280 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
281 {
282   if( thePolyline.IsNull() )
283     return false;
284
285   TopoDS_Shape aShape = thePolyline->GetShape();
286   return LocalPartition( aShape, "" );
287 }
288
289 /**
290   Merge the given faces in the land cover
291   @param theFaces the faces to merge in the land cover map
292   @param theType the Strickler type for the merged land cover
293   @return if the merge is successful
294 */
295 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
296 {
297   // 1. to remove the merged faces from the current map
298   Remove( theFaces );
299
300   // 2. to fuse the faces into the new face
301   /*BOPAlgo_PaveFiller aPF;
302   aPF.SetArguments( theFaces );
303   aPF.SetFuzzyValue( 1E-2 ); 
304   aPF.SetRunParallel( Standard_False );
305   aPF.Perform();
306   int iErr = aPF.ErrorStatus();
307   if( iErr )
308     return false;
309
310   BOPAlgo_BOP aBOP;
311   aBOP.SetArguments( theFaces );
312   aBOP.SetOperation( BOPAlgo_FUSE );
313   aBOP.SetRunParallel( Standard_False );
314   aBOP.PerformWithFiller(aPF);
315   iErr = aBOP.ErrorStatus();
316   if( iErr )
317     return false;
318
319   TopoDS_Shape aMergedShape = aBOP.Shape();
320   if( aMergedShape.ShapeType()!=TopAbs_FACE )
321     return false;*/
322
323  
324   ////
325   TopoDS_Shape MergedFace;
326   MergeFaces(theFaces, true, MergedFace);
327   //BRepTools::Write(aMergedShape, "c:/sh_.brep");
328
329   // 3. to add the face into the map
330   return LocalPartition( TopoDS_Face( /*aMergedShape*/ ), theType );
331 }
332
333 bool HYDROData_LandCoverMap::MergeFaces(const TopTools_ListOfShape& theFaces, bool IsToUnify, TopoDS_Shape& outSh)
334 {
335   int iErr;
336   TopTools_ListIteratorOfListOfShape aIt;
337   BOPCol_ListOfShape aLC;
338   aIt.Initialize(theFaces);
339   for (; aIt.More(); aIt.Next()) {
340     const TopoDS_Shape& aS=aIt.Value();
341     aLC.Append(aS);
342   }
343   BOPAlgo_PaveFiller aPF;
344   aPF.SetArguments(aLC);
345   aPF.SetRunParallel(Standard_False);
346   aPF.SetFuzzyValue(1e-02);
347
348   aPF.Perform();
349   iErr=aPF.ErrorStatus();
350   if (iErr)
351     return false;
352
353   BOPAlgo_Builder aBuilder;
354   aIt.Initialize(theFaces);
355   for (; aIt.More(); aIt.Next()) {
356     const TopoDS_Shape& aS=aIt.Value();
357     aBuilder.AddArgument(aS);
358   }
359
360   aBuilder.PerformWithFiller(aPF); 
361   iErr = aBuilder.ErrorStatus();
362   if (iErr)
363     return false;
364   const TopoDS_Shape& aMergedShape=aBuilder.Shape();
365
366   //
367   BRep_Builder BB;
368   TopoDS_Shell she; 
369   BB.MakeShell(she); 
370   she.Closed(Standard_False);
371   TopExp_Explorer ff(aMergedShape, TopAbs_FACE);
372   for(; ff.More(); ff.Next()) 
373   {
374     const TopoDS_Face& F = TopoDS::Face(ff.Current());
375     if (F.IsNull()) 
376       continue;
377     if (F.ShapeType() == TopAbs_FACE) {
378       BB.Add(she, F);
379       she.Closed (Standard_False);
380     }
381   }
382
383   if (IsToUnify)
384   {
385     ShapeUpgrade_UnifySameDomain USD;
386     USD.Initialize(she);
387     USD.Build();
388     outSh = USD.Shape();
389   }
390   else
391   {
392     outSh = she;
393   }
394
395   ff.Init(outSh, TopAbs_FACE);
396   int i = 0;
397   TopoDS_Face OneF;
398   for(; ff.More(); ff.Next(), i++) 
399     OneF = TopoDS::Face(ff.Current());
400   if (i == 1)
401     outSh = OneF;
402
403   return true;
404
405 }
406
407 /**
408   Get the shape of the land cover map
409 */
410 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
411 {
412   return HYDROData_Entity::GetShape( DataTag_Shape );
413 }
414
415 /**
416   Set the shape of the land cover map
417   @param theShape the new shape for the land cover map
418 */
419 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
420 {
421   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
422 }
423
424 /**
425   Perform the local partition algorithm on the land cover
426   @param theNewShape the new shape to add into the land cover
427   @param theNewType the new Strickler type for the new land cover
428   @return if the local partition is successful
429 */
430 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
431 {
432   if( theNewShape.IsNull() )
433     return false;
434
435   BOPCol_ListOfShape aShapesList;
436   BOPAlgo_PaveFiller aPaveFiller;
437   HYDROData_MapOfFaceToStricklerType aNewFaces;
438
439   // add faces to shapes list
440   Iterator anIt( *this );
441   for( ; anIt.More(); anIt.Next() )
442     aShapesList.Append( anIt.Face() );
443   aShapesList.Append( theNewShape );
444
445   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
446   {
447     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
448     StoreLandCovers( aNewFaces );
449     return true;
450   }
451
452   // prepare pave filler
453   aPaveFiller.SetArguments( aShapesList );
454   aPaveFiller.Perform();
455   Standard_Integer anError = aPaveFiller.ErrorStatus();
456   if( anError )
457     return false;
458
459   // add faces to builder
460   BOPAlgo_Builder aBuilder;
461   anIt.Init( *this );
462   for( ; anIt.More(); anIt.Next() )
463     aBuilder.AddArgument( anIt.Face() );
464   aBuilder.AddArgument( theNewShape );
465
466   // perform the partition with the pave filler
467   aBuilder.PerformWithFiller( aPaveFiller );
468   anError = aBuilder.ErrorStatus();
469   if( anError )
470     return false;
471
472   // analysis of the history
473   //     a. to fill map of shapes which come from the new face
474   NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
475   //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
476   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
477   TopTools_ListIteratorOfListOfShape aMIt( aModified );
478   for( ; aMIt.More(); aMIt.Next() )
479   {
480     //std::cout << "   " << aMIt.Value() << std::endl;
481     aShapesFromNewFace.Add( aMIt.Value() );
482   }
483
484   //     b. to fill map of parts except parts from new face
485   anIt.Init( *this );
486   for( ; anIt.More(); anIt.Next() )
487   {
488     QString aSType = anIt.StricklerType();
489     //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
490     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
491     TopTools_ListIteratorOfListOfShape aMIt( aModified );
492     for( ; aMIt.More(); aMIt.Next() )
493     {
494       TopoDS_Shape aShape = aMIt.Value();
495       bool isFace = aShape.ShapeType()==TopAbs_FACE;
496       bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
497       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
498       if( isFace && !isAlsoFromNew )
499         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
500     }
501   }
502
503   //     c. add the new shape if it is face with its type
504   if( theNewShape.ShapeType()==TopAbs_FACE )
505     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
506   
507   // convert map of shape to type to compound and list of types
508   StoreLandCovers( aNewFaces );
509   return true;
510 }
511
512 /**
513   Replace the set of land covers in the land cover map
514   @param theMap the map of shape (face) to Strickler type (string)
515 */
516 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
517 {
518   TopoDS_Compound aCompound;
519   BRep_Builder aCompoundBuilder;
520   aCompoundBuilder.MakeCompound( aCompound );
521
522   int n = theMap.Size();
523   Handle( TDataStd_ExtStringArray ) aTypes = 
524     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
525   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
526   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
527   {
528     TopoDS_Face aFace = aNFIt.Key();
529     QString aType = aNFIt.Value();
530     aCompoundBuilder.Add( aCompound, aFace );
531     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
532   }
533
534   SetShape( aCompound );
535 }
536
537 /**
538   Find the land cover for the given point
539   @param thePoint the point laying in some land cover
540   @param theType the returned type
541   @return the found land cover's face
542 */
543 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
544 {
545   //TODO: some more optimal algorithm
546   Iterator anIt( *this );
547   for( ; anIt.More(); anIt.Next() )
548     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
549     {
550       theType = anIt.StricklerType();
551       return anIt.Face();
552     }
553
554   theType = "";
555   return TopoDS_Face();
556 }