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