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