Salome HOME
new iteration process in TestViewer;
[modules/hydro.git] / src / HYDROData / HYDROData_LandCoverMap.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
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
25 #include <BOPAlgo_BOP.hxx>
26 #include <BOPAlgo_Builder.hxx>
27 #include <BOPAlgo_PaveFiller.hxx>
28 #include <BOPCol_ListOfShape.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepAdaptor_Curve.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <GCPnts_QuasiUniformDeflection.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
43 #include <BOPAlgo_PaveFiller.hxx>
44 #include <BRepTools.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <ShapeUpgrade_UnifySameDomain.hxx>
47 #include <TopExp.hxx>
48 #include <TDataStd_Real.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <ShapeBuild_ReShape.hxx>
51 #include <ShapeFix_Shape.hxx>
52 #include <BRepCheck_Shell.hxx>
53 #include <BRepCheck_ListOfStatus.hxx>
54
55
56 #include <QFile>
57 #include <QString>
58 #include <QTextStream>
59
60 const char TELEMAC_FORMAT = 'f';
61 const int TELEMAC_PRECISION = 3;
62
63
64 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
65 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
66
67 /**
68   Constructor
69   @param theMap the land cover map to iterate through
70 */
71 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
72 {
73   Init( theMap );
74 }
75
76 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
77 {
78   if( theMap.IsNull() )
79   {
80     myExplorer = 0;
81     myIndex = -1;
82   }
83   else
84     Init( *theMap );
85 }
86
87 /**
88   Initialize the iterator
89   @param theMap the land cover map to iterate through
90 */
91 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
92 {
93   TopoDS_Shape aShape = theMap.GetShape();
94   if( aShape.IsNull() )
95     myExplorer = 0;
96   else
97   {
98     myExplorer = new TopExp_Explorer();
99     myExplorer->Init( aShape, TopAbs_FACE );
100   }
101   
102   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
103   if( myArray.IsNull() )
104     myIndex = -1;
105   else
106     myIndex = myArray->Lower();
107 }
108
109 /**
110   Destructor
111 */
112 HYDROData_LandCoverMap::Explorer::~Explorer()
113 {
114   delete myExplorer;
115 }
116
117 /**
118   Return the current 0-based index of the iterator
119   @return the current index
120 */
121 int HYDROData_LandCoverMap::Explorer::Index() const
122 {
123   if( myArray.IsNull() )
124     return -1;
125   else
126     return myIndex - myArray->Lower();
127 }
128
129 /**
130   Return if the iterator has more elements
131   @return if the iterator has more elements
132 */
133 bool HYDROData_LandCoverMap::Explorer::More() const
134 {
135   return !myArray.IsNull() && myExplorer && myExplorer->More();
136 }
137
138 /**
139   Move iterator to the next element
140 */
141 void HYDROData_LandCoverMap::Explorer::Next()
142 {
143   if( myExplorer )
144   {
145     myExplorer->Next();
146     myIndex++;
147   }
148 }
149
150 /**
151   Get the current land cover (face)
152   @return the land cover's face
153 */
154 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
155 {
156   if( myExplorer )
157     return TopoDS::Face( myExplorer->Current() );
158   else
159     return TopoDS_Face();
160 }
161
162 /**
163   Get the current land cover's Strickler type 
164   @return the land cover's Strickler type 
165 */
166 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
167 {
168   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
169     return "";
170   else
171     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
172 }
173
174 /**
175   Set the Strickler type for the current land cover
176   @param theType the Strickler type
177 */
178 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
179 {
180   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
181     return;
182   else
183     myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
184 }
185
186 /**
187   Constructor
188 */
189 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
190   : HYDROData_Entity( Geom_No )
191 {
192 }
193
194 /**
195   Destructor
196 */
197 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
198 {
199 }
200
201 /**
202   Get object's kind
203   @return object's kind
204 */
205 const ObjectKind HYDROData_LandCoverMap::GetKind() const
206 {
207   return KIND_LAND_COVER_MAP;
208 }
209
210 int HYDROData_LandCoverMap::GetLCCount() const
211 {
212   Explorer anIt( *this );
213   int i = 0;
214   for( ; anIt.More(); anIt.Next() )
215     i++;
216   return i;
217 }
218
219 bool HYDROData_LandCoverMap::IsEmpty() const
220 {
221   Explorer anIt( *this );
222   if ( !anIt.More() )
223     return true;
224   else
225     return false;
226 }
227
228 /**
229   Load attributes from DBF File
230 ///
231 */
232 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName, 
233                                                                      const QString& theFieldName, 
234                                                                      const QStringList& theDBFValues,
235                                                                      const QStringList& theStricklerTypes,
236                                                                      const QList<int>& theIndices )
237 {
238   if (theDBFValues.size() != theStricklerTypes.size())
239     return DBFStatus_DIFF_SIZE_ERROR;
240   HYDROData_ShapeFile aDBFImporter;
241   if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
242     return DBFStatus_OPEN_FILE_ERROR; //cant open file
243
244   QStringList FieldList = aDBFImporter.DBF_GetFieldList();
245   int FieldNameIndex = FieldList.indexOf(theFieldName);
246   if (FieldNameIndex == -1)
247     return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
248
249   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
250   aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV ); 
251
252   bool allOK = true;
253   Explorer anIt( *this );
254   for( ; anIt.More(); anIt.Next() )
255   {
256     int CurIndex = anIt.Index();
257     HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[theIndices[CurIndex]];
258     int StricklerTypesInd = theDBFValues.indexOf(QString(AValue.myStrVal));
259     if ( StricklerTypesInd != -1)
260       anIt.SetStricklerType(theStricklerTypes[StricklerTypesInd]);
261     else
262       allOK = false;
263   }
264   if (allOK)
265     return DBFStatus_OK;
266   else
267     return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
268 }
269
270 /**
271   Export attributes to DBF File
272 ///
273 */
274 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName, 
275                                         const QString& theFieldName, 
276                                         const QStringList& theDBFValues,
277                                         const QStringList& theStricklerTypes) const
278 {
279   if (theDBFValues.size() != theStricklerTypes.size())
280     return;
281   HYDROData_ShapeFile anExporter; 
282   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
283   Explorer anIt( *this );
284   for( ; anIt.More(); anIt.Next() )
285   {
286     QString CurST = anIt.StricklerType();
287     HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
288     aCurAttrV.myIsNull = false;
289     int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
290     if (StricklerTypesInd != -1)
291     {
292       aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
293       aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
294       theAttrV.push_back(aCurAttrV);
295     }
296     else
297       aCurAttrV.myIsNull = true;
298   }
299   //use actual str value; not the raw value
300   anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
301
302 }
303
304 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
305 {
306   int aHashX = HashCode( thePoint.X(), theUpper );
307   int aHashY = HashCode( thePoint.Y(), theUpper );
308   return (aHashX^aHashY)%theUpper;
309 }
310
311 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
312 {
313   return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
314 }
315
316 bool EdgeDiscretization( const TopoDS_Edge& theEdge, 
317                          Standard_Real theDeflection,
318                          NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
319                          QList<int>& theVerticesIds )
320 {
321   BRepAdaptor_Curve aCurve( theEdge );
322   GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
323   if( !aDiscrete.IsDone() )
324     return false;
325
326   int n = aDiscrete.NbPoints();
327   for( int i=1; i<=n; i++ )
328   {
329     gp_Pnt aPnt = aDiscrete.Value( i );
330     int anId;
331     if( theVerticesMap.Contains( aPnt ) )
332       anId = theVerticesMap.FindIndex( aPnt );
333     else
334     {
335       anId = theVerticesMap.Size();
336       theVerticesMap.Add( aPnt );
337     }
338     theVerticesIds.append( anId );
339   }
340   return true;
341 }
342
343 /**
344   Export the land cover map for the solver (Telemac)
345   @param theFileName the name of file
346   @return if the export is successful
347 */
348 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
349 {
350   TopoDS_Shape aLandCoverMapShape = GetShape();
351   TopTools_ListOfShape aListOfFaces;
352   TopExp_Explorer anExp( aLandCoverMapShape, TopAbs_FACE );
353   for( ; anExp.More(); anExp.Next() )
354     aListOfFaces.Append( anExp.Current() );
355
356   TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
357
358   NCollection_IndexedMap<gp_Pnt> aVerticesMap;
359   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
360   NCollection_IndexedDataMap< TopoDS_Face, QList<int> > aFacesMap;
361
362   // add into the map all edges existing in the shell
363   TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
364   for( ; anExp1.More(); anExp1.Next() )
365   {
366     TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
367     QList<int> aVerticesIdsList;
368     if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
369       anEdgesMap.Add( anEdge, aVerticesIdsList );
370   }
371
372   // add into the map all faces existing in the shell and correspondence between face and edges ids
373   TopExp_Explorer anExp2( aShape, TopAbs_FACE );
374   for( ; anExp2.More(); anExp2.Next() )
375   {
376     TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
377     TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
378     QList<int> anEdgesIdsList;
379     for( ; anExp3.More(); anExp3.Next() )
380     {
381       TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
382       int anEdgeId = anEdgesMap.FindIndex( anEdge );
383       anEdgesIdsList.append( anEdgeId );
384     }
385     aFacesMap.Add( aFace, anEdgesIdsList );
386   }
387
388   QFile aFile( theFileName );
389   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
390     return false;
391
392   QTextStream aStream( &aFile );
393   aStream << "# nodes\n";
394   NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
395   for( ; anIt1.More(); anIt1.Next() )
396   {
397     gp_Pnt aPnt = anIt1.Value();
398     aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
399     aStream << " ";
400     aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
401     aStream << " ";
402     aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
403     aStream << "\n";
404   }
405   aStream << "\n";
406
407   aStream << "# edges\n";
408   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
409   for( ; anIt2.More(); anIt2.Next() )
410   {
411     QList<int> aVerticesIds = anIt2.Value();
412     foreach( int anId, aVerticesIds )
413       aStream << anId << " ";
414     aStream << "\n";
415   }
416   aStream << "\n";
417
418   aStream << "# faces\n";
419   NCollection_IndexedDataMap< TopoDS_Face, QList<int> >::Iterator anIt3( aFacesMap );
420   for( ; anIt3.More(); anIt3.Next() )
421   {
422     QList<int> anEdgesIds = anIt3.Value();
423     foreach( int anId, anEdgesIds )
424       aStream << anId << " ";
425     aStream << "\n";
426   }
427   aStream << "\n";
428
429   aFile.close();
430   return true;
431 }
432
433 /**
434   Add a new object as land cover
435   @param theObject the object to add as land cover
436   @param theType the Strickler type for the new land cover
437   @return if the addition is successful
438 */
439 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
440 {
441   if( theObject.IsNull() )
442     return false;
443
444   TopoDS_Shape aShape = theObject->GetTopShape();
445   if( aShape.ShapeType()!=TopAbs_FACE )
446     return false;
447
448   TopoDS_Face aFace = TopoDS::Face( aShape );
449   return LocalPartition( aFace, theType );
450 }
451
452 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
453 {
454   if( !theWire.Closed() )
455     return false;
456
457   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
458   return LocalPartition( aFace, theType );
459 }
460
461 /**
462   Add a new polyline as land cover
463   @param thePolyline the polyline to add as land cover
464   @param theType the Strickler type for the new land cover
465   @return if the addition is successful
466 */
467 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
468 {
469   if( thePolyline.IsNull() )
470     return false;
471
472   TopoDS_Shape aShape = thePolyline->GetShape();
473   if( aShape.ShapeType()==TopAbs_WIRE )
474     return Add( TopoDS::Wire( aShape ), theType );
475
476   if( aShape.ShapeType()==TopAbs_COMPOUND )
477   {
478     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
479     for( ; anExp.More(); anExp.Next() )
480     {
481       TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
482       if( !Add( aPart, theType ) )
483         return false;
484     }
485     return true;
486   }
487
488   return false;
489 }
490
491 /**
492   Remove the given face from land cover map
493   @param theFace the face to be removed
494   @return if the removing is successful
495 */
496 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
497 {
498   TopTools_ListOfShape aList;
499   aList.Append( theFace );
500   return Remove( aList );
501 }
502
503 /**
504   Remove the given faces from land cover map
505   @param theFacesToRemove the face list to be removed
506   @return if the removing is successful
507 */
508 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
509 {
510   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
511   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
512   for( ; aFIt.More(); aFIt.Next() )
513   {
514     TopoDS_Shape aShape = aFIt.Value();
515     if( aShape.ShapeType()==TopAbs_FACE )
516       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
517   }
518
519   Explorer anIt( *this );
520   for( ; anIt.More(); anIt.Next() )
521     if( !aFacesToRemove.Contains( anIt.Face() ) )
522       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
523
524   if ( aNewFaces.IsEmpty() )
525     return false;
526
527   StoreLandCovers( aNewFaces );
528   return true;
529 }
530
531 /**
532   Split the land cover map by the given polyline
533   @param thePolyline the tool polyline to split the land cover map
534   @return if the removing is successful
535 */
536 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
537 {
538   if( thePolyline.IsNull() )
539     return false;
540
541   TopoDS_Shape aShape = thePolyline->GetShape();
542   return Split( aShape );
543 }
544
545
546 /**
547   Split the land cover map by the given polyline
548   @param theShape the tool polyline to split the land cover map
549   @return if the removing is successful
550 */
551 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
552 {
553   return LocalPartition( theShape, "" );
554 }
555
556
557 /**
558   Merge the given faces in the land cover
559   @param theFaces the faces to merge in the land cover map
560   @param theType the Strickler type for the merged land cover
561   @return if the merge is successful
562 */
563 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
564 {
565   // 1. to fuse the faces into the new face
566   TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
567   if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
568   {
569     // 2. to remove the merged faces from the current map
570     Remove( theFaces );
571
572     // 3. to add the face into the map
573     return LocalPartition( TopoDS::Face( aMergedFace ), theType );
574   }
575   return false;
576 }
577
578 /**
579   Merge the given faces into the shell/face
580   @param theFaces the faces to merge
581   @param IsToUnify if the common edges should be removed (fused)
582   @param theTolerance the operation's tolerance
583   @return result shape (face or shell)
584 */
585 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
586                                                  bool IsToUnify, double theTolerance )
587 {
588   int anError;
589   TopTools_ListIteratorOfListOfShape anIt;
590   BOPCol_ListOfShape aLC;
591   anIt.Initialize(theFaces);
592   for( ; anIt.More(); anIt.Next() )
593   {
594     if (anIt.Value().ShapeType() != TopAbs_FACE)
595       return TopoDS_Shape();
596     aLC.Append( anIt.Value() );
597   }
598
599   BOPAlgo_PaveFiller aPF;
600   aPF.SetArguments( aLC );
601   aPF.SetRunParallel( Standard_False );
602   aPF.SetFuzzyValue( theTolerance );
603
604   aPF.Perform();
605   anError = aPF.ErrorStatus();
606   if( anError )
607     return TopoDS_Shape();
608
609   BOPAlgo_Builder anAlgo;
610   anIt.Initialize( theFaces );
611   for( ; anIt.More(); anIt.Next() )
612     anAlgo.AddArgument( anIt.Value() );
613
614   anAlgo.PerformWithFiller( aPF ); 
615   anError = anAlgo.ErrorStatus();
616   if( anError )
617     return TopoDS_Shape();
618
619   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
620
621   BRep_Builder aBuilder;
622   TopoDS_Shell aShell; 
623   aBuilder.MakeShell( aShell ); 
624   aShell.Closed( Standard_False );
625   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
626   for( ; anExplorer.More(); anExplorer.Next() ) 
627   {
628     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
629     if( aFace.IsNull() ) 
630       continue;
631     if( aFace.ShapeType() == TopAbs_FACE )
632     {
633       aBuilder.Add( aShell, aFace );
634       aShell.Closed( Standard_False );
635     }
636   }
637
638   TopoDS_Shape aResult;
639   if( IsToUnify )
640   {
641     ShapeUpgrade_UnifySameDomain aUSD;
642     aUSD.Initialize( aShell );
643     aUSD.Build();
644     aResult = aUSD.Shape();
645   }
646   else
647     aResult = aShell;
648
649   anExplorer.Init( aResult, TopAbs_FACE );
650   int n = 0;
651   TopoDS_Face anOneFace;
652   for( ; anExplorer.More(); anExplorer.Next(), n++ ) 
653     anOneFace = TopoDS::Face( anExplorer.Current() );
654
655   if (n == 1)
656     aResult = anOneFace;
657   else if (aResult.ShapeType() == TopAbs_SHELL)
658   {
659     BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
660     if (aBCS.Status().First() != BRepCheck_NoError)
661     {
662       ShapeFix_Shell aFixer;
663       aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
664       aResult = aFixer.Shape();
665     }
666   }
667
668   return aResult;
669 }
670
671 /**
672   Change Strickler type for the list of faces to the given one
673   @param theFaces the faces to change type
674   @param theType the Strickler type for the given land cover(s)
675   @return if the change type operation is successful
676 */
677 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
678 {
679   HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
680   TopTools_ListIteratorOfListOfShape aFIt( theFaces );
681   for( ; aFIt.More(); aFIt.Next() )
682   {
683     TopoDS_Shape aShape = aFIt.Value();
684     if( aShape.ShapeType()==TopAbs_FACE )
685       aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
686   }
687
688   int aNbChanges = 0;
689   Explorer anIt( *this );
690   for( ; anIt.More(); anIt.Next() )
691     if( aFacesToChangeType.Contains( anIt.Face() ) )
692     {
693       anIt.SetStricklerType( theType );
694       aNbChanges++;
695     }
696   if ( aNbChanges != theFaces.Extent() )
697     return false;
698
699   return true;
700 }
701
702 /**
703   Get the shape of the land cover map
704 */
705 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
706 {
707   return HYDROData_Entity::GetShape( DataTag_Shape );
708 }
709
710 /**
711   Get Strickler type of the given land cover
712   @param theLandCover the land cover to get Strickler type of
713   @return name of Strickler type
714 */
715 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
716 {
717   QString aType = "";
718
719   Explorer anIt( *this );
720   for( ; anIt.More(); anIt.Next() )
721     if( anIt.Face().IsEqual( theLandCover) )
722     {
723       aType = anIt.StricklerType();
724       break;
725     }
726
727   return aType;
728 }
729
730 /**
731   Set the shape of the land cover map
732   @param theShape the new shape for the land cover map
733 */
734 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
735 {
736   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
737 }
738
739 /**
740   Perform the local partition algorithm on the land cover
741   @param theNewShape the new shape to add into the land cover
742   @param theNewType the new Strickler type for the new land cover
743   @return if the local partition is successful
744 */
745 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
746 {
747   if( theNewShape.IsNull() )
748     return false;
749
750   BOPCol_ListOfShape aShapesList;
751   BOPAlgo_PaveFiller aPaveFiller;
752   HYDROData_MapOfFaceToStricklerType aNewFaces;
753
754   // add faces to shapes list
755   Explorer anIt( *this );
756   for( ; anIt.More(); anIt.Next() )
757     aShapesList.Append( anIt.Face() );
758   aShapesList.Append( theNewShape );
759
760   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
761   {
762     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
763     StoreLandCovers( aNewFaces );
764     return true;
765   }
766
767   // prepare pave filler
768   aPaveFiller.SetArguments( aShapesList );
769   aPaveFiller.Perform();
770   Standard_Integer anError = aPaveFiller.ErrorStatus();
771   if( anError )
772     return false;
773
774   // add faces to builder
775   BOPAlgo_Builder aBuilder;
776   anIt.Init( *this );
777   for( ; anIt.More(); anIt.Next() )
778     aBuilder.AddArgument( anIt.Face() );
779   aBuilder.AddArgument( theNewShape );
780
781   // perform the partition with the pave filler
782   aBuilder.PerformWithFiller( aPaveFiller );
783   anError = aBuilder.ErrorStatus();
784   if( anError )
785     return false;
786
787   //std::cout << "History:" << std::endl;
788   // analysis of the history
789   //     a. to fill map of shapes which come from the new face
790   NCollection_IndexedMap<int> aShapesFromNewFace;
791   //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
792   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
793   TopTools_ListIteratorOfListOfShape aMIt( aModified );
794   for( ; aMIt.More(); aMIt.Next() )
795   {
796     //std::cout << "   " << aMIt.Value() << std::endl;
797     int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
798     aShapesFromNewFace.Add( aKey );
799   }
800
801   //     b. to fill map of parts except parts from new face
802   anIt.Init( *this );
803   for( ; anIt.More(); anIt.Next() )
804   {
805     QString aSType = anIt.StricklerType();
806     //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
807     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
808     if( aModified.Extent() == 0 )
809       aModified.Append( anIt.Face() );
810
811     TopTools_ListIteratorOfListOfShape aMIt( aModified );
812     for( ; aMIt.More(); aMIt.Next() )
813     {
814       TopoDS_Shape aShape = aMIt.Value();
815       bool isFace = aShape.ShapeType()==TopAbs_FACE;
816       int aKey = (int)(uintptr_t)aShape.TShape().operator->();
817       bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
818       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
819       if( isFace && !isAlsoFromNew )
820         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
821     }
822   }
823
824   //     c. add the new shape if it is face with its type
825   if( theNewShape.ShapeType()==TopAbs_FACE )
826     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
827   
828   // convert map of shape to type to compound and list of types
829   StoreLandCovers( aNewFaces );
830   return true;
831 }
832
833 /**
834   Replace the set of land covers in the land cover map
835   @param theMap the map of shape (face) to Strickler type (string)
836 */
837 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
838 {
839   TopTools_ListOfShape aListOfFaces;
840
841   int n = theMap.Size();
842
843   Handle( TDataStd_ExtStringArray ) aTypes = 
844     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
845
846   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
847   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
848   {
849     TopoDS_Face aFace = aNFIt.Key();
850     if( aFace.IsNull() )
851       continue;
852     QString aType = aNFIt.Value();
853     aListOfFaces.Append(aFace);
854     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
855   }
856
857   TopoDS_Shape aResult;
858   if( aListOfFaces.Extent() == 1 )
859     aResult = aListOfFaces.First();
860   else if( aListOfFaces.Extent() > 1 )
861     aResult = MergeFaces( aListOfFaces, false );
862
863   //remove internal edges
864   aResult = RemoveInternal(aResult);
865   SetShape( aResult );
866 }
867
868 /**
869   Find the land cover for the given point
870   @param thePoint the point laying in some land cover
871   @param theType the returned type
872   @return the found land cover's face
873 */
874 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
875 {
876   //TODO: some more optimal algorithm
877   Explorer anIt( *this );
878   for( ; anIt.More(); anIt.Next() )
879     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
880     {
881       theType = anIt.StricklerType();
882       return anIt.Face();
883     }
884
885   theType = "";
886   return TopoDS_Face();
887 }
888
889 /**
890   Dump to Python
891   @param theTreatedObjects the map of treated objects
892 */
893 QStringList HYDROData_LandCoverMap::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
894 {
895   QStringList aResList = dumpObjectCreation( theTreatedObjects );
896   QString aName = GetObjPyName();
897
898   //Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
899   //setPythonReferenceObject( theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
900
901   //HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
902   //for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
903   //{
904     //const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
905     //setPythonReferenceObject( theTreatedObjects, aResList, aProfile, "AddProfile" );
906   //}
907
908   //TODO
909
910   return aResList;
911 }
912
913 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
914 {
915   //Shape must be topologically correct
916   TopExp_Explorer anExp(InSh, TopAbs_EDGE);
917   TopTools_ListOfShape anEdgesToRemove;
918
919   for(; anExp.More(); anExp.Next() )
920   {
921     TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
922     if (CurEdge.Orientation() == TopAbs_INTERNAL)
923       anEdgesToRemove.Append(CurEdge);
924   }
925  
926   Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
927   TopoDS_Shape OutSh = aReshape->Apply(InSh);
928   TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
929   for (; aIt.More(); aIt.Next()) 
930     aReshape->Remove(aIt.Value());
931   OutSh = aReshape->Apply(InSh);
932
933   Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
934   sfs->Init(OutSh);
935   sfs->Perform();
936   return sfs->Shape();
937
938 }
939
940 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
941 {
942   Handle(TDataStd_Real) anAttr;
943   TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
944   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
945     aLabel.AddAttribute( anAttr = new TDataStd_Real() );
946   anAttr->Set( theTransparency );
947 }
948
949 double HYDROData_LandCoverMap::GetTransparency() const
950 {
951   Handle(TDataStd_Real) anAttr;
952   TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
953   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
954     return 0.5;
955
956   return anAttr->Get();
957   
958 }