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