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