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