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