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