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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_LandCoverMap.h>
20 #include <HYDROData_Object.h>
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Tool.h>
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>
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>
40 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
41 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
43 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
49 @param theMap the land cover map to iterate through
51 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
57 Initialize the iterator
58 @param theMap the land cover map to iterate through
60 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
62 TopoDS_Shape aShape = theMap.GetShape();
66 myIterator = new TopoDS_Iterator( aShape );
68 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
74 HYDROData_LandCoverMap::Iterator::~Iterator()
80 Return if the iterator has more elements
81 @return if the iterator has more elements
83 bool HYDROData_LandCoverMap::Iterator::More() const
85 return !myArray.IsNull() && myIterator && myIterator->More();
89 Move iterator to the next element
91 void HYDROData_LandCoverMap::Iterator::Next()
98 Get the current land cover (face)
99 @return the land cover's face
101 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
104 return TopoDS::Face( myIterator->Value() );
106 return TopoDS_Face();
110 Get the current land cover's Strickler type
111 @return the land cover's Strickler type
113 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
115 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
118 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
124 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
131 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
137 @return object's kind
139 const ObjectKind HYDROData_LandCoverMap::GetKind() const
141 return KIND_LAND_COVER_MAP;
145 Import the land cover map from QGIS
146 @param theFileName the name of file
147 @return if the import is successful
149 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
156 Export the land cover map to QGIS
157 @param theFileName the name of file
158 @return if the export is successful
160 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
167 Export the land cover map for the solver (Telemac)
168 @param theFileName the name of file
169 @return if the export is successful
171 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
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
183 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
185 if( theObject.IsNull() )
188 TopoDS_Shape aShape = theObject->GetTopShape();
189 if( aShape.ShapeType()!=TopAbs_FACE )
192 TopoDS_Face aFace = TopoDS::Face( aShape );
193 return LocalPartition( aFace, theType );
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
202 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
209 Remove the given face from land cover map
210 @param theFace the face to be removed
211 @return if the removing is successful
213 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
215 TopTools_ListOfShape aList;
216 aList.Append( theFace );
217 return Remove( aList );
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
225 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesList )
227 TopoDS_Compound aCompound = TopoDS::Compound( GetShape() );
228 BRep_Builder aCompoundBuilder;
229 //TODO: aCompoundBuilder.Remove( aCompound, theFace );
231 SetShape( aCompound );
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
240 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
242 if( thePolyline.IsNull() )
245 TopoDS_Shape aShape = thePolyline->GetShape();
246 return LocalPartition( aShape, "" );
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
255 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
257 // 1. to remove the merged faces from the current map
260 // 2. to fuse the faces into the new face
262 TopoDS_Face aMergedFace;
264 // 3. to add the face into the map
265 return LocalPartition( aMergedFace, theType );
269 Get the shape of the land cover map
271 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
273 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
274 if ( !aLabel.IsNull() )
276 Handle(TNaming_NamedShape) aNamedShape;
277 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
278 return aNamedShape->Get();
280 return TopoDS_Shape();
284 Set the shape of the land cover map
285 @param theShape the new shape for the land cover map
287 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
289 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
290 aBuilder.Generated( theShape );
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
299 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
301 if( theNewShape.IsNull() )
304 BOPCol_ListOfShape aShapesList;
305 BOPAlgo_PaveFiller aPaveFiller;
306 HYDROData_MapOfFaceToStricklerType aNewFaces;
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 );
314 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
316 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
317 StoreLandCovers( aNewFaces );
321 // prepare pave filler
322 aPaveFiller.SetArguments( aShapesList );
323 aPaveFiller.Perform();
324 Standard_Integer anError = aPaveFiller.ErrorStatus();
328 // add faces to builder
329 BOPAlgo_Builder aBuilder;
331 for( ; anIt.More(); anIt.Next() )
332 aBuilder.AddArgument( anIt.Face() );
333 aBuilder.AddArgument( theNewShape );
335 // perform the partition with the pave filler
336 aBuilder.PerformWithFiller( aPaveFiller );
337 anError = aBuilder.ErrorStatus();
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() )
349 //std::cout << " " << aMIt.Value() << std::endl;
350 aShapesFromNewFace.Add( aMIt.Value() );
353 // b. to fill map of parts except parts from new face
355 for( ; anIt.More(); anIt.Next() )
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() )
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 );
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 );
376 // convert map of shape to type to compound and list of types
377 StoreLandCovers( aNewFaces );
382 Replace the set of land covers in the land cover map
383 @param theMap the map of shape (face) to Strickler type (string)
385 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
387 TopoDS_Compound aCompound;
388 BRep_Builder aCompoundBuilder;
389 aCompoundBuilder.MakeCompound( aCompound );
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++ )
397 aCompoundBuilder.Add( aCompound, aNFIt.Key() );
398 aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) );
401 SetShape( aCompound );