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>
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_StricklerTable.h>
26 #include <HYDROData_ShapesTool.h>
28 #include <BOPAlgo_BOP.hxx>
29 #include <BOPAlgo_Builder.hxx>
30 #include <BOPAlgo_PaveFiller.hxx>
31 #include <BOPCol_ListOfShape.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepAlgoAPI_Fuse.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <GCPnts_QuasiUniformDeflection.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Shell.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_SequenceOfShape.hxx>
47 #include <BOPAlgo_PaveFiller.hxx>
48 #include <BRepTools.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <ShapeUpgrade_UnifySameDomain.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53 #include <ShapeBuild_ReShape.hxx>
54 #include <ShapeFix_Shape.hxx>
55 #include <BRepCheck_Shell.hxx>
56 #include <BRepCheck_ListOfStatus.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <Handle_Geom_Curve.hxx>
59 #include <Handle_Geom_Line.hxx>
60 #include <Handle_Geom_TrimmedCurve.hxx>
61 #include <Geom_TrimmedCurve.hxx>
62 #include <TopTools_DataMapOfShapeListOfShape.hxx>
63 #include <NCollection_DoubleMap.hxx>
68 #include <QTextStream>
72 #include "HYDRO_trace.hxx"
74 const char TELEMAC_FORMAT = 'f';
75 const int TELEMAC_PRECISION = 3;
78 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
79 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
83 @param theMap the land cover map to iterate through
85 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
90 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
102 Initialize the iterator
103 @param theMap the land cover map to iterate through
105 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
107 TopoDS_Shape aShape = theMap.GetShape();
108 if( aShape.IsNull() )
112 myExplorer = new TopExp_Explorer();
113 myExplorer->Init( aShape, TopAbs_FACE );
116 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
117 if( myArray.IsNull() )
120 myIndex = myArray->Lower();
126 HYDROData_LandCoverMap::Explorer::~Explorer()
132 Return the current 0-based index of the iterator
133 @return the current index
135 int HYDROData_LandCoverMap::Explorer::Index() const
137 if( myArray.IsNull() )
140 return myIndex - myArray->Lower();
144 Return if the iterator has more elements
145 @return if the iterator has more elements
147 bool HYDROData_LandCoverMap::Explorer::More() const
149 return !myArray.IsNull() && myExplorer && myExplorer->More();
153 Move iterator to the next element
155 void HYDROData_LandCoverMap::Explorer::Next()
165 Get the current land cover (face)
166 @return the land cover's face
168 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
171 return TopoDS::Face( myExplorer->Current() );
173 return TopoDS_Face();
177 Get the current land cover's Strickler type
178 @return the land cover's Strickler type
180 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
182 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
185 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
189 Set the Strickler type for the current land cover
190 @param theType the Strickler type
192 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
194 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
197 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
203 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
204 : HYDROData_Entity( Geom_No )
211 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
217 @return object's kind
219 const ObjectKind HYDROData_LandCoverMap::GetKind() const
221 return KIND_LAND_COVER_MAP;
224 int HYDROData_LandCoverMap::GetLCCount() const
226 Explorer anIt( *this );
228 for( ; anIt.More(); anIt.Next() )
233 bool HYDROData_LandCoverMap::IsEmpty() const
235 Explorer anIt( *this );
243 Load attributes from DBF File
246 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
247 const QString& theFieldName,
248 const QStringList& theDBFValues,
249 const QStringList& theStricklerTypes,
250 const QList<int>& theIndices )
252 if (theDBFValues.size() != theStricklerTypes.size())
253 return DBFStatus_DIFF_SIZE_ERROR;
254 HYDROData_ShapeFile aDBFImporter;
255 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
256 return DBFStatus_OPEN_FILE_ERROR; //cant open file
258 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
259 int FieldNameIndex = FieldList.indexOf(theFieldName);
260 if (FieldNameIndex == -1)
261 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
263 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
264 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
267 Explorer anIt( *this );
268 for( ; anIt.More(); anIt.Next() )
270 int CurIndex = anIt.Index();
271 int anIndex = CurIndex;
272 if( !theIndices.isEmpty() )
273 anIndex = theIndices[CurIndex];
275 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
276 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
277 if ( StricklerTypesInd != -1)
278 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
285 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
289 Export attributes to DBF File
292 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
293 const QString& theFieldName,
294 const QStringList& theDBFValues,
295 const QStringList& theStricklerTypes) const
297 if (theDBFValues.size() != theStricklerTypes.size())
299 HYDROData_ShapeFile anExporter;
300 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
301 Explorer anIt( *this );
302 for( ; anIt.More(); anIt.Next() )
304 QString CurST = anIt.StricklerType();
305 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
306 aCurAttrV.myIsNull = false;
307 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
308 if (StricklerTypesInd != -1)
310 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
311 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
312 theAttrV.push_back(aCurAttrV);
315 aCurAttrV.myIsNull = true;
317 //use actual str value; not the raw value
318 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
322 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
324 int aHashX = HashCode( thePoint.X(), theUpper );
325 int aHashY = HashCode( thePoint.Y(), theUpper );
326 return (aHashX^aHashY)%theUpper;
329 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
331 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
334 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
335 Standard_Real theDeflection,
336 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
337 QList<int>& theVerticesIds )
339 BRepAdaptor_Curve aCurve( theEdge );
340 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
341 if( !aDiscrete.IsDone() )
344 int n = aDiscrete.NbPoints();
345 for( int i=1; i<=n; i++ )
347 gp_Pnt aPnt = aDiscrete.Value( i );
349 if( theVerticesMap.Contains( aPnt ) )
350 anId = theVerticesMap.FindIndex( aPnt );
353 anId = theVerticesMap.Size();
354 theVerticesMap.Add( aPnt );
356 theVerticesIds.append( anId );
362 Export the land cover map for the solver (Telemac)
363 @param theFileName the name of file
364 @return if the export is successful
366 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
367 double theDeflection,
368 const Handle(HYDROData_StricklerTable)& theTable ) const
370 TopoDS_Shape aLandCoverMapShape = GetShape();
371 TopTools_ListOfShape aListOfFaces;
372 Explorer anIt( *this );
373 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
374 for( ; anIt.More(); anIt.Next() )
376 aListOfFaces.Append( anIt.Face() );
377 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
380 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false, NULL );
382 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
383 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
384 typedef QPair< QString, QList<int> > FaceData;
385 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
387 // add into the map all edges existing in the shell
388 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
389 for( ; anExp1.More(); anExp1.Next() )
391 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
392 QList<int> aVerticesIdsList;
393 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
394 anEdgesMap.Add( anEdge, aVerticesIdsList );
397 // add into the map all faces existing in the shell and correspondence between face and edges ids
398 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
399 for( ; anExp2.More(); anExp2.Next() )
401 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
402 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
403 QList<int> anEdgesIdsList;
404 for( ; anExp3.More(); anExp3.Next() )
406 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
407 int anEdgeId = anEdgesMap.FindIndex( anEdge );
408 anEdgesIdsList.append( anEdgeId );
412 aData.first = aTypesMap[aFace.TShape()];
413 aData.second = anEdgesIdsList;
414 aFacesMap.Add( aFace, aData );
417 QFile aFile( theFileName );
418 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
421 QTextStream aStream( &aFile );
422 aStream << "# nodes\n";
423 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
424 for( ; anIt1.More(); anIt1.Next() )
426 gp_Pnt aPnt = anIt1.Value();
427 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
429 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
431 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
436 aStream << "# edges\n";
437 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
438 for( ; anIt2.More(); anIt2.Next() )
440 QList<int> aVerticesIds = anIt2.Value();
441 foreach( int anId, aVerticesIds )
442 aStream << anId << " ";
447 aStream << "# faces\n";
448 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
449 for( ; anIt3.More(); anIt3.Next() )
451 QString aType = anIt3.Value().first;
452 double aCoeff = theTable->Get( aType, 0.0 );
453 QList<int> anEdgesIds = anIt3.Value().second;
454 //aStream << "\"" << aType << "\" ";
455 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
456 foreach( int anId, anEdgesIds )
457 aStream << anId << " ";
467 Add a new object as land cover
468 @param theObject the object to add as land cover
469 @param theType the Strickler type for the new land cover
470 @return if the addition is successful
472 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
474 if( theObject.IsNull() )
477 TopoDS_Shape aShape = theObject->GetTopShape();
480 if( aShape.ShapeType() ==TopAbs_FACE )
482 aFace = TopoDS::Face(aShape);
484 else if ( aShape.ShapeType() ==TopAbs_COMPOUND )
486 TopoDS_Iterator It(aShape);
487 for (; It.More(); It.Next())
488 if (It.Value().ShapeType() == TopAbs_FACE)
490 aFace = TopoDS::Face(It.Value());
497 return LocalPartition( aFace, theType );
500 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
502 if( !theWire.Closed() )
505 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
506 return LocalPartition( aFace, theType );
510 Add a new polyline as land cover
511 @param thePolyline the polyline to add as land cover
512 @param theType the Strickler type for the new land cover
513 @return if the addition is successful
515 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
517 if( thePolyline.IsNull() )
520 TopoDS_Shape aShape = thePolyline->GetShape();
521 if( aShape.ShapeType()==TopAbs_WIRE )
522 return Add( TopoDS::Wire( aShape ), theType );
524 if( aShape.ShapeType()==TopAbs_COMPOUND )
526 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
527 for( ; anExp.More(); anExp.Next() )
529 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
530 if( !Add( aPart, theType ) )
540 Remove the given face from land cover map
541 @param theFace the face to be removed
542 @return if the removing is successful
544 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
546 TopTools_ListOfShape aList;
547 aList.Append( theFace );
548 return Remove( aList );
552 Remove the given faces from land cover map
553 @param theFacesToRemove the face list to be removed
554 @return if the removing is successful
556 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
558 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
559 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
560 for( ; aFIt.More(); aFIt.Next() )
562 TopoDS_Shape aShape = aFIt.Value();
563 if( aShape.ShapeType()==TopAbs_FACE )
564 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
567 Explorer anIt( *this );
568 for( ; anIt.More(); anIt.Next() )
569 if( !aFacesToRemove.Contains( anIt.Face() ) )
570 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
572 if ( aNewFaces.IsEmpty() )
575 StoreLandCovers( aNewFaces );
580 Split the land cover map by the given polyline
581 @param thePolyline the tool polyline to split the land cover map
582 @return if the removing is successful
584 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
586 if( thePolyline.IsNull() )
589 TopoDS_Shape aShape = thePolyline->GetShape();
590 return Split( aShape );
595 Split the land cover map by the given polyline
596 @param theShape the tool polyline to split the land cover map
597 @return if the removing is successful
599 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
601 int aNbCL = GetLCCount();
602 bool aResult = LocalPartition( theShape, "" );
603 return aResult && aNbCL != GetLCCount();
608 Merge the given faces in the land cover
609 @param theFaces the faces to merge in the land cover map
610 @param theType the Strickler type for the merged land cover
611 @return if the merge is successful
613 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
615 // 1. to fuse the faces into the new face
616 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true, NULL );
618 if( !aMergedFace.IsNull() )
620 // 2. to remove the merged faces from the current map
622 TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
623 for( ; Exp.More(); Exp.Next() )
625 const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
626 // 3. to add the face into the map
627 aStat = aStat && LocalPartition( aCF, theType );
636 Merge the given faces into the shell/face
637 @param theFaces the faces to merge
638 @param IsToUnify if the common edges should be removed (fused)
639 @param theTolerance the operation's tolerance
640 @return result shape (face or shell)
643 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
645 TopTools_IndexedDataMapOfShapeListOfShape* theShHistory,
649 TopTools_ListIteratorOfListOfShape anIt;
650 BOPCol_ListOfShape aLC;
651 anIt.Initialize(theFaces);
652 for( ; anIt.More(); anIt.Next() )
654 if (anIt.Value().ShapeType() != TopAbs_FACE)
655 return TopoDS_Shape();
656 aLC.Append( anIt.Value() );
659 BOPAlgo_PaveFiller aPF;
660 aPF.SetArguments( aLC );
661 aPF.SetRunParallel( Standard_False );
662 aPF.SetFuzzyValue( theTolerance );
665 anError = aPF.ErrorStatus();
667 return TopoDS_Shape();
669 BOPAlgo_Builder anAlgo;
670 anIt.Initialize( theFaces );
671 for( ; anIt.More(); anIt.Next() )
672 anAlgo.AddArgument( anIt.Value() );
674 anAlgo.PerformWithFiller( aPF );
675 anError = anAlgo.ErrorStatus();
677 return TopoDS_Shape();
679 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
681 // retrieve history of modifications
684 theShHistory->Clear();
685 anIt.Initialize(theFaces);
686 for( ; anIt.More(); anIt.Next() )
688 const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
689 theShHistory->Add(anIt.Value(), aMLS);
694 BRep_Builder aBuilder;
696 aBuilder.MakeShell( aShell );
697 aShell.Closed( Standard_False );
698 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
699 for( ; anExplorer.More(); anExplorer.Next() )
701 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
704 if( aFace.ShapeType() == TopAbs_FACE )
706 aBuilder.Add( aShell, aFace );
707 aShell.Closed( Standard_False );
711 TopoDS_Shape aResult;
714 ShapeUpgrade_UnifySameDomain aUSD;
715 aUSD.Initialize( aShell );
717 aResult = aUSD.Shape();
722 anExplorer.Init( aResult, TopAbs_FACE );
724 TopoDS_Face anOneFace;
725 for( ; anExplorer.More(); anExplorer.Next(), n++ )
726 anOneFace = TopoDS::Face( anExplorer.Current() );
730 else if (aResult.ShapeType() == TopAbs_SHELL)
732 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
733 if (aBCS.Status().First() != BRepCheck_NoError)
735 ShapeFix_Shell aFixer;
736 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
737 aResult = aFixer.Shape();
744 Change Strickler type for the list of faces to the given one
745 @param theFaces the faces to change type
746 @param theType the Strickler type for the given land cover(s)
747 @return if the change type operation is successful
749 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
751 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
752 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
753 for( ; aFIt.More(); aFIt.Next() )
755 TopoDS_Shape aShape = aFIt.Value();
756 if( aShape.ShapeType()==TopAbs_FACE )
757 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
761 Explorer anIt( *this );
762 for( ; anIt.More(); anIt.Next() )
763 if( aFacesToChangeType.Contains( anIt.Face() ) )
765 anIt.SetStricklerType( theType );
768 if ( aNbChanges != theFaces.Extent() )
775 Get the shape of the land cover map
777 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
779 return HYDROData_Entity::GetShape( DataTag_Shape );
783 Get Strickler type of the given land cover
784 @param theLandCover the land cover to get Strickler type of
785 @return name of Strickler type
787 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
791 Explorer anIt( *this );
792 for( ; anIt.More(); anIt.Next() )
793 if( anIt.Face().IsEqual( theLandCover) )
795 aType = anIt.StricklerType();
803 Set the shape of the land cover map
804 @param theShape the new shape for the land cover map
806 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
808 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
812 Perform the local partition algorithm on the land cover
813 @param theNewShape the new shape to add into the land cover
814 @param theNewType the new Strickler type for the new land cover
815 @return if the local partition is successful
817 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
819 if( theNewShape.IsNull() )
822 BOPCol_ListOfShape aShapesList;
823 BOPAlgo_PaveFiller aPaveFiller;
824 HYDROData_MapOfFaceToStricklerType aNewFaces;
826 // add faces to shapes list
827 Explorer anIt( *this );
828 for( ; anIt.More(); anIt.Next() )
829 aShapesList.Append( anIt.Face() );
830 aShapesList.Append( theNewShape );
832 DEBTRACE("theNewType " << theNewType);
833 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
836 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
837 StoreLandCovers( aNewFaces );
842 // prepare pave filler
843 aPaveFiller.SetArguments( aShapesList );
844 aPaveFiller.Perform();
845 Standard_Integer anError = aPaveFiller.ErrorStatus();
850 // add faces to builder
851 BOPAlgo_Builder aBuilder;
853 for( ; anIt.More(); anIt.Next() )
854 aBuilder.AddArgument( anIt.Face() );
855 aBuilder.AddArgument( theNewShape );
857 // perform the partition with the pave filler
858 aBuilder.PerformWithFiller( aPaveFiller );
859 anError = aBuilder.ErrorStatus();
863 //std::cout << "History:" << std::endl;
864 // analysis of the history
865 // a. to fill map of shapes which come from the new face
866 NCollection_IndexedMap<int> aShapesFromNewFace;
867 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
868 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
869 TopTools_ListIteratorOfListOfShape aMIt( aModified );
870 for( ; aMIt.More(); aMIt.Next() )
872 //std::cout << " " << aMIt.Value() << std::endl;
873 DEBTRACE(aMIt.Value());
874 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
875 aShapesFromNewFace.Add( aKey );
878 // b. to fill map of parts except parts from new face
880 for( ; anIt.More(); anIt.Next() )
882 QString aSType = anIt.StricklerType();
883 DEBTRACE(anIt.StricklerType());
884 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
885 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
887 if( aModified.Extent() == 0 )
888 aModified.Append( anIt.Face() );
890 TopTools_ListIteratorOfListOfShape aMIt( aModified );
891 for( ; aMIt.More(); aMIt.Next() )
893 TopoDS_Shape aShape = aMIt.Value();
894 bool isFace = aShape.ShapeType()==TopAbs_FACE;
895 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
896 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
897 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
898 if( isFace && !isAlsoFromNew )
899 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
903 // c. add the new shape if it is face with its type
904 if( theNewShape.ShapeType()==TopAbs_FACE )
905 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
907 // convert map of shape to type to compound and list of types
908 StoreLandCovers( aNewFaces );
913 Replace the set of land covers in the land cover map
914 @param theMap the map of shape (face) to Strickler type (string)
916 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
918 TopTools_ListOfShape aListOfFaces;
920 for( int i = 1; i <= theMap.Extent(); i++ )
922 TopoDS_Face aFace = theMap.FindKey(i);
925 aListOfFaces.Append(aFace);
928 TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
930 TopoDS_Shape aResult;
931 if( aListOfFaces.Extent() == 1 )
932 aResult = aListOfFaces.First();
933 else if( aListOfFaces.Extent() > 1 )
934 aResult = MergeFaces( aListOfFaces, false, &ShHistory );
936 //remove internal edges
937 //if nothing changes => the result shape should be the same
938 //hence the map will be empty
940 NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
941 RemoveInternal(aResult, &ShF2FHistory);
943 //one face => mark as unchanged
944 if( aListOfFaces.Extent() == 1 )
945 ShHistory.Add(aResult, TopTools_ListOfShape());
947 NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
950 for( int i = 1; i <= theMap.Extent(); i++ )
952 TopoDS_Face aFF = theMap.FindKey(i);
955 TopTools_ListOfShape aLS = ShHistory.FindFromKey(aFF);
958 QString aSType = theMap.FindFromKey(aFF);
959 if (ShF2FHistory.Contains(aFF))
960 aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
962 aChF2ST.Add(aFF, aSType);
966 TopTools_ListIteratorOfListOfShape anIt(aLS);
967 for (; anIt.More(); anIt.Next())
969 QString aSType = theMap.FindFromKey(aFF);
970 const TopoDS_Face& aMF = TopoDS::Face(anIt.Value());
971 if (ShF2FHistory.Contains(aFF))
972 aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
974 aChF2ST.Add(aFF, aSType);
981 //Explorer Exp(*this);
982 TopExp_Explorer FExp(aResult, TopAbs_FACE);
983 for( ; FExp.More(); FExp.Next() )
985 TopoDS_Face aFace = TopoDS::Face(FExp.Current());
987 if (aChF2ST.Contains(aFace))
988 aST = aChF2ST.FindFromKey(aFace);
992 Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
994 foreach (QString aST, aSTypes)
996 aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1003 Checks that object has 2D presentation. Reimlemented to retun true.
1005 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1011 Find the land cover for the given point
1012 @param thePoint the point laying in some land cover
1013 @param theType the returned type
1014 @return the found land cover's face
1016 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1018 //TODO: some more optimal algorithm
1019 Explorer anIt( *this );
1020 for( ; anIt.More(); anIt.Next() )
1021 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
1023 theType = anIt.StricklerType();
1028 return TopoDS_Face();
1031 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1033 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
1034 foreach( QString anItem, theList )
1035 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
1040 @param theTreatedObjects the map of treated objects
1042 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
1043 MapOfTreatedObjects& theTreatedObjects ) const
1045 QStringList aResList = dumpObjectCreation( theTreatedObjects );
1046 QString aName = GetObjPyName();
1048 QString aShpFileName = thePyScriptPath;
1049 aShpFileName.replace( ".py", ".shp" );
1050 QString aDbfFileName = thePyScriptPath;
1051 aDbfFileName.replace( ".py", ".dbf" );
1053 ExportSHP( aShpFileName, true, 0.1 );
1055 QString anAttr = "CODE_06"; //TODO: some custom choice
1056 QStringList anAttrValues, aTypes;
1057 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
1058 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
1060 aResList << QString( "%1.ImportSHP( '%2' )" ).
1061 arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1063 Dump( "attr_values", anAttrValues, aResList );
1064 Dump( "types", aTypes, aResList );
1065 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
1066 arg( aName ).arg( QFileInfo( aDbfFileName ).fileName() ).arg( anAttr );
1071 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1073 //Shape must be topologically correct
1074 TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1076 for(; anExpF.More(); anExpF.Next() )
1078 TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1080 TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1081 TopTools_ListOfShape anEdgesToRemove;
1083 for(; anExp.More(); anExp.Next() )
1085 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1086 if (CurEdge.Orientation() == TopAbs_INTERNAL)
1087 anEdgesToRemove.Append(CurEdge);
1090 if (!anEdgesToRemove.IsEmpty())
1092 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
1093 TopoDS_Shape OutF = aReshape->Apply(CurFace);
1094 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
1095 for (; aIt.More(); aIt.Next())
1096 aReshape->Remove(aIt.Value());
1097 OutF = aReshape->Apply(CurFace);
1099 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1102 OutF = sfs->Shape();
1103 aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1107 Handle_ShapeBuild_ReShape anExtReshape = new ShapeBuild_ReShape();
1108 for (int i = 1; i <= aF2FReplace->Extent(); i++)
1110 TopoDS_Face aFK = aF2FReplace->FindKey(i);
1111 TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1112 anExtReshape->Replace(aFK, aFV);
1113 ShToRebuild = anExtReshape->Apply(ShToRebuild);
1118 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1120 SetDouble( DataTag_Transparency, theTransparency );
1123 double HYDROData_LandCoverMap::GetTransparency() const
1125 return GetDouble( DataTag_Transparency, 0.5 );
1128 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1129 const QList<int>& theIndices )
1131 HYDROData_ShapeFile anImporter;
1132 QStringList aPolyList;
1133 TopTools_SequenceOfShape aFaces;
1134 int aSHapeType = -1;
1135 int Stat = anImporter.ImportPolygons(HYDROData_Document::Document(1), theSHPFileName, aPolyList, aFaces, aSHapeType);
1140 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1141 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1142 if (maxInd > aPolyList.length())
1145 if (theIndices.empty())
1148 for ( int i = 1; i <=aFaces.Length(); i++ )
1150 TopoDS_Shape aShape = aFaces(i);
1151 if ( aShape.IsNull() )
1153 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1158 //import given indices
1159 foreach ( int Ind, theIndices )
1161 TopoDS_Shape aShape = aFaces(Ind + 1);
1162 if ( aShape.IsNull() )
1164 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1168 StoreLandCovers(aMapFace2ST);
1172 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1174 HYDROData_ShapeFile anExporter;
1176 anExporter.Export(theSHPFileName, this, aList, bUseDiscr, theDefl );
1183 bool HYDROData_LandCoverMap::CheckLinear()
1185 TopoDS_Shape InpShape = GetShape();
1186 TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1187 for (; anEdgeEx.More(); anEdgeEx.Next())
1189 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1191 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
1192 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1195 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1198 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1209 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1211 TopoDS_Shape aShape = GetShape();
1212 TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1213 SetShape( aLocatedShape );