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 myIndex = myArray->Lower();
101 HYDROData_LandCoverMap::Iterator::~Iterator()
107 Return the current 0-based index of the iterator
108 @return the current index
110 int HYDROData_LandCoverMap::Iterator::Index() const
112 if( myArray.IsNull() )
115 return myIndex - myArray->Lower();
119 Return if the iterator has more elements
120 @return if the iterator has more elements
122 bool HYDROData_LandCoverMap::Iterator::More() const
124 return !myArray.IsNull() && myIterator && myIterator->More();
128 Move iterator to the next element
130 void HYDROData_LandCoverMap::Iterator::Next()
140 Get the current land cover (face)
141 @return the land cover's face
143 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
146 return TopoDS::Face( myIterator->Value() );
148 return TopoDS_Face();
152 Get the current land cover's Strickler type
153 @return the land cover's Strickler type
155 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
157 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
160 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
164 Set the Strickler type for the current land cover
165 @param theType the Strickler type
167 void HYDROData_LandCoverMap::Iterator::SetStricklerType( const QString& theType )
169 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
172 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
178 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
179 : HYDROData_Entity( Geom_No )
186 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
192 @return object's kind
194 const ObjectKind HYDROData_LandCoverMap::GetKind() const
196 return KIND_LAND_COVER_MAP;
200 Import the land cover map from QGIS
201 @param theFileName the name of file
202 @return if the import is successful
204 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
211 Export the land cover map to QGIS
212 @param theFileName the name of file
213 @return if the export is successful
215 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
221 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
223 int aHashX = HashCode( thePoint.X(), theUpper );
224 int aHashY = HashCode( thePoint.Y(), theUpper );
225 return (aHashX^aHashY)%theUpper;
228 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
230 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
233 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
234 Standard_Real theDeflection,
235 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
236 QList<int>& theVerticesIds )
238 BRepAdaptor_Curve aCurve( theEdge );
239 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
240 if( !aDiscrete.IsDone() )
243 int n = aDiscrete.NbPoints();
244 for( int i=1; i<=n; i++ )
246 gp_Pnt aPnt = aDiscrete.Value( i );
248 if( theVerticesMap.Contains( aPnt ) )
249 anId = theVerticesMap.FindIndex( aPnt );
252 anId = theVerticesMap.Size();
253 theVerticesMap.Add( aPnt );
255 theVerticesIds.append( anId );
261 Export the land cover map for the solver (Telemac)
262 @param theFileName the name of file
263 @return if the export is successful
265 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
267 TopoDS_Shape aLandCoverMapShape = GetShape();
268 TopTools_ListOfShape aListOfFaces;
269 TopExp_Explorer anExp( aLandCoverMapShape, TopAbs_FACE );
270 for( ; anExp.More(); anExp.Next() )
271 aListOfFaces.Append( anExp.Current() );
273 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
275 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
276 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
277 NCollection_IndexedDataMap< TopoDS_Face, QList<int> > aFacesMap;
279 // add into the map all edges existing in the shell
280 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
281 for( ; anExp1.More(); anExp1.Next() )
283 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
284 QList<int> aVerticesIdsList;
285 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
286 anEdgesMap.Add( anEdge, aVerticesIdsList );
289 // add into the map all faces existing in the shell and correspondence between face and edges ids
290 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
291 for( ; anExp2.More(); anExp2.Next() )
293 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
294 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
295 QList<int> anEdgesIdsList;
296 for( ; anExp3.More(); anExp3.Next() )
298 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
299 int anEdgeId = anEdgesMap.FindIndex( anEdge );
300 anEdgesIdsList.append( anEdgeId );
302 aFacesMap.Add( aFace, anEdgesIdsList );
305 QFile aFile( theFileName );
306 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
309 QTextStream aStream( &aFile );
310 aStream << "# nodes\n";
311 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
312 for( ; anIt1.More(); anIt1.Next() )
314 gp_Pnt aPnt = anIt1.Value();
315 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
317 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
319 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
324 aStream << "# edges\n";
325 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
326 for( ; anIt2.More(); anIt2.Next() )
328 QList<int> aVerticesIds = anIt2.Value();
329 foreach( int anId, aVerticesIds )
330 aStream << anId << " ";
335 aStream << "# faces\n";
336 NCollection_IndexedDataMap< TopoDS_Face, QList<int> >::Iterator anIt3( aFacesMap );
337 for( ; anIt3.More(); anIt3.Next() )
339 QList<int> anEdgesIds = anIt3.Value();
340 foreach( int anId, anEdgesIds )
341 aStream << anId << " ";
351 Add a new object as land cover
352 @param theObject the object to add as land cover
353 @param theType the Strickler type for the new land cover
354 @return if the addition is successful
356 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
358 if( theObject.IsNull() )
361 TopoDS_Shape aShape = theObject->GetTopShape();
362 if( aShape.ShapeType()!=TopAbs_FACE )
365 TopoDS_Face aFace = TopoDS::Face( aShape );
366 return LocalPartition( aFace, theType );
370 Add a new polyline as land cover
371 @param thePolyline the polyline to add as land cover
372 @param theType the Strickler type for the new land cover
373 @return if the addition is successful
375 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
377 if( thePolyline.IsNull() )
380 TopoDS_Shape aShape = thePolyline->GetShape();
381 if( aShape.ShapeType()!=TopAbs_WIRE )
384 TopoDS_Wire aWire = TopoDS::Wire( aShape );
385 if( !aWire.Closed() )
388 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
389 return LocalPartition( aFace, theType );
393 Remove the given face from land cover map
394 @param theFace the face to be removed
395 @return if the removing is successful
397 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
399 TopTools_ListOfShape aList;
400 aList.Append( theFace );
401 return Remove( aList );
405 Remove the given faces from land cover map
406 @param theFacesToRemove the face list to be removed
407 @return if the removing is successful
409 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
411 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
412 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
413 for( ; aFIt.More(); aFIt.Next() )
415 TopoDS_Shape aShape = aFIt.Value();
416 if( aShape.ShapeType()==TopAbs_FACE )
417 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
420 Iterator anIt( *this );
421 for( ; anIt.More(); anIt.Next() )
422 if( !aFacesToRemove.Contains( anIt.Face() ) )
423 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
425 StoreLandCovers( aNewFaces );
430 Split the land cover map by the given polyline
431 @param thePolyline the tool polyline to split the land cover map
432 @return if the removing is successful
434 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
436 if( thePolyline.IsNull() )
439 TopoDS_Shape aShape = thePolyline->GetShape();
440 return LocalPartition( aShape, "" );
444 Merge the given faces in the land cover
445 @param theFaces the faces to merge in the land cover map
446 @param theType the Strickler type for the merged land cover
447 @return if the merge is successful
449 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
451 // 1. to fuse the faces into the new face
452 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
453 if( aMergedFace.ShapeType()==TopAbs_FACE )
455 // 2. to remove the merged faces from the current map
458 // 3. to add the face into the map
459 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
464 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
465 bool IsToUnify, double theTolerance )
468 TopTools_ListIteratorOfListOfShape anIt;
469 BOPCol_ListOfShape aLC;
470 anIt.Initialize(theFaces);
471 for( ; anIt.More(); anIt.Next() )
472 aLC.Append( anIt.Value() );
474 BOPAlgo_PaveFiller aPF;
475 aPF.SetArguments( aLC );
476 aPF.SetRunParallel( Standard_False );
477 aPF.SetFuzzyValue( theTolerance );
480 anError = aPF.ErrorStatus();
482 return TopoDS_Shape();
484 BOPAlgo_Builder anAlgo;
485 anIt.Initialize( theFaces );
486 for( ; anIt.More(); anIt.Next() )
487 anAlgo.AddArgument( anIt.Value() );
489 anAlgo.PerformWithFiller( aPF );
490 anError = anAlgo.ErrorStatus();
492 return TopoDS_Shape();
494 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
496 BRep_Builder aBuilder;
498 aBuilder.MakeShell( aShell );
499 aShell.Closed( Standard_False );
500 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
501 for( ; anExplorer.More(); anExplorer.Next() )
503 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
506 if( aFace.ShapeType() == TopAbs_FACE )
508 aBuilder.Add( aShell, aFace );
509 aShell.Closed( Standard_False );
513 TopoDS_Shape aResult;
516 ShapeUpgrade_UnifySameDomain aUSD;
517 aUSD.Initialize( aShell );
519 aResult = aUSD.Shape();
524 anExplorer.Init( aResult, TopAbs_FACE );
526 TopoDS_Face anOneFace;
527 for( ; anExplorer.More(); anExplorer.Next(), i++ )
528 anOneFace = TopoDS::Face( anExplorer.Current() );
537 Get the shape of the land cover map
539 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
541 return HYDROData_Entity::GetShape( DataTag_Shape );
545 Set the shape of the land cover map
546 @param theShape the new shape for the land cover map
548 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
550 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
554 Perform the local partition algorithm on the land cover
555 @param theNewShape the new shape to add into the land cover
556 @param theNewType the new Strickler type for the new land cover
557 @return if the local partition is successful
559 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
561 if( theNewShape.IsNull() )
564 BOPCol_ListOfShape aShapesList;
565 BOPAlgo_PaveFiller aPaveFiller;
566 HYDROData_MapOfFaceToStricklerType aNewFaces;
568 // add faces to shapes list
569 Iterator anIt( *this );
570 for( ; anIt.More(); anIt.Next() )
571 aShapesList.Append( anIt.Face() );
572 aShapesList.Append( theNewShape );
574 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
576 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
577 StoreLandCovers( aNewFaces );
581 // prepare pave filler
582 aPaveFiller.SetArguments( aShapesList );
583 aPaveFiller.Perform();
584 Standard_Integer anError = aPaveFiller.ErrorStatus();
588 // add faces to builder
589 BOPAlgo_Builder aBuilder;
591 for( ; anIt.More(); anIt.Next() )
592 aBuilder.AddArgument( anIt.Face() );
593 aBuilder.AddArgument( theNewShape );
595 // perform the partition with the pave filler
596 aBuilder.PerformWithFiller( aPaveFiller );
597 anError = aBuilder.ErrorStatus();
601 // analysis of the history
602 // a. to fill map of shapes which come from the new face
603 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
604 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
605 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
606 TopTools_ListIteratorOfListOfShape aMIt( aModified );
607 for( ; aMIt.More(); aMIt.Next() )
609 //std::cout << " " << aMIt.Value() << std::endl;
610 aShapesFromNewFace.Add( aMIt.Value() );
613 // b. to fill map of parts except parts from new face
615 for( ; anIt.More(); anIt.Next() )
617 QString aSType = anIt.StricklerType();
618 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
619 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
620 TopTools_ListIteratorOfListOfShape aMIt( aModified );
621 for( ; aMIt.More(); aMIt.Next() )
623 TopoDS_Shape aShape = aMIt.Value();
624 bool isFace = aShape.ShapeType()==TopAbs_FACE;
625 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
626 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
627 if( isFace && !isAlsoFromNew )
628 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
632 // c. add the new shape if it is face with its type
633 if( theNewShape.ShapeType()==TopAbs_FACE )
634 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
636 // convert map of shape to type to compound and list of types
637 StoreLandCovers( aNewFaces );
642 Replace the set of land covers in the land cover map
643 @param theMap the map of shape (face) to Strickler type (string)
645 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
647 TopoDS_Compound aCompound;
648 BRep_Builder aCompoundBuilder;
649 aCompoundBuilder.MakeCompound( aCompound );
651 int n = theMap.Size();
652 Handle( TDataStd_ExtStringArray ) aTypes =
653 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
654 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
655 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
657 TopoDS_Face aFace = aNFIt.Key();
658 QString aType = aNFIt.Value();
659 aCompoundBuilder.Add( aCompound, aFace );
660 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
663 SetShape( aCompound );
667 Find the land cover for the given point
668 @param thePoint the point laying in some land cover
669 @param theType the returned type
670 @return the found land cover's face
672 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
674 //TODO: some more optimal algorithm
675 Iterator anIt( *this );
676 for( ; anIt.More(); anIt.Next() )
677 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
679 theType = anIt.StricklerType();
684 return TopoDS_Face();