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>
11 #include <TDataStd_Integer.hxx>
17 #include <QStringList>
23 #include <OSD_Timer.hxx>
26 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
29 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
30 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
32 HYDROData_Bathymetry::HYDROData_Bathymetry()
33 : HYDROData_IAltitudeObject()
37 HYDROData_Bathymetry::~HYDROData_Bathymetry()
41 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
45 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
46 if ( aDocument.IsNull() )
49 QString aDocName = aDocument->GetDocPyName();
50 QString aBathymetryName = GetName();
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 );
57 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
58 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
60 QString aFilePath = GetFilePath();
61 if ( !aFilePath.isEmpty() )
63 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
64 .arg( aBathymetryName ).arg( aFilePath );
68 // TODO : bathymetry is composed from other bathymetry(ies)
74 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
76 RemoveAltitudePoints();
78 if ( thePoints.isEmpty() )
82 Handle(TDataStd_RealArray) aCoordsArray =
83 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
85 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
86 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
87 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
89 const AltitudePoint& aPoint = *aListItBeg;
91 aCoordsArray->SetValue( i * 3, aPoint.X() );
92 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
93 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
99 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
101 AltitudePoints aPoints;
103 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
104 if ( aLabel.IsNull() )
107 Handle(TDataStd_RealArray) aCoordsArray;
108 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
111 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
116 AltitudePoint aPoint;
117 aPoint.SetX( aCoordsArray->Value( i++ ) );
118 aPoint.SetY( aCoordsArray->Value( i++ ) );
119 aPoint.SetZ( aCoordsArray->Value( i++ ) );
127 void HYDROData_Bathymetry::RemoveAltitudePoints()
129 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
130 if ( !aLabel.IsNull() )
132 aLabel.ForgetAllAttributes();
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 )
143 double aCoordX = thePoint.X();
144 double aCoordY = thePoint.Y();
148 aCoordX = theFirstPoint.X();
150 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
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();
159 aCoordY = theFirstPoint.Y();
161 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
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();
169 theResPoint.SetX( aCoordX );
170 theResPoint.SetY( aCoordY );
172 // Calculate coefficient for interpolation
173 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
174 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
176 double aInterCoeff = 0;
178 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
181 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
182 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
184 // Calculate interpolated value
185 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
187 theResPoint.SetZ( aResVal );
190 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
192 double anInvalidAltitude = GetInvalidAltitude();
193 double aResAltitude = anInvalidAltitude;
195 AltitudePoints anAltitudePoints = GetAltitudePoints();
196 if ( anAltitudePoints.isEmpty() )
199 QPolygonF aBoundingRect;
202 // [ 0 (top-left) ] [ 1 (top-right) ]
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 ) };
210 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
211 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
212 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
214 const AltitudePoint& aPoint = *aListItBeg;
216 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
217 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
219 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
221 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
223 aResAltitude = aPoint.Z();
226 else if ( aDeltaY < 0 ) // top side
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;
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;
243 else if ( aDeltaX < 0 ) // left side
245 if ( ValuesEquals( aDeltaY, 0.0 ) )
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;
253 else if ( aDeltaY < 0 )
256 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
257 aBounds[ 0 ] = aPoint;
261 // bottom left corner
262 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
263 aBounds[ 2 ] = aPoint;
268 if ( ValuesEquals( aDeltaY, 0.0 ) )
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;
276 else if ( aDeltaY < 0 )
279 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
280 aBounds[ 1 ] = aPoint;
284 // bottom right corner
285 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
286 aBounds[ 3 ] = aPoint;
290 // Update bounding rectangle of our global grid
291 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
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;
302 // Check if requested point is inside of our bounding rectangle
303 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
306 // Calculate result altitude for point
307 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
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 );
313 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
314 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
316 AltitudePoint aResPoint( aFirstPoint );
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 );
322 aResAltitude = aResPoint.Z();
327 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
329 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
330 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
333 QString HYDROData_Bathymetry::GetFilePath() const
337 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
338 if ( !aLabel.IsNull() )
340 Handle(TDataStd_AsciiString) anAsciiStr;
341 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
342 aRes = QString( anAsciiStr->Get().ToCString() );
348 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
349 const bool theIsUpdate )
351 bool anIsAltitudesInverted = IsAltitudesInverted();
352 if ( anIsAltitudesInverted == theIsInverted )
355 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
362 // Update altitude points
363 AltitudePoints anAltitudePoints = GetAltitudePoints();
364 if ( anAltitudePoints.isEmpty() )
367 AltitudePoints::iterator aListItBeg = anAltitudePoints.begin();
368 AltitudePoints::iterator aListItEnd = anAltitudePoints.end();
369 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
371 AltitudePoint& aPoint = *aListItBeg;
372 aPoint.SetZ( aPoint.Z() * -1 );
375 SetAltitudePoints( anAltitudePoints );
378 bool HYDROData_Bathymetry::IsAltitudesInverted() const
382 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
383 if ( !aLabel.IsNull() )
385 Handle(TDataStd_Integer) anIntVal;
386 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
387 aRes = (bool)anIntVal->Get();
393 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
395 // Try to open the file
396 QFile aFile( theFileName );
397 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
402 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
404 AltitudePoints aPoints;
406 // Try to import the file
407 if ( aFileSuf == "xyz" )
408 aRes = importFromXYZFile( aFile, aPoints );
415 // Update file path and altitude points of this Bathymetry
416 SetFilePath( theFileName );
417 SetAltitudePoints( aPoints );
420 return aRes && !aPoints.isEmpty();
423 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
424 AltitudePoints& thePoints ) const
426 if ( !theFile.isOpen() )
429 // Strings in file is written as:
430 // 1. X(float) Y(float) Z(float)
431 // 2. X(float) Y(float) Z(float)
439 bool anIsAltitudesInverted = IsAltitudesInverted();
440 while ( !theFile.atEnd() )
442 QString aLine = theFile.readLine().simplified();
443 if ( aLine.isEmpty() )
446 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
447 if ( aValues.length() < 3 )
450 AltitudePoint aPoint;
452 QString anX = aValues.value( 0 );
453 QString anY = aValues.value( 1 );
454 QString aZ = aValues.value( 2 );
456 bool isXOk = false, isYOk = false, isZOk = false;
458 aPoint.SetX( anX.toDouble( &isXOk ) );
459 aPoint.SetY( anY.toDouble( &isYOk ) );
460 aPoint.SetZ( aZ.toDouble( &isZOk ) );
462 if ( !isXOk || !isYOk || !isZOk )
465 // Invert the z value if requested
466 if ( anIsAltitudesInverted )
467 aPoint.SetZ( -aPoint.Z() );
474 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
475 aTimer.Show( stream );