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