2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
5 #include "HYDROData_PolylineXY.h"
7 #include <boost/math/special_functions/fpclassify.hpp>
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_AsciiString.hxx>
14 #include <TDataStd_Integer.hxx>
20 #include <QStringList>
26 #include <OSD_Timer.hxx>
29 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
32 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
33 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
35 HYDROData_Bathymetry::HYDROData_Bathymetry()
36 : HYDROData_IAltitudeObject()
40 HYDROData_Bathymetry::~HYDROData_Bathymetry()
44 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
48 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
49 if ( aDocument.IsNull() )
52 QString aDocName = aDocument->GetDocPyName();
53 QString aBathymetryName = GetName();
55 aResList << QString( "%1 = %2.CreateObject( %3 );" )
56 .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
57 aResList << QString( "%1.SetName( \"%2\" );" )
58 .arg( aBathymetryName ).arg( aBathymetryName );
60 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
61 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
63 QString aFilePath = GetFilePath();
64 if ( !aFilePath.isEmpty() )
66 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
67 .arg( aBathymetryName ).arg( aFilePath );
71 // TODO : bathymetry is composed from other bathymetry(ies)
77 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
79 RemoveAltitudePoints();
81 if ( thePoints.isEmpty() )
85 Handle(TDataStd_RealArray) aCoordsArray =
86 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
88 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
89 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
90 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
92 const AltitudePoint& aPoint = *aListItBeg;
94 aCoordsArray->SetValue( i * 3, aPoint.X() );
95 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
96 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
102 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
104 AltitudePoints aPoints;
106 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
107 if ( aLabel.IsNull() )
110 Handle(TDataStd_RealArray) aCoordsArray;
111 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
114 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
119 AltitudePoint aPoint;
120 aPoint.SetX( aCoordsArray->Value( i++ ) );
121 aPoint.SetY( aCoordsArray->Value( i++ ) );
122 aPoint.SetZ( aCoordsArray->Value( i++ ) );
130 void HYDROData_Bathymetry::RemoveAltitudePoints()
132 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
133 if ( !aLabel.IsNull() )
135 aLabel.ForgetAllAttributes();
140 void interpolateAltitudeForPoints( const gp_XY& thePoint,
141 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
142 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
143 HYDROData_Bathymetry::AltitudePoint& theResPoint,
144 const bool& theIsVertical )
146 double aCoordX = thePoint.X();
147 double aCoordY = thePoint.Y();
151 aCoordX = theFirstPoint.X();
153 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
155 // Recalculate X coordinate by equation of line from two points
156 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
157 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
162 aCoordY = theFirstPoint.Y();
164 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
166 // Recalculate y by equation of line from two points
167 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
168 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
172 theResPoint.SetX( aCoordX );
173 theResPoint.SetY( aCoordY );
175 // Calculate coefficient for interpolation
176 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
177 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
179 double aInterCoeff = 0;
181 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
184 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
185 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
187 // Calculate interpolated value
188 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
190 theResPoint.SetZ( aResVal );
193 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
195 double anInvalidAltitude = GetInvalidAltitude();
196 double aResAltitude = anInvalidAltitude;
198 AltitudePoints anAltitudePoints = GetAltitudePoints();
199 if ( anAltitudePoints.isEmpty() )
202 QPolygonF aBoundingRect;
205 // [ 0 (top-left) ] [ 1 (top-right) ]
207 // [ 2 (bot-left) ] [ 3 (bot-right) ]
208 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
209 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
210 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
211 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
213 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
214 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
215 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
217 const AltitudePoint& aPoint = *aListItBeg;
219 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
220 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
222 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
224 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
226 aResAltitude = aPoint.Z();
229 else if ( aDeltaY < 0 ) // top side
232 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
233 aBounds[ 0 ] = aPoint;
234 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
235 aBounds[ 1 ] = aPoint;
240 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
241 aBounds[ 2 ] = aPoint;
242 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
243 aBounds[ 3 ] = aPoint;
246 else if ( aDeltaX < 0 ) // left side
248 if ( ValuesEquals( aDeltaY, 0.0 ) )
251 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
252 aBounds[ 0 ] = aPoint;
253 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
254 aBounds[ 2 ] = aPoint;
256 else if ( aDeltaY < 0 )
259 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
260 aBounds[ 0 ] = aPoint;
264 // bottom left corner
265 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
266 aBounds[ 2 ] = aPoint;
271 if ( ValuesEquals( aDeltaY, 0.0 ) )
274 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
275 aBounds[ 1 ] = aPoint;
276 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
277 aBounds[ 3 ] = aPoint;
279 else if ( aDeltaY < 0 )
282 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
283 aBounds[ 1 ] = aPoint;
287 // bottom right corner
288 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
289 aBounds[ 3 ] = aPoint;
293 // Update bounding rectangle of our global grid
294 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
297 const double LIMIT = 1E300;
298 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
299 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
300 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
301 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
302 return anInvalidAltitude;
305 // Check if requested point is inside of our bounding rectangle
306 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
309 // Calculate result altitude for point
310 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
312 // At first we merge top and bottom borders
313 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
314 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
316 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
317 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
319 AltitudePoint aResPoint( aFirstPoint );
321 // At last we merge left and right borders
322 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
323 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
325 aResAltitude = aResPoint.Z();
330 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
332 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
333 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
336 QString HYDROData_Bathymetry::GetFilePath() const
340 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
341 if ( !aLabel.IsNull() )
343 Handle(TDataStd_AsciiString) anAsciiStr;
344 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
345 aRes = QString( anAsciiStr->Get().ToCString() );
351 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
352 const bool theIsUpdate )
354 bool anIsAltitudesInverted = IsAltitudesInverted();
355 if ( anIsAltitudesInverted == theIsInverted )
358 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
365 // Update altitude points
366 AltitudePoints anAltitudePoints = GetAltitudePoints();
367 if ( anAltitudePoints.isEmpty() )
370 AltitudePoints::iterator aListItBeg = anAltitudePoints.begin();
371 AltitudePoints::iterator aListItEnd = anAltitudePoints.end();
372 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
374 AltitudePoint& aPoint = *aListItBeg;
375 aPoint.SetZ( aPoint.Z() * -1 );
378 SetAltitudePoints( anAltitudePoints );
381 bool HYDROData_Bathymetry::IsAltitudesInverted() const
385 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
386 if ( !aLabel.IsNull() )
388 Handle(TDataStd_Integer) anIntVal;
389 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
390 aRes = (bool)anIntVal->Get();
396 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
398 // Try to open the file
399 QFile aFile( theFileName );
400 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
405 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
407 AltitudePoints aPoints;
409 // Try to import the file
410 if ( aFileSuf == "xyz" )
411 aRes = importFromXYZFile( aFile, aPoints );
418 // Update file path and altitude points of this Bathymetry
419 SetFilePath( theFileName );
420 SetAltitudePoints( aPoints );
423 return aRes && !aPoints.isEmpty();
426 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
427 AltitudePoints& thePoints ) const
429 if ( !theFile.isOpen() )
432 // Strings in file is written as:
433 // 1. X(float) Y(float) Z(float)
434 // 2. X(float) Y(float) Z(float)
442 bool anIsAltitudesInverted = IsAltitudesInverted();
443 while ( !theFile.atEnd() )
445 QString aLine = theFile.readLine().simplified();
446 if ( aLine.isEmpty() )
449 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
450 if ( aValues.length() < 3 )
453 AltitudePoint aPoint;
455 QString anX = aValues.value( 0 );
456 QString anY = aValues.value( 1 );
457 QString aZ = aValues.value( 2 );
459 bool isXOk = false, isYOk = false, isZOk = false;
461 aPoint.SetX( anX.toDouble( &isXOk ) );
462 aPoint.SetY( anY.toDouble( &isYOk ) );
463 aPoint.SetZ( aZ.toDouble( &isZOk ) );
465 if ( !isXOk || !isYOk || !isZOk )
468 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
469 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
470 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
473 // Invert the z value if requested
474 if ( anIsAltitudesInverted )
475 aPoint.SetZ( -aPoint.Z() );
482 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
483 aTimer.Show( stream );
490 bool HYDROData_Bathymetry::CreateBoundaryPolyline() const
492 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
493 Handle_HYDROData_PolylineXY aResult =
494 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
496 if( aResult.IsNull() )
500 QString aPolylinePref = GetName() + "_Boundary";
501 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
502 aResult->SetName( aPolylineName );
504 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
506 AltitudePoints aPoints = GetAltitudePoints();
507 foreach( AltitudePoint aPnt, aPoints )
509 double x = aPnt.X(), y = aPnt.Y();
510 if( isFirst || x<Xmin )
512 if( isFirst || x>Xmax )
514 if( isFirst || y<Ymin )
516 if( isFirst || y>Ymax )
521 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
522 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
523 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
524 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
525 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );