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>
71 #include <QTextStream>
75 #include "HYDRO_trace.hxx"
77 const char TELEMAC_FORMAT = 'f';
78 const int TELEMAC_PRECISION = 3;
81 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
82 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
86 @param theMap the land cover map to iterate through
88 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
93 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
105 Initialize the iterator
106 @param theMap the land cover map to iterate through
108 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
110 TopoDS_Shape aShape = theMap.GetShape();
111 if( aShape.IsNull() )
115 myExplorer = new TopExp_Explorer();
116 myExplorer->Init( aShape, TopAbs_FACE );
119 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
120 if( myArray.IsNull() )
123 myIndex = myArray->Lower();
129 HYDROData_LandCoverMap::Explorer::~Explorer()
135 Return the current 0-based index of the iterator
136 @return the current index
138 int HYDROData_LandCoverMap::Explorer::Index() const
140 if( myArray.IsNull() )
143 return myIndex - myArray->Lower();
147 Return if the iterator has more elements
148 @return if the iterator has more elements
150 bool HYDROData_LandCoverMap::Explorer::More() const
152 return !myArray.IsNull() && myExplorer && myExplorer->More();
156 Move iterator to the next element
158 void HYDROData_LandCoverMap::Explorer::Next()
168 Get the current land cover (face)
169 @return the land cover's face
171 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
174 return TopoDS::Face( myExplorer->Current() );
176 return TopoDS_Face();
180 Get the current land cover's Strickler type
181 @return the land cover's Strickler type
183 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
185 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
188 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
192 Set the Strickler type for the current land cover
193 @param theType the Strickler type
195 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
197 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
200 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
206 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
207 : HYDROData_Entity( Geom_No )
214 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
220 @return object's kind
222 const ObjectKind HYDROData_LandCoverMap::GetKind() const
224 return KIND_LAND_COVER_MAP;
227 int HYDROData_LandCoverMap::GetLCCount() const
229 Explorer anIt( *this );
231 for( ; anIt.More(); anIt.Next() )
236 bool HYDROData_LandCoverMap::IsEmpty() const
238 Explorer anIt( *this );
246 Load attributes from DBF File
249 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
250 const QString& theFieldName,
251 const QStringList& theDBFValues,
252 const QStringList& theStricklerTypes,
253 const QList<int>& theIndices )
255 if (theDBFValues.size() != theStricklerTypes.size())
256 return DBFStatus_DIFF_SIZE_ERROR;
257 HYDROData_ShapeFile aDBFImporter;
258 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
259 return DBFStatus_OPEN_FILE_ERROR; //cant open file
261 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
262 int FieldNameIndex = FieldList.indexOf(theFieldName);
263 if (FieldNameIndex == -1)
264 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
266 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
267 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
270 Explorer anIt( *this );
271 for( ; anIt.More(); anIt.Next() )
273 int CurIndex = anIt.Index();
274 int anIndex = CurIndex;
275 if( !theIndices.isEmpty() )
276 anIndex = theIndices[CurIndex];
278 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
279 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
280 if ( StricklerTypesInd != -1)
281 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
288 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
292 Export attributes to DBF File
295 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
296 const QString& theFieldName,
297 const QStringList& theDBFValues,
298 const QStringList& theStricklerTypes) const
300 if (theDBFValues.size() != theStricklerTypes.size())
302 HYDROData_ShapeFile anExporter;
303 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
304 Explorer anIt( *this );
305 for( ; anIt.More(); anIt.Next() )
307 QString CurST = anIt.StricklerType();
308 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
309 aCurAttrV.myIsNull = false;
310 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
311 if (StricklerTypesInd != -1)
313 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
314 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
315 theAttrV.push_back(aCurAttrV);
318 aCurAttrV.myIsNull = true;
320 //use actual str value; not the raw value
321 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
325 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
327 int aHashX = HashCode( thePoint.X(), theUpper );
328 int aHashY = HashCode( thePoint.Y(), theUpper );
329 return (aHashX^aHashY)%theUpper;
332 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
334 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
337 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
338 Standard_Real theDeflection,
339 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
340 QList<int>& theVerticesIds )
342 BRepAdaptor_Curve aCurve( theEdge );
343 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
344 if( !aDiscrete.IsDone() )
347 int n = aDiscrete.NbPoints();
348 for( int i=1; i<=n; i++ )
350 gp_Pnt aPnt = aDiscrete.Value( i );
352 if( theVerticesMap.Contains( aPnt ) )
353 anId = theVerticesMap.FindIndex( aPnt );
356 anId = theVerticesMap.Size();
357 theVerticesMap.Add( aPnt );
359 theVerticesIds.append( anId );
365 Export the land cover map for the solver (Telemac)
366 @param theFileName the name of file
367 @return if the export is successful
369 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
370 double theDeflection,
371 const Handle(HYDROData_StricklerTable)& theTable,
372 QString& statMessage) const
374 TopoDS_Shape aLandCoverMapShape = GetShape();
375 TopTools_ListOfShape aListOfFaces;
376 Explorer anIt( *this );
377 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
378 for( ; anIt.More(); anIt.Next() )
380 aListOfFaces.Append( anIt.Face() );
381 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
384 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false, NULL );
386 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
387 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
388 typedef QPair< QString, QList<int> > FaceData;
389 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
391 // add into the map all edges existing in the shell
392 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
393 for( ; anExp1.More(); anExp1.Next() )
395 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
396 QList<int> aVerticesIdsList;
397 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
398 anEdgesMap.Add( anEdge, aVerticesIdsList );
401 // add into the map all faces existing in the shell and correspondence between face and edges ids
402 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
403 for( ; anExp2.More(); anExp2.Next() )
405 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
406 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
407 QList<int> anEdgesIdsList;
408 for( ; anExp3.More(); anExp3.Next() )
410 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
411 int anEdgeId = anEdgesMap.FindIndex( anEdge );
412 anEdgesIdsList.append( anEdgeId );
416 aData.first = aTypesMap[aFace.TShape()];
417 aData.second = anEdgesIdsList;
418 aFacesMap.Add( aFace, aData );
421 QFile aFile( theFileName );
422 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
424 QString homeFilePath = QDir::home().absoluteFilePath( theFileName );
425 aFile.setFileName(homeFilePath);
426 if (aFile.open( QFile::WriteOnly | QFile::Text ) )
427 statMessage = "Telemac file have been exported to the home directory: " + homeFilePath;
433 QString absFilePath = QDir::current().absoluteFilePath( theFileName );
434 statMessage = "Telemac file have been exported to the current directory: " + absFilePath;
437 QTextStream aStream( &aFile );
438 aStream << "# nodes\n";
439 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
440 for( ; anIt1.More(); anIt1.Next() )
442 gp_Pnt aPnt = anIt1.Value();
443 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
445 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
447 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
452 aStream << "# edges\n";
453 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
454 for( ; anIt2.More(); anIt2.Next() )
456 QList<int> aVerticesIds = anIt2.Value();
457 foreach( int anId, aVerticesIds )
458 aStream << anId << " ";
463 aStream << "# faces\n";
464 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
465 for( ; anIt3.More(); anIt3.Next() )
467 QString aType = anIt3.Value().first;
468 double aCoeff = theTable->Get( aType, 0.0 );
469 QList<int> anEdgesIds = anIt3.Value().second;
470 //aStream << "\"" << aType << "\" ";
471 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
472 foreach( int anId, anEdgesIds )
473 aStream << anId << " ";
483 Add a new object as land cover
484 @param theObject the object to add as land cover
485 @param theType the Strickler type for the new land cover
486 @return if the addition is successful
488 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
490 if( theObject.IsNull() )
493 TopoDS_Shape aShape = theObject->GetTopShape();
496 if( aShape.ShapeType() ==TopAbs_FACE )
498 aFace = TopoDS::Face(aShape);
500 else if ( aShape.ShapeType() ==TopAbs_COMPOUND )
502 TopoDS_Iterator It(aShape);
503 for (; It.More(); It.Next())
504 if (It.Value().ShapeType() == TopAbs_FACE)
506 aFace = TopoDS::Face(It.Value());
513 return LocalPartition( aFace, theType );
516 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
518 if( !theWire.Closed() )
521 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
522 return LocalPartition( aFace, theType );
526 Add a new polyline as land cover
527 @param thePolyline the polyline to add as land cover
528 @param theType the Strickler type for the new land cover
529 @return if the addition is successful
531 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
533 if( thePolyline.IsNull() )
536 TopoDS_Shape aShape = thePolyline->GetShape();
537 if( aShape.ShapeType()==TopAbs_WIRE )
538 return Add( TopoDS::Wire( aShape ), theType );
540 if( aShape.ShapeType()==TopAbs_COMPOUND )
542 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
543 for( ; anExp.More(); anExp.Next() )
545 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
546 if( !Add( aPart, theType ) )
556 Remove the given face from land cover map
557 @param theFace the face to be removed
558 @return if the removing is successful
560 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
562 TopTools_ListOfShape aList;
563 aList.Append( theFace );
564 return Remove( aList );
568 Remove the given faces from land cover map
569 @param theFacesToRemove the face list to be removed
570 @return if the removing is successful
572 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
574 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
575 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
576 for( ; aFIt.More(); aFIt.Next() )
578 TopoDS_Shape aShape = aFIt.Value();
579 if( aShape.ShapeType()==TopAbs_FACE )
580 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
583 Explorer anIt( *this );
584 for( ; anIt.More(); anIt.Next() )
585 if( !aFacesToRemove.Contains( anIt.Face() ) )
586 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
588 if ( aNewFaces.IsEmpty() )
591 StoreLandCovers( aNewFaces );
596 Split the land cover map by the given polyline
597 @param thePolyline the tool polyline to split the land cover map
598 @return if the removing is successful
600 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
602 if( thePolyline.IsNull() )
605 TopoDS_Shape aShape = thePolyline->GetShape();
606 return Split( aShape );
611 Split the land cover map by the given polyline
612 @param theShape the tool polyline to split the land cover map
613 @return if the removing is successful
615 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
617 int aNbCL = GetLCCount();
618 bool aResult = LocalPartition( theShape, "" );
619 return aResult && aNbCL != GetLCCount();
624 Merge the given faces in the land cover
625 @param theFaces the faces to merge in the land cover map
626 @param theType the Strickler type for the merged land cover
627 @return if the merge is successful
629 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
631 // 1. to fuse the faces into the new face
632 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true, NULL );
634 if( !aMergedFace.IsNull() )
636 // 2. to remove the merged faces from the current map
638 TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
639 for( ; Exp.More(); Exp.Next() )
641 const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
642 // 3. to add the face into the map
643 aStat = aStat && LocalPartition( aCF, theType );
652 Merge the given faces into the shell/face
653 @param theFaces the faces to merge
654 @param IsToUnify if the common edges should be removed (fused)
655 @param theTolerance the operation's tolerance
656 @return result shape (face or shell)
659 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
661 TopTools_IndexedDataMapOfShapeListOfShape* theShHistory,
664 //DEBTRACE("MergeFaces");
666 TopTools_ListIteratorOfListOfShape anIt;
667 BOPCol_ListOfShape aLC;
668 anIt.Initialize(theFaces);
669 for( ; anIt.More(); anIt.Next() )
671 if (anIt.Value().ShapeType() != TopAbs_FACE)
672 return TopoDS_Shape();
673 aLC.Append( anIt.Value() );
676 BOPAlgo_PaveFiller aPF;
677 aPF.SetArguments( aLC );
678 aPF.SetRunParallel( Standard_False );
679 aPF.SetFuzzyValue( theTolerance );
682 anError = aPF.ErrorStatus();
684 return TopoDS_Shape();
686 BOPAlgo_Builder anAlgo;
687 anIt.Initialize( theFaces );
688 for( ; anIt.More(); anIt.Next() )
689 anAlgo.AddArgument( anIt.Value() );
691 anAlgo.PerformWithFiller( aPF );
692 anError = anAlgo.ErrorStatus();
694 return TopoDS_Shape();
696 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
698 // retrieve history of modifications
701 theShHistory->Clear();
702 anIt.Initialize(theFaces);
703 for( ; anIt.More(); anIt.Next() )
705 const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
706 theShHistory->Add(anIt.Value(), aMLS);
711 BRep_Builder aBuilder;
713 aBuilder.MakeShell( aShell );
714 aShell.Closed( Standard_False );
715 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
716 for( ; anExplorer.More(); anExplorer.Next() )
718 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
721 if( aFace.ShapeType() == TopAbs_FACE )
723 aBuilder.Add( aShell, aFace );
724 aShell.Closed( Standard_False );
728 TopoDS_Shape aResult;
731 ShapeUpgrade_UnifySameDomain aUSD;
732 aUSD.Initialize( aShell );
734 aResult = aUSD.Shape();
739 anExplorer.Init( aResult, TopAbs_FACE );
741 TopoDS_Face anOneFace;
742 for( ; anExplorer.More(); anExplorer.Next(), n++ )
743 anOneFace = TopoDS::Face( anExplorer.Current() );
747 else if (aResult.ShapeType() == TopAbs_SHELL)
749 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
750 if (aBCS.Status().First() != BRepCheck_NoError)
752 ShapeFix_Shell aFixer;
753 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
754 aResult = aFixer.Shape();
761 Change Strickler type for the list of faces to the given one
762 @param theFaces the faces to change type
763 @param theType the Strickler type for the given land cover(s)
764 @return if the change type operation is successful
766 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
768 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
769 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
770 for( ; aFIt.More(); aFIt.Next() )
772 TopoDS_Shape aShape = aFIt.Value();
773 if( aShape.ShapeType()==TopAbs_FACE )
774 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
778 Explorer anIt( *this );
779 for( ; anIt.More(); anIt.Next() )
780 if( aFacesToChangeType.Contains( anIt.Face() ) )
782 anIt.SetStricklerType( theType );
785 if ( aNbChanges != theFaces.Extent() )
792 Get the shape of the land cover map
794 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
796 return HYDROData_Entity::GetShape( DataTag_Shape );
800 Get Strickler type of the given land cover
801 @param theLandCover the land cover to get Strickler type of
802 @return name of Strickler type
804 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
808 Explorer anIt( *this );
809 for( ; anIt.More(); anIt.Next() )
810 if( anIt.Face().IsEqual( theLandCover) )
812 aType = anIt.StricklerType();
820 Set the shape of the land cover map
821 @param theShape the new shape for the land cover map
823 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
825 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
829 Perform the local partition algorithm on the land cover
830 @param theNewShape the new shape to add into the land cover
831 @param theNewType the new Strickler type for the new land cover
832 @return if the local partition is successful
834 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
836 if( theNewShape.IsNull() )
839 BOPCol_ListOfShape aShapesList;
840 BOPAlgo_PaveFiller aPaveFiller;
841 HYDROData_MapOfFaceToStricklerType aNewFaces;
843 // add faces to shapes list
844 Explorer anIt( *this );
845 for( ; anIt.More(); anIt.Next() )
846 aShapesList.Append( anIt.Face() );
847 aShapesList.Append( theNewShape );
849 //DEBTRACE("theNewType " << theNewType);
850 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
852 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
853 StoreLandCovers( aNewFaces );
857 // prepare pave filler
858 aPaveFiller.SetArguments( aShapesList );
859 aPaveFiller.Perform();
860 Standard_Integer anError = aPaveFiller.ErrorStatus();
864 // add faces to builder
865 BOPAlgo_Builder aBuilder;
867 for( ; anIt.More(); anIt.Next() )
868 aBuilder.AddArgument( anIt.Face() );
869 aBuilder.AddArgument( theNewShape );
871 // perform the partition with the pave filler
872 aBuilder.PerformWithFiller( aPaveFiller );
873 anError = aBuilder.ErrorStatus();
877 //std::cout << "History:" << std::endl;
878 // analysis of the history
879 // a. to fill map of shapes which come from the new face
880 NCollection_IndexedMap<int> aShapesFromNewFace;
881 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
882 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
883 TopTools_ListIteratorOfListOfShape aMIt( aModified );
884 for( ; aMIt.More(); aMIt.Next() )
886 //std::cout << " " << aMIt.Value() << std::endl;
887 //DEBTRACE(aMIt.Value());
888 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
889 aShapesFromNewFace.Add( aKey );
892 // b. to fill map of parts except parts from new face
894 for( ; anIt.More(); anIt.Next() )
896 QString aSType = anIt.StricklerType();
897 //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
898 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
899 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
901 if( aModified.Extent() == 0 )
902 aModified.Append( anIt.Face() );
903 //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
905 TopTools_ListIteratorOfListOfShape aMIt( aModified );
906 for( ; aMIt.More(); aMIt.Next() )
908 TopoDS_Shape aShape = aMIt.Value();
909 bool isFace = aShape.ShapeType()==TopAbs_FACE;
910 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
911 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
912 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
913 if( isFace && !isAlsoFromNew )
914 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
916 //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
918 // c. add the new shape if it is face with its type
919 if( theNewShape.ShapeType()==TopAbs_FACE )
920 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
921 //DEBTRACE(theNewShape << " " << theNewType);
923 // convert map of shape to type to compound and list of types
924 StoreLandCovers( aNewFaces );
926 // anIt.Init( *this );
927 // for( ; anIt.More(); anIt.Next() )
929 // DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
935 Replace the set of land covers in the land cover map
936 @param theMap the map of shape (face) to Strickler type (string)
938 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
940 TopTools_ListOfShape aListOfFaces;
942 //DEBTRACE("theMap.Extent() " << theMap.Extent());
943 for( int i = 1; i <= theMap.Extent(); i++ )
945 TopoDS_Face aFace = theMap.FindKey(i);
948 aListOfFaces.Append(aFace);
951 TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
954 TopoDS_Shape aResult;
955 if( aListOfFaces.Extent() == 1 )
956 aResult = aListOfFaces.First();
957 else if( aListOfFaces.Extent() > 1 )
958 aResult = MergeFaces( aListOfFaces, false, &ShHistory );
960 //remove internal edges
961 //if nothing changes => the result shape should be the same
962 //hence the map will be empty
964 NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
965 RemoveInternal(aResult, &ShF2FHistory);
967 //one face => mark as unchanged
968 if( aListOfFaces.Extent() == 1 )
969 ShHistory.Add(aResult, TopTools_ListOfShape());
971 NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
974 for( int i = 1; i <= theMap.Extent(); i++ )
976 TopoDS_Face aFF = theMap.FindKey(i);
977 //DEBTRACE(" --- " << aFF);
980 //DEBTRACE(ShHistory.IsEmpty());
981 //DEBTRACE(aFF.Checked());
982 TopTools_ListOfShape aLS;
985 aLS = ShHistory.FindFromKey(aFF); //TODO: bug to fix. Observed on an incomplete split of a face
989 DEBTRACE("TODO: bug to fix. Observed on an incomplete split of a face");
990 //continue; // No, keep aLS empty and propagate the type of the original face
994 //DEBTRACE("--- aLS.IsEmpty()");
995 QString aSType = theMap.FindFromKey(aFF);
996 //DEBTRACE(" --- " << aSType.toStdString());
997 if (ShF2FHistory.Contains(aFF))
999 //DEBTRACE("ShF2FHistory.FindFromKey(aFF) " << ShF2FHistory.FindFromKey(aFF));
1000 aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
1004 //DEBTRACE("aFF " << aFF);
1005 aChF2ST.Add(aFF, aSType);
1010 //DEBTRACE("--- !aLS.IsEmpty()");
1011 TopTools_ListIteratorOfListOfShape anIt(aLS);
1012 for (; anIt.More(); anIt.Next())
1014 QString aSType = theMap.FindFromKey(aFF);
1015 //DEBTRACE(" --- " << aSType.toStdString());
1016 const TopoDS_Face& aMF = TopoDS::Face(anIt.Value());
1017 //if (ShF2FHistory.Contains(aFF))
1018 if (ShF2FHistory.Contains(aMF))
1020 //DEBTRACE("ShF2FHistory.FindFromKey(aMF) " << ShF2FHistory.FindFromKey(aFF));
1021 aChF2ST.Add(ShF2FHistory.FindFromKey(aMF), aSType);
1025 //DEBTRACE("aMF " << aMF);
1026 aChF2ST.Add(aMF, aSType);
1032 SetShape( aResult );
1034 //Explorer Exp(*this);
1035 TopExp_Explorer FExp(aResult, TopAbs_FACE);
1036 for( ; FExp.More(); FExp.Next() )
1038 TopoDS_Face aFace = TopoDS::Face(FExp.Current());
1040 if (aChF2ST.Contains(aFace))
1041 aST = aChF2ST.FindFromKey(aFace);
1042 //DEBTRACE("aFace " << aFace << " aST " << aST.toStdString());
1046 Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
1048 foreach (QString aST, aSTypes)
1050 //DEBTRACE("aST " << aST.toStdString());
1051 aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1058 Checks that object has 2D presentation. Reimlemented to retun true.
1060 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1066 Find the land cover for the given point
1067 @param thePoint the point laying in some land cover
1068 @param theType the returned type
1069 @return the found land cover's face
1071 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1073 //TODO: some more optimal algorithm
1074 Explorer anIt( *this );
1075 for( ; anIt.More(); anIt.Next() )
1076 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
1078 theType = anIt.StricklerType();
1083 return TopoDS_Face();
1086 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1088 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
1089 foreach( QString anItem, theList )
1090 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
1095 @param theTreatedObjects the map of treated objects
1097 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
1098 MapOfTreatedObjects& theTreatedObjects ) const
1100 QStringList aResList = dumpObjectCreation( theTreatedObjects );
1101 QString aName = GetObjPyName();
1103 QString aShpFileName = thePyScriptPath;
1104 aShpFileName.replace( ".py", ".shp" );
1105 QString aDbfFileName = thePyScriptPath;
1106 aDbfFileName.replace( ".py", ".dbf" );
1108 ExportSHP( aShpFileName, true, 0.1 );
1110 QString anAttr = "CODE_06"; //TODO: some custom choice
1111 QStringList anAttrValues, aTypes;
1112 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
1113 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
1115 aResList << QString( "%1.ImportSHP( '%2' )" ).
1116 arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1118 Dump( "attr_values", anAttrValues, aResList );
1119 Dump( "types", aTypes, aResList );
1120 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
1121 arg( aName ).arg( QFileInfo( aDbfFileName ).fileName() ).arg( anAttr );
1126 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1128 //DEBTRACE("RemoveInternal");
1129 //Shape must be topologically correct
1130 TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1132 for(; anExpF.More(); anExpF.Next() )
1134 TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1136 TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1137 TopTools_ListOfShape anEdgesToRemove;
1139 for(; anExp.More(); anExp.Next() )
1141 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1142 if (CurEdge.Orientation() == TopAbs_INTERNAL)
1143 anEdgesToRemove.Append(CurEdge);
1146 if (!anEdgesToRemove.IsEmpty())
1148 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
1149 TopoDS_Shape OutF = aReshape->Apply(CurFace);
1150 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
1151 for (; aIt.More(); aIt.Next())
1152 aReshape->Remove(aIt.Value());
1153 OutF = aReshape->Apply(CurFace);
1155 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1158 OutF = sfs->Shape();
1159 aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1163 Handle_ShapeBuild_ReShape anExtReshape = new ShapeBuild_ReShape();
1164 for (int i = 1; i <= aF2FReplace->Extent(); i++)
1166 TopoDS_Face aFK = aF2FReplace->FindKey(i);
1167 TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1168 anExtReshape->Replace(aFK, aFV);
1169 ShToRebuild = anExtReshape->Apply(ShToRebuild);
1171 // for (int i = 1; i <= aF2FReplace->Extent(); i++)
1173 // DEBTRACE("aF2FReplace key,value " << aF2FReplace->FindKey(i) << " " << aF2FReplace->FindFromIndex(i));
1178 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1180 SetDouble( DataTag_Transparency, theTransparency );
1183 double HYDROData_LandCoverMap::GetTransparency() const
1185 return GetDouble( DataTag_Transparency, 0.5 );
1188 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1189 const QList<int>& theIndices )
1191 HYDROData_ShapeFile anImporter;
1192 QStringList aPolyList;
1193 TopTools_SequenceOfShape aFaces;
1194 int aSHapeType = -1;
1195 int Stat = anImporter.ImportPolygons(HYDROData_Document::Document(1), theSHPFileName, aPolyList, aFaces, aSHapeType);
1200 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1201 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1202 if (maxInd > aPolyList.length())
1205 if (theIndices.empty())
1208 for ( int i = 1; i <=aFaces.Length(); i++ )
1210 TopoDS_Shape aShape = aFaces(i);
1211 if ( aShape.IsNull() )
1213 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1218 //import given indices
1219 foreach ( int Ind, theIndices )
1221 TopoDS_Shape aShape = aFaces(Ind + 1);
1222 if ( aShape.IsNull() )
1224 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1228 StoreLandCovers(aMapFace2ST);
1232 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1234 HYDROData_ShapeFile anExporter;
1236 anExporter.Export(HYDROData_Document::Document(1), theSHPFileName, this, aList, bUseDiscr, theDefl );
1243 bool HYDROData_LandCoverMap::CheckLinear()
1245 TopoDS_Shape InpShape = GetShape();
1246 TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1247 for (; anEdgeEx.More(); anEdgeEx.Next())
1249 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1251 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
1252 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1255 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1258 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1269 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1271 TopoDS_Shape aShape = GetShape();
1272 TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1273 SetShape( aLocatedShape );
1276 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints, std::vector<std::set <QString> >& theTypes ) const
1278 HYDROData_LCM_FaceClassifier FC(this);
1279 FC.Classify(thePoints, theTypes, NULL);
1282 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,
1283 Handle(HYDROData_StricklerTable) theTable,
1284 std::vector<double>& theCoeffs, double DefValue, bool UseMax ) const
1286 std::vector<std::set <QString> > Types;
1287 HYDROData_LCM_FaceClassifier FC(this);
1288 FC.Classify(thePoints, Types, NULL);
1289 theCoeffs.resize(thePoints.size());
1290 for (size_t i = 0; i < Types.size(); i++)
1292 const std::set<QString>& SStr = Types[i];
1294 theCoeffs[i] = DefValue;
1297 std::set<QString>::const_iterator it;
1298 std::vector<double> C1;
1299 for (it = SStr.begin(); it != SStr.end(); ++it)
1300 C1.push_back(theTable->Get( *it, DefValue ));
1303 Val = *(std::max_element( C1.begin(), C1.end() ) );
1305 Val = *(std::min_element( C1.begin(), C1.end() ) );