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