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 <NCollection_IndexedMap.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TNaming_NamedShape.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 )
223 Remove the given face from land cover map
224 @param theFace the face to be removed
225 @return if the removing is successful
227 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
229 TopTools_ListOfShape aList;
230 aList.Append( theFace );
231 return Remove( aList );
235 Remove the given faces from land cover map
236 @param theFacesList the face list to be removed
237 @return if the removing is successful
239 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesList )
241 TopoDS_Compound aCompound = TopoDS::Compound( GetShape() );
242 BRep_Builder aCompoundBuilder;
243 //TODO: aCompoundBuilder.Remove( aCompound, theFace );
245 SetShape( aCompound );
250 Split the land cover map by the given polyline
251 @param thePolyline the tool polyline to split the land cover map
252 @return if the removing is successful
254 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
256 if( thePolyline.IsNull() )
259 TopoDS_Shape aShape = thePolyline->GetShape();
260 return LocalPartition( aShape, "" );
264 Merge the given faces in the land cover
265 @param theFaces the faces to merge in the land cover map
266 @param theType the Strickler type for the merged land cover
267 @return if the merge is successful
269 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
271 // 1. to remove the merged faces from the current map
274 // 2. to fuse the faces into the new face
276 TopoDS_Face aMergedFace;
278 // 3. to add the face into the map
279 return LocalPartition( aMergedFace, theType );
283 Get the shape of the land cover map
285 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
287 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
288 if ( !aLabel.IsNull() )
290 Handle(TNaming_NamedShape) aNamedShape;
291 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
292 return aNamedShape->Get();
294 return TopoDS_Shape();
298 Set the shape of the land cover map
299 @param theShape the new shape for the land cover map
301 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
303 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
304 aBuilder.Generated( theShape );
308 Perform the local partition algorithm on the land cover
309 @param theNewShape the new shape to add into the land cover
310 @param theNewType the new Strickler type for the new land cover
311 @return if the local partition is successful
313 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
315 if( theNewShape.IsNull() )
318 BOPCol_ListOfShape aShapesList;
319 BOPAlgo_PaveFiller aPaveFiller;
320 HYDROData_MapOfFaceToStricklerType aNewFaces;
322 // add faces to shapes list
323 Iterator anIt( *this );
324 for( ; anIt.More(); anIt.Next() )
325 aShapesList.Append( anIt.Face() );
326 aShapesList.Append( theNewShape );
328 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
330 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
331 StoreLandCovers( aNewFaces );
335 // prepare pave filler
336 aPaveFiller.SetArguments( aShapesList );
337 aPaveFiller.Perform();
338 Standard_Integer anError = aPaveFiller.ErrorStatus();
342 // add faces to builder
343 BOPAlgo_Builder aBuilder;
345 for( ; anIt.More(); anIt.Next() )
346 aBuilder.AddArgument( anIt.Face() );
347 aBuilder.AddArgument( theNewShape );
349 // perform the partition with the pave filler
350 aBuilder.PerformWithFiller( aPaveFiller );
351 anError = aBuilder.ErrorStatus();
355 // analysis of the history
356 // a. to fill map of shapes which come from the new face
357 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
358 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
359 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
360 TopTools_ListIteratorOfListOfShape aMIt( aModified );
361 for( ; aMIt.More(); aMIt.Next() )
363 //std::cout << " " << aMIt.Value() << std::endl;
364 aShapesFromNewFace.Add( aMIt.Value() );
367 // b. to fill map of parts except parts from new face
369 for( ; anIt.More(); anIt.Next() )
371 QString aSType = anIt.StricklerType();
372 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
373 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
374 TopTools_ListIteratorOfListOfShape aMIt( aModified );
375 for( ; aMIt.More(); aMIt.Next() )
377 TopoDS_Shape aShape = aMIt.Value();
378 bool isFace = aShape.ShapeType()==TopAbs_FACE;
379 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
380 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
381 if( isFace && !isAlsoFromNew )
382 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
386 // c. add the new shape if it is face with its type
387 if( theNewShape.ShapeType()==TopAbs_FACE )
388 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
390 // convert map of shape to type to compound and list of types
391 StoreLandCovers( aNewFaces );
396 Replace the set of land covers in the land cover map
397 @param theMap the map of shape (face) to Strickler type (string)
399 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
401 TopoDS_Compound aCompound;
402 BRep_Builder aCompoundBuilder;
403 aCompoundBuilder.MakeCompound( aCompound );
405 int n = theMap.Size();
406 Handle( TDataStd_ExtStringArray ) aTypes =
407 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
408 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
409 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
411 TopoDS_Face aFace = aNFIt.Key();
412 QString aType = aNFIt.Value();
413 aCompoundBuilder.Add( aCompound, aFace );
414 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
417 SetShape( aCompound );