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 <BRepAdaptor_Curve.hxx>
30 #include <BRepAlgoAPI_Fuse.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <GCPnts_QuasiUniformDeflection.hxx>
33 #include <NCollection_IndexedMap.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <BOPAlgo_PaveFiller.hxx>
43 #include <BRepTools.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <ShapeUpgrade_UnifySameDomain.hxx>
49 #include <QTextStream>
51 const char TELEMAC_FORMAT = 'f';
52 const int TELEMAC_PRECISION = 3;
55 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
56 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
58 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
64 @param theMap the land cover map to iterate through
66 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
71 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
83 Initialize the iterator
84 @param theMap the land cover map to iterate through
86 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
88 TopoDS_Shape aShape = theMap.GetShape();
92 myIterator = new TopoDS_Iterator( aShape );
94 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
95 if( myArray.IsNull() )
98 myIndex = myArray->Lower();
104 HYDROData_LandCoverMap::Iterator::~Iterator()
110 Return the current 0-based index of the iterator
111 @return the current index
113 int HYDROData_LandCoverMap::Iterator::Index() const
115 if( myArray.IsNull() )
118 return myIndex - myArray->Lower();
122 Return if the iterator has more elements
123 @return if the iterator has more elements
125 bool HYDROData_LandCoverMap::Iterator::More() const
127 return !myArray.IsNull() && myIterator && myIterator->More();
131 Move iterator to the next element
133 void HYDROData_LandCoverMap::Iterator::Next()
143 Get the current land cover (face)
144 @return the land cover's face
146 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
149 return TopoDS::Face( myIterator->Value() );
151 return TopoDS_Face();
155 Get the current land cover's Strickler type
156 @return the land cover's Strickler type
158 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
160 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
163 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
167 Set the Strickler type for the current land cover
168 @param theType the Strickler type
170 void HYDROData_LandCoverMap::Iterator::SetStricklerType( const QString& theType )
172 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
175 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
181 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
182 : HYDROData_Entity( Geom_No )
189 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
195 @return object's kind
197 const ObjectKind HYDROData_LandCoverMap::GetKind() const
199 return KIND_LAND_COVER_MAP;
203 Import the land cover map from QGIS
204 @param theFileName the name of file
205 @return if the import is successful
207 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
214 Export the land cover map to QGIS
215 @param theFileName the name of file
216 @return if the export is successful
218 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
224 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
226 int aHashX = HashCode( thePoint.X(), theUpper );
227 int aHashY = HashCode( thePoint.Y(), theUpper );
228 return (aHashX^aHashY)%theUpper;
231 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
233 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
236 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
237 Standard_Real theDeflection,
238 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
239 QList<int>& theVerticesIds )
241 BRepAdaptor_Curve aCurve( theEdge );
242 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
243 if( !aDiscrete.IsDone() )
246 int n = aDiscrete.NbPoints();
247 for( int i=1; i<=n; i++ )
249 gp_Pnt aPnt = aDiscrete.Value( i );
251 if( theVerticesMap.Contains( aPnt ) )
252 anId = theVerticesMap.FindIndex( aPnt );
255 anId = theVerticesMap.Size();
256 theVerticesMap.Add( aPnt );
258 theVerticesIds.append( anId );
264 Export the land cover map for the solver (Telemac)
265 @param theFileName the name of file
266 @return if the export is successful
268 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
270 TopoDS_Shape aLandCoverMapShape = GetShape();
271 TopTools_ListOfShape aListOfFaces;
272 TopExp_Explorer anExp( aLandCoverMapShape, TopAbs_FACE );
273 for( ; anExp.More(); anExp.Next() )
274 aListOfFaces.Append( anExp.Current() );
276 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
278 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
279 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
280 NCollection_IndexedDataMap< TopoDS_Face, QList<int> > aFacesMap;
282 // add into the map all edges existing in the shell
283 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
284 for( ; anExp1.More(); anExp1.Next() )
286 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
287 QList<int> aVerticesIdsList;
288 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
289 anEdgesMap.Add( anEdge, aVerticesIdsList );
292 // add into the map all faces existing in the shell and correspondence between face and edges ids
293 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
294 for( ; anExp2.More(); anExp2.Next() )
296 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
297 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
298 QList<int> anEdgesIdsList;
299 for( ; anExp3.More(); anExp3.Next() )
301 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
302 int anEdgeId = anEdgesMap.FindIndex( anEdge );
303 anEdgesIdsList.append( anEdgeId );
305 aFacesMap.Add( aFace, anEdgesIdsList );
308 QFile aFile( theFileName );
309 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
312 QTextStream aStream( &aFile );
313 aStream << "# nodes\n";
314 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
315 for( ; anIt1.More(); anIt1.Next() )
317 gp_Pnt aPnt = anIt1.Value();
318 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
320 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
322 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
327 aStream << "# edges\n";
328 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
329 for( ; anIt2.More(); anIt2.Next() )
331 QList<int> aVerticesIds = anIt2.Value();
332 foreach( int anId, aVerticesIds )
333 aStream << anId << " ";
338 aStream << "# faces\n";
339 NCollection_IndexedDataMap< TopoDS_Face, QList<int> >::Iterator anIt3( aFacesMap );
340 for( ; anIt3.More(); anIt3.Next() )
342 QList<int> anEdgesIds = anIt3.Value();
343 foreach( int anId, anEdgesIds )
344 aStream << anId << " ";
354 Add a new object as land cover
355 @param theObject the object to add as land cover
356 @param theType the Strickler type for the new land cover
357 @return if the addition is successful
359 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
361 if( theObject.IsNull() )
364 TopoDS_Shape aShape = theObject->GetTopShape();
365 if( aShape.ShapeType()!=TopAbs_FACE )
368 TopoDS_Face aFace = TopoDS::Face( aShape );
369 return LocalPartition( aFace, theType );
373 Add a new polyline as land cover
374 @param thePolyline the polyline to add as land cover
375 @param theType the Strickler type for the new land cover
376 @return if the addition is successful
378 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
380 if( thePolyline.IsNull() )
383 TopoDS_Shape aShape = thePolyline->GetShape();
384 if( aShape.ShapeType()!=TopAbs_WIRE )
387 TopoDS_Wire aWire = TopoDS::Wire( aShape );
388 if( !aWire.Closed() )
391 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
392 return LocalPartition( aFace, theType );
396 Remove the given face from land cover map
397 @param theFace the face to be removed
398 @return if the removing is successful
400 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
402 TopTools_ListOfShape aList;
403 aList.Append( theFace );
404 return Remove( aList );
408 Remove the given faces from land cover map
409 @param theFacesToRemove the face list to be removed
410 @return if the removing is successful
412 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
414 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
415 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
416 for( ; aFIt.More(); aFIt.Next() )
418 TopoDS_Shape aShape = aFIt.Value();
419 if( aShape.ShapeType()==TopAbs_FACE )
420 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
423 Iterator anIt( *this );
424 for( ; anIt.More(); anIt.Next() )
425 if( !aFacesToRemove.Contains( anIt.Face() ) )
426 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
428 StoreLandCovers( aNewFaces );
433 Split the land cover map by the given polyline
434 @param thePolyline the tool polyline to split the land cover map
435 @return if the removing is successful
437 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
439 if( thePolyline.IsNull() )
442 TopoDS_Shape aShape = thePolyline->GetShape();
443 return LocalPartition( aShape, "" );
447 Merge the given faces in the land cover
448 @param theFaces the faces to merge in the land cover map
449 @param theType the Strickler type for the merged land cover
450 @return if the merge is successful
452 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
454 // 1. to fuse the faces into the new face
455 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
456 if( aMergedFace.ShapeType()==TopAbs_FACE )
458 // 2. to remove the merged faces from the current map
461 // 3. to add the face into the map
462 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
467 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
468 bool IsToUnify, double theTolerance )
471 TopTools_ListIteratorOfListOfShape anIt;
472 BOPCol_ListOfShape aLC;
473 anIt.Initialize(theFaces);
474 for( ; anIt.More(); anIt.Next() )
475 aLC.Append( anIt.Value() );
477 BOPAlgo_PaveFiller aPF;
478 aPF.SetArguments( aLC );
479 aPF.SetRunParallel( Standard_False );
480 aPF.SetFuzzyValue( theTolerance );
483 anError = aPF.ErrorStatus();
485 return TopoDS_Shape();
487 BOPAlgo_Builder anAlgo;
488 anIt.Initialize( theFaces );
489 for( ; anIt.More(); anIt.Next() )
490 anAlgo.AddArgument( anIt.Value() );
492 anAlgo.PerformWithFiller( aPF );
493 anError = anAlgo.ErrorStatus();
495 return TopoDS_Shape();
497 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
499 BRep_Builder aBuilder;
501 aBuilder.MakeShell( aShell );
502 aShell.Closed( Standard_False );
503 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
504 for( ; anExplorer.More(); anExplorer.Next() )
506 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
509 if( aFace.ShapeType() == TopAbs_FACE )
511 aBuilder.Add( aShell, aFace );
512 aShell.Closed( Standard_False );
516 TopoDS_Shape aResult;
519 ShapeUpgrade_UnifySameDomain aUSD;
520 aUSD.Initialize( aShell );
522 aResult = aUSD.Shape();
527 anExplorer.Init( aResult, TopAbs_FACE );
529 TopoDS_Face anOneFace;
530 for( ; anExplorer.More(); anExplorer.Next(), i++ )
531 anOneFace = TopoDS::Face( anExplorer.Current() );
540 Get the shape of the land cover map
542 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
544 return HYDROData_Entity::GetShape( DataTag_Shape );
548 Set the shape of the land cover map
549 @param theShape the new shape for the land cover map
551 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
553 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
557 Perform the local partition algorithm on the land cover
558 @param theNewShape the new shape to add into the land cover
559 @param theNewType the new Strickler type for the new land cover
560 @return if the local partition is successful
562 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
564 if( theNewShape.IsNull() )
567 BOPCol_ListOfShape aShapesList;
568 BOPAlgo_PaveFiller aPaveFiller;
569 HYDROData_MapOfFaceToStricklerType aNewFaces;
571 // add faces to shapes list
572 Iterator anIt( *this );
573 for( ; anIt.More(); anIt.Next() )
574 aShapesList.Append( anIt.Face() );
575 aShapesList.Append( theNewShape );
577 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
579 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
580 StoreLandCovers( aNewFaces );
584 // prepare pave filler
585 aPaveFiller.SetArguments( aShapesList );
586 aPaveFiller.Perform();
587 Standard_Integer anError = aPaveFiller.ErrorStatus();
591 // add faces to builder
592 BOPAlgo_Builder aBuilder;
594 for( ; anIt.More(); anIt.Next() )
595 aBuilder.AddArgument( anIt.Face() );
596 aBuilder.AddArgument( theNewShape );
598 // perform the partition with the pave filler
599 aBuilder.PerformWithFiller( aPaveFiller );
600 anError = aBuilder.ErrorStatus();
604 // analysis of the history
605 // a. to fill map of shapes which come from the new face
606 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
607 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
608 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
609 TopTools_ListIteratorOfListOfShape aMIt( aModified );
610 for( ; aMIt.More(); aMIt.Next() )
612 //std::cout << " " << aMIt.Value() << std::endl;
613 aShapesFromNewFace.Add( aMIt.Value() );
616 // b. to fill map of parts except parts from new face
618 for( ; anIt.More(); anIt.Next() )
620 QString aSType = anIt.StricklerType();
621 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
622 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
623 TopTools_ListIteratorOfListOfShape aMIt( aModified );
624 for( ; aMIt.More(); aMIt.Next() )
626 TopoDS_Shape aShape = aMIt.Value();
627 bool isFace = aShape.ShapeType()==TopAbs_FACE;
628 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
629 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
630 if( isFace && !isAlsoFromNew )
631 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
635 // c. add the new shape if it is face with its type
636 if( theNewShape.ShapeType()==TopAbs_FACE )
637 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
639 // convert map of shape to type to compound and list of types
640 StoreLandCovers( aNewFaces );
645 Replace the set of land covers in the land cover map
646 @param theMap the map of shape (face) to Strickler type (string)
648 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
650 TopoDS_Compound aCompound;
651 BRep_Builder aCompoundBuilder;
652 aCompoundBuilder.MakeCompound( aCompound );
654 int n = theMap.Size();
655 Handle( TDataStd_ExtStringArray ) aTypes =
656 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
657 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
658 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
660 TopoDS_Face aFace = aNFIt.Key();
661 QString aType = aNFIt.Value();
662 aCompoundBuilder.Add( aCompound, aFace );
663 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
666 SetShape( aCompound );
670 Find the land cover for the given point
671 @param thePoint the point laying in some land cover
672 @param theType the returned type
673 @return the found land cover's face
675 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
677 //TODO: some more optimal algorithm
678 Iterator anIt( *this );
679 for( ; anIt.More(); anIt.Next() )
680 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
682 theType = anIt.StricklerType();
687 return TopoDS_Face();