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