Salome HOME
patch for correct compilation (Windows)
[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) && (l0<=1) && (l1>=0) && (l1<=1) && (l2>=0) && (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, int theMethod) const
354 {
355   DEBTRACE("GetAltitudeForPoint p(" << thePoint.X() << ", " << thePoint.Y() << "), interpolation method: " << theMethod);
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 = false;
388   if (theMethod)
389     isBathyInterpolRequired =true;
390   if (isBathyInterpolRequired)
391     {
392       vtkPolyData* aDelaunay2D = GetVtkDelaunay2D();
393       vtkIdList* cells= vtkIdList::New();
394       cells->Allocate(64);
395       vtkIdList* points= vtkIdList::New();
396       points->Allocate(64);
397       aDelaunay2D->GetPointCells(nodeIndex, cells);
398       vtkIdType nbCells = cells->GetNumberOfIds();
399       DEBTRACE("  triangles on nearest point: " << nbCells);
400       bool isInside = false;
401       for (int i=0; i<nbCells; i++)
402         {
403           aDelaunay2D->GetCellPoints(cells->GetId(i), points);
404           double z = 0;
405           isInside = interpolZtriangle(thePoint, aDelaunay2D, points, z);
406           if (isInside)
407             {
408               aResAltitude = z;
409               DEBTRACE("  interpolated z: " << z);
410               break;
411             }
412         }
413       if (!isInside) DEBTRACE("  point outside triangles, nearest z kept");
414     }
415   return aResAltitude;
416 }
417
418 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
419 {
420   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
421 }
422
423 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
424 {
425   TCollection_AsciiString aRes;
426
427   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
428   if ( !aLabel.IsNull() )
429   {
430     Handle(TDataStd_AsciiString) anAsciiStr;
431     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
432       aRes = anAsciiStr->Get();
433   }
434
435   return aRes;
436 }
437
438 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
439                                                  const bool theIsUpdate )
440 {
441   bool anIsAltitudesInverted = IsAltitudesInverted();
442   if ( anIsAltitudesInverted == theIsInverted )
443     return;
444
445   TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
446
447   Changed( Geom_Z );
448
449   if ( !theIsUpdate )
450     return;
451
452   // Update altitude points
453   AltitudePoints anAltitudePoints = GetAltitudePoints();
454   if ( anAltitudePoints.IsEmpty() )
455     return;
456
457   AltitudePoints::Iterator anIter( anAltitudePoints );
458   for ( ; anIter.More(); anIter.Next() )
459   {
460     AltitudePoint& aPoint = anIter.ChangeValue();
461     aPoint.SetZ( aPoint.Z() * -1 );
462   }
463
464   SetAltitudePoints( anAltitudePoints );
465 }
466
467 bool HYDROData_Bathymetry::IsAltitudesInverted() const
468 {
469   bool aRes = false;
470
471   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
472   if ( !aLabel.IsNull() )
473   {
474     Handle(TDataStd_Integer) anIntVal;
475     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
476       aRes = (bool)anIntVal->Get();
477   }
478
479   return aRes;
480 }
481
482 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
483 {
484   // Try to open the file
485   QFile aFile( theFileName.ToCString() );
486   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
487     return false;
488
489   bool aRes = false;
490
491   QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
492
493   AltitudePoints aPoints;
494
495   // Try to import the file
496   if ( aFileSuf == "xyz" )
497     aRes = importFromXYZFile( aFile, aPoints );
498   else if ( aFileSuf == "asc" )
499     aRes = importFromASCFile( aFile, aPoints );
500
501   // Close the file
502   aFile.close();
503   
504
505   // Convert from global to local CS
506   Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
507   AltitudePoints::Iterator anIter( aPoints );
508   for ( ; anIter.More(); anIter.Next() )
509   {
510     AltitudePoint& aPoint = anIter.ChangeValue();
511     aDoc->Transform( aPoint, true );
512   }
513
514   if ( aRes )
515   {
516     // Update file path and altitude points of this Bathymetry
517     SetFilePath( theFileName );
518     SetAltitudePoints( aPoints );
519   }
520
521   return aRes && !aPoints.IsEmpty();
522 }
523
524 bool HYDROData_Bathymetry::importFromXYZFile( QFile&          theFile,
525                                               AltitudePoints& thePoints ) const
526 {
527   if ( !theFile.isOpen() )
528     return false;
529
530   // Strings in file is written as:
531   //  1. X(float) Y(float) Z(float)
532   //  2. X(float) Y(float) Z(float)
533   //  ...
534
535 #ifdef _TIMER
536   OSD_Timer aTimer;
537   aTimer.Start();
538 #endif
539
540   bool anIsAltitudesInverted = IsAltitudesInverted();
541   while ( !theFile.atEnd() )
542   {
543     QString aLine = theFile.readLine().simplified();
544     if ( aLine.isEmpty() )
545       continue;
546
547     QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
548     if ( aValues.length() < 3 )
549       return false;
550
551     AltitudePoint aPoint;
552     
553     QString anX = aValues.value( 0 );
554     QString anY = aValues.value( 1 );
555     QString aZ  = aValues.value( 2 );
556
557     bool isXOk = false, isYOk = false, isZOk = false;
558
559     aPoint.SetX( anX.toDouble( &isXOk ) );
560     aPoint.SetY( anY.toDouble( &isYOk ) );
561     aPoint.SetZ( aZ.toDouble( &isZOk ) );
562
563     if ( !isXOk || !isYOk || !isZOk )
564       return false;
565
566     if ( HYDROData_Tool::IsNan( aPoint.X() ) || HYDROData_Tool::IsInf( aPoint.X() ) ||
567          HYDROData_Tool::IsNan( aPoint.Y() ) || HYDROData_Tool::IsInf( aPoint.Y() ) ||
568          HYDROData_Tool::IsNan( aPoint.Z() ) || HYDROData_Tool::IsInf( aPoint.Z() ) )
569       return false;
570
571     // Invert the z value if requested
572     if ( anIsAltitudesInverted )
573       aPoint.SetZ( -aPoint.Z() );
574
575     thePoints.Append( aPoint );
576   }
577
578 #ifdef _TIMER
579   aTimer.Stop();
580   std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
581   aTimer.Show( stream );
582 #endif
583
584   return true;
585 }
586
587 bool HYDROData_Bathymetry::importFromASCFile( QFile&          theFile,
588                                               AltitudePoints& thePoints ) const
589 {
590   if ( !theFile.isOpen() )
591     return false;
592
593   QString aLine;
594   QStringList aStrList;
595
596   int aNCols;
597   int aNRows;
598   double anXllCorner; 
599   double anYllCorner; 
600   double aCellSize; 
601   double aNoDataValue;
602
603   aLine = theFile.readLine().simplified();
604   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
605   if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
606     return false;
607   aNCols = aStrList[1].toInt();
608
609   aLine = theFile.readLine().simplified();
610   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
611   if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
612     return false;
613   aNRows = aStrList[1].toInt();
614
615   aLine = theFile.readLine().simplified();
616   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
617   if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
618     return false;
619   anXllCorner = aStrList[1].toDouble();
620
621   aLine = theFile.readLine().simplified();
622   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
623   if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
624     return false;
625   anYllCorner = aStrList[1].toDouble();
626
627   aLine = theFile.readLine().simplified();
628   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
629   if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
630     return false;
631   aCellSize = aStrList[1].toDouble();
632
633   aLine = theFile.readLine().simplified();
634   aStrList = aLine.split( ' ', QString::SkipEmptyParts );
635   if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
636     return false;
637   aNoDataValue = aStrList[1].toDouble();
638
639   bool anIsAltitudesInverted = IsAltitudesInverted();
640
641   int i = 0;
642   int aStrLength = 0;
643   while ( !theFile.atEnd() )
644   {
645     aLine = theFile.readLine().simplified();
646     aStrList = aLine.split( ' ', QString::SkipEmptyParts );
647
648     aStrLength =  aStrList.length();
649     if ( aStrLength == 0 )
650       continue;
651
652     if ( aStrLength != aNRows )
653       return false;
654
655     for (int j = 0; j < aNCols; j++)
656     {
657       if (aStrList[j].toDouble() != aNoDataValue)
658       {
659         AltitudePoint aPoint;
660         aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
661         aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
662         aPoint.SetZ(aStrList[j].toDouble());
663
664         if ( anIsAltitudesInverted )
665          aPoint.SetZ( -aPoint.Z() );
666
667         thePoints.Append(aPoint);
668       }
669     }
670     i++;
671
672   }
673
674   return true;
675
676 }
677
678
679 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
680 {
681   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
682   Handle_HYDROData_PolylineXY aResult = 
683     Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
684
685   if( aResult.IsNull() )
686     return aResult;
687
688   //search free name
689   QString aPolylinePref = GetName() + "_Boundary";
690   QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
691   aResult->SetName( aPolylineName );
692
693   double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
694   bool isFirst = true;
695   AltitudePoints aPoints = GetAltitudePoints();
696
697   AltitudePoints::Iterator anIter( aPoints );
698   for ( ; anIter.More(); anIter.Next() )
699   {
700     const AltitudePoint& aPoint = anIter.Value();
701
702     double x = aPoint.X(), y = aPoint.Y();
703     if( isFirst || x<Xmin )
704       Xmin = x;
705     if( isFirst || x>Xmax )
706       Xmax = x;
707     if( isFirst || y<Ymin )
708       Ymin = y;
709     if( isFirst || y>Ymax )
710       Ymax = y;
711     isFirst = false;
712   }
713
714   aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
715   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
716   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
717   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
718   aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
719   
720   aResult->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
721   
722   aResult->Update();
723
724   return aResult;
725 }
726
727 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
728 {
729   gp_XYZ aDelta( theDx, theDy, 0 );
730   AltitudePoints aPoints = GetAltitudePoints();
731   AltitudePoints::Iterator anIter( aPoints );
732   for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
733   {
734     AltitudePoint& aPoint = anIter.ChangeValue();
735     aPoint += aDelta;
736   }
737   SetAltitudePoints( aPoints );
738 }
739