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>
64 #include <HYDROData_LCM_FaceClassifier.h>
70 #include <QTextStream>
74 #include "HYDRO_trace.hxx"
76 const char TELEMAC_FORMAT = 'f';
77 const int TELEMAC_PRECISION = 3;
80 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
81 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
85 @param theMap the land cover map to iterate through
87 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
92 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
104 Initialize the iterator
105 @param theMap the land cover map to iterate through
107 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
109 TopoDS_Shape aShape = theMap.GetShape();
110 if( aShape.IsNull() )
114 myExplorer = new TopExp_Explorer();
115 myExplorer->Init( aShape, TopAbs_FACE );
118 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
119 if( myArray.IsNull() )
122 myIndex = myArray->Lower();
128 HYDROData_LandCoverMap::Explorer::~Explorer()
134 Return the current 0-based index of the iterator
135 @return the current index
137 int HYDROData_LandCoverMap::Explorer::Index() const
139 if( myArray.IsNull() )
142 return myIndex - myArray->Lower();
146 Return if the iterator has more elements
147 @return if the iterator has more elements
149 bool HYDROData_LandCoverMap::Explorer::More() const
151 return !myArray.IsNull() && myExplorer && myExplorer->More();
155 Move iterator to the next element
157 void HYDROData_LandCoverMap::Explorer::Next()
167 Get the current land cover (face)
168 @return the land cover's face
170 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
173 return TopoDS::Face( myExplorer->Current() );
175 return TopoDS_Face();
179 Get the current land cover's Strickler type
180 @return the land cover's Strickler type
182 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
184 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
187 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
191 Set the Strickler type for the current land cover
192 @param theType the Strickler type
194 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
196 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
199 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
205 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
206 : HYDROData_Entity( Geom_No )
213 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
219 @return object's kind
221 const ObjectKind HYDROData_LandCoverMap::GetKind() const
223 return KIND_LAND_COVER_MAP;
226 int HYDROData_LandCoverMap::GetLCCount() const
228 Explorer anIt( *this );
230 for( ; anIt.More(); anIt.Next() )
235 bool HYDROData_LandCoverMap::IsEmpty() const
237 Explorer anIt( *this );
245 Load attributes from DBF File
248 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
249 const QString& theFieldName,
250 const QStringList& theDBFValues,
251 const QStringList& theStricklerTypes,
252 const QList<int>& theIndices )
254 if (theDBFValues.size() != theStricklerTypes.size())
255 return DBFStatus_DIFF_SIZE_ERROR;
256 HYDROData_ShapeFile aDBFImporter;
257 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
258 return DBFStatus_OPEN_FILE_ERROR; //cant open file
260 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
261 int FieldNameIndex = FieldList.indexOf(theFieldName);
262 if (FieldNameIndex == -1)
263 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
265 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
266 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
269 Explorer anIt( *this );
270 for( ; anIt.More(); anIt.Next() )
272 int CurIndex = anIt.Index();
273 int anIndex = CurIndex;
274 if( !theIndices.isEmpty() )
275 anIndex = theIndices[CurIndex];
277 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
278 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
279 if ( StricklerTypesInd != -1)
280 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
287 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
291 Export attributes to DBF File
294 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
295 const QString& theFieldName,
296 const QStringList& theDBFValues,
297 const QStringList& theStricklerTypes) const
299 if (theDBFValues.size() != theStricklerTypes.size())
301 HYDROData_ShapeFile anExporter;
302 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
303 Explorer anIt( *this );
304 for( ; anIt.More(); anIt.Next() )
306 QString CurST = anIt.StricklerType();
307 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
308 aCurAttrV.myIsNull = false;
309 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
310 if (StricklerTypesInd != -1)
312 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
313 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
314 theAttrV.push_back(aCurAttrV);
317 aCurAttrV.myIsNull = true;
319 //use actual str value; not the raw value
320 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
324 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
326 int aHashX = HashCode( thePoint.X(), theUpper );
327 int aHashY = HashCode( thePoint.Y(), theUpper );
328 return (aHashX^aHashY)%theUpper;
331 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
333 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
336 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
337 Standard_Real theDeflection,
338 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
339 QList<int>& theVerticesIds )
341 BRepAdaptor_Curve aCurve( theEdge );
342 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
343 if( !aDiscrete.IsDone() )
346 int n = aDiscrete.NbPoints();
347 for( int i=1; i<=n; i++ )
349 gp_Pnt aPnt = aDiscrete.Value( i );
351 if( theVerticesMap.Contains( aPnt ) )
352 anId = theVerticesMap.FindIndex( aPnt );
355 anId = theVerticesMap.Size();
356 theVerticesMap.Add( aPnt );
358 theVerticesIds.append( anId );
364 Export the land cover map for the solver (Telemac)
365 @param theFileName the name of file
366 @return if the export is successful
368 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
369 double theDeflection,
370 const Handle(HYDROData_StricklerTable)& theTable ) const
372 TopoDS_Shape aLandCoverMapShape = GetShape();
373 TopTools_ListOfShape aListOfFaces;
374 Explorer anIt( *this );
375 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
376 for( ; anIt.More(); anIt.Next() )
378 aListOfFaces.Append( anIt.Face() );
379 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
382 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false, NULL );
384 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
385 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
386 typedef QPair< QString, QList<int> > FaceData;
387 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
389 // add into the map all edges existing in the shell
390 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
391 for( ; anExp1.More(); anExp1.Next() )
393 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
394 QList<int> aVerticesIdsList;
395 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
396 anEdgesMap.Add( anEdge, aVerticesIdsList );
399 // add into the map all faces existing in the shell and correspondence between face and edges ids
400 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
401 for( ; anExp2.More(); anExp2.Next() )
403 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
404 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
405 QList<int> anEdgesIdsList;
406 for( ; anExp3.More(); anExp3.Next() )
408 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
409 int anEdgeId = anEdgesMap.FindIndex( anEdge );
410 anEdgesIdsList.append( anEdgeId );
414 aData.first = aTypesMap[aFace.TShape()];
415 aData.second = anEdgesIdsList;
416 aFacesMap.Add( aFace, aData );
419 QFile aFile( theFileName );
420 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
423 QTextStream aStream( &aFile );
424 aStream << "# nodes\n";
425 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
426 for( ; anIt1.More(); anIt1.Next() )
428 gp_Pnt aPnt = anIt1.Value();
429 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
431 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
433 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
438 aStream << "# edges\n";
439 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
440 for( ; anIt2.More(); anIt2.Next() )
442 QList<int> aVerticesIds = anIt2.Value();
443 foreach( int anId, aVerticesIds )
444 aStream << anId << " ";
449 aStream << "# faces\n";
450 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
451 for( ; anIt3.More(); anIt3.Next() )
453 QString aType = anIt3.Value().first;
454 double aCoeff = theTable->Get( aType, 0.0 );
455 QList<int> anEdgesIds = anIt3.Value().second;
456 //aStream << "\"" << aType << "\" ";
457 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
458 foreach( int anId, anEdgesIds )
459 aStream << anId << " ";
469 Add a new object as land cover
470 @param theObject the object to add as land cover
471 @param theType the Strickler type for the new land cover
472 @return if the addition is successful
474 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
476 if( theObject.IsNull() )
479 TopoDS_Shape aShape = theObject->GetTopShape();
482 if( aShape.ShapeType() ==TopAbs_FACE )
484 aFace = TopoDS::Face(aShape);
486 else if ( aShape.ShapeType() ==TopAbs_COMPOUND )
488 TopoDS_Iterator It(aShape);
489 for (; It.More(); It.Next())
490 if (It.Value().ShapeType() == TopAbs_FACE)
492 aFace = TopoDS::Face(It.Value());
499 return LocalPartition( aFace, theType );
502 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
504 if( !theWire.Closed() )
507 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
508 return LocalPartition( aFace, theType );
512 Add a new polyline as land cover
513 @param thePolyline the polyline to add as land cover
514 @param theType the Strickler type for the new land cover
515 @return if the addition is successful
517 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
519 if( thePolyline.IsNull() )
522 TopoDS_Shape aShape = thePolyline->GetShape();
523 if( aShape.ShapeType()==TopAbs_WIRE )
524 return Add( TopoDS::Wire( aShape ), theType );
526 if( aShape.ShapeType()==TopAbs_COMPOUND )
528 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
529 for( ; anExp.More(); anExp.Next() )
531 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
532 if( !Add( aPart, theType ) )
542 Remove the given face from land cover map
543 @param theFace the face to be removed
544 @return if the removing is successful
546 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
548 TopTools_ListOfShape aList;
549 aList.Append( theFace );
550 return Remove( aList );
554 Remove the given faces from land cover map
555 @param theFacesToRemove the face list to be removed
556 @return if the removing is successful
558 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
560 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
561 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
562 for( ; aFIt.More(); aFIt.Next() )
564 TopoDS_Shape aShape = aFIt.Value();
565 if( aShape.ShapeType()==TopAbs_FACE )
566 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
569 Explorer anIt( *this );
570 for( ; anIt.More(); anIt.Next() )
571 if( !aFacesToRemove.Contains( anIt.Face() ) )
572 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
574 if ( aNewFaces.IsEmpty() )
577 StoreLandCovers( aNewFaces );
582 Split the land cover map by the given polyline
583 @param thePolyline the tool polyline to split the land cover map
584 @return if the removing is successful
586 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
588 if( thePolyline.IsNull() )
591 TopoDS_Shape aShape = thePolyline->GetShape();
592 return Split( aShape );
597 Split the land cover map by the given polyline
598 @param theShape the tool polyline to split the land cover map
599 @return if the removing is successful
601 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
603 int aNbCL = GetLCCount();
604 bool aResult = LocalPartition( theShape, "" );
605 return aResult && aNbCL != GetLCCount();
610 Merge the given faces in the land cover
611 @param theFaces the faces to merge in the land cover map
612 @param theType the Strickler type for the merged land cover
613 @return if the merge is successful
615 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
617 // 1. to fuse the faces into the new face
618 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true, NULL );
620 if( !aMergedFace.IsNull() )
622 // 2. to remove the merged faces from the current map
624 TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
625 for( ; Exp.More(); Exp.Next() )
627 const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
628 // 3. to add the face into the map
629 aStat = aStat && LocalPartition( aCF, theType );
638 Merge the given faces into the shell/face
639 @param theFaces the faces to merge
640 @param IsToUnify if the common edges should be removed (fused)
641 @param theTolerance the operation's tolerance
642 @return result shape (face or shell)
645 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
647 TopTools_IndexedDataMapOfShapeListOfShape* theShHistory,
650 //DEBTRACE("MergeFaces");
652 TopTools_ListIteratorOfListOfShape anIt;
653 BOPCol_ListOfShape aLC;
654 anIt.Initialize(theFaces);
655 for( ; anIt.More(); anIt.Next() )
657 if (anIt.Value().ShapeType() != TopAbs_FACE)
658 return TopoDS_Shape();
659 aLC.Append( anIt.Value() );
662 BOPAlgo_PaveFiller aPF;
663 aPF.SetArguments( aLC );
664 aPF.SetRunParallel( Standard_False );
665 aPF.SetFuzzyValue( theTolerance );
668 anError = aPF.ErrorStatus();
670 return TopoDS_Shape();
672 BOPAlgo_Builder anAlgo;
673 anIt.Initialize( theFaces );
674 for( ; anIt.More(); anIt.Next() )
675 anAlgo.AddArgument( anIt.Value() );
677 anAlgo.PerformWithFiller( aPF );
678 anError = anAlgo.ErrorStatus();
680 return TopoDS_Shape();
682 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
684 // retrieve history of modifications
687 theShHistory->Clear();
688 anIt.Initialize(theFaces);
689 for( ; anIt.More(); anIt.Next() )
691 const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
692 theShHistory->Add(anIt.Value(), aMLS);
697 BRep_Builder aBuilder;
699 aBuilder.MakeShell( aShell );
700 aShell.Closed( Standard_False );
701 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
702 for( ; anExplorer.More(); anExplorer.Next() )
704 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
707 if( aFace.ShapeType() == TopAbs_FACE )
709 aBuilder.Add( aShell, aFace );
710 aShell.Closed( Standard_False );
714 TopoDS_Shape aResult;
717 ShapeUpgrade_UnifySameDomain aUSD;
718 aUSD.Initialize( aShell );
720 aResult = aUSD.Shape();
725 anExplorer.Init( aResult, TopAbs_FACE );
727 TopoDS_Face anOneFace;
728 for( ; anExplorer.More(); anExplorer.Next(), n++ )
729 anOneFace = TopoDS::Face( anExplorer.Current() );
733 else if (aResult.ShapeType() == TopAbs_SHELL)
735 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
736 if (aBCS.Status().First() != BRepCheck_NoError)
738 ShapeFix_Shell aFixer;
739 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
740 aResult = aFixer.Shape();
747 Change Strickler type for the list of faces to the given one
748 @param theFaces the faces to change type
749 @param theType the Strickler type for the given land cover(s)
750 @return if the change type operation is successful
752 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
754 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
755 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
756 for( ; aFIt.More(); aFIt.Next() )
758 TopoDS_Shape aShape = aFIt.Value();
759 if( aShape.ShapeType()==TopAbs_FACE )
760 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
764 Explorer anIt( *this );
765 for( ; anIt.More(); anIt.Next() )
766 if( aFacesToChangeType.Contains( anIt.Face() ) )
768 anIt.SetStricklerType( theType );
771 if ( aNbChanges != theFaces.Extent() )
778 Get the shape of the land cover map
780 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
782 return HYDROData_Entity::GetShape( DataTag_Shape );
786 Get Strickler type of the given land cover
787 @param theLandCover the land cover to get Strickler type of
788 @return name of Strickler type
790 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
794 Explorer anIt( *this );
795 for( ; anIt.More(); anIt.Next() )
796 if( anIt.Face().IsEqual( theLandCover) )
798 aType = anIt.StricklerType();
806 Set the shape of the land cover map
807 @param theShape the new shape for the land cover map
809 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
811 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
815 Perform the local partition algorithm on the land cover
816 @param theNewShape the new shape to add into the land cover
817 @param theNewType the new Strickler type for the new land cover
818 @return if the local partition is successful
820 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
822 if( theNewShape.IsNull() )
825 BOPCol_ListOfShape aShapesList;
826 BOPAlgo_PaveFiller aPaveFiller;
827 HYDROData_MapOfFaceToStricklerType aNewFaces;
829 // add faces to shapes list
830 Explorer anIt( *this );
831 for( ; anIt.More(); anIt.Next() )
832 aShapesList.Append( anIt.Face() );
833 aShapesList.Append( theNewShape );
835 //DEBTRACE("theNewType " << theNewType);
836 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
838 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
839 StoreLandCovers( aNewFaces );
843 // prepare pave filler
844 aPaveFiller.SetArguments( aShapesList );
845 aPaveFiller.Perform();
846 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() << " " << anIt.Face());
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() );
889 //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
891 TopTools_ListIteratorOfListOfShape aMIt( aModified );
892 for( ; aMIt.More(); aMIt.Next() )
894 TopoDS_Shape aShape = aMIt.Value();
895 bool isFace = aShape.ShapeType()==TopAbs_FACE;
896 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
897 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
898 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
899 if( isFace && !isAlsoFromNew )
900 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
902 //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
904 // c. add the new shape if it is face with its type
905 if( theNewShape.ShapeType()==TopAbs_FACE )
906 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
907 //DEBTRACE(theNewShape << " " << theNewType);
909 // convert map of shape to type to compound and list of types
910 StoreLandCovers( aNewFaces );
912 // anIt.Init( *this );
913 // for( ; anIt.More(); anIt.Next() )
915 // DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
921 Replace the set of land covers in the land cover map
922 @param theMap the map of shape (face) to Strickler type (string)
924 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
926 TopTools_ListOfShape aListOfFaces;
928 //DEBTRACE("theMap.Extent() " << theMap.Extent());
929 for( int i = 1; i <= theMap.Extent(); i++ )
931 TopoDS_Face aFace = theMap.FindKey(i);
934 aListOfFaces.Append(aFace);
937 TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
940 TopoDS_Shape aResult;
941 if( aListOfFaces.Extent() == 1 )
942 aResult = aListOfFaces.First();
943 else if( aListOfFaces.Extent() > 1 )
944 aResult = MergeFaces( aListOfFaces, false, &ShHistory );
946 //remove internal edges
947 //if nothing changes => the result shape should be the same
948 //hence the map will be empty
950 NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
951 RemoveInternal(aResult, &ShF2FHistory);
953 //one face => mark as unchanged
954 if( aListOfFaces.Extent() == 1 )
955 ShHistory.Add(aResult, TopTools_ListOfShape());
957 NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
960 for( int i = 1; i <= theMap.Extent(); i++ )
962 TopoDS_Face aFF = theMap.FindKey(i);
963 //DEBTRACE(" --- " << aFF);
966 //DEBTRACE(ShHistory.IsEmpty());
967 //DEBTRACE(aFF.Checked());
968 TopTools_ListOfShape aLS;
971 aLS = ShHistory.FindFromKey(aFF); //TODO: bug to fix. Observed on an incomplete split of a face
975 DEBTRACE("TODO: bug to fix. Observed on an incomplete split of a face");
976 //continue; // No, keep aLS empty and propagate the type of the original face
980 //DEBTRACE("--- aLS.IsEmpty()");
981 QString aSType = theMap.FindFromKey(aFF);
982 //DEBTRACE(" --- " << aSType.toStdString());
983 if (ShF2FHistory.Contains(aFF))
985 //DEBTRACE("ShF2FHistory.FindFromKey(aFF) " << ShF2FHistory.FindFromKey(aFF));
986 aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
990 //DEBTRACE("aFF " << aFF);
991 aChF2ST.Add(aFF, aSType);
996 //DEBTRACE("--- !aLS.IsEmpty()");
997 TopTools_ListIteratorOfListOfShape anIt(aLS);
998 for (; anIt.More(); anIt.Next())
1000 QString aSType = theMap.FindFromKey(aFF);
1001 //DEBTRACE(" --- " << aSType.toStdString());
1002 const TopoDS_Face& aMF = TopoDS::Face(anIt.Value());
1003 //if (ShF2FHistory.Contains(aFF))
1004 if (ShF2FHistory.Contains(aMF))
1006 //DEBTRACE("ShF2FHistory.FindFromKey(aMF) " << ShF2FHistory.FindFromKey(aFF));
1007 aChF2ST.Add(ShF2FHistory.FindFromKey(aMF), aSType);
1011 //DEBTRACE("aMF " << aMF);
1012 aChF2ST.Add(aMF, aSType);
1018 SetShape( aResult );
1020 //Explorer Exp(*this);
1021 TopExp_Explorer FExp(aResult, TopAbs_FACE);
1022 for( ; FExp.More(); FExp.Next() )
1024 TopoDS_Face aFace = TopoDS::Face(FExp.Current());
1026 if (aChF2ST.Contains(aFace))
1027 aST = aChF2ST.FindFromKey(aFace);
1028 //DEBTRACE("aFace " << aFace << " aST " << aST.toStdString());
1032 Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
1034 foreach (QString aST, aSTypes)
1036 //DEBTRACE("aST " << aST.toStdString());
1037 aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1044 Checks that object has 2D presentation. Reimlemented to retun true.
1046 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1052 Find the land cover for the given point
1053 @param thePoint the point laying in some land cover
1054 @param theType the returned type
1055 @return the found land cover's face
1057 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1059 //TODO: some more optimal algorithm
1060 Explorer anIt( *this );
1061 for( ; anIt.More(); anIt.Next() )
1062 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
1064 theType = anIt.StricklerType();
1069 return TopoDS_Face();
1072 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1074 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
1075 foreach( QString anItem, theList )
1076 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
1081 @param theTreatedObjects the map of treated objects
1083 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
1084 MapOfTreatedObjects& theTreatedObjects ) const
1086 QStringList aResList = dumpObjectCreation( theTreatedObjects );
1087 QString aName = GetObjPyName();
1089 QString aShpFileName = thePyScriptPath;
1090 aShpFileName.replace( ".py", ".shp" );
1091 QString aDbfFileName = thePyScriptPath;
1092 aDbfFileName.replace( ".py", ".dbf" );
1094 ExportSHP( aShpFileName, true, 0.1 );
1096 QString anAttr = "CODE_06"; //TODO: some custom choice
1097 QStringList anAttrValues, aTypes;
1098 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
1099 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
1101 aResList << QString( "%1.ImportSHP( '%2' )" ).
1102 arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1104 Dump( "attr_values", anAttrValues, aResList );
1105 Dump( "types", aTypes, aResList );
1106 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
1107 arg( aName ).arg( QFileInfo( aDbfFileName ).fileName() ).arg( anAttr );
1112 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1114 //DEBTRACE("RemoveInternal");
1115 //Shape must be topologically correct
1116 TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1118 for(; anExpF.More(); anExpF.Next() )
1120 TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1122 TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1123 TopTools_ListOfShape anEdgesToRemove;
1125 for(; anExp.More(); anExp.Next() )
1127 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1128 if (CurEdge.Orientation() == TopAbs_INTERNAL)
1129 anEdgesToRemove.Append(CurEdge);
1132 if (!anEdgesToRemove.IsEmpty())
1134 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
1135 TopoDS_Shape OutF = aReshape->Apply(CurFace);
1136 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
1137 for (; aIt.More(); aIt.Next())
1138 aReshape->Remove(aIt.Value());
1139 OutF = aReshape->Apply(CurFace);
1141 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1144 OutF = sfs->Shape();
1145 aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1149 Handle_ShapeBuild_ReShape anExtReshape = new ShapeBuild_ReShape();
1150 for (int i = 1; i <= aF2FReplace->Extent(); i++)
1152 TopoDS_Face aFK = aF2FReplace->FindKey(i);
1153 TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1154 anExtReshape->Replace(aFK, aFV);
1155 ShToRebuild = anExtReshape->Apply(ShToRebuild);
1157 // for (int i = 1; i <= aF2FReplace->Extent(); i++)
1159 // DEBTRACE("aF2FReplace key,value " << aF2FReplace->FindKey(i) << " " << aF2FReplace->FindFromIndex(i));
1164 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1166 SetDouble( DataTag_Transparency, theTransparency );
1169 double HYDROData_LandCoverMap::GetTransparency() const
1171 return GetDouble( DataTag_Transparency, 0.5 );
1174 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1175 const QList<int>& theIndices )
1177 HYDROData_ShapeFile anImporter;
1178 QStringList aPolyList;
1179 TopTools_SequenceOfShape aFaces;
1180 int aSHapeType = -1;
1181 int Stat = anImporter.ImportPolygons(HYDROData_Document::Document(1), theSHPFileName, aPolyList, aFaces, aSHapeType);
1186 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1187 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1188 if (maxInd > aPolyList.length())
1191 if (theIndices.empty())
1194 for ( int i = 1; i <=aFaces.Length(); i++ )
1196 TopoDS_Shape aShape = aFaces(i);
1197 if ( aShape.IsNull() )
1199 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1204 //import given indices
1205 foreach ( int Ind, theIndices )
1207 TopoDS_Shape aShape = aFaces(Ind + 1);
1208 if ( aShape.IsNull() )
1210 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1214 StoreLandCovers(aMapFace2ST);
1218 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1220 HYDROData_ShapeFile anExporter;
1222 anExporter.Export(HYDROData_Document::Document(1), theSHPFileName, this, aList, bUseDiscr, theDefl );
1229 bool HYDROData_LandCoverMap::CheckLinear()
1231 TopoDS_Shape InpShape = GetShape();
1232 TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1233 for (; anEdgeEx.More(); anEdgeEx.Next())
1235 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1237 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
1238 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1241 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1244 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1255 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1257 TopoDS_Shape aShape = GetShape();
1258 TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1259 SetShape( aLocatedShape );
1262 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_Pnt2d>& thePoints, std::vector<std::set <QString> >& theTypes ) const
1264 HYDROData_LCM_FaceClassifier FC(this);
1265 FC.Classify(thePoints, theTypes, NULL);
1268 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_Pnt2d>& thePoints,
1269 Handle(HYDROData_StricklerTable) theTable,
1270 std::vector<double>& theCoeffs, double DefValue, bool UseMax ) const
1272 std::vector<std::set <QString> > Types;
1273 HYDROData_LCM_FaceClassifier FC(this);
1274 FC.Classify(thePoints, Types, NULL);
1275 theCoeffs.resize(thePoints.size());
1276 for (size_t i = 0; i < Types.size(); i++)
1278 const std::set<QString>& SStr = Types[i];
1280 theCoeffs[i] = DefValue;
1283 std::set<QString>::const_iterator it;
1284 std::vector<double> C1(SStr.size());
1285 for (it = SStr.begin(); it != SStr.end(); ++it)
1286 C1.push_back(theTable->Get( *it, DefValue ));
1289 Val = *(std::max_element(std::begin(C1), std::end(C1)));
1291 Val = *(std::min_element(std::begin(C1), std::end(C1)));