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