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 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
43 QStringList aResList = dumpObjectCreation( theTreatedObjects );
44 QString aBathymetryName = GetObjPyName();
46 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
47 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
49 TCollection_AsciiString aFilePath = GetFilePath();
50 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
51 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
53 aResList << QString( "" );
54 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
55 aResList << QString( "" );
60 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
62 RemoveAltitudePoints();
64 if ( thePoints.IsEmpty() )
68 Handle(TDataStd_RealArray) aCoordsArray =
69 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
71 AltitudePoints::Iterator anIter( thePoints );
72 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
74 const AltitudePoint& aPoint = anIter.Value();
76 aCoordsArray->SetValue( i * 3, aPoint.X() );
77 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
78 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
84 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
86 AltitudePoints aPoints;
88 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
89 if ( aLabel.IsNull() )
92 Handle(TDataStd_RealArray) aCoordsArray;
93 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
96 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
101 AltitudePoint aPoint;
102 aPoint.SetX( aCoordsArray->Value( i++ ) );
103 aPoint.SetY( aCoordsArray->Value( i++ ) );
104 aPoint.SetZ( aCoordsArray->Value( i++ ) );
106 aPoints.Append( aPoint );
112 void HYDROData_Bathymetry::RemoveAltitudePoints()
114 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
115 if ( !aLabel.IsNull() )
117 aLabel.ForgetAllAttributes();
122 void interpolateAltitudeForPoints( const gp_XY& thePoint,
123 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
124 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
125 HYDROData_Bathymetry::AltitudePoint& theResPoint,
126 const bool& theIsVertical )
128 double aCoordX = thePoint.X();
129 double aCoordY = thePoint.Y();
133 aCoordX = theFirstPoint.X();
135 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
137 // Recalculate X coordinate by equation of line from two points
138 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
139 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
144 aCoordY = theFirstPoint.Y();
146 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
148 // Recalculate y by equation of line from two points
149 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
150 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
154 theResPoint.SetX( aCoordX );
155 theResPoint.SetY( aCoordY );
157 // Calculate coefficient for interpolation
158 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
159 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
161 double aInterCoeff = 0;
163 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
166 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
167 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
169 // Calculate interpolated value
170 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
172 theResPoint.SetZ( aResVal );
175 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
177 double anInvalidAltitude = GetInvalidAltitude();
178 double aResAltitude = anInvalidAltitude;
180 AltitudePoints anAltitudePoints = GetAltitudePoints();
181 if ( anAltitudePoints.IsEmpty() )
184 QPolygonF aBoundingRect;
187 // [ 0 (top-left) ] [ 1 (top-right) ]
189 // [ 2 (bot-left) ] [ 3 (bot-right) ]
190 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
191 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
192 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
193 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
195 AltitudePoints::Iterator anIter( anAltitudePoints );
196 for ( ; anIter.More(); anIter.Next() )
198 const AltitudePoint& aPoint = anIter.Value();
200 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
201 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
203 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
205 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
207 aResAltitude = aPoint.Z();
210 else if ( aDeltaY < 0 ) // top side
213 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
214 aBounds[ 0 ] = aPoint;
215 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
216 aBounds[ 1 ] = aPoint;
221 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
222 aBounds[ 2 ] = aPoint;
223 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
224 aBounds[ 3 ] = aPoint;
227 else if ( aDeltaX < 0 ) // left side
229 if ( ValuesEquals( aDeltaY, 0.0 ) )
232 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
233 aBounds[ 0 ] = aPoint;
234 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
235 aBounds[ 2 ] = aPoint;
237 else if ( aDeltaY < 0 )
240 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
241 aBounds[ 0 ] = aPoint;
245 // bottom left corner
246 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
247 aBounds[ 2 ] = aPoint;
252 if ( ValuesEquals( aDeltaY, 0.0 ) )
255 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
256 aBounds[ 1 ] = aPoint;
257 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
258 aBounds[ 3 ] = aPoint;
260 else if ( aDeltaY < 0 )
263 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
264 aBounds[ 1 ] = aPoint;
268 // bottom right corner
269 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
270 aBounds[ 3 ] = aPoint;
274 // Update bounding rectangle of our global grid
275 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
278 const double LIMIT = 1E300;
279 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
280 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
281 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
282 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
283 return anInvalidAltitude;
286 // Check if requested point is inside of our bounding rectangle
287 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
290 // Calculate result altitude for point
291 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
293 // At first we merge top and bottom borders
294 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
295 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
297 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
298 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
300 AltitudePoint aResPoint( aFirstPoint );
302 // At last we merge left and right borders
303 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
304 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
306 aResAltitude = aResPoint.Z();
311 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
313 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
316 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
318 TCollection_AsciiString aRes;
320 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
321 if ( !aLabel.IsNull() )
323 Handle(TDataStd_AsciiString) anAsciiStr;
324 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
325 aRes = anAsciiStr->Get();
331 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
332 const bool theIsUpdate )
334 bool anIsAltitudesInverted = IsAltitudesInverted();
335 if ( anIsAltitudesInverted == theIsInverted )
338 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
345 // Update altitude points
346 AltitudePoints anAltitudePoints = GetAltitudePoints();
347 if ( anAltitudePoints.IsEmpty() )
350 AltitudePoints::Iterator anIter( anAltitudePoints );
351 for ( ; anIter.More(); anIter.Next() )
353 AltitudePoint& aPoint = anIter.ChangeValue();
354 aPoint.SetZ( aPoint.Z() * -1 );
357 SetAltitudePoints( anAltitudePoints );
360 bool HYDROData_Bathymetry::IsAltitudesInverted() const
364 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
365 if ( !aLabel.IsNull() )
367 Handle(TDataStd_Integer) anIntVal;
368 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
369 aRes = (bool)anIntVal->Get();
375 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
377 // Try to open the file
378 QFile aFile( theFileName.ToCString() );
379 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
384 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
386 AltitudePoints aPoints;
388 // Try to import the file
389 if ( aFileSuf == "xyz" )
390 aRes = importFromXYZFile( aFile, aPoints );
396 // Convert from global to local CS
397 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
398 AltitudePoints::Iterator anIter( aPoints );
399 for ( ; anIter.More(); anIter.Next() )
401 AltitudePoint& aPoint = anIter.ChangeValue();
402 aDoc->Transform( aPoint, true );
407 // Update file path and altitude points of this Bathymetry
408 SetFilePath( theFileName );
409 SetAltitudePoints( aPoints );
412 return aRes && !aPoints.IsEmpty();
415 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
416 AltitudePoints& thePoints ) const
418 if ( !theFile.isOpen() )
421 // Strings in file is written as:
422 // 1. X(float) Y(float) Z(float)
423 // 2. X(float) Y(float) Z(float)
431 bool anIsAltitudesInverted = IsAltitudesInverted();
432 while ( !theFile.atEnd() )
434 QString aLine = theFile.readLine().simplified();
435 if ( aLine.isEmpty() )
438 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
439 if ( aValues.length() < 3 )
442 AltitudePoint aPoint;
444 QString anX = aValues.value( 0 );
445 QString anY = aValues.value( 1 );
446 QString aZ = aValues.value( 2 );
448 bool isXOk = false, isYOk = false, isZOk = false;
450 aPoint.SetX( anX.toDouble( &isXOk ) );
451 aPoint.SetY( anY.toDouble( &isYOk ) );
452 aPoint.SetZ( aZ.toDouble( &isZOk ) );
454 if ( !isXOk || !isYOk || !isZOk )
457 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
458 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
459 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
462 // Invert the z value if requested
463 if ( anIsAltitudesInverted )
464 aPoint.SetZ( -aPoint.Z() );
466 thePoints.Append( aPoint );
471 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
472 aTimer.Show( stream );
479 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
481 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
482 Handle_HYDROData_PolylineXY aResult =
483 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
485 if( aResult.IsNull() )
489 QString aPolylinePref = GetName() + "_Boundary";
490 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
491 aResult->SetName( aPolylineName );
493 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
495 AltitudePoints aPoints = GetAltitudePoints();
497 AltitudePoints::Iterator anIter( aPoints );
498 for ( ; anIter.More(); anIter.Next() )
500 const AltitudePoint& aPoint = anIter.Value();
502 double x = aPoint.X(), y = aPoint.Y();
503 if( isFirst || x<Xmin )
505 if( isFirst || x>Xmax )
507 if( isFirst || y<Ymin )
509 if( isFirst || y>Ymax )
514 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
515 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
516 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
517 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
518 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );