1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Bathymetry.h"
20 #include "HYDROData_Document.h"
21 #include "HYDROData_Tool.h"
22 #include "HYDROData_PolylineXY.h"
24 #include <boost/math/special_functions/fpclassify.hpp>
29 #include <TDataStd_RealArray.hxx>
30 #include <TDataStd_AsciiString.hxx>
31 #include <TDataStd_Integer.hxx>
37 #include <QStringList>
43 #include <OSD_Timer.hxx>
46 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
47 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
49 HYDROData_Bathymetry::HYDROData_Bathymetry()
50 : HYDROData_IAltitudeObject()
54 HYDROData_Bathymetry::~HYDROData_Bathymetry()
58 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
60 QStringList aResList = dumpObjectCreation( theTreatedObjects );
61 QString aBathymetryName = GetObjPyName();
63 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
64 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
66 TCollection_AsciiString aFilePath = GetFilePath();
67 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
68 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
70 aResList << QString( "" );
71 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
72 aResList << QString( "" );
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.Length() * 3 - 1 );
88 AltitudePoints::Iterator anIter( thePoints );
89 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
91 const AltitudePoint& aPoint = anIter.Value();
93 aCoordsArray->SetValue( i * 3, aPoint.X() );
94 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
95 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
101 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
103 AltitudePoints aPoints;
105 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
106 if ( aLabel.IsNull() )
109 Handle(TDataStd_RealArray) aCoordsArray;
110 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
113 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
118 AltitudePoint aPoint;
119 aPoint.SetX( aCoordsArray->Value( i++ ) );
120 aPoint.SetY( aCoordsArray->Value( i++ ) );
121 aPoint.SetZ( aCoordsArray->Value( i++ ) );
123 aPoints.Append( aPoint );
129 void HYDROData_Bathymetry::RemoveAltitudePoints()
131 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
132 if ( !aLabel.IsNull() )
134 aLabel.ForgetAllAttributes();
139 void interpolateAltitudeForPoints( const gp_XY& thePoint,
140 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
141 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
142 HYDROData_Bathymetry::AltitudePoint& theResPoint,
143 const bool& theIsVertical )
145 double aCoordX = thePoint.X();
146 double aCoordY = thePoint.Y();
150 aCoordX = theFirstPoint.X();
152 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
154 // Recalculate X coordinate by equation of line from two points
155 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
156 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
161 aCoordY = theFirstPoint.Y();
163 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
165 // Recalculate y by equation of line from two points
166 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
167 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
171 theResPoint.SetX( aCoordX );
172 theResPoint.SetY( aCoordY );
174 // Calculate coefficient for interpolation
175 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
176 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
178 double aInterCoeff = 0;
180 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
183 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
184 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
186 // Calculate interpolated value
187 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
189 theResPoint.SetZ( aResVal );
192 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
194 double anInvalidAltitude = GetInvalidAltitude();
195 double aResAltitude = anInvalidAltitude;
197 AltitudePoints anAltitudePoints = GetAltitudePoints();
198 if ( anAltitudePoints.IsEmpty() )
201 QPolygonF aBoundingRect;
204 // [ 0 (top-left) ] [ 1 (top-right) ]
206 // [ 2 (bot-left) ] [ 3 (bot-right) ]
207 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
208 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
209 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
210 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
212 AltitudePoints::Iterator anIter( anAltitudePoints );
213 for ( ; anIter.More(); anIter.Next() )
215 const AltitudePoint& aPoint = anIter.Value();
217 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
218 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
220 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
222 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
224 aResAltitude = aPoint.Z();
227 else if ( aDeltaY < 0 ) // top side
230 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
231 aBounds[ 0 ] = aPoint;
232 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
233 aBounds[ 1 ] = aPoint;
238 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
239 aBounds[ 2 ] = aPoint;
240 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
241 aBounds[ 3 ] = aPoint;
244 else if ( aDeltaX < 0 ) // left side
246 if ( ValuesEquals( aDeltaY, 0.0 ) )
249 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
250 aBounds[ 0 ] = aPoint;
251 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
252 aBounds[ 2 ] = aPoint;
254 else if ( aDeltaY < 0 )
257 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
258 aBounds[ 0 ] = aPoint;
262 // bottom left corner
263 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
264 aBounds[ 2 ] = aPoint;
269 if ( ValuesEquals( aDeltaY, 0.0 ) )
272 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
273 aBounds[ 1 ] = aPoint;
274 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
275 aBounds[ 3 ] = aPoint;
277 else if ( aDeltaY < 0 )
280 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
281 aBounds[ 1 ] = aPoint;
285 // bottom right corner
286 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
287 aBounds[ 3 ] = aPoint;
291 // Update bounding rectangle of our global grid
292 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
295 const double LIMIT = 1E300;
296 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
297 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
298 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
299 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
300 return anInvalidAltitude;
303 // Check if requested point is inside of our bounding rectangle
304 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
307 // Calculate result altitude for point
308 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
310 // At first we merge top and bottom borders
311 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
312 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
314 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
315 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
317 AltitudePoint aResPoint( aFirstPoint );
319 // At last we merge left and right borders
320 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
321 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
323 aResAltitude = aResPoint.Z();
328 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
330 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
333 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
335 TCollection_AsciiString aRes;
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 = anAsciiStr->Get();
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 anIter( anAltitudePoints );
368 for ( ; anIter.More(); anIter.Next() )
370 AltitudePoint& aPoint = anIter.ChangeValue();
371 aPoint.SetZ( aPoint.Z() * -1 );
374 SetAltitudePoints( anAltitudePoints );
377 bool HYDROData_Bathymetry::IsAltitudesInverted() const
381 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
382 if ( !aLabel.IsNull() )
384 Handle(TDataStd_Integer) anIntVal;
385 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
386 aRes = (bool)anIntVal->Get();
392 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
394 // Try to open the file
395 QFile aFile( theFileName.ToCString() );
396 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
401 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
403 AltitudePoints aPoints;
405 // Try to import the file
406 if ( aFileSuf == "xyz" )
407 aRes = importFromXYZFile( aFile, aPoints );
408 else if ( aFileSuf == "asc" )
409 aRes = importFromASCFile( aFile, aPoints );
415 // Convert from global to local CS
416 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
417 AltitudePoints::Iterator anIter( aPoints );
418 for ( ; anIter.More(); anIter.Next() )
420 AltitudePoint& aPoint = anIter.ChangeValue();
421 aDoc->Transform( aPoint, true );
426 // Update file path and altitude points of this Bathymetry
427 SetFilePath( theFileName );
428 SetAltitudePoints( aPoints );
431 return aRes && !aPoints.IsEmpty();
434 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
435 AltitudePoints& thePoints ) const
437 if ( !theFile.isOpen() )
440 // Strings in file is written as:
441 // 1. X(float) Y(float) Z(float)
442 // 2. X(float) Y(float) Z(float)
450 bool anIsAltitudesInverted = IsAltitudesInverted();
451 while ( !theFile.atEnd() )
453 QString aLine = theFile.readLine().simplified();
454 if ( aLine.isEmpty() )
457 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
458 if ( aValues.length() < 3 )
461 AltitudePoint aPoint;
463 QString anX = aValues.value( 0 );
464 QString anY = aValues.value( 1 );
465 QString aZ = aValues.value( 2 );
467 bool isXOk = false, isYOk = false, isZOk = false;
469 aPoint.SetX( anX.toDouble( &isXOk ) );
470 aPoint.SetY( anY.toDouble( &isYOk ) );
471 aPoint.SetZ( aZ.toDouble( &isZOk ) );
473 if ( !isXOk || !isYOk || !isZOk )
476 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
477 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
478 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
481 // Invert the z value if requested
482 if ( anIsAltitudesInverted )
483 aPoint.SetZ( -aPoint.Z() );
485 thePoints.Append( aPoint );
490 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
491 aTimer.Show( stream );
497 bool HYDROData_Bathymetry::importFromASCFile( QFile& theFile,
498 AltitudePoints& thePoints ) const
500 if ( !theFile.isOpen() )
504 QStringList aStrList;
513 aLine = theFile.readLine().simplified();
514 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
515 if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
517 aNCols = aStrList[1].toInt();
519 aLine = theFile.readLine().simplified();
520 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
521 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
523 aNRows = aStrList[1].toInt();
525 aLine = theFile.readLine().simplified();
526 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
527 if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
529 anXllCorner = aStrList[1].toDouble();
531 aLine = theFile.readLine().simplified();
532 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
533 if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
535 anYllCorner = aStrList[1].toDouble();
537 aLine = theFile.readLine().simplified();
538 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
539 if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
541 aCellSize = aStrList[1].toDouble();
543 aLine = theFile.readLine().simplified();
544 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
545 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
547 aNoDataValue = aStrList[1].toDouble();
549 bool anIsAltitudesInverted = IsAltitudesInverted();
553 while ( !theFile.atEnd() )
555 aLine = theFile.readLine().simplified();
556 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
558 aStrLength = aStrList.length();
559 if ( aStrLength == 0 )
562 if ( aStrLength != aNRows )
565 for (int j = 0; j < aNCols; j++)
567 if (aStrList[j].toDouble() != aNoDataValue)
569 AltitudePoint aPoint;
570 aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
571 aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
572 aPoint.SetZ(aStrList[j].toDouble());
574 if ( anIsAltitudesInverted )
575 aPoint.SetZ( -aPoint.Z() );
577 thePoints.Append(aPoint);
589 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
591 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
592 Handle_HYDROData_PolylineXY aResult =
593 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
595 if( aResult.IsNull() )
599 QString aPolylinePref = GetName() + "_Boundary";
600 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
601 aResult->SetName( aPolylineName );
603 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
605 AltitudePoints aPoints = GetAltitudePoints();
607 AltitudePoints::Iterator anIter( aPoints );
608 for ( ; anIter.More(); anIter.Next() )
610 const AltitudePoint& aPoint = anIter.Value();
612 double x = aPoint.X(), y = aPoint.Y();
613 if( isFirst || x<Xmin )
615 if( isFirst || x>Xmax )
617 if( isFirst || y<Ymin )
619 if( isFirst || y>Ymax )
624 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
625 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
626 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
627 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
628 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
634 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
636 gp_XYZ aDelta( theDx, theDy, 0 );
637 AltitudePoints aPoints = GetAltitudePoints();
638 AltitudePoints::Iterator anIter( aPoints );
639 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
641 AltitudePoint& aPoint = anIter.ChangeValue();
644 SetAltitudePoints( aPoints );