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