Salome HOME
f384999d11989ae221cc289c50543da1c91bb8d3
[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 <NCollection_IndexedMap.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TNaming_NamedShape.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   //TODO
219   return false;
220 }
221
222 /**
223   Remove the given face from land cover map
224   @param theFace the face to be removed
225   @return if the removing is successful
226 */
227 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
228 {
229   TopTools_ListOfShape aList;
230   aList.Append( theFace );
231   return Remove( aList );
232 }
233
234 /**
235   Remove the given faces from land cover map
236   @param theFacesList the face list to be removed
237   @return if the removing is successful
238 */
239 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesList )
240 {
241   TopoDS_Compound aCompound = TopoDS::Compound( GetShape() );
242   BRep_Builder aCompoundBuilder;
243   //TODO: aCompoundBuilder.Remove( aCompound, theFace );
244   //TODO: update types
245   SetShape( aCompound );
246   return true;
247 }
248
249 /**
250   Split the land cover map by the given polyline
251   @param thePolyline the tool polyline to split the land cover map
252   @return if the removing is successful
253 */
254 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
255 {
256   if( thePolyline.IsNull() )
257     return false;
258
259   TopoDS_Shape aShape = thePolyline->GetShape();
260   return LocalPartition( aShape, "" );
261 }
262
263 /**
264   Merge the given faces in the land cover
265   @param theFaces the faces to merge in the land cover map
266   @param theType the Strickler type for the merged land cover
267   @return if the merge is successful
268 */
269 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
270 {
271   // 1. to remove the merged faces from the current map
272   Remove( theFaces );
273
274   // 2. to fuse the faces into the new face
275   // TODO
276   TopoDS_Face aMergedFace;
277
278   // 3. to add the face into the map
279   return LocalPartition( aMergedFace, theType );
280 }
281
282 /**
283   Get the shape of the land cover map
284 */
285 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
286 {
287   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
288   if ( !aLabel.IsNull() )
289   {
290     Handle(TNaming_NamedShape) aNamedShape;
291     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
292       return aNamedShape->Get();
293   }
294   return TopoDS_Shape();
295 }
296
297 /**
298   Set the shape of the land cover map
299   @param theShape the new shape for the land cover map
300 */
301 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
302 {
303   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
304   aBuilder.Generated( theShape );
305 }
306
307 /**
308   Perform the local partition algorithm on the land cover
309   @param theNewShape the new shape to add into the land cover
310   @param theNewType the new Strickler type for the new land cover
311   @return if the local partition is successful
312 */
313 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
314 {
315   if( theNewShape.IsNull() )
316     return false;
317
318   BOPCol_ListOfShape aShapesList;
319   BOPAlgo_PaveFiller aPaveFiller;
320   HYDROData_MapOfFaceToStricklerType aNewFaces;
321
322   // add faces to shapes list
323   Iterator anIt( *this );
324   for( ; anIt.More(); anIt.Next() )
325     aShapesList.Append( anIt.Face() );
326   aShapesList.Append( theNewShape );
327
328   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
329   {
330     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
331     StoreLandCovers( aNewFaces );
332     return true;
333   }
334
335   // prepare pave filler
336   aPaveFiller.SetArguments( aShapesList );
337   aPaveFiller.Perform();
338   Standard_Integer anError = aPaveFiller.ErrorStatus();
339   if( anError )
340     return false;
341
342   // add faces to builder
343   BOPAlgo_Builder aBuilder;
344   anIt.Init( *this );
345   for( ; anIt.More(); anIt.Next() )
346     aBuilder.AddArgument( anIt.Face() );
347   aBuilder.AddArgument( theNewShape );
348
349   // perform the partition with the pave filler
350   aBuilder.PerformWithFiller( aPaveFiller );
351   anError = aBuilder.ErrorStatus();
352   if( anError )
353     return false;
354
355   // analysis of the history
356   //     a. to fill map of shapes which come from the new face
357   NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
358   //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
359   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
360   TopTools_ListIteratorOfListOfShape aMIt( aModified );
361   for( ; aMIt.More(); aMIt.Next() )
362   {
363     //std::cout << "   " << aMIt.Value() << std::endl;
364     aShapesFromNewFace.Add( aMIt.Value() );
365   }
366
367   //     b. to fill map of parts except parts from new face
368   anIt.Init( *this );
369   for( ; anIt.More(); anIt.Next() )
370   {
371     QString aSType = anIt.StricklerType();
372     //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
373     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
374     TopTools_ListIteratorOfListOfShape aMIt( aModified );
375     for( ; aMIt.More(); aMIt.Next() )
376     {
377       TopoDS_Shape aShape = aMIt.Value();
378       bool isFace = aShape.ShapeType()==TopAbs_FACE;
379       bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
380       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
381       if( isFace && !isAlsoFromNew )
382         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
383     }
384   }
385
386   //     c. add the new shape if it is face with its type
387   if( theNewShape.ShapeType()==TopAbs_FACE )
388     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
389   
390   // convert map of shape to type to compound and list of types
391   StoreLandCovers( aNewFaces );
392   return true;
393 }
394
395 /**
396   Replace the set of land covers in the land cover map
397   @param theMap the map of shape (face) to Strickler type (string)
398 */
399 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
400 {
401   TopoDS_Compound aCompound;
402   BRep_Builder aCompoundBuilder;
403   aCompoundBuilder.MakeCompound( aCompound );
404
405   int n = theMap.Size();
406   Handle( TDataStd_ExtStringArray ) aTypes = 
407     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
408   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
409   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
410   {
411     TopoDS_Face aFace = aNFIt.Key();
412     QString aType = aNFIt.Value();
413     aCompoundBuilder.Add( aCompound, aFace );
414     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
415   }
416
417   SetShape( aCompound );
418 }