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