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