Salome HOME
bug #237: fatal error on profile
[modules/hydro.git] / src / HYDROData / HYDROData_Bathymetry.cxx
1
2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
5
6 #include <gp_XY.hxx>
7 #include <gp_XYZ.hxx>
8
9 #include <TDataStd_RealArray.hxx>
10 #include <TDataStd_AsciiString.hxx>
11 #include <TDataStd_Integer.hxx>
12
13 #include <QFile>
14 #include <QFileInfo>
15 #include <QPointF>
16 #include <QPolygonF>
17 #include <QStringList>
18
19 #include <math.h>
20
21 #define _TIMER
22 #ifdef _TIMER
23 #include <OSD_Timer.hxx>
24 #endif
25
26 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
27
28
29 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
30 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
31
32 HYDROData_Bathymetry::HYDROData_Bathymetry()
33 : HYDROData_IAltitudeObject()
34 {
35 }
36
37 HYDROData_Bathymetry::~HYDROData_Bathymetry()
38 {
39 }
40
41 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 {
43   QStringList aResList;
44
45   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
46   if ( aDocument.IsNull() )
47     return aResList;
48                              
49   QString aDocName = aDocument->GetDocPyName();
50   QString aBathymetryName = GetName();
51
52   aResList << QString( "%1 = %2.CreateObject( %3 );" )
53               .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
54   aResList << QString( "%1.SetName( \"%2\" );" )
55               .arg( aBathymetryName ).arg( aBathymetryName );
56
57   aResList << QString( "%1.SetAltitudesInverted( %2 );" )
58               .arg( aBathymetryName ).arg( IsAltitudesInverted() );
59
60   QString aFilePath = GetFilePath();
61   if ( !aFilePath.isEmpty() )
62   {
63     aResList << QString( "%1.ImportFromFile( \"%2\" );" )
64                 .arg( aBathymetryName ).arg( aFilePath );
65   }
66   else
67   {
68     // TODO : bathymetry is composed from other bathymetry(ies)
69   }
70
71   return aResList;
72 }
73
74 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
75 {
76   RemoveAltitudePoints();
77
78   if ( thePoints.isEmpty() )
79     return;
80
81   // Save coordinates
82   Handle(TDataStd_RealArray) aCoordsArray = 
83     TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
84
85   AltitudePoints::const_iterator aListItBeg =  thePoints.constBegin();
86   AltitudePoints::const_iterator aListItEnd =  thePoints.constEnd();
87   for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
88   {
89     const AltitudePoint& aPoint = *aListItBeg;
90
91     aCoordsArray->SetValue( i * 3, aPoint.X() );
92     aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
93     aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
94   }
95
96   SetToUpdate( true );
97 }
98
99 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
100 {
101   AltitudePoints aPoints;
102
103   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
104   if ( aLabel.IsNull() )
105     return aPoints;
106
107   Handle(TDataStd_RealArray) aCoordsArray;
108   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
109     return aPoints;
110
111   for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
112   {
113     if ( i + 3 > n + 1 )
114       break;
115
116     AltitudePoint aPoint;
117     aPoint.SetX( aCoordsArray->Value( i++ ) );
118     aPoint.SetY( aCoordsArray->Value( i++ ) );
119     aPoint.SetZ( aCoordsArray->Value( i++ ) );
120
121     aPoints << aPoint;
122   }
123
124   return aPoints;
125 }
126
127 void HYDROData_Bathymetry::RemoveAltitudePoints()
128 {
129   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
130   if ( !aLabel.IsNull() )
131   {
132     aLabel.ForgetAllAttributes();
133     SetToUpdate( true );
134   }
135 }
136
137 void interpolateAltitudeForPoints( const gp_XY&                               thePoint,
138                                    const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
139                                    const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
140                                    HYDROData_Bathymetry::AltitudePoint&       theResPoint,
141                                    const bool&                                theIsVertical )
142 {
143   double aCoordX = thePoint.X();
144   double aCoordY = thePoint.Y();
145
146   if ( theIsVertical )
147   {
148     aCoordX = theFirstPoint.X();
149
150     if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
151     {
152       // Recalculate X coordinate by equation of line from two points
153       aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
154                   ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
155     }
156   }
157   else
158   {
159     aCoordY = theFirstPoint.Y();
160
161     if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
162     {
163       // Recalculate y by equation of line from two points
164       aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
165                   ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
166     }
167   }
168
169   theResPoint.SetX( aCoordX );
170   theResPoint.SetY( aCoordY );
171
172   // Calculate coefficient for interpolation
173   double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
174                          Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
175
176   double aInterCoeff = 0;
177   if ( aLength != 0 )
178    aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
179
180
181   double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
182                             Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
183
184   // Calculate interpolated value
185   double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
186
187   theResPoint.SetZ( aResVal );
188 }
189
190 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
191 {
192   double anInvalidAltitude = GetInvalidAltitude();
193   double aResAltitude = anInvalidAltitude;
194   
195   AltitudePoints anAltitudePoints = GetAltitudePoints();
196   if ( anAltitudePoints.isEmpty() )
197     return aResAltitude;
198
199   QPolygonF aBoundingRect;
200
201   // Boundary plane
202   // [ 0 (top-left) ]          [ 1 (top-right) ]
203   //                  thePoint
204   // [ 2 (bot-left) ]          [ 3 (bot-right) ] 
205   AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
206                                  AltitudePoint(  DBL_MAX, -DBL_MAX, anInvalidAltitude ),
207                                  AltitudePoint( -DBL_MAX,  DBL_MAX, anInvalidAltitude ),
208                                  AltitudePoint(  DBL_MAX,  DBL_MAX, anInvalidAltitude ) }; 
209
210   AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
211   AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
212   for ( ; aListItBeg != aListItEnd; ++aListItBeg )
213   {
214     const AltitudePoint& aPoint = *aListItBeg;
215
216     double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
217     double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
218
219     if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
220     {
221       if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
222       {
223         aResAltitude = aPoint.Z();
224         return aResAltitude;
225       }
226       else if ( aDeltaY < 0 ) // top side
227       {
228         // top border
229         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
230           aBounds[ 0 ] = aPoint;
231         if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
232           aBounds[ 1 ] = aPoint;
233       }
234       else
235       {
236         // bottom border
237         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
238           aBounds[ 2 ] = aPoint;
239         if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
240           aBounds[ 3 ] = aPoint;
241       }
242     }
243     else if ( aDeltaX < 0 ) // left side
244     {
245       if ( ValuesEquals( aDeltaY, 0.0 ) )
246       {
247         // Left border
248         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
249           aBounds[ 0 ] = aPoint;
250         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
251           aBounds[ 2 ] = aPoint;
252       }
253       else if ( aDeltaY < 0 )
254       {
255         // top left corner
256         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
257           aBounds[ 0 ] = aPoint;
258       }
259       else
260       {
261         // bottom left corner
262         if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
263           aBounds[ 2 ] = aPoint;
264       }
265     }
266     else // right side
267     {
268       if ( ValuesEquals( aDeltaY, 0.0 ) )
269       {
270         // Right border
271         if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
272           aBounds[ 1 ] = aPoint;
273         if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
274           aBounds[ 3 ] = aPoint;
275       }
276       else if ( aDeltaY < 0 )
277       {
278         // top right corner
279         if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
280           aBounds[ 1 ] = aPoint;
281       }
282       else
283       {
284         // bottom right corner
285         if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
286           aBounds[ 3 ] = aPoint;
287       }
288     }
289
290     // Update bounding rectangle of our global grid
291     aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
292   }
293
294   const double LIMIT = 1E300;
295   if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
296       fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
297       fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
298       fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
299     return anInvalidAltitude;
300
301
302   // Check if requested point is inside of our bounding rectangle
303   if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
304     return aResAltitude;
305
306   // Calculate result altitude for point
307   AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
308
309   // At first we merge top and bottom borders
310   if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
311     interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
312
313   if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
314     interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
315
316   AltitudePoint aResPoint( aFirstPoint );
317
318   // At last we merge left and right borders
319   if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
320     interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
321     
322   aResAltitude = aResPoint.Z();
323
324   return aResAltitude;
325 }
326
327 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
328 {
329   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
330   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
331 }
332
333 QString HYDROData_Bathymetry::GetFilePath() const
334 {
335   QString aRes;
336
337   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
338   if ( !aLabel.IsNull() )
339   {
340     Handle(TDataStd_AsciiString) anAsciiStr;
341     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
342       aRes = QString( anAsciiStr->Get().ToCString() );
343   }
344
345   return aRes;
346 }
347
348 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
349                                                  const bool theIsUpdate )
350 {
351   bool anIsAltitudesInverted = IsAltitudesInverted();
352   if ( anIsAltitudesInverted == theIsInverted )
353     return;
354
355   TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
356
357   SetToUpdate( true );
358
359   if ( !theIsUpdate )
360     return;
361
362   // Update altitude points
363   AltitudePoints anAltitudePoints = GetAltitudePoints();
364   if ( anAltitudePoints.isEmpty() )
365     return;
366
367   AltitudePoints::iterator aListItBeg = anAltitudePoints.begin();
368   AltitudePoints::iterator aListItEnd = anAltitudePoints.end();
369   for ( ; aListItBeg != aListItEnd; ++aListItBeg )
370   {
371     AltitudePoint& aPoint = *aListItBeg;
372     aPoint.SetZ( aPoint.Z() * -1 );
373   }
374
375   SetAltitudePoints( anAltitudePoints );
376 }
377
378 bool HYDROData_Bathymetry::IsAltitudesInverted() const
379 {
380   bool aRes = false;
381
382   TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
383   if ( !aLabel.IsNull() )
384   {
385     Handle(TDataStd_Integer) anIntVal;
386     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
387       aRes = (bool)anIntVal->Get();
388   }
389
390   return aRes;
391 }
392
393 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
394 {
395   // Try to open the file
396   QFile aFile( theFileName );
397   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
398     return false;
399
400   bool aRes = false;
401
402   QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
403
404   AltitudePoints aPoints;
405
406   // Try to import the file
407   if ( aFileSuf == "xyz" )
408     aRes = importFromXYZFile( aFile, aPoints );
409     
410   // Close the file
411   aFile.close();
412
413   if ( aRes )
414   {
415     // Update file path and altitude points of this Bathymetry
416     SetFilePath( theFileName );
417     SetAltitudePoints( aPoints );
418   }
419
420   return aRes && !aPoints.isEmpty();
421 }
422
423 bool HYDROData_Bathymetry::importFromXYZFile( QFile&          theFile,
424                                               AltitudePoints& thePoints ) const
425 {
426   if ( !theFile.isOpen() )
427     return false;
428
429   // Strings in file is written as:
430   //  1. X(float) Y(float) Z(float)
431   //  2. X(float) Y(float) Z(float)
432   //  ...
433
434 #ifdef _TIMER
435   OSD_Timer aTimer;
436   aTimer.Start();
437 #endif
438
439   bool anIsAltitudesInverted = IsAltitudesInverted();
440   while ( !theFile.atEnd() )
441   {
442     QString aLine = theFile.readLine().simplified();
443     if ( aLine.isEmpty() )
444       continue;
445
446     QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
447     if ( aValues.length() < 3 )
448       return false;
449
450     AltitudePoint aPoint;
451     
452     QString anX = aValues.value( 0 );
453     QString anY = aValues.value( 1 );
454     QString aZ  = aValues.value( 2 );
455
456     bool isXOk = false, isYOk = false, isZOk = false;
457
458     aPoint.SetX( anX.toDouble( &isXOk ) );
459     aPoint.SetY( anY.toDouble( &isYOk ) );
460     aPoint.SetZ( aZ.toDouble( &isZOk ) );
461
462     if ( !isXOk || !isYOk || !isZOk )
463       return false;
464
465     // Invert the z value if requested
466     if ( anIsAltitudesInverted )
467       aPoint.SetZ( -aPoint.Z() );
468
469     thePoints << aPoint;
470   }
471
472 #ifdef _TIMER
473   aTimer.Stop();
474   std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
475   aTimer.Show( stream );
476 #endif
477
478   return true;
479 }
480
481
482
483