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