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_BOP.hxx>
25 #include <BOPAlgo_Builder.hxx>
26 #include <BOPAlgo_PaveFiller.hxx>
27 #include <BOPCol_ListOfShape.hxx>
28 #include <BRep_Builder.hxx>
29 #include <BRepAlgoAPI_Fuse.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <NCollection_IndexedMap.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Iterator.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
42 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
44 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
50 @param theMap the land cover map to iterate through
52 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
57 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
69 Initialize the iterator
70 @param theMap the land cover map to iterate through
72 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
74 TopoDS_Shape aShape = theMap.GetShape();
78 myIterator = new TopoDS_Iterator( aShape );
80 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
86 HYDROData_LandCoverMap::Iterator::~Iterator()
92 Return if the iterator has more elements
93 @return if the iterator has more elements
95 bool HYDROData_LandCoverMap::Iterator::More() const
97 return !myArray.IsNull() && myIterator && myIterator->More();
101 Move iterator to the next element
103 void HYDROData_LandCoverMap::Iterator::Next()
113 Get the current land cover (face)
114 @return the land cover's face
116 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
119 return TopoDS::Face( myIterator->Value() );
121 return TopoDS_Face();
125 Get the current land cover's Strickler type
126 @return the land cover's Strickler type
128 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
130 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
133 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
139 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
140 : HYDROData_Entity( Geom_No )
147 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
153 @return object's kind
155 const ObjectKind HYDROData_LandCoverMap::GetKind() const
157 return KIND_LAND_COVER_MAP;
161 Import the land cover map from QGIS
162 @param theFileName the name of file
163 @return if the import is successful
165 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
172 Export the land cover map to QGIS
173 @param theFileName the name of file
174 @return if the export is successful
176 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
183 Export the land cover map for the solver (Telemac)
184 @param theFileName the name of file
185 @return if the export is successful
187 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
194 Add a new object as land cover
195 @param theObject the object to add as land cover
196 @param theType the Strickler type for the new land cover
197 @return if the addition is successful
199 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
201 if( theObject.IsNull() )
204 TopoDS_Shape aShape = theObject->GetTopShape();
205 if( aShape.ShapeType()!=TopAbs_FACE )
208 TopoDS_Face aFace = TopoDS::Face( aShape );
209 return LocalPartition( aFace, theType );
213 Add a new polyline as land cover
214 @param thePolyline the polyline to add as land cover
215 @param theType the Strickler type for the new land cover
216 @return if the addition is successful
218 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
220 if( thePolyline.IsNull() )
223 TopoDS_Shape aShape = thePolyline->GetShape();
224 if( aShape.ShapeType()!=TopAbs_WIRE )
227 TopoDS_Wire aWire = TopoDS::Wire( aShape );
228 if( !aWire.Closed() )
231 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
232 return LocalPartition( aFace, theType );
236 Remove the given face from land cover map
237 @param theFace the face to be removed
238 @return if the removing is successful
240 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
242 TopTools_ListOfShape aList;
243 aList.Append( theFace );
244 return Remove( aList );
248 Remove the given faces from land cover map
249 @param theFacesToRemove the face list to be removed
250 @return if the removing is successful
252 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
254 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
255 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
256 for( ; aFIt.More(); aFIt.Next() )
258 TopoDS_Shape aShape = aFIt.Value();
259 if( aShape.ShapeType()==TopAbs_FACE )
260 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
263 Iterator anIt( *this );
264 for( ; anIt.More(); anIt.Next() )
265 if( !aFacesToRemove.Contains( anIt.Face() ) )
266 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
268 StoreLandCovers( aNewFaces );
273 Split the land cover map by the given polyline
274 @param thePolyline the tool polyline to split the land cover map
275 @return if the removing is successful
277 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
279 if( thePolyline.IsNull() )
282 TopoDS_Shape aShape = thePolyline->GetShape();
283 return LocalPartition( aShape, "" );
287 Merge the given faces in the land cover
288 @param theFaces the faces to merge in the land cover map
289 @param theType the Strickler type for the merged land cover
290 @return if the merge is successful
292 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
294 // 1. to remove the merged faces from the current map
297 // 2. to fuse the faces into the new face
298 BOPAlgo_PaveFiller aPF;
299 aPF.SetArguments( theFaces );
300 aPF.SetFuzzyValue( 1E-2 );
301 aPF.SetRunParallel( Standard_False );
303 int iErr = aPF.ErrorStatus();
308 aBOP.SetArguments( theFaces );
309 aBOP.SetOperation( BOPAlgo_FUSE );
310 aBOP.SetRunParallel( Standard_False );
311 aBOP.PerformWithFiller(aPF);
312 iErr = aBOP.ErrorStatus();
316 TopoDS_Shape aMergedShape = aBOP.Shape();
317 if( aMergedShape.ShapeType()!=TopAbs_FACE )
320 // 3. to add the face into the map
321 return LocalPartition( TopoDS::Face( aMergedShape ), theType );
325 Get the shape of the land cover map
327 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
329 return HYDROData_Entity::GetShape( DataTag_Shape );
333 Set the shape of the land cover map
334 @param theShape the new shape for the land cover map
336 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
338 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
342 Perform the local partition algorithm on the land cover
343 @param theNewShape the new shape to add into the land cover
344 @param theNewType the new Strickler type for the new land cover
345 @return if the local partition is successful
347 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
349 if( theNewShape.IsNull() )
352 BOPCol_ListOfShape aShapesList;
353 BOPAlgo_PaveFiller aPaveFiller;
354 HYDROData_MapOfFaceToStricklerType aNewFaces;
356 // add faces to shapes list
357 Iterator anIt( *this );
358 for( ; anIt.More(); anIt.Next() )
359 aShapesList.Append( anIt.Face() );
360 aShapesList.Append( theNewShape );
362 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
364 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
365 StoreLandCovers( aNewFaces );
369 // prepare pave filler
370 aPaveFiller.SetArguments( aShapesList );
371 aPaveFiller.Perform();
372 Standard_Integer anError = aPaveFiller.ErrorStatus();
376 // add faces to builder
377 BOPAlgo_Builder aBuilder;
379 for( ; anIt.More(); anIt.Next() )
380 aBuilder.AddArgument( anIt.Face() );
381 aBuilder.AddArgument( theNewShape );
383 // perform the partition with the pave filler
384 aBuilder.PerformWithFiller( aPaveFiller );
385 anError = aBuilder.ErrorStatus();
389 // analysis of the history
390 // a. to fill map of shapes which come from the new face
391 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
392 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
393 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
394 TopTools_ListIteratorOfListOfShape aMIt( aModified );
395 for( ; aMIt.More(); aMIt.Next() )
397 //std::cout << " " << aMIt.Value() << std::endl;
398 aShapesFromNewFace.Add( aMIt.Value() );
401 // b. to fill map of parts except parts from new face
403 for( ; anIt.More(); anIt.Next() )
405 QString aSType = anIt.StricklerType();
406 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
407 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
408 TopTools_ListIteratorOfListOfShape aMIt( aModified );
409 for( ; aMIt.More(); aMIt.Next() )
411 TopoDS_Shape aShape = aMIt.Value();
412 bool isFace = aShape.ShapeType()==TopAbs_FACE;
413 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
414 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
415 if( isFace && !isAlsoFromNew )
416 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
420 // c. add the new shape if it is face with its type
421 if( theNewShape.ShapeType()==TopAbs_FACE )
422 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
424 // convert map of shape to type to compound and list of types
425 StoreLandCovers( aNewFaces );
430 Replace the set of land covers in the land cover map
431 @param theMap the map of shape (face) to Strickler type (string)
433 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
435 TopoDS_Compound aCompound;
436 BRep_Builder aCompoundBuilder;
437 aCompoundBuilder.MakeCompound( aCompound );
439 int n = theMap.Size();
440 Handle( TDataStd_ExtStringArray ) aTypes =
441 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
442 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
443 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
445 TopoDS_Face aFace = aNFIt.Key();
446 QString aType = aNFIt.Value();
447 aCompoundBuilder.Add( aCompound, aFace );
448 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
451 SetShape( aCompound );
455 Find the land cover for the given point
456 @param thePoint the point laying in some land cover
457 @param theType the returned type
458 @return the found land cover's face
460 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
462 //TODO: some more optimal algorithm
463 Iterator anIt( *this );
464 for( ; anIt.More(); anIt.Next() )
465 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
467 theType = anIt.StricklerType();
472 return TopoDS_Face();