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>
24 #include <BOPAlgo_Builder.hxx>
25 #include <BOPAlgo_PaveFiller.hxx>
26 #include <BOPCol_ListOfShape.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepAlgoAPI_Fuse.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30 #include <NCollection_IndexedMap.hxx>
32 #include <TopoDS_Compound.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Shell.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
41 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
43 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
49 @param theMap the land cover map to iterate through
51 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
56 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
68 Initialize the iterator
69 @param theMap the land cover map to iterate through
71 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
73 TopoDS_Shape aShape = theMap.GetShape();
77 myIterator = new TopoDS_Iterator( aShape );
79 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
85 HYDROData_LandCoverMap::Iterator::~Iterator()
91 Return if the iterator has more elements
92 @return if the iterator has more elements
94 bool HYDROData_LandCoverMap::Iterator::More() const
96 return !myArray.IsNull() && myIterator && myIterator->More();
100 Move iterator to the next element
102 void HYDROData_LandCoverMap::Iterator::Next()
112 Get the current land cover (face)
113 @return the land cover's face
115 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
118 return TopoDS::Face( myIterator->Value() );
120 return TopoDS_Face();
124 Get the current land cover's Strickler type
125 @return the land cover's Strickler type
127 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
129 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
132 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
138 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
145 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
151 @return object's kind
153 const ObjectKind HYDROData_LandCoverMap::GetKind() const
155 return KIND_LAND_COVER_MAP;
159 Import the land cover map from QGIS
160 @param theFileName the name of file
161 @return if the import is successful
163 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
170 Export the land cover map to QGIS
171 @param theFileName the name of file
172 @return if the export is successful
174 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
181 Export the land cover map for the solver (Telemac)
182 @param theFileName the name of file
183 @return if the export is successful
185 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
192 Add a new object as land cover
193 @param theObject the object to add as land cover
194 @param theType the Strickler type for the new land cover
195 @return if the addition is successful
197 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
199 if( theObject.IsNull() )
202 TopoDS_Shape aShape = theObject->GetTopShape();
203 if( aShape.ShapeType()!=TopAbs_FACE )
206 TopoDS_Face aFace = TopoDS::Face( aShape );
207 return LocalPartition( aFace, theType );
211 Add a new polyline as land cover
212 @param thePolyline the polyline to add as land cover
213 @param theType the Strickler type for the new land cover
214 @return if the addition is successful
216 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
218 if( thePolyline.IsNull() )
221 TopoDS_Shape aShape = thePolyline->GetShape();
222 if( aShape.ShapeType()!=TopAbs_WIRE )
225 TopoDS_Wire aWire = TopoDS::Wire( aShape );
226 if( !aWire.Closed() )
229 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
230 return LocalPartition( aFace, theType );
234 Remove the given face from land cover map
235 @param theFace the face to be removed
236 @return if the removing is successful
238 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
240 TopTools_ListOfShape aList;
241 aList.Append( theFace );
242 return Remove( aList );
246 Remove the given faces from land cover map
247 @param theFacesToRemove the face list to be removed
248 @return if the removing is successful
250 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
252 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
253 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
254 for( ; aFIt.More(); aFIt.Next() )
256 TopoDS_Shape aShape = aFIt.Value();
257 if( aShape.ShapeType()==TopAbs_FACE )
258 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
261 Iterator anIt( *this );
262 for( ; anIt.More(); anIt.Next() )
263 if( !aFacesToRemove.Contains( anIt.Face() ) )
264 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
266 StoreLandCovers( aNewFaces );
271 Split the land cover map by the given polyline
272 @param thePolyline the tool polyline to split the land cover map
273 @return if the removing is successful
275 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
277 if( thePolyline.IsNull() )
280 TopoDS_Shape aShape = thePolyline->GetShape();
281 return LocalPartition( aShape, "" );
285 Merge the given faces in the land cover
286 @param theFaces the faces to merge in the land cover map
287 @param theType the Strickler type for the merged land cover
288 @return if the merge is successful
290 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
292 // 1. to remove the merged faces from the current map
295 // 2. to fuse the faces into the new face
296 BRepAlgoAPI_Fuse aFuse;
297 aFuse.SetArguments( theFaces );
299 if( !aFuse.IsDone() )
302 TopoDS_Shape aMergedShape = aFuse.Shape();
303 if( aMergedShape.ShapeType()!=TopAbs_FACE )
306 // 3. to add the face into the map
307 return LocalPartition( TopoDS::Face( aMergedShape ), theType );
311 Get the shape of the land cover map
313 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
315 return HYDROData_Entity::GetShape( DataTag_Shape );
319 Set the shape of the land cover map
320 @param theShape the new shape for the land cover map
322 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
324 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
328 Perform the local partition algorithm on the land cover
329 @param theNewShape the new shape to add into the land cover
330 @param theNewType the new Strickler type for the new land cover
331 @return if the local partition is successful
333 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
335 if( theNewShape.IsNull() )
338 BOPCol_ListOfShape aShapesList;
339 BOPAlgo_PaveFiller aPaveFiller;
340 HYDROData_MapOfFaceToStricklerType aNewFaces;
342 // add faces to shapes list
343 Iterator anIt( *this );
344 for( ; anIt.More(); anIt.Next() )
345 aShapesList.Append( anIt.Face() );
346 aShapesList.Append( theNewShape );
348 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
350 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
351 StoreLandCovers( aNewFaces );
355 // prepare pave filler
356 aPaveFiller.SetArguments( aShapesList );
357 aPaveFiller.Perform();
358 Standard_Integer anError = aPaveFiller.ErrorStatus();
362 // add faces to builder
363 BOPAlgo_Builder aBuilder;
365 for( ; anIt.More(); anIt.Next() )
366 aBuilder.AddArgument( anIt.Face() );
367 aBuilder.AddArgument( theNewShape );
369 // perform the partition with the pave filler
370 aBuilder.PerformWithFiller( aPaveFiller );
371 anError = aBuilder.ErrorStatus();
375 // analysis of the history
376 // a. to fill map of shapes which come from the new face
377 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
378 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
379 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
380 TopTools_ListIteratorOfListOfShape aMIt( aModified );
381 for( ; aMIt.More(); aMIt.Next() )
383 //std::cout << " " << aMIt.Value() << std::endl;
384 aShapesFromNewFace.Add( aMIt.Value() );
387 // b. to fill map of parts except parts from new face
389 for( ; anIt.More(); anIt.Next() )
391 QString aSType = anIt.StricklerType();
392 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
393 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
394 TopTools_ListIteratorOfListOfShape aMIt( aModified );
395 for( ; aMIt.More(); aMIt.Next() )
397 TopoDS_Shape aShape = aMIt.Value();
398 bool isFace = aShape.ShapeType()==TopAbs_FACE;
399 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
400 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
401 if( isFace && !isAlsoFromNew )
402 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
406 // c. add the new shape if it is face with its type
407 if( theNewShape.ShapeType()==TopAbs_FACE )
408 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
410 // convert map of shape to type to compound and list of types
411 StoreLandCovers( aNewFaces );
416 Replace the set of land covers in the land cover map
417 @param theMap the map of shape (face) to Strickler type (string)
419 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
421 TopoDS_Compound aCompound;
422 BRep_Builder aCompoundBuilder;
423 aCompoundBuilder.MakeCompound( aCompound );
425 int n = theMap.Size();
426 Handle( TDataStd_ExtStringArray ) aTypes =
427 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
428 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
429 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
431 TopoDS_Face aFace = aNFIt.Key();
432 QString aType = aNFIt.Value();
433 aCompoundBuilder.Add( aCompound, aFace );
434 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
437 SetShape( aCompound );