Salome HOME
Merge remote-tracking branch 'origin/master' into BR_LAND_COVER_MAP
[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
39 #include <QString>
40
41 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
42 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
43
44 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
45 {
46 };
47
48 /**
49   Constructor
50   @param theMap the land cover map to iterate through
51 */
52 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
53 {
54   Init( theMap );
55 }
56
57 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
58 {
59   if( theMap.IsNull() )
60   {
61     myIterator = 0;
62     myIndex = -1;
63   }
64   else
65     Init( *theMap );
66 }
67
68 /**
69   Initialize the iterator
70   @param theMap the land cover map to iterate through
71 */
72 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
73 {
74   TopoDS_Shape aShape = theMap.GetShape();
75   if( aShape.IsNull() )
76     myIterator = 0;
77   else
78     myIterator = new TopoDS_Iterator( aShape );
79   myIndex = 0;
80   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
81 }
82
83 /**
84   Destructor
85 */
86 HYDROData_LandCoverMap::Iterator::~Iterator()
87 {
88   delete myIterator;
89 }
90
91 /**
92   Return if the iterator has more elements
93   @return if the iterator has more elements
94 */
95 bool HYDROData_LandCoverMap::Iterator::More() const
96 {
97   return !myArray.IsNull() && myIterator && myIterator->More();
98 }
99
100 /**
101   Move iterator to the next element
102 */
103 void HYDROData_LandCoverMap::Iterator::Next()
104 {
105   if( myIterator )
106   {
107     myIterator->Next();
108     myIndex++;
109   }
110 }
111
112 /**
113   Get the current land cover (face)
114   @return the land cover's face
115 */
116 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
117 {
118   if( myIterator )
119     return TopoDS::Face( myIterator->Value() );
120   else
121     return TopoDS_Face();
122 }
123
124 /**
125   Get the current land cover's Strickler type 
126   @return the land cover's Strickler type 
127 */
128 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
129 {
130   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
131     return "";
132   else
133     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
134 }
135
136 /**
137   Constructor
138 */
139 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
140 {
141 }
142
143 /**
144   Destructor
145 */
146 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
147 {
148 }
149
150 /**
151   Get object's kind
152   @return object's kind
153 */
154 const ObjectKind HYDROData_LandCoverMap::GetKind() const
155 {
156   return KIND_LAND_COVER_MAP;
157 }
158
159 /**
160   Import the land cover map from QGIS
161   @param theFileName the name of file
162   @return if the import is successful
163 */
164 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
165 {
166   //TODO
167   return false;
168 }
169
170 /**
171   Export the land cover map to QGIS
172   @param theFileName the name of file
173   @return if the export is successful
174 */
175 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
176 {
177   //TODO
178   return false;
179 }
180
181 /**
182   Export the land cover map for the solver (Telemac)
183   @param theFileName the name of file
184   @return if the export is successful
185 */
186 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
187 {
188   //TODO
189   return false;
190 }
191
192 /**
193   Add a new object as land cover
194   @param theObject the object to add as land cover
195   @param theType the Strickler type for the new land cover
196   @return if the addition is successful
197 */
198 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
199 {
200   if( theObject.IsNull() )
201     return false;
202
203   TopoDS_Shape aShape = theObject->GetTopShape();
204   if( aShape.ShapeType()!=TopAbs_FACE )
205     return false;
206
207   TopoDS_Face aFace = TopoDS::Face( aShape );
208   return LocalPartition( aFace, theType );
209 }
210
211 /**
212   Add a new polyline as land cover
213   @param thePolyline the polyline to add as land cover
214   @param theType the Strickler type for the new land cover
215   @return if the addition is successful
216 */
217 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
218 {
219   if( thePolyline.IsNull() )
220     return false;
221
222   TopoDS_Shape aShape = thePolyline->GetShape();
223   if( aShape.ShapeType()!=TopAbs_WIRE )
224     return false;
225
226   TopoDS_Wire aWire = TopoDS::Wire( aShape );
227   if( !aWire.Closed() )
228     return false;
229
230   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
231   return LocalPartition( aFace, theType );
232 }
233
234 /**
235   Remove the given face from land cover map
236   @param theFace the face to be removed
237   @return if the removing is successful
238 */
239 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
240 {
241   TopTools_ListOfShape aList;
242   aList.Append( theFace );
243   return Remove( aList );
244 }
245
246 /**
247   Remove the given faces from land cover map
248   @param theFacesToRemove the face list to be removed
249   @return if the removing is successful
250 */
251 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
252 {
253   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
254   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
255   for( ; aFIt.More(); aFIt.Next() )
256   {
257     TopoDS_Shape aShape = aFIt.Value();
258     if( aShape.ShapeType()==TopAbs_FACE )
259       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
260   }
261
262   Iterator anIt( *this );
263   for( ; anIt.More(); anIt.Next() )
264     if( !aFacesToRemove.Contains( anIt.Face() ) )
265       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
266
267   StoreLandCovers( aNewFaces );
268   return true;
269 }
270
271 /**
272   Split the land cover map by the given polyline
273   @param thePolyline the tool polyline to split the land cover map
274   @return if the removing is successful
275 */
276 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
277 {
278   if( thePolyline.IsNull() )
279     return false;
280
281   TopoDS_Shape aShape = thePolyline->GetShape();
282   return LocalPartition( aShape, "" );
283 }
284
285 /**
286   Merge the given faces in the land cover
287   @param theFaces the faces to merge in the land cover map
288   @param theType the Strickler type for the merged land cover
289   @return if the merge is successful
290 */
291 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
292 {
293   // 1. to remove the merged faces from the current map
294   Remove( theFaces );
295
296   // 2. to fuse the faces into the new face
297   BOPAlgo_PaveFiller aPF;
298   aPF.SetArguments( theFaces );
299   aPF.SetFuzzyValue( 1E-2 ); 
300   aPF.SetRunParallel( Standard_False );
301   aPF.Perform();
302   int iErr = aPF.ErrorStatus();
303   if( iErr )
304     return false;
305
306   BOPAlgo_BOP aBOP;
307   aBOP.SetArguments( theFaces );
308   aBOP.SetOperation( BOPAlgo_FUSE );
309   aBOP.SetRunParallel( Standard_False );
310   aBOP.PerformWithFiller(aPF);
311   iErr = aBOP.ErrorStatus();
312   if( iErr )
313     return false;
314
315   TopoDS_Shape aMergedShape = aBOP.Shape();
316   if( aMergedShape.ShapeType()!=TopAbs_FACE )
317     return false;
318
319   // 3. to add the face into the map
320   return LocalPartition( TopoDS::Face( aMergedShape ), theType );
321 }
322
323 /**
324   Get the shape of the land cover map
325 */
326 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
327 {
328   return HYDROData_Entity::GetShape( DataTag_Shape );
329 }
330
331 /**
332   Set the shape of the land cover map
333   @param theShape the new shape for the land cover map
334 */
335 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
336 {
337   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
338 }
339
340 /**
341   Perform the local partition algorithm on the land cover
342   @param theNewShape the new shape to add into the land cover
343   @param theNewType the new Strickler type for the new land cover
344   @return if the local partition is successful
345 */
346 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
347 {
348   if( theNewShape.IsNull() )
349     return false;
350
351   BOPCol_ListOfShape aShapesList;
352   BOPAlgo_PaveFiller aPaveFiller;
353   HYDROData_MapOfFaceToStricklerType aNewFaces;
354
355   // add faces to shapes list
356   Iterator anIt( *this );
357   for( ; anIt.More(); anIt.Next() )
358     aShapesList.Append( anIt.Face() );
359   aShapesList.Append( theNewShape );
360
361   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
362   {
363     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
364     StoreLandCovers( aNewFaces );
365     return true;
366   }
367
368   // prepare pave filler
369   aPaveFiller.SetArguments( aShapesList );
370   aPaveFiller.Perform();
371   Standard_Integer anError = aPaveFiller.ErrorStatus();
372   if( anError )
373     return false;
374
375   // add faces to builder
376   BOPAlgo_Builder aBuilder;
377   anIt.Init( *this );
378   for( ; anIt.More(); anIt.Next() )
379     aBuilder.AddArgument( anIt.Face() );
380   aBuilder.AddArgument( theNewShape );
381
382   // perform the partition with the pave filler
383   aBuilder.PerformWithFiller( aPaveFiller );
384   anError = aBuilder.ErrorStatus();
385   if( anError )
386     return false;
387
388   // analysis of the history
389   //     a. to fill map of shapes which come from the new face
390   NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
391   //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
392   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
393   TopTools_ListIteratorOfListOfShape aMIt( aModified );
394   for( ; aMIt.More(); aMIt.Next() )
395   {
396     //std::cout << "   " << aMIt.Value() << std::endl;
397     aShapesFromNewFace.Add( aMIt.Value() );
398   }
399
400   //     b. to fill map of parts except parts from new face
401   anIt.Init( *this );
402   for( ; anIt.More(); anIt.Next() )
403   {
404     QString aSType = anIt.StricklerType();
405     //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
406     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
407     TopTools_ListIteratorOfListOfShape aMIt( aModified );
408     for( ; aMIt.More(); aMIt.Next() )
409     {
410       TopoDS_Shape aShape = aMIt.Value();
411       bool isFace = aShape.ShapeType()==TopAbs_FACE;
412       bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
413       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
414       if( isFace && !isAlsoFromNew )
415         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
416     }
417   }
418
419   //     c. add the new shape if it is face with its type
420   if( theNewShape.ShapeType()==TopAbs_FACE )
421     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
422   
423   // convert map of shape to type to compound and list of types
424   StoreLandCovers( aNewFaces );
425   return true;
426 }
427
428 /**
429   Replace the set of land covers in the land cover map
430   @param theMap the map of shape (face) to Strickler type (string)
431 */
432 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
433 {
434   TopoDS_Compound aCompound;
435   BRep_Builder aCompoundBuilder;
436   aCompoundBuilder.MakeCompound( aCompound );
437
438   int n = theMap.Size();
439   Handle( TDataStd_ExtStringArray ) aTypes = 
440     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
441   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
442   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
443   {
444     TopoDS_Face aFace = aNFIt.Key();
445     QString aType = aNFIt.Value();
446     aCompoundBuilder.Add( aCompound, aFace );
447     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
448   }
449
450   SetShape( aCompound );
451 }
452
453 /**
454   Find the land cover for the given point
455   @param thePoint the point laying in some land cover
456   @param theType the returned type
457   @return the found land cover's face
458 */
459 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
460 {
461   //TODO: some more optimal algorithm
462   Iterator anIt( *this );
463   for( ; anIt.More(); anIt.Next() )
464     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
465     {
466       theType = anIt.StricklerType();
467       return anIt.Face();
468     }
469
470   theType = "";
471   return TopoDS_Face();
472 }