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()
146 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
152 @return object's kind
154 const ObjectKind HYDROData_LandCoverMap::GetKind() const
156 return KIND_LAND_COVER_MAP;
160 Import the land cover map from QGIS
161 @param theFileName the name of file
162 @return if the import is successful
164 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
171 Export the land cover map to QGIS
172 @param theFileName the name of file
173 @return if the export is successful
175 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
182 Export the land cover map for the solver (Telemac)
183 @param theFileName the name of file
184 @return if the export is successful
186 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
193 Add a new object as land cover
194 @param theObject the object to add as land cover
195 @param theType the Strickler type for the new land cover
196 @return if the addition is successful
198 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
200 if( theObject.IsNull() )
203 TopoDS_Shape aShape = theObject->GetTopShape();
204 if( aShape.ShapeType()!=TopAbs_FACE )
207 TopoDS_Face aFace = TopoDS::Face( aShape );
208 return LocalPartition( aFace, theType );
212 Add a new polyline as land cover
213 @param thePolyline the polyline to add as land cover
214 @param theType the Strickler type for the new land cover
215 @return if the addition is successful
217 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
219 if( thePolyline.IsNull() )
222 TopoDS_Shape aShape = thePolyline->GetShape();
223 if( aShape.ShapeType()!=TopAbs_WIRE )
226 TopoDS_Wire aWire = TopoDS::Wire( aShape );
227 if( !aWire.Closed() )
230 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
231 return LocalPartition( aFace, theType );
235 Remove the given face from land cover map
236 @param theFace the face to be removed
237 @return if the removing is successful
239 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
241 TopTools_ListOfShape aList;
242 aList.Append( theFace );
243 return Remove( aList );
247 Remove the given faces from land cover map
248 @param theFacesToRemove the face list to be removed
249 @return if the removing is successful
251 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
253 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
254 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
255 for( ; aFIt.More(); aFIt.Next() )
257 TopoDS_Shape aShape = aFIt.Value();
258 if( aShape.ShapeType()==TopAbs_FACE )
259 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
262 Iterator anIt( *this );
263 for( ; anIt.More(); anIt.Next() )
264 if( !aFacesToRemove.Contains( anIt.Face() ) )
265 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
267 StoreLandCovers( aNewFaces );
272 Split the land cover map by the given polyline
273 @param thePolyline the tool polyline to split the land cover map
274 @return if the removing is successful
276 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
278 if( thePolyline.IsNull() )
281 TopoDS_Shape aShape = thePolyline->GetShape();
282 return LocalPartition( aShape, "" );
286 Merge the given faces in the land cover
287 @param theFaces the faces to merge in the land cover map
288 @param theType the Strickler type for the merged land cover
289 @return if the merge is successful
291 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
293 // 1. to remove the merged faces from the current map
296 // 2. to fuse the faces into the new face
297 BOPAlgo_PaveFiller aPF;
298 aPF.SetArguments( theFaces );
299 aPF.SetFuzzyValue( 1E-2 );
300 aPF.SetRunParallel( Standard_False );
302 int iErr = aPF.ErrorStatus();
307 aBOP.SetArguments( theFaces );
308 aBOP.SetOperation( BOPAlgo_FUSE );
309 aBOP.SetRunParallel( Standard_False );
310 aBOP.PerformWithFiller(aPF);
311 iErr = aBOP.ErrorStatus();
315 TopoDS_Shape aMergedShape = aBOP.Shape();
316 if( aMergedShape.ShapeType()!=TopAbs_FACE )
319 // 3. to add the face into the map
320 return LocalPartition( TopoDS::Face( aMergedShape ), theType );
324 Get the shape of the land cover map
326 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
328 return HYDROData_Entity::GetShape( DataTag_Shape );
332 Set the shape of the land cover map
333 @param theShape the new shape for the land cover map
335 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
337 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
341 Perform the local partition algorithm on the land cover
342 @param theNewShape the new shape to add into the land cover
343 @param theNewType the new Strickler type for the new land cover
344 @return if the local partition is successful
346 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
348 if( theNewShape.IsNull() )
351 BOPCol_ListOfShape aShapesList;
352 BOPAlgo_PaveFiller aPaveFiller;
353 HYDROData_MapOfFaceToStricklerType aNewFaces;
355 // add faces to shapes list
356 Iterator anIt( *this );
357 for( ; anIt.More(); anIt.Next() )
358 aShapesList.Append( anIt.Face() );
359 aShapesList.Append( theNewShape );
361 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
363 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
364 StoreLandCovers( aNewFaces );
368 // prepare pave filler
369 aPaveFiller.SetArguments( aShapesList );
370 aPaveFiller.Perform();
371 Standard_Integer anError = aPaveFiller.ErrorStatus();
375 // add faces to builder
376 BOPAlgo_Builder aBuilder;
378 for( ; anIt.More(); anIt.Next() )
379 aBuilder.AddArgument( anIt.Face() );
380 aBuilder.AddArgument( theNewShape );
382 // perform the partition with the pave filler
383 aBuilder.PerformWithFiller( aPaveFiller );
384 anError = aBuilder.ErrorStatus();
388 // analysis of the history
389 // a. to fill map of shapes which come from the new face
390 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
391 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
392 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
393 TopTools_ListIteratorOfListOfShape aMIt( aModified );
394 for( ; aMIt.More(); aMIt.Next() )
396 //std::cout << " " << aMIt.Value() << std::endl;
397 aShapesFromNewFace.Add( aMIt.Value() );
400 // b. to fill map of parts except parts from new face
402 for( ; anIt.More(); anIt.Next() )
404 QString aSType = anIt.StricklerType();
405 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
406 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
407 TopTools_ListIteratorOfListOfShape aMIt( aModified );
408 for( ; aMIt.More(); aMIt.Next() )
410 TopoDS_Shape aShape = aMIt.Value();
411 bool isFace = aShape.ShapeType()==TopAbs_FACE;
412 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
413 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
414 if( isFace && !isAlsoFromNew )
415 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
419 // c. add the new shape if it is face with its type
420 if( theNewShape.ShapeType()==TopAbs_FACE )
421 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
423 // convert map of shape to type to compound and list of types
424 StoreLandCovers( aNewFaces );
429 Replace the set of land covers in the land cover map
430 @param theMap the map of shape (face) to Strickler type (string)
432 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
434 TopoDS_Compound aCompound;
435 BRep_Builder aCompoundBuilder;
436 aCompoundBuilder.MakeCompound( aCompound );
438 int n = theMap.Size();
439 Handle( TDataStd_ExtStringArray ) aTypes =
440 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
441 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
442 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
444 TopoDS_Face aFace = aNFIt.Key();
445 QString aType = aNFIt.Value();
446 aCompoundBuilder.Add( aCompound, aFace );
447 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
450 SetShape( aCompound );
454 Find the land cover for the given point
455 @param thePoint the point laying in some land cover
456 @param theType the returned type
457 @return the found land cover's face
459 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
461 //TODO: some more optimal algorithm
462 Iterator anIt( *this );
463 for( ; anIt.More(); anIt.Next() )
464 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
466 theType = anIt.StricklerType();
471 return TopoDS_Face();