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