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 = GetName();
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() );
56 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
58 RemoveAltitudePoints();
60 if ( thePoints.IsEmpty() )
64 Handle(TDataStd_RealArray) aCoordsArray =
65 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
67 AltitudePoints::Iterator anIter( thePoints );
68 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
70 const AltitudePoint& aPoint = anIter.Value();
72 aCoordsArray->SetValue( i * 3, aPoint.X() );
73 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
74 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
80 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
82 AltitudePoints aPoints;
84 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
85 if ( aLabel.IsNull() )
88 Handle(TDataStd_RealArray) aCoordsArray;
89 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
92 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
98 aPoint.SetX( aCoordsArray->Value( i++ ) );
99 aPoint.SetY( aCoordsArray->Value( i++ ) );
100 aPoint.SetZ( aCoordsArray->Value( i++ ) );
102 aPoints.Append( aPoint );
108 void HYDROData_Bathymetry::RemoveAltitudePoints()
110 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
111 if ( !aLabel.IsNull() )
113 aLabel.ForgetAllAttributes();
118 void interpolateAltitudeForPoints( const gp_XY& thePoint,
119 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
120 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
121 HYDROData_Bathymetry::AltitudePoint& theResPoint,
122 const bool& theIsVertical )
124 double aCoordX = thePoint.X();
125 double aCoordY = thePoint.Y();
129 aCoordX = theFirstPoint.X();
131 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
133 // Recalculate X coordinate by equation of line from two points
134 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
135 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
140 aCoordY = theFirstPoint.Y();
142 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
144 // Recalculate y by equation of line from two points
145 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
146 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
150 theResPoint.SetX( aCoordX );
151 theResPoint.SetY( aCoordY );
153 // Calculate coefficient for interpolation
154 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
155 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
157 double aInterCoeff = 0;
159 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
162 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
163 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
165 // Calculate interpolated value
166 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
168 theResPoint.SetZ( aResVal );
171 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
173 double anInvalidAltitude = GetInvalidAltitude();
174 double aResAltitude = anInvalidAltitude;
176 AltitudePoints anAltitudePoints = GetAltitudePoints();
177 if ( anAltitudePoints.IsEmpty() )
180 QPolygonF aBoundingRect;
183 // [ 0 (top-left) ] [ 1 (top-right) ]
185 // [ 2 (bot-left) ] [ 3 (bot-right) ]
186 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
187 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
188 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
189 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
191 AltitudePoints::Iterator anIter( anAltitudePoints );
192 for ( ; anIter.More(); anIter.Next() )
194 const AltitudePoint& aPoint = anIter.Value();
196 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
197 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
199 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
201 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
203 aResAltitude = aPoint.Z();
206 else if ( aDeltaY < 0 ) // top side
209 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
210 aBounds[ 0 ] = aPoint;
211 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
212 aBounds[ 1 ] = aPoint;
217 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
218 aBounds[ 2 ] = aPoint;
219 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
220 aBounds[ 3 ] = aPoint;
223 else if ( aDeltaX < 0 ) // left side
225 if ( ValuesEquals( aDeltaY, 0.0 ) )
228 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
229 aBounds[ 0 ] = aPoint;
230 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
231 aBounds[ 2 ] = aPoint;
233 else if ( aDeltaY < 0 )
236 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
237 aBounds[ 0 ] = aPoint;
241 // bottom left corner
242 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
243 aBounds[ 2 ] = aPoint;
248 if ( ValuesEquals( aDeltaY, 0.0 ) )
251 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
252 aBounds[ 1 ] = aPoint;
253 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
254 aBounds[ 3 ] = aPoint;
256 else if ( aDeltaY < 0 )
259 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
260 aBounds[ 1 ] = aPoint;
264 // bottom right corner
265 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
266 aBounds[ 3 ] = aPoint;
270 // Update bounding rectangle of our global grid
271 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
274 const double LIMIT = 1E300;
275 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
276 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
277 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
278 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
279 return anInvalidAltitude;
282 // Check if requested point is inside of our bounding rectangle
283 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
286 // Calculate result altitude for point
287 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
289 // At first we merge top and bottom borders
290 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
291 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
293 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
294 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
296 AltitudePoint aResPoint( aFirstPoint );
298 // At last we merge left and right borders
299 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
300 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
302 aResAltitude = aResPoint.Z();
307 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
309 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
312 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
314 TCollection_AsciiString aRes;
316 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
317 if ( !aLabel.IsNull() )
319 Handle(TDataStd_AsciiString) anAsciiStr;
320 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
321 aRes = anAsciiStr->Get();
327 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
328 const bool theIsUpdate )
330 bool anIsAltitudesInverted = IsAltitudesInverted();
331 if ( anIsAltitudesInverted == theIsInverted )
334 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
341 // Update altitude points
342 AltitudePoints anAltitudePoints = GetAltitudePoints();
343 if ( anAltitudePoints.IsEmpty() )
346 AltitudePoints::Iterator anIter( anAltitudePoints );
347 for ( ; anIter.More(); anIter.Next() )
349 AltitudePoint& aPoint = anIter.ChangeValue();
350 aPoint.SetZ( aPoint.Z() * -1 );
353 SetAltitudePoints( anAltitudePoints );
356 bool HYDROData_Bathymetry::IsAltitudesInverted() const
360 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
361 if ( !aLabel.IsNull() )
363 Handle(TDataStd_Integer) anIntVal;
364 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
365 aRes = (bool)anIntVal->Get();
371 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
373 // Try to open the file
374 QFile aFile( theFileName.ToCString() );
375 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
380 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
382 AltitudePoints aPoints;
384 // Try to import the file
385 if ( aFileSuf == "xyz" )
386 aRes = importFromXYZFile( aFile, aPoints );
393 // Update file path and altitude points of this Bathymetry
394 SetFilePath( theFileName );
395 SetAltitudePoints( aPoints );
398 return aRes && !aPoints.IsEmpty();
401 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
402 AltitudePoints& thePoints ) const
404 if ( !theFile.isOpen() )
407 // Strings in file is written as:
408 // 1. X(float) Y(float) Z(float)
409 // 2. X(float) Y(float) Z(float)
417 bool anIsAltitudesInverted = IsAltitudesInverted();
418 while ( !theFile.atEnd() )
420 QString aLine = theFile.readLine().simplified();
421 if ( aLine.isEmpty() )
424 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
425 if ( aValues.length() < 3 )
428 AltitudePoint aPoint;
430 QString anX = aValues.value( 0 );
431 QString anY = aValues.value( 1 );
432 QString aZ = aValues.value( 2 );
434 bool isXOk = false, isYOk = false, isZOk = false;
436 aPoint.SetX( anX.toDouble( &isXOk ) );
437 aPoint.SetY( anY.toDouble( &isYOk ) );
438 aPoint.SetZ( aZ.toDouble( &isZOk ) );
440 if ( !isXOk || !isYOk || !isZOk )
443 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
444 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
445 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
448 // Invert the z value if requested
449 if ( anIsAltitudesInverted )
450 aPoint.SetZ( -aPoint.Z() );
452 thePoints.Append( aPoint );
457 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
458 aTimer.Show( stream );
465 bool HYDROData_Bathymetry::CreateBoundaryPolyline() const
467 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
468 Handle_HYDROData_PolylineXY aResult =
469 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
471 if( aResult.IsNull() )
475 QString aPolylinePref = GetName() + "_Boundary";
476 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
477 aResult->SetName( aPolylineName );
479 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
481 AltitudePoints aPoints = GetAltitudePoints();
483 AltitudePoints::Iterator anIter( aPoints );
484 for ( ; anIter.More(); anIter.Next() )
486 const AltitudePoint& aPoint = anIter.Value();
488 double x = aPoint.X(), y = aPoint.Y();
489 if( isFirst || x<Xmin )
491 if( isFirst || x>Xmax )
493 if( isFirst || y<Ymin )
495 if( isFirst || y>Ymax )
500 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
501 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
502 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
503 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
504 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );