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>
38 #include <QStringList>
44 #include <OSD_Timer.hxx>
47 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
48 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
50 HYDROData_Bathymetry::HYDROData_Bathymetry()
51 : HYDROData_IAltitudeObject()
55 HYDROData_Bathymetry::~HYDROData_Bathymetry()
59 QStringList HYDROData_Bathymetry::DumpToPython( const QString& thePyScriptPath,
60 MapOfTreatedObjects& theTreatedObjects ) const
62 QStringList aResList = dumpObjectCreation( theTreatedObjects );
63 QString aBathymetryName = GetObjPyName();
65 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
66 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
68 TCollection_AsciiString aFilePath = GetFilePath();
69 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
70 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
72 aResList << QString( "" );
73 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
74 aResList << QString( "" );
79 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
81 RemoveAltitudePoints();
83 if ( thePoints.IsEmpty() )
87 Handle(TDataStd_RealArray) aCoordsArray =
88 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
90 AltitudePoints::Iterator anIter( thePoints );
91 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
93 const AltitudePoint& aPoint = anIter.Value();
95 aCoordsArray->SetValue( i * 3, aPoint.X() );
96 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
97 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
103 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints(bool IsConvertToGlobal) const
105 AltitudePoints aPoints;
107 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
108 if ( aLabel.IsNull() )
111 Handle(TDataStd_RealArray) aCoordsArray;
112 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
115 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
116 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
121 AltitudePoint aPoint;
122 aPoint.SetX( aCoordsArray->Value( i++ ) );
123 aPoint.SetY( aCoordsArray->Value( i++ ) );
124 aPoint.SetZ( aCoordsArray->Value( i++ ) );
126 if( IsConvertToGlobal )
127 aDoc->Transform( aPoint, false );
128 aPoints.Append( aPoint );
134 void HYDROData_Bathymetry::RemoveAltitudePoints()
136 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
137 if ( !aLabel.IsNull() )
139 aLabel.ForgetAllAttributes();
144 void interpolateAltitudeForPoints( const gp_XY& thePoint,
145 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
146 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
147 HYDROData_Bathymetry::AltitudePoint& theResPoint,
148 const bool& theIsVertical )
150 double aCoordX = thePoint.X();
151 double aCoordY = thePoint.Y();
155 aCoordX = theFirstPoint.X();
157 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
159 // Recalculate X coordinate by equation of line from two points
160 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
161 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
166 aCoordY = theFirstPoint.Y();
168 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
170 // Recalculate y by equation of line from two points
171 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
172 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
176 theResPoint.SetX( aCoordX );
177 theResPoint.SetY( aCoordY );
179 // Calculate coefficient for interpolation
180 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
181 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
183 double aInterCoeff = 0;
185 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
188 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
189 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
191 // Calculate interpolated value
192 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
194 theResPoint.SetZ( aResVal );
197 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
199 double anInvalidAltitude = GetInvalidAltitude();
200 double aResAltitude = anInvalidAltitude;
202 AltitudePoints anAltitudePoints = GetAltitudePoints();
203 if ( anAltitudePoints.IsEmpty() )
206 QPolygonF aBoundingRect;
209 // [ 0 (top-left) ] [ 1 (top-right) ]
211 // [ 2 (bot-left) ] [ 3 (bot-right) ]
212 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
213 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
214 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
215 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
217 AltitudePoints::Iterator anIter( anAltitudePoints );
218 for ( ; anIter.More(); anIter.Next() )
220 const AltitudePoint& aPoint = anIter.Value();
222 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
223 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
225 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
227 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
229 aResAltitude = aPoint.Z();
232 else if ( aDeltaY < 0 ) // top side
235 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
236 aBounds[ 0 ] = aPoint;
237 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
238 aBounds[ 1 ] = aPoint;
243 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
244 aBounds[ 2 ] = aPoint;
245 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
246 aBounds[ 3 ] = aPoint;
249 else if ( aDeltaX < 0 ) // left side
251 if ( ValuesEquals( aDeltaY, 0.0 ) )
254 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
255 aBounds[ 0 ] = aPoint;
256 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
257 aBounds[ 2 ] = aPoint;
259 else if ( aDeltaY < 0 )
262 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
263 aBounds[ 0 ] = aPoint;
267 // bottom left corner
268 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
269 aBounds[ 2 ] = aPoint;
274 if ( ValuesEquals( aDeltaY, 0.0 ) )
277 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
278 aBounds[ 1 ] = aPoint;
279 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
280 aBounds[ 3 ] = aPoint;
282 else if ( aDeltaY < 0 )
285 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
286 aBounds[ 1 ] = aPoint;
290 // bottom right corner
291 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
292 aBounds[ 3 ] = aPoint;
296 // Update bounding rectangle of our global grid
297 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
300 const double LIMIT = 1E300;
301 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
302 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
303 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
304 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
305 return anInvalidAltitude;
308 // Check if requested point is inside of our bounding rectangle
309 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
312 // Calculate result altitude for point
313 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
315 // At first we merge top and bottom borders
316 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
317 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
319 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
320 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
322 AltitudePoint aResPoint( aFirstPoint );
324 // At last we merge left and right borders
325 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
326 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
328 aResAltitude = aResPoint.Z();
333 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
335 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
338 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
340 TCollection_AsciiString aRes;
342 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
343 if ( !aLabel.IsNull() )
345 Handle(TDataStd_AsciiString) anAsciiStr;
346 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
347 aRes = anAsciiStr->Get();
353 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
354 const bool theIsUpdate )
356 bool anIsAltitudesInverted = IsAltitudesInverted();
357 if ( anIsAltitudesInverted == theIsInverted )
360 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
367 // Update altitude points
368 AltitudePoints anAltitudePoints = GetAltitudePoints();
369 if ( anAltitudePoints.IsEmpty() )
372 AltitudePoints::Iterator anIter( anAltitudePoints );
373 for ( ; anIter.More(); anIter.Next() )
375 AltitudePoint& aPoint = anIter.ChangeValue();
376 aPoint.SetZ( aPoint.Z() * -1 );
379 SetAltitudePoints( anAltitudePoints );
382 bool HYDROData_Bathymetry::IsAltitudesInverted() const
386 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
387 if ( !aLabel.IsNull() )
389 Handle(TDataStd_Integer) anIntVal;
390 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
391 aRes = (bool)anIntVal->Get();
397 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
399 // Try to open the file
400 QFile aFile( theFileName.ToCString() );
401 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
406 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
408 AltitudePoints aPoints;
410 // Try to import the file
411 if ( aFileSuf == "xyz" )
412 aRes = importFromXYZFile( aFile, aPoints );
413 else if ( aFileSuf == "asc" )
414 aRes = importFromASCFile( aFile, aPoints );
420 // Convert from global to local CS
421 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
422 AltitudePoints::Iterator anIter( aPoints );
423 for ( ; anIter.More(); anIter.Next() )
425 AltitudePoint& aPoint = anIter.ChangeValue();
426 aDoc->Transform( aPoint, true );
431 // Update file path and altitude points of this Bathymetry
432 SetFilePath( theFileName );
433 SetAltitudePoints( aPoints );
436 return aRes && !aPoints.IsEmpty();
439 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
440 AltitudePoints& thePoints ) const
442 if ( !theFile.isOpen() )
445 // Strings in file is written as:
446 // 1. X(float) Y(float) Z(float)
447 // 2. X(float) Y(float) Z(float)
455 bool anIsAltitudesInverted = IsAltitudesInverted();
456 while ( !theFile.atEnd() )
458 QString aLine = theFile.readLine().simplified();
459 if ( aLine.isEmpty() )
462 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
463 if ( aValues.length() < 3 )
466 AltitudePoint aPoint;
468 QString anX = aValues.value( 0 );
469 QString anY = aValues.value( 1 );
470 QString aZ = aValues.value( 2 );
472 bool isXOk = false, isYOk = false, isZOk = false;
474 aPoint.SetX( anX.toDouble( &isXOk ) );
475 aPoint.SetY( anY.toDouble( &isYOk ) );
476 aPoint.SetZ( aZ.toDouble( &isZOk ) );
478 if ( !isXOk || !isYOk || !isZOk )
481 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
482 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
483 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
486 // Invert the z value if requested
487 if ( anIsAltitudesInverted )
488 aPoint.SetZ( -aPoint.Z() );
490 thePoints.Append( aPoint );
495 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
496 aTimer.Show( stream );
502 bool HYDROData_Bathymetry::importFromASCFile( QFile& theFile,
503 AltitudePoints& thePoints ) const
505 if ( !theFile.isOpen() )
509 QStringList aStrList;
518 aLine = theFile.readLine().simplified();
519 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
520 if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
522 aNCols = aStrList[1].toInt();
524 aLine = theFile.readLine().simplified();
525 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
526 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
528 aNRows = aStrList[1].toInt();
530 aLine = theFile.readLine().simplified();
531 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
532 if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
534 anXllCorner = aStrList[1].toDouble();
536 aLine = theFile.readLine().simplified();
537 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
538 if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
540 anYllCorner = aStrList[1].toDouble();
542 aLine = theFile.readLine().simplified();
543 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
544 if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
546 aCellSize = aStrList[1].toDouble();
548 aLine = theFile.readLine().simplified();
549 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
550 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
552 aNoDataValue = aStrList[1].toDouble();
554 bool anIsAltitudesInverted = IsAltitudesInverted();
558 while ( !theFile.atEnd() )
560 aLine = theFile.readLine().simplified();
561 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
563 aStrLength = aStrList.length();
564 if ( aStrLength == 0 )
567 if ( aStrLength != aNRows )
570 for (int j = 0; j < aNCols; j++)
572 if (aStrList[j].toDouble() != aNoDataValue)
574 AltitudePoint aPoint;
575 aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
576 aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
577 aPoint.SetZ(aStrList[j].toDouble());
579 if ( anIsAltitudesInverted )
580 aPoint.SetZ( -aPoint.Z() );
582 thePoints.Append(aPoint);
594 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
596 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
597 Handle_HYDROData_PolylineXY aResult =
598 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
600 if( aResult.IsNull() )
604 QString aPolylinePref = GetName() + "_Boundary";
605 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
606 aResult->SetName( aPolylineName );
608 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
610 AltitudePoints aPoints = GetAltitudePoints();
612 AltitudePoints::Iterator anIter( aPoints );
613 for ( ; anIter.More(); anIter.Next() )
615 const AltitudePoint& aPoint = anIter.Value();
617 double x = aPoint.X(), y = aPoint.Y();
618 if( isFirst || x<Xmin )
620 if( isFirst || x>Xmax )
622 if( isFirst || y<Ymin )
624 if( isFirst || y>Ymax )
629 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
630 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
631 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
632 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
633 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
635 aResult->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
642 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
644 gp_XYZ aDelta( theDx, theDy, 0 );
645 AltitudePoints aPoints = GetAltitudePoints();
646 AltitudePoints::Iterator anIter( aPoints );
647 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
649 AltitudePoint& aPoint = anIter.ChangeValue();
652 SetAltitudePoints( aPoints );