2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
9 #include <TDataStd_RealArray.hxx>
10 #include <TDataStd_AsciiString.hxx>
16 #include <QStringList>
20 #include <OSD_Timer.hxx>
23 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
26 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
27 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
29 HYDROData_Bathymetry::HYDROData_Bathymetry()
30 : HYDROData_IAltitudeObject()
34 HYDROData_Bathymetry::~HYDROData_Bathymetry()
38 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
43 if ( aDocument.IsNull() )
46 QString aDocName = aDocument->GetDocPyName();
47 QString aBathymetryName = GetName();
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 );
54 QString aFilePath = GetFilePath();
55 if ( !aFilePath.isEmpty() )
57 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
58 .arg( aBathymetryName ).arg( aFilePath );
62 // TODO : bathymetry is composed from other bathymetry(ies)
68 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
70 RemoveAltitudePoints();
72 if ( thePoints.isEmpty() )
76 Handle(TDataStd_RealArray) aCoordsArray =
77 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
79 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
80 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
81 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
83 const AltitudePoint& aPoint = *aListItBeg;
85 aCoordsArray->SetValue( i * 3, aPoint.X() );
86 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
87 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
91 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
93 AltitudePoints aPoints;
95 Handle(TDataStd_RealArray) aCoordsArray;
96 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
99 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
104 AltitudePoint aPoint;
105 aPoint.SetX( aCoordsArray->Value( i++ ) );
106 aPoint.SetY( aCoordsArray->Value( i++ ) );
107 aPoint.SetZ( aCoordsArray->Value( i++ ) );
115 void HYDROData_Bathymetry::RemoveAltitudePoints()
117 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
118 aLab.ForgetAllAttributes();
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 )
127 double aCoordX = thePoint.X();
128 double aCoordY = thePoint.Y();
132 aCoordX = theFirstPoint.X();
134 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
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();
143 aCoordY = theFirstPoint.Y();
145 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
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();
153 theResPoint.SetX( aCoordX );
154 theResPoint.SetY( aCoordY );
156 // Calculate coefficient for interpolation
157 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
158 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
160 double aInterCoeff = 0;
162 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
165 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
166 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
168 // Calculate interpolated value
169 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
171 theResPoint.SetZ( aResVal );
174 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
176 double aResAltitude = GetInvalidAltitude();
178 AltitudePoints anAltitudePoints = GetAltitudePoints();
179 if ( anAltitudePoints.isEmpty() )
182 QPolygonF aBoundingRect;
185 // [ 0 (top-left) ] [ 1 (top-right) ]
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() ) };
193 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
194 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
195 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
197 const AltitudePoint& aPoint = *aListItBeg;
199 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
200 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
202 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
204 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
206 aResAltitude = aPoint.Z();
209 else if ( aDeltaY < 0 ) // top side
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;
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;
226 else if ( aDeltaX < 0 ) // left side
228 if ( ValuesEquals( aDeltaY, 0.0 ) )
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;
236 else if ( aDeltaY < 0 )
239 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
240 aBounds[ 0 ] = aPoint;
244 // bottom left corner
245 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
246 aBounds[ 2 ] = aPoint;
251 if ( ValuesEquals( aDeltaY, 0.0 ) )
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;
259 else if ( aDeltaY < 0 )
262 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
263 aBounds[ 1 ] = aPoint;
267 // bottom right corner
268 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
269 aBounds[ 3 ] = aPoint;
273 // Update bounding rectangle of our global grid
274 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
277 // Check if requested point is inside of our bounding rectangle
278 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
281 // Calculate result altitude for point
282 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
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 );
288 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
289 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
291 AltitudePoint aResPoint( aFirstPoint );
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 );
297 aResAltitude = aResPoint.Z();
302 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
304 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
305 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
308 QString HYDROData_Bathymetry::GetFilePath() const
312 Handle(TDataStd_AsciiString) anAsciiStr;
313 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
314 aRes = QString( anAsciiStr->Get().ToCString() );
319 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
321 // Try to open the file
322 QFile aFile( theFileName );
323 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
328 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
330 AltitudePoints aPoints;
332 // Try to import the file
333 if ( aFileSuf == "xyz" )
334 aRes = importFromXYZFile( aFile, aPoints );
341 // Update file path and altitude points of this Bathymetry
342 SetFilePath( theFileName );
343 SetAltitudePoints( aPoints );
346 return aRes && !aPoints.isEmpty();
349 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
350 AltitudePoints& thePoints )
352 if ( !theFile.isOpen() )
355 // Strings in file is written as:
356 // 1. X(float) Y(float) Z(float)
357 // 2. X(float) Y(float) Z(float)
365 while ( !theFile.atEnd() )
367 QString aLine = theFile.readLine().simplified();
368 if ( aLine.isEmpty() )
371 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
372 if ( aValues.length() < 3 )
375 AltitudePoint aPoint;
377 QString anX = aValues.value( 0 );
378 QString anY = aValues.value( 1 );
379 QString aZ = aValues.value( 2 );
381 bool isXOk = false, isYOk = false, isZOk = false;
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 ) );
388 if ( !isXOk || !isYOk || !isZOk )
396 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
397 aTimer.Show( stream );