Salome HOME
202ffe337a3007267452b65c5f35a3639410af98
[modules/hydro.git] / src / HYDROData / HYDROData_Bathymetry.cxx
1
2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
5 #include "HYDROData_PolylineXY.h"
6
7 #include <boost/math/special_functions/fpclassify.hpp>
8
9 #include <gp_XY.hxx>
10 #include <gp_XYZ.hxx>
11
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_AsciiString.hxx>
14 #include <TDataStd_Integer.hxx>
15
16 #include <QFile>
17 #include <QFileInfo>
18 #include <QPointF>
19 #include <QPolygonF>
20 #include <QStringList>
21
22 #include <math.h>
23
24 // #define _TIMER
25 #ifdef _TIMER
26 #include <OSD_Timer.hxx>
27 #endif
28
29 #define _DEVDEBUG_
30 #include "HYDRO_trace.hxx"
31
32 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
33 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
34
35 //HYDROData_QuadtreeNode* HYDROData_Bathymetry::myQuadtree = 0;
36 std::map<int, HYDROData_QuadtreeNode*> HYDROData_Bathymetry::myQuadtrees;
37
38 HYDROData_Bathymetry::HYDROData_Bathymetry()
39 : HYDROData_IAltitudeObject()
40 {
41   //DEBTRACE("HYDROData_Bathymetry constructor start " << this);
42 //  if (! myQuadtree)
43 //    myQuadtree = new HYDROData_QuadtreeNode(0, 30, 5, 0.);
44   //DEBTRACE("HYDROData_Bathymetry constructor end   " << this);
45 }
46
47 HYDROData_Bathymetry::~HYDROData_Bathymetry()
48 {
49   //DEBTRACE("HYDROData_Bathymetry destructor start " << this);
50 //  if (myQuadtree)
51 //    delete myQuadtree;
52 //      Nodes_3D::iterator it = myListOfNodes.begin();
53 //      for( ; it != myListOfNodes.end(); ++it)
54 //              delete *it;
55 //    myListOfNodes.clear();
56 }
57
58 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
59 {
60   QStringList aResList = dumpObjectCreation( theTreatedObjects );
61   QString aBathymetryName = GetObjPyName();
62
63   aResList << QString( "%1.SetAltitudesInverted( %2 );" )
64               .arg( aBathymetryName ).arg( IsAltitudesInverted() );
65
66   TCollection_AsciiString aFilePath = GetFilePath();
67   aResList << QString( "%1.ImportFromFile( \"%2\" );" )
68               .arg( aBathymetryName ).arg( aFilePath.ToCString() );
69
70   aResList << QString( "" );
71   aResList << QString( "%1.Update();" ).arg( aBathymetryName );
72   aResList << QString( "" );
73
74   return aResList;
75 }
76
77 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
78 {
79   RemoveAltitudePoints();
80
81   if ( thePoints.IsEmpty() )
82     return;
83
84   // Save coordinates
85   Handle(TDataStd_RealArray) aCoordsArray = 
86     TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
87
88   AltitudePoints::Iterator anIter( thePoints );
89   for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
90   {
91     const AltitudePoint& aPoint = anIter.Value();
92
93     aCoordsArray->SetValue( i * 3, aPoint.X() );
94     aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
95     aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
96   }
97
98   SetToUpdate( true );
99 }
100
101 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
102 {
103   AltitudePoints aPoints;
104
105   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
106   if ( aLabel.IsNull() )
107     return aPoints;
108
109   Handle(TDataStd_RealArray) aCoordsArray;
110   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
111     return aPoints;
112
113   for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
114   {
115     if ( i + 3 > n + 1 )
116       break;
117
118     AltitudePoint aPoint;
119     aPoint.SetX( aCoordsArray->Value( i++ ) );
120     aPoint.SetY( aCoordsArray->Value( i++ ) );
121     aPoint.SetZ( aCoordsArray->Value( i++ ) );
122
123     aPoints.Append( aPoint );
124   }
125
126   return aPoints;
127 }
128
129 HYDROData_QuadtreeNode* HYDROData_Bathymetry::GetQuadtreeNodes() const
130 {
131   TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
132   if (aLabel.IsNull())
133     return 0;
134   int labkey = myLab.Tag();
135   int altkey = aLabel.Tag();
136   //DEBTRACE("GetQuadtreeNodes this labkey altkey "<<this<<" "<<labkey<<" "<<altkey);
137 //  if (myQuadtree->isEmpty() )
138   if (myQuadtrees.find(labkey) == myQuadtrees.end())
139     {
140       DEBTRACE("GetQuadtreeNodes init " << this << " " << labkey);
141       HYDROData_QuadtreeNode* aQuadtree = new HYDROData_QuadtreeNode(0, 30, 5, 0.);
142       myQuadtrees[labkey] = aQuadtree;
143       TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
144       if (aLabel.IsNull())
145         return 0;
146
147       Handle(TDataStd_RealArray) aCoordsArray;
148       if (!aLabel.FindAttribute(TDataStd_RealArray::GetID(), aCoordsArray))
149         return 0;
150
151       Nodes_3D* aListOfNodes = new Nodes_3D();
152
153       for (int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n;)
154         {
155           if (i + 3 > n + 1)
156             break;
157
158           double x = aCoordsArray->Value(i++);
159           double y = aCoordsArray->Value(i++);
160           double z = aCoordsArray->Value(i++);
161           gp_XYZ* aPoint = new gp_XYZ(x, y, z);
162           aListOfNodes->push_back(aPoint);
163         }
164       DEBTRACE("  GetQuadtreeNodes call setNodesAndCompute");
165       aQuadtree->setNodesAndCompute(aListOfNodes);
166       return aQuadtree;
167     }
168   else
169     return myQuadtrees[labkey];
170 }
171
172 void HYDROData_Bathymetry::RemoveAltitudePoints()
173 {
174   TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
175   if (!aLabel.IsNull())
176     {
177       aLabel.ForgetAllAttributes();
178       SetToUpdate(true);
179     }
180 }
181
182 void interpolateAltitudeForPoints( const gp_XY&                               thePoint,
183                                    const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
184                                    const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
185                                    HYDROData_Bathymetry::AltitudePoint&       theResPoint,
186                                    const bool&                                theIsVertical )
187 {
188   double aCoordX = thePoint.X();
189   double aCoordY = thePoint.Y();
190
191   if ( theIsVertical )
192   {
193     aCoordX = theFirstPoint.X();
194
195     if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
196     {
197       // Recalculate X coordinate by equation of line from two points
198       aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
199                   ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
200     }
201   }
202   else
203   {
204     aCoordY = theFirstPoint.Y();
205
206     if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
207     {
208       // Recalculate y by equation of line from two points
209       aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
210                   ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
211     }
212   }
213
214   theResPoint.SetX( aCoordX );
215   theResPoint.SetY( aCoordY );
216
217   // Calculate coefficient for interpolation
218   double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
219                          Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
220
221   double aInterCoeff = 0;
222   if ( aLength != 0 )
223    aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
224
225
226   double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
227                             Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
228
229   // Calculate interpolated value
230   double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
231
232   theResPoint.SetZ( aResVal );
233 }
234
235 double HYDROData_Bathymetry::GetAltitudeForPoint(const gp_XY& thePoint) const
236 {
237   //DEBTRACE("GetAltitudeForPoint p(" << thePoint.X() << ", " << thePoint.Y() << ")");
238   double anInvalidAltitude = GetInvalidAltitude();
239   double aResAltitude = anInvalidAltitude;
240
241   HYDROData_QuadtreeNode* aQuadtree = GetQuadtreeNodes();
242   if (!aQuadtree)
243     {
244       DEBTRACE("  no Quadtree");
245       return aResAltitude;
246     }
247
248   std::map<double, const gp_XYZ*> dist2nodes;
249   aQuadtree->NodesAround(thePoint, dist2nodes, 1.0);
250   if (dist2nodes.size())
251     {
252       std::map<double, const gp_XYZ*>::const_iterator it = dist2nodes.begin();
253       aResAltitude = it->second->Z();
254       //DEBTRACE("  number of points found: " << dist2nodes.size() << " nearest z: " << aResAltitude);
255     }
256
257   return aResAltitude;
258   
259
260 //  AltitudePoints anAltitudePoints = GetAltitudePoints();
261 //  if ( anAltitudePoints.IsEmpty() )
262 //    return aResAltitude;
263 //
264 //  QPolygonF aBoundingRect;
265 //
266 //  // Boundary plane
267 //  // [ 0 (top-left) ]          [ 1 (top-right) ]
268 //  //                  thePoint
269 //  // [ 2 (bot-left) ]          [ 3 (bot-right) ]
270 //  AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
271 //                                 AltitudePoint(  DBL_MAX, -DBL_MAX, anInvalidAltitude ),
272 //                                 AltitudePoint( -DBL_MAX,  DBL_MAX, anInvalidAltitude ),
273 //                                 AltitudePoint(  DBL_MAX,  DBL_MAX, anInvalidAltitude ) };
274 //
275 //  AltitudePoints::Iterator anIter( anAltitudePoints );
276 //  for ( ; anIter.More(); anIter.Next() )
277 //  {
278 //    const AltitudePoint& aPoint = anIter.Value();
279 //
280 //    double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
281 //    double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
282 //
283 //    if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
284 //    {
285 //      if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
286 //      {
287 //        aResAltitude = aPoint.Z();
288 //        return aResAltitude;
289 //      }
290 //      else if ( aDeltaY < 0 ) // top side
291 //      {
292 //        // top border
293 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
294 //          aBounds[ 0 ] = aPoint;
295 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
296 //          aBounds[ 1 ] = aPoint;
297 //      }
298 //      else
299 //      {
300 //        // bottom border
301 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
302 //          aBounds[ 2 ] = aPoint;
303 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
304 //          aBounds[ 3 ] = aPoint;
305 //      }
306 //    }
307 //    else if ( aDeltaX < 0 ) // left side
308 //    {
309 //      if ( ValuesEquals( aDeltaY, 0.0 ) )
310 //      {
311 //        // Left border
312 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
313 //          aBounds[ 0 ] = aPoint;
314 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
315 //          aBounds[ 2 ] = aPoint;
316 //      }
317 //      else if ( aDeltaY < 0 )
318 //      {
319 //        // top left corner
320 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
321 //          aBounds[ 0 ] = aPoint;
322 //      }
323 //      else
324 //      {
325 //        // bottom left corner
326 //        if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
327 //          aBounds[ 2 ] = aPoint;
328 //      }
329 //    }
330 //    else // right side
331 //    {
332 //      if ( ValuesEquals( aDeltaY, 0.0 ) )
333 //      {
334 //        // Right border
335 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
336 //          aBounds[ 1 ] = aPoint;
337 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
338 //          aBounds[ 3 ] = aPoint;
339 //      }
340 //      else if ( aDeltaY < 0 )
341 //      {
342 //        // top right corner
343 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
344 //          aBounds[ 1 ] = aPoint;
345 //      }
346 //      else
347 //      {
348 //        // bottom right corner
349 //        if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
350 //          aBounds[ 3 ] = aPoint;
351 //      }
352 //    }
353 //
354 //    // Update bounding rectangle of our global grid
355 //    aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
356 //  }
357 //
358 //  const double LIMIT = 1E300;
359 //  if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
360 //      fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
361 //      fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
362 //      fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
363 //    return anInvalidAltitude;
364 //
365 //
366 //  // Check if requested point is inside of our bounding rectangle
367 //  if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
368 //    return aResAltitude;
369 //
370 //  // Calculate result altitude for point
371 //  AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
372 //
373 //  // At first we merge top and bottom borders
374 //  if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
375 //    interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
376 //
377 //  if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
378 //    interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
379 //
380 //  AltitudePoint aResPoint( aFirstPoint );
381 //
382 //  // At last we merge left and right borders
383 //  if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
384 //    interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
385 //
386 //  aResAltitude = aResPoint.Z();
387 //
388 //  return aResAltitude;
389 }
390
391 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
392 {
393   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
394 }
395
396 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
397 {
398   TCollection_AsciiString aRes;
399
400   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
401   if ( !aLabel.IsNull() )
402   {
403     Handle(TDataStd_AsciiString) anAsciiStr;
404     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
405       aRes = anAsciiStr->Get();
406   }
407
408   return aRes;
409 }
410
411 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
412                                                  const bool theIsUpdate )
413 {
414   bool anIsAltitudesInverted = IsAltitudesInverted();
415   if ( anIsAltitudesInverted == theIsInverted )
416     return;
417
418   TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
419
420   SetToUpdate( true );
421
422   if ( !theIsUpdate )
423     return;
424
425   // Update altitude points
426   AltitudePoints anAltitudePoints = GetAltitudePoints();
427   if ( anAltitudePoints.IsEmpty() )
428     return;
429
430   AltitudePoints::Iterator anIter( anAltitudePoints );
431   for ( ; anIter.More(); anIter.Next() )
432   {
433     AltitudePoint& aPoint = anIter.ChangeValue();
434     aPoint.SetZ( aPoint.Z() * -1 );
435   }
436
437   SetAltitudePoints( anAltitudePoints );
438 }
439
440 bool HYDROData_Bathymetry::IsAltitudesInverted() const
441 {
442   bool aRes = false;
443
444   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
445   if ( !aLabel.IsNull() )
446   {
447     Handle(TDataStd_Integer) anIntVal;
448     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
449       aRes = (bool)anIntVal->Get();
450   }
451
452   return aRes;
453 }
454
455 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
456 {
457   // Try to open the file
458   QFile aFile( theFileName.ToCString() );
459   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
460     return false;
461
462   bool aRes = false;
463
464   QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
465
466   AltitudePoints aPoints;
467
468   // Try to import the file
469   if ( aFileSuf == "xyz" )
470     aRes = importFromXYZFile( aFile, aPoints );
471
472   // Close the file
473   aFile.close();
474   
475
476   // Convert from global to local CS
477   Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
478   AltitudePoints::Iterator anIter( aPoints );
479   for ( ; anIter.More(); anIter.Next() )
480   {
481     AltitudePoint& aPoint = anIter.ChangeValue();
482     aDoc->Transform( aPoint, true );
483   }
484
485   if ( aRes )
486   {
487     // Update file path and altitude points of this Bathymetry
488     SetFilePath( theFileName );
489     SetAltitudePoints( aPoints );
490   }
491
492   return aRes && !aPoints.IsEmpty();
493 }
494
495 bool HYDROData_Bathymetry::importFromXYZFile( QFile&          theFile,
496                                               AltitudePoints& thePoints ) const
497 {
498   if ( !theFile.isOpen() )
499     return false;
500
501   // Strings in file is written as:
502   //  1. X(float) Y(float) Z(float)
503   //  2. X(float) Y(float) Z(float)
504   //  ...
505
506 #ifdef _TIMER
507   OSD_Timer aTimer;
508   aTimer.Start();
509 #endif
510
511   bool anIsAltitudesInverted = IsAltitudesInverted();
512   while ( !theFile.atEnd() )
513   {
514     QString aLine = theFile.readLine().simplified();
515     if ( aLine.isEmpty() )
516       continue;
517
518     QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
519     if ( aValues.length() < 3 )
520       return false;
521
522     AltitudePoint aPoint;
523     
524     QString anX = aValues.value( 0 );
525     QString anY = aValues.value( 1 );
526     QString aZ  = aValues.value( 2 );
527
528     bool isXOk = false, isYOk = false, isZOk = false;
529
530     aPoint.SetX( anX.toDouble( &isXOk ) );
531     aPoint.SetY( anY.toDouble( &isYOk ) );
532     aPoint.SetZ( aZ.toDouble( &isZOk ) );
533
534     if ( !isXOk || !isYOk || !isZOk )
535       return false;
536
537     if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
538          boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
539          boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
540       return false;
541
542     // Invert the z value if requested
543     if ( anIsAltitudesInverted )
544       aPoint.SetZ( -aPoint.Z() );
545
546     thePoints.Append( aPoint );
547   }
548
549 #ifdef _TIMER
550   aTimer.Stop();
551   std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
552   aTimer.Show( stream );
553 #endif
554
555   return true;
556 }
557
558
559 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
560 {
561   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
562   Handle_HYDROData_PolylineXY aResult = 
563     Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
564
565   if( aResult.IsNull() )
566     return aResult;
567
568   //search free name
569   QString aPolylinePref = GetName() + "_Boundary";
570   QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
571   aResult->SetName( aPolylineName );
572
573   double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
574   bool isFirst = true;
575   AltitudePoints aPoints = GetAltitudePoints();
576
577   AltitudePoints::Iterator anIter( aPoints );
578   for ( ; anIter.More(); anIter.Next() )
579   {
580     const AltitudePoint& aPoint = anIter.Value();
581
582     double x = aPoint.X(), y = aPoint.Y();
583     if( isFirst || x<Xmin )
584       Xmin = x;
585     if( isFirst || x>Xmax )
586       Xmax = x;
587     if( isFirst || y<Ymin )
588       Ymin = y;
589     if( isFirst || y>Ymax )
590       Ymax = y;
591     isFirst = false;
592   }
593
594   aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
595   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
596   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
597   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
598   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
599   aResult->Update();
600
601   return aResult;
602 }
603
604 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
605 {
606   gp_XYZ aDelta( theDx, theDy, 0 );
607   AltitudePoints aPoints = GetAltitudePoints();
608   AltitudePoints::Iterator anIter( aPoints );
609   for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
610   {
611     AltitudePoint& aPoint = anIter.ChangeValue();
612     aPoint += aDelta;
613   }
614   SetAltitudePoints( aPoints );
615 }
616