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>
38 #include <BOPAlgo_PaveFiller.hxx>
39 #include <BRepTools.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <ShapeUpgrade_UnifySameDomain.hxx>
45 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
46 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
48 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
54 @param theMap the land cover map to iterate through
56 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
61 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
73 Initialize the iterator
74 @param theMap the land cover map to iterate through
76 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
78 TopoDS_Shape aShape = theMap.GetShape();
82 myIterator = new TopoDS_Iterator( aShape );
84 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
90 HYDROData_LandCoverMap::Iterator::~Iterator()
96 Return if the iterator has more elements
97 @return if the iterator has more elements
99 bool HYDROData_LandCoverMap::Iterator::More() const
101 return !myArray.IsNull() && myIterator && myIterator->More();
105 Move iterator to the next element
107 void HYDROData_LandCoverMap::Iterator::Next()
117 Get the current land cover (face)
118 @return the land cover's face
120 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
123 return TopoDS::Face( myIterator->Value() );
125 return TopoDS_Face();
129 Get the current land cover's Strickler type
130 @return the land cover's Strickler type
132 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
134 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
137 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
143 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
150 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
156 @return object's kind
158 const ObjectKind HYDROData_LandCoverMap::GetKind() const
160 return KIND_LAND_COVER_MAP;
164 Import the land cover map from QGIS
165 @param theFileName the name of file
166 @return if the import is successful
168 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
175 Export the land cover map to QGIS
176 @param theFileName the name of file
177 @return if the export is successful
179 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
186 Export the land cover map for the solver (Telemac)
187 @param theFileName the name of file
188 @return if the export is successful
190 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
197 Add a new object as land cover
198 @param theObject the object 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_Object )& theObject, const QString& theType )
204 if( theObject.IsNull() )
207 TopoDS_Shape aShape = theObject->GetTopShape();
208 if( aShape.ShapeType()!=TopAbs_FACE )
211 TopoDS_Face aFace = TopoDS::Face( aShape );
212 return LocalPartition( aFace, theType );
216 Add a new polyline as land cover
217 @param thePolyline the polyline to add as land cover
218 @param theType the Strickler type for the new land cover
219 @return if the addition is successful
221 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
223 if( thePolyline.IsNull() )
226 TopoDS_Shape aShape = thePolyline->GetShape();
227 if( aShape.ShapeType()!=TopAbs_WIRE )
230 TopoDS_Wire aWire = TopoDS::Wire( aShape );
231 if( !aWire.Closed() )
234 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
235 return LocalPartition( aFace, theType );
239 Remove the given face from land cover map
240 @param theFace the face to be removed
241 @return if the removing is successful
243 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
245 TopTools_ListOfShape aList;
246 aList.Append( theFace );
247 return Remove( aList );
251 Remove the given faces from land cover map
252 @param theFacesToRemove the face list to be removed
253 @return if the removing is successful
255 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
257 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
258 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
259 for( ; aFIt.More(); aFIt.Next() )
261 TopoDS_Shape aShape = aFIt.Value();
262 if( aShape.ShapeType()==TopAbs_FACE )
263 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
266 Iterator anIt( *this );
267 for( ; anIt.More(); anIt.Next() )
268 if( !aFacesToRemove.Contains( anIt.Face() ) )
269 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
271 StoreLandCovers( aNewFaces );
276 Split the land cover map by the given polyline
277 @param thePolyline the tool polyline to split the land cover map
278 @return if the removing is successful
280 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
282 if( thePolyline.IsNull() )
285 TopoDS_Shape aShape = thePolyline->GetShape();
286 return LocalPartition( aShape, "" );
290 Merge the given faces in the land cover
291 @param theFaces the faces to merge in the land cover map
292 @param theType the Strickler type for the merged land cover
293 @return if the merge is successful
295 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
297 // 1. to remove the merged faces from the current map
300 // 2. to fuse the faces into the new face
301 /*BOPAlgo_PaveFiller aPF;
302 aPF.SetArguments( theFaces );
303 aPF.SetFuzzyValue( 1E-2 );
304 aPF.SetRunParallel( Standard_False );
306 int iErr = aPF.ErrorStatus();
311 aBOP.SetArguments( theFaces );
312 aBOP.SetOperation( BOPAlgo_FUSE );
313 aBOP.SetRunParallel( Standard_False );
314 aBOP.PerformWithFiller(aPF);
315 iErr = aBOP.ErrorStatus();
319 TopoDS_Shape aMergedShape = aBOP.Shape();
320 if( aMergedShape.ShapeType()!=TopAbs_FACE )
325 TopoDS_Shape MergedFace;
326 MergeFaces(theFaces, true, MergedFace);
327 //BRepTools::Write(aMergedShape, "c:/sh_.brep");
329 // 3. to add the face into the map
330 return LocalPartition( TopoDS_Face( /*aMergedShape*/ ), theType );
333 bool HYDROData_LandCoverMap::MergeFaces(const TopTools_ListOfShape& theFaces, bool IsToUnify, TopoDS_Shape& outSh)
336 TopTools_ListIteratorOfListOfShape aIt;
337 BOPCol_ListOfShape aLC;
338 aIt.Initialize(theFaces);
339 for (; aIt.More(); aIt.Next()) {
340 const TopoDS_Shape& aS=aIt.Value();
343 BOPAlgo_PaveFiller aPF;
344 aPF.SetArguments(aLC);
345 aPF.SetRunParallel(Standard_False);
346 aPF.SetFuzzyValue(1e-02);
349 iErr=aPF.ErrorStatus();
353 BOPAlgo_Builder aBuilder;
354 aIt.Initialize(theFaces);
355 for (; aIt.More(); aIt.Next()) {
356 const TopoDS_Shape& aS=aIt.Value();
357 aBuilder.AddArgument(aS);
360 aBuilder.PerformWithFiller(aPF);
361 iErr = aBuilder.ErrorStatus();
364 const TopoDS_Shape& aMergedShape=aBuilder.Shape();
370 she.Closed(Standard_False);
371 TopExp_Explorer ff(aMergedShape, TopAbs_FACE);
372 for(; ff.More(); ff.Next())
374 const TopoDS_Face& F = TopoDS::Face(ff.Current());
377 if (F.ShapeType() == TopAbs_FACE) {
379 she.Closed (Standard_False);
385 ShapeUpgrade_UnifySameDomain USD;
395 ff.Init(outSh, TopAbs_FACE);
398 for(; ff.More(); ff.Next(), i++)
399 OneF = TopoDS::Face(ff.Current());
408 Get the shape of the land cover map
410 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
412 return HYDROData_Entity::GetShape( DataTag_Shape );
416 Set the shape of the land cover map
417 @param theShape the new shape for the land cover map
419 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
421 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
425 Perform the local partition algorithm on the land cover
426 @param theNewShape the new shape to add into the land cover
427 @param theNewType the new Strickler type for the new land cover
428 @return if the local partition is successful
430 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
432 if( theNewShape.IsNull() )
435 BOPCol_ListOfShape aShapesList;
436 BOPAlgo_PaveFiller aPaveFiller;
437 HYDROData_MapOfFaceToStricklerType aNewFaces;
439 // add faces to shapes list
440 Iterator anIt( *this );
441 for( ; anIt.More(); anIt.Next() )
442 aShapesList.Append( anIt.Face() );
443 aShapesList.Append( theNewShape );
445 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
447 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
448 StoreLandCovers( aNewFaces );
452 // prepare pave filler
453 aPaveFiller.SetArguments( aShapesList );
454 aPaveFiller.Perform();
455 Standard_Integer anError = aPaveFiller.ErrorStatus();
459 // add faces to builder
460 BOPAlgo_Builder aBuilder;
462 for( ; anIt.More(); anIt.Next() )
463 aBuilder.AddArgument( anIt.Face() );
464 aBuilder.AddArgument( theNewShape );
466 // perform the partition with the pave filler
467 aBuilder.PerformWithFiller( aPaveFiller );
468 anError = aBuilder.ErrorStatus();
472 // analysis of the history
473 // a. to fill map of shapes which come from the new face
474 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
475 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
476 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
477 TopTools_ListIteratorOfListOfShape aMIt( aModified );
478 for( ; aMIt.More(); aMIt.Next() )
480 //std::cout << " " << aMIt.Value() << std::endl;
481 aShapesFromNewFace.Add( aMIt.Value() );
484 // b. to fill map of parts except parts from new face
486 for( ; anIt.More(); anIt.Next() )
488 QString aSType = anIt.StricklerType();
489 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
490 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
491 TopTools_ListIteratorOfListOfShape aMIt( aModified );
492 for( ; aMIt.More(); aMIt.Next() )
494 TopoDS_Shape aShape = aMIt.Value();
495 bool isFace = aShape.ShapeType()==TopAbs_FACE;
496 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
497 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
498 if( isFace && !isAlsoFromNew )
499 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
503 // c. add the new shape if it is face with its type
504 if( theNewShape.ShapeType()==TopAbs_FACE )
505 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
507 // convert map of shape to type to compound and list of types
508 StoreLandCovers( aNewFaces );
513 Replace the set of land covers in the land cover map
514 @param theMap the map of shape (face) to Strickler type (string)
516 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
518 TopoDS_Compound aCompound;
519 BRep_Builder aCompoundBuilder;
520 aCompoundBuilder.MakeCompound( aCompound );
522 int n = theMap.Size();
523 Handle( TDataStd_ExtStringArray ) aTypes =
524 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
525 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
526 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
528 TopoDS_Face aFace = aNFIt.Key();
529 QString aType = aNFIt.Value();
530 aCompoundBuilder.Add( aCompound, aFace );
531 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
534 SetShape( aCompound );
538 Find the land cover for the given point
539 @param thePoint the point laying in some land cover
540 @param theType the returned type
541 @return the found land cover's face
543 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
545 //TODO: some more optimal algorithm
546 Iterator anIt( *this );
547 for( ; anIt.More(); anIt.Next() )
548 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
550 theType = anIt.StricklerType();
555 return TopoDS_Face();