Salome HOME
Merge branch 'BR_HYDRO_IMPS_WIN' of ssh://gitolite3@git.salome-platform.org/modules...
[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 #include <HYDROData_Document.h>
25 #include <HYDROData_StricklerTable.h>
26 #include <HYDROData_ShapesTool.h>
27
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>
37 #include <TopoDS.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>
51 #include <TopExp.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>
65 #include <QDir>
66
67 #include <stdexcept>
68
69 #include <QFile>
70 #include <QString>
71 #include <QTextStream>
72 #include <QFileInfo>
73
74 #define _DEVDEBUG_
75 #include "HYDRO_trace.hxx"
76
77 const char TELEMAC_FORMAT = 'f';
78 const int TELEMAC_PRECISION = 3;
79
80
81 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
82 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
83
84 /**
85   Constructor
86   @param theMap the land cover map to iterate through
87 */
88 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
89 {
90   Init( theMap );
91 }
92
93 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
94 {
95   if( theMap.IsNull() )
96   {
97     myExplorer = 0;
98     myIndex = -1;
99   }
100   else
101     Init( *theMap );
102 }
103
104 /**
105   Initialize the iterator
106   @param theMap the land cover map to iterate through
107 */
108 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
109 {
110   TopoDS_Shape aShape = theMap.GetShape();
111   if( aShape.IsNull() )
112     myExplorer = 0;
113   else
114   {
115     myExplorer = new TopExp_Explorer();
116     myExplorer->Init( aShape, TopAbs_FACE );
117   }
118   
119   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
120   if( myArray.IsNull() )
121     myIndex = -1;
122   else
123     myIndex = myArray->Lower();
124 }
125
126 /**
127   Destructor
128 */
129 HYDROData_LandCoverMap::Explorer::~Explorer()
130 {
131   delete myExplorer;
132 }
133
134 /**
135   Return the current 0-based index of the iterator
136   @return the current index
137 */
138 int HYDROData_LandCoverMap::Explorer::Index() const
139 {
140   if( myArray.IsNull() )
141     return -1;
142   else
143     return myIndex - myArray->Lower();
144 }
145
146 /**
147   Return if the iterator has more elements
148   @return if the iterator has more elements
149 */
150 bool HYDROData_LandCoverMap::Explorer::More() const
151 {
152   return !myArray.IsNull() && myExplorer && myExplorer->More();
153 }
154
155 /**
156   Move iterator to the next element
157 */
158 void HYDROData_LandCoverMap::Explorer::Next()
159 {
160   if( myExplorer )
161   {
162     myExplorer->Next();
163     myIndex++;
164   }
165 }
166
167 /**
168   Get the current land cover (face)
169   @return the land cover's face
170 */
171 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
172 {
173   if( myExplorer )
174     return TopoDS::Face( myExplorer->Current() );
175   else
176     return TopoDS_Face();
177 }
178
179 /**
180   Get the current land cover's Strickler type 
181   @return the land cover's Strickler type 
182 */
183 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
184 {
185   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
186     return "";
187   else
188     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
189 }
190
191 /**
192   Set the Strickler type for the current land cover
193   @param theType the Strickler type
194 */
195 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
196 {
197   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
198     return;
199   else
200     myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
201 }
202
203 /**
204   Constructor
205 */
206 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
207   : HYDROData_Entity( Geom_No )
208 {
209 }
210
211 /**
212   Destructor
213 */
214 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
215 {
216 }
217
218 /**
219   Get object's kind
220   @return object's kind
221 */
222 const ObjectKind HYDROData_LandCoverMap::GetKind() const
223 {
224   return KIND_LAND_COVER_MAP;
225 }
226
227 int HYDROData_LandCoverMap::GetLCCount() const
228 {
229   Explorer anIt( *this );
230   int i = 0;
231   for( ; anIt.More(); anIt.Next() )
232     i++;
233   return i;
234 }
235
236 bool HYDROData_LandCoverMap::IsEmpty() const
237 {
238   Explorer anIt( *this );
239   if ( !anIt.More() )
240     return true;
241   else
242     return false;
243 }
244
245 /**
246   Load attributes from DBF File
247 ///
248 */
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 )
254 {
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
260
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
265
266   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
267   aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV ); 
268
269   bool allOK = true;
270   Explorer anIt( *this );
271   for( ; anIt.More(); anIt.Next() )
272   {
273     int CurIndex = anIt.Index();
274     int anIndex = CurIndex;
275     if( !theIndices.isEmpty() )
276       anIndex = theIndices[CurIndex];
277
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] );
282     else
283       allOK = false;
284   }
285   if (allOK)
286     return DBFStatus_OK;
287   else
288     return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
289 }
290
291 /**
292   Export attributes to DBF File
293 ///
294 */
295 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName, 
296                                         const QString& theFieldName, 
297                                         const QStringList& theDBFValues,
298                                         const QStringList& theStricklerTypes) const
299 {
300   if (theDBFValues.size() != theStricklerTypes.size())
301     return;
302   HYDROData_ShapeFile anExporter; 
303   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
304   Explorer anIt( *this );
305   for( ; anIt.More(); anIt.Next() )
306   {
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)
312     {
313       aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
314       aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
315       theAttrV.push_back(aCurAttrV);
316     }
317     else
318       aCurAttrV.myIsNull = true;
319   }
320   //use actual str value; not the raw value
321   anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
322
323 }
324
325 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
326 {
327   int aHashX = HashCode( thePoint.X(), theUpper );
328   int aHashY = HashCode( thePoint.Y(), theUpper );
329   return (aHashX^aHashY)%theUpper;
330 }
331
332 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
333 {
334   return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
335 }
336
337 bool EdgeDiscretization( const TopoDS_Edge& theEdge, 
338                          Standard_Real theDeflection,
339                          NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
340                          QList<int>& theVerticesIds )
341 {
342   BRepAdaptor_Curve aCurve( theEdge );
343   GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
344   if( !aDiscrete.IsDone() )
345     return false;
346
347   int n = aDiscrete.NbPoints();
348   for( int i=1; i<=n; i++ )
349   {
350     gp_Pnt aPnt = aDiscrete.Value( i );
351     int anId;
352     if( theVerticesMap.Contains( aPnt ) )
353       anId = theVerticesMap.FindIndex( aPnt );
354     else
355     {
356       anId = theVerticesMap.Size();
357       theVerticesMap.Add( aPnt );
358     }
359     theVerticesIds.append( anId );
360   }
361   return true;
362 }
363
364 /**
365   Export the land cover map for the solver (Telemac)
366   @param theFileName the name of file
367   @return if the export is successful
368 */
369 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
370                                             double theDeflection,
371                                             const Handle(HYDROData_StricklerTable)& theTable,
372                                             QString& statMessage) const
373 {
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() )
379   {
380     aListOfFaces.Append( anIt.Face() );
381     aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
382   }
383
384   TopoDS_Shape aShape = MergeFaces( aListOfFaces, false, NULL );
385
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;
390
391   // add into the map all edges existing in the shell
392   TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
393   for( ; anExp1.More(); anExp1.Next() )
394   {
395     TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
396     QList<int> aVerticesIdsList;
397     if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
398       anEdgesMap.Add( anEdge, aVerticesIdsList );
399   }
400
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() )
404   {
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() )
409     {
410       TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
411       int anEdgeId = anEdgesMap.FindIndex( anEdge );
412       anEdgesIdsList.append( anEdgeId );
413     }
414
415     FaceData aData;
416     aData.first = aTypesMap[aFace.TShape()];
417     aData.second = anEdgesIdsList;
418     aFacesMap.Add( aFace, aData );
419   }
420
421   QFile aFile( theFileName );
422   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
423   {
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;
428     else
429       return false;
430   }
431   else
432   {
433     QString absFilePath = QDir::current().absoluteFilePath( theFileName );
434     statMessage = "Telemac file have been exported to the current directory: " + absFilePath;
435   }
436
437   QTextStream aStream( &aFile );
438   aStream << "# nodes\n";
439   NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
440   for( ; anIt1.More(); anIt1.Next() )
441   {
442     gp_Pnt aPnt = anIt1.Value();
443     aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
444     aStream << " ";
445     aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
446     aStream << " ";
447     aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
448     aStream << "\n";
449   }
450   aStream << "\n";
451
452   aStream << "# edges\n";
453   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
454   for( ; anIt2.More(); anIt2.Next() )
455   {
456     QList<int> aVerticesIds = anIt2.Value();
457     foreach( int anId, aVerticesIds )
458       aStream << anId << " ";
459     aStream << "\n";
460   }
461   aStream << "\n";
462
463   aStream << "# faces\n";
464   NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
465   for( ; anIt3.More(); anIt3.Next() )
466   {
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 << " ";
474     aStream << "\n";
475   }
476   aStream << "\n";
477
478   aFile.close();
479   return true;
480 }
481
482 /**
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
487 */
488 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
489 {
490   if( theObject.IsNull() )
491     return false;
492
493   TopoDS_Shape aShape = theObject->GetTopShape();
494   TopoDS_Face aFace;
495
496   if( aShape.ShapeType() ==TopAbs_FACE )
497   {
498     aFace = TopoDS::Face(aShape);
499   }
500   else if ( aShape.ShapeType() ==TopAbs_COMPOUND )
501   {
502     TopoDS_Iterator It(aShape);
503     for (; It.More(); It.Next())
504       if (It.Value().ShapeType() == TopAbs_FACE)
505       {
506         aFace = TopoDS::Face(It.Value());
507         break;
508       }
509   }
510
511   if (aFace.IsNull())
512     return false;
513   return LocalPartition( aFace, theType );
514 }
515
516 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
517 {
518   if( !theWire.Closed() )
519     return false;
520
521   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
522   return LocalPartition( aFace, theType );
523 }
524
525 /**
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
530 */
531 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
532 {
533   if( thePolyline.IsNull() )
534     return false;
535
536   TopoDS_Shape aShape = thePolyline->GetShape();
537   if( aShape.ShapeType()==TopAbs_WIRE )
538     return Add( TopoDS::Wire( aShape ), theType );
539
540   if( aShape.ShapeType()==TopAbs_COMPOUND )
541   {
542     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
543     for( ; anExp.More(); anExp.Next() )
544     {
545       TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
546       if( !Add( aPart, theType ) )
547         return false;
548     }
549     return true;
550   }
551
552   return false;
553 }
554
555 /**
556   Remove the given face from land cover map
557   @param theFace the face to be removed
558   @return if the removing is successful
559 */
560 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
561 {
562   TopTools_ListOfShape aList;
563   aList.Append( theFace );
564   return Remove( aList );
565 }
566
567 /**
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
571 */
572 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
573 {
574   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
575   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
576   for( ; aFIt.More(); aFIt.Next() )
577   {
578     TopoDS_Shape aShape = aFIt.Value();
579     if( aShape.ShapeType()==TopAbs_FACE )
580       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
581   }
582
583   Explorer anIt( *this );
584   for( ; anIt.More(); anIt.Next() )
585     if( !aFacesToRemove.Contains( anIt.Face() ) )
586       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
587
588   if ( aNewFaces.IsEmpty() )
589     return false;
590
591   StoreLandCovers( aNewFaces );
592   return true;
593 }
594
595 /**
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
599 */
600 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
601 {
602   if( thePolyline.IsNull() )
603     return false;
604
605   TopoDS_Shape aShape = thePolyline->GetShape();
606   return Split( aShape );
607 }
608
609
610 /**
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
614 */
615 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
616 {
617   int aNbCL = GetLCCount();
618   bool aResult = LocalPartition( theShape, "" );
619   return aResult && aNbCL != GetLCCount();
620 }
621
622
623 /**
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
628 */
629 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
630 {
631   // 1. to fuse the faces into the new face
632   TopoDS_Shape aMergedFace = MergeFaces( theFaces, true, NULL );  
633   bool aStat = true;
634   if( !aMergedFace.IsNull() )
635   { 
636     // 2. to remove the merged faces from the current map
637     Remove( theFaces );
638     TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
639     for( ; Exp.More(); Exp.Next() )
640     {
641       const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
642       // 3. to add the face into the map
643       aStat = aStat && LocalPartition( aCF, theType );
644     }
645   }
646   else
647     aStat = false;
648   return aStat;
649 }
650
651 /**
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)
657 */
658  
659 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
660                                                  bool IsToUnify, 
661                                                  TopTools_IndexedDataMapOfShapeListOfShape* theShHistory, 
662                                                  double theTolerance)
663 {
664   //DEBTRACE("MergeFaces");
665   int anError;
666   TopTools_ListIteratorOfListOfShape anIt;
667   BOPCol_ListOfShape aLC;
668   anIt.Initialize(theFaces);
669   for( ; anIt.More(); anIt.Next() )
670   {
671     if (anIt.Value().ShapeType() != TopAbs_FACE)
672       return TopoDS_Shape();
673     aLC.Append( anIt.Value() );
674   }
675
676   BOPAlgo_PaveFiller aPF;
677   aPF.SetArguments( aLC );
678   aPF.SetRunParallel( Standard_False );
679   aPF.SetFuzzyValue( theTolerance );
680
681   aPF.Perform();
682   anError = aPF.ErrorStatus();
683   if( anError )
684     return TopoDS_Shape();
685
686   BOPAlgo_Builder anAlgo;
687   anIt.Initialize( theFaces );
688   for( ; anIt.More(); anIt.Next() )
689     anAlgo.AddArgument( anIt.Value() );
690
691   anAlgo.PerformWithFiller( aPF ); 
692   anError = anAlgo.ErrorStatus();
693   if( anError )
694     return TopoDS_Shape();
695
696   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
697
698   // retrieve history of modifications
699   if (theShHistory)
700   {
701     theShHistory->Clear();
702     anIt.Initialize(theFaces);
703     for( ; anIt.More(); anIt.Next() )
704     {
705       const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
706       theShHistory->Add(anIt.Value(), aMLS);
707     }
708   }
709   //
710
711   BRep_Builder aBuilder;
712   TopoDS_Shell aShell; 
713   aBuilder.MakeShell( aShell ); 
714   aShell.Closed( Standard_False );
715   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
716   for( ; anExplorer.More(); anExplorer.Next() ) 
717   {
718     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
719     if( aFace.IsNull() ) 
720       continue;
721     if( aFace.ShapeType() == TopAbs_FACE )
722     {
723       aBuilder.Add( aShell, aFace );
724       aShell.Closed( Standard_False );
725     }
726   }
727
728   TopoDS_Shape aResult;
729   if( IsToUnify )
730   {
731     ShapeUpgrade_UnifySameDomain aUSD;
732     aUSD.Initialize( aShell );
733     aUSD.Build();
734     aResult = aUSD.Shape();
735   }
736   else
737     aResult = aShell;
738
739   anExplorer.Init( aResult, TopAbs_FACE );
740   int n = 0;
741   TopoDS_Face anOneFace;
742   for( ; anExplorer.More(); anExplorer.Next(), n++ ) 
743     anOneFace = TopoDS::Face( anExplorer.Current() );
744
745   if (n == 1)
746     aResult = anOneFace;
747   else if (aResult.ShapeType() == TopAbs_SHELL)
748   {
749     BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
750     if (aBCS.Status().First() != BRepCheck_NoError)
751     {
752       ShapeFix_Shell aFixer;
753       aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
754       aResult = aFixer.Shape();
755     }
756   }
757
758   return aResult;
759 }
760 /**
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
765 */
766 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
767 {
768   HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
769   TopTools_ListIteratorOfListOfShape aFIt( theFaces );
770   for( ; aFIt.More(); aFIt.Next() )
771   {
772     TopoDS_Shape aShape = aFIt.Value();
773     if( aShape.ShapeType()==TopAbs_FACE )
774       aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
775   }
776
777   int aNbChanges = 0;
778   Explorer anIt( *this );
779   for( ; anIt.More(); anIt.Next() )
780     if( aFacesToChangeType.Contains( anIt.Face() ) )
781     {
782       anIt.SetStricklerType( theType );
783       aNbChanges++;
784     }
785   if ( aNbChanges != theFaces.Extent() )
786     return false;
787
788   return true;
789 }
790
791 /**
792   Get the shape of the land cover map
793 */
794 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
795 {
796   return HYDROData_Entity::GetShape( DataTag_Shape );
797 }
798
799 /**
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
803 */
804 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
805 {
806   QString aType = "";
807
808   Explorer anIt( *this );
809   for( ; anIt.More(); anIt.Next() )
810     if( anIt.Face().IsEqual( theLandCover) )
811     {
812       aType = anIt.StricklerType();
813       break;
814     }
815
816   return aType;
817 }
818
819 /**
820   Set the shape of the land cover map
821   @param theShape the new shape for the land cover map
822 */
823 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
824 {
825   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
826 }
827
828 /**
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
833 */
834 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
835 {
836   if( theNewShape.IsNull() )
837     return false;
838
839   BOPCol_ListOfShape aShapesList;
840   BOPAlgo_PaveFiller aPaveFiller;
841   HYDROData_MapOfFaceToStricklerType aNewFaces;
842
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 );
848
849   //DEBTRACE("theNewType " << theNewType);
850   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
851   {
852     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
853     StoreLandCovers( aNewFaces );
854     return true;
855   }
856
857   // prepare pave filler
858   aPaveFiller.SetArguments( aShapesList );
859   aPaveFiller.Perform();
860   Standard_Integer anError = aPaveFiller.ErrorStatus();
861   if( anError )
862     return false;
863
864   // add faces to builder
865   BOPAlgo_Builder aBuilder;
866   anIt.Init( *this );
867   for( ; anIt.More(); anIt.Next() )
868     aBuilder.AddArgument( anIt.Face() );
869   aBuilder.AddArgument( theNewShape );
870
871   // perform the partition with the pave filler
872   aBuilder.PerformWithFiller( aPaveFiller );
873   anError = aBuilder.ErrorStatus();
874   if( anError )
875     return false;
876
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() )
885   {
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 );
890   }
891
892   //     b. to fill map of parts except parts from new face
893   anIt.Init( *this );
894   for( ; anIt.More(); anIt.Next() )
895   {
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() );
900     //
901     if( aModified.Extent() == 0 )
902       aModified.Append( anIt.Face() );
903     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
904
905     TopTools_ListIteratorOfListOfShape aMIt( aModified );
906     for( ; aMIt.More(); aMIt.Next() )
907     {
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 );
915     }
916     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
917   }
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);
922   
923   // convert map of shape to type to compound and list of types
924   StoreLandCovers( aNewFaces );
925
926 //  anIt.Init( *this );
927 //  for( ; anIt.More(); anIt.Next() )
928 //  {
929 //     DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
930 //  }
931   return true;
932 }
933
934 /**
935   Replace the set of land covers in the land cover map
936   @param theMap the map of shape (face) to Strickler type (string)
937 */
938 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
939 {
940   TopTools_ListOfShape aListOfFaces;
941
942   //DEBTRACE("theMap.Extent() " << theMap.Extent());
943   for( int i = 1; i <= theMap.Extent(); i++ )
944   {
945     TopoDS_Face aFace = theMap.FindKey(i);
946     if( aFace.IsNull() )
947       continue;
948     aListOfFaces.Append(aFace);
949   }
950
951   TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
952   ShHistory.Clear();
953
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 );
959
960   //remove internal edges
961   //if nothing changes => the result shape should be the same
962   //hence the map will be empty
963
964   NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
965   RemoveInternal(aResult, &ShF2FHistory);
966
967   //one face => mark as unchanged
968   if( aListOfFaces.Extent() == 1 )
969     ShHistory.Add(aResult, TopTools_ListOfShape());
970
971   NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
972   QStringList aSTypes;
973   //
974   for( int i = 1; i <= theMap.Extent(); i++ )
975   {
976     TopoDS_Face aFF = theMap.FindKey(i);
977     //DEBTRACE("  --- " << aFF);
978     if( aFF.IsNull() )
979       continue;
980     //DEBTRACE(ShHistory.IsEmpty());
981     //DEBTRACE(aFF.Checked());
982     TopTools_ListOfShape aLS;
983     try
984     {
985       aLS = ShHistory.FindFromKey(aFF); //TODO: bug to fix. Observed on an incomplete split of a face
986     }
987     catch (...)
988     {
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
991     }
992     if (aLS.IsEmpty())
993     {
994       //DEBTRACE("--- aLS.IsEmpty()");
995       QString aSType = theMap.FindFromKey(aFF);
996       //DEBTRACE("  --- " << aSType.toStdString());
997       if (ShF2FHistory.Contains(aFF))
998         {
999           //DEBTRACE("ShF2FHistory.FindFromKey(aFF) " << ShF2FHistory.FindFromKey(aFF));
1000           aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
1001         }
1002       else
1003         {
1004           //DEBTRACE("aFF " << aFF);
1005           aChF2ST.Add(aFF, aSType);
1006         }
1007     }
1008     else
1009     {
1010       //DEBTRACE("--- !aLS.IsEmpty()");
1011       TopTools_ListIteratorOfListOfShape anIt(aLS);
1012       for (; anIt.More(); anIt.Next())
1013       {
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))
1019           {
1020             //DEBTRACE("ShF2FHistory.FindFromKey(aMF) " << ShF2FHistory.FindFromKey(aFF));
1021             aChF2ST.Add(ShF2FHistory.FindFromKey(aMF), aSType);
1022           }
1023         else
1024           {
1025             //DEBTRACE("aMF " << aMF);
1026             aChF2ST.Add(aMF, aSType);
1027           }
1028       }
1029     }
1030   }
1031   //
1032   SetShape( aResult );
1033   //
1034   //Explorer Exp(*this);
1035   TopExp_Explorer FExp(aResult, TopAbs_FACE);
1036   for( ; FExp.More(); FExp.Next() )
1037   {
1038     TopoDS_Face aFace = TopoDS::Face(FExp.Current());
1039     QString aST = ""; 
1040     if (aChF2ST.Contains(aFace))
1041       aST = aChF2ST.FindFromKey(aFace);
1042     //DEBTRACE("aFace "  << aFace << " aST " << aST.toStdString());
1043     aSTypes << aST;
1044   }
1045   
1046   Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
1047   int k = 0;
1048   foreach (QString aST, aSTypes)
1049   {
1050     //DEBTRACE("aST " << aST.toStdString());
1051     aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1052     k++;
1053   }
1054
1055 }
1056
1057 /**
1058    Checks that object has 2D presentation. Reimlemented to retun true.
1059 */
1060 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1061 {
1062   return true;
1063 }
1064
1065 /**
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
1070 */
1071 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1072 {
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 )
1077     {
1078       theType = anIt.StricklerType();
1079       return anIt.Face();
1080     }
1081
1082   theType = "";
1083   return TopoDS_Face();
1084 }
1085
1086 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1087 {
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 ) );
1091 }
1092
1093 /**
1094   Dump to Python
1095   @param theTreatedObjects the map of treated objects
1096 */
1097 QStringList HYDROData_LandCoverMap::DumpToPython( const QString&       thePyScriptPath,
1098                                                   MapOfTreatedObjects& theTreatedObjects ) const
1099 {
1100   QStringList aResList = dumpObjectCreation( theTreatedObjects );
1101   QString aName = GetObjPyName();
1102
1103   QString aShpFileName = thePyScriptPath;
1104   aShpFileName.replace( ".py", ".shp" );
1105   QString aDbfFileName = thePyScriptPath;
1106   aDbfFileName.replace( ".py", ".dbf" );
1107
1108   ExportSHP( aShpFileName, true, 0.1 );
1109
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 );
1114
1115   aResList << QString( "%1.ImportSHP( '%2' )" ).
1116     arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1117
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 );
1122
1123   return aResList;
1124 }
1125
1126 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1127 {
1128   //DEBTRACE("RemoveInternal");
1129   //Shape must be topologically correct
1130   TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1131   //
1132   for(; anExpF.More(); anExpF.Next() )
1133   {
1134     TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1135     //
1136     TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1137     TopTools_ListOfShape anEdgesToRemove;
1138     //
1139     for(; anExp.More(); anExp.Next() )
1140     {
1141       TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1142       if (CurEdge.Orientation() == TopAbs_INTERNAL)
1143         anEdgesToRemove.Append(CurEdge);
1144     }
1145     //
1146     if (!anEdgesToRemove.IsEmpty())
1147     {
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);
1154
1155       Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1156       sfs->Init(OutF);
1157       sfs->Perform();
1158       OutF = sfs->Shape();
1159       aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1160     }
1161   }
1162   //
1163   Handle_ShapeBuild_ReShape anExtReshape = new ShapeBuild_ReShape();
1164   for (int i = 1; i <= aF2FReplace->Extent(); i++)
1165   {
1166     TopoDS_Face aFK = aF2FReplace->FindKey(i);
1167     TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1168     anExtReshape->Replace(aFK, aFV);
1169     ShToRebuild = anExtReshape->Apply(ShToRebuild);
1170   }
1171 //  for (int i = 1; i <= aF2FReplace->Extent(); i++)
1172 //    {
1173 //      DEBTRACE("aF2FReplace key,value " << aF2FReplace->FindKey(i) << " " << aF2FReplace->FindFromIndex(i));
1174 //    }
1175
1176 }
1177
1178 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1179 {
1180   SetDouble( DataTag_Transparency, theTransparency );
1181 }
1182
1183 double HYDROData_LandCoverMap::GetTransparency() const
1184 {
1185   return GetDouble( DataTag_Transparency, 0.5 );
1186 }
1187
1188 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1189                                         const QList<int>& theIndices )
1190 {
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);
1196   //
1197   if (Stat != 1)
1198     return false;
1199   //
1200   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1201   int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1202   if (maxInd > aPolyList.length())
1203     return false;
1204   //
1205   if (theIndices.empty())
1206   {
1207     //import all shapes
1208     for ( int i = 1; i <=aFaces.Length(); i++ )
1209     {
1210       TopoDS_Shape aShape = aFaces(i);
1211       if ( aShape.IsNull() ) 
1212         continue;
1213       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1214     }
1215   }
1216   else
1217   {
1218     //import given indices
1219     foreach ( int Ind, theIndices )
1220     {
1221       TopoDS_Shape aShape = aFaces(Ind + 1);
1222       if ( aShape.IsNull() ) 
1223         continue;
1224       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1225     }
1226   }
1227   //
1228   StoreLandCovers(aMapFace2ST);
1229   return true;
1230 }
1231
1232 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1233 {
1234   HYDROData_ShapeFile anExporter;
1235   QStringList aList;
1236   anExporter.Export(HYDROData_Document::Document(1), theSHPFileName, this, aList, bUseDiscr, theDefl );
1237   if (aList.empty())
1238     return true;
1239   else 
1240     return false;
1241 }
1242
1243 bool HYDROData_LandCoverMap::CheckLinear()
1244 {
1245   TopoDS_Shape InpShape = GetShape();
1246   TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1247   for (; anEdgeEx.More(); anEdgeEx.Next()) 
1248   {
1249     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1250     double aFP, aLP;
1251     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
1252     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1253     if (aLine.IsNull())
1254     {
1255       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1256       if (!aTC.IsNull())
1257       {
1258         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1259         if (aLine.IsNull())
1260           return false;
1261       }
1262       else
1263         return false;
1264     }
1265   }
1266   return true;
1267 }
1268
1269 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1270 {
1271   TopoDS_Shape aShape = GetShape();
1272   TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1273   SetShape( aLocatedShape );
1274 }
1275
1276 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints, std::vector<std::set <QString> >& theTypes ) const
1277 {
1278   HYDROData_LCM_FaceClassifier FC(this);
1279   FC.Classify(thePoints, theTypes, NULL);
1280 }
1281
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
1285 {
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++)
1291   {
1292     const std::set<QString>& SStr = Types[i];
1293     if (SStr.empty())
1294       theCoeffs[i] = DefValue;
1295     else
1296     {
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 ));
1301       double Val;
1302       if (UseMax)
1303         Val = *(std::max_element( C1.begin(), C1.end() ) );
1304       else
1305         Val = *(std::min_element( C1.begin(), C1.end() ) );
1306       theCoeffs[i] = Val;
1307     }
1308   }
1309 }