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( MapOfTreatedObjects& theTreatedObjects ) const
61 QStringList aResList = dumpObjectCreation( theTreatedObjects );
62 QString aBathymetryName = GetObjPyName();
64 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
65 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
67 TCollection_AsciiString aFilePath = GetFilePath();
68 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
69 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
71 aResList << QString( "" );
72 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
73 aResList << QString( "" );
78 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
80 RemoveAltitudePoints();
82 if ( thePoints.IsEmpty() )
86 Handle(TDataStd_RealArray) aCoordsArray =
87 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
89 AltitudePoints::Iterator anIter( thePoints );
90 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
92 const AltitudePoint& aPoint = anIter.Value();
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++ ) );
124 aPoints.Append( aPoint );
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::Iterator anIter( anAltitudePoints );
214 for ( ; anIter.More(); anIter.Next() )
216 const AltitudePoint& aPoint = anIter.Value();
218 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
219 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
221 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
223 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
225 aResAltitude = aPoint.Z();
228 else if ( aDeltaY < 0 ) // top side
231 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
232 aBounds[ 0 ] = aPoint;
233 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
234 aBounds[ 1 ] = aPoint;
239 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
240 aBounds[ 2 ] = aPoint;
241 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
242 aBounds[ 3 ] = aPoint;
245 else if ( aDeltaX < 0 ) // left side
247 if ( ValuesEquals( aDeltaY, 0.0 ) )
250 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
251 aBounds[ 0 ] = aPoint;
252 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
253 aBounds[ 2 ] = aPoint;
255 else if ( aDeltaY < 0 )
258 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
259 aBounds[ 0 ] = aPoint;
263 // bottom left corner
264 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
265 aBounds[ 2 ] = aPoint;
270 if ( ValuesEquals( aDeltaY, 0.0 ) )
273 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
274 aBounds[ 1 ] = aPoint;
275 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
276 aBounds[ 3 ] = aPoint;
278 else if ( aDeltaY < 0 )
281 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
282 aBounds[ 1 ] = aPoint;
286 // bottom right corner
287 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
288 aBounds[ 3 ] = aPoint;
292 // Update bounding rectangle of our global grid
293 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
296 const double LIMIT = 1E300;
297 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
298 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
299 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
300 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
301 return anInvalidAltitude;
304 // Check if requested point is inside of our bounding rectangle
305 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
308 // Calculate result altitude for point
309 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
311 // At first we merge top and bottom borders
312 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
313 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
315 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
316 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
318 AltitudePoint aResPoint( aFirstPoint );
320 // At last we merge left and right borders
321 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
322 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
324 aResAltitude = aResPoint.Z();
329 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
331 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
334 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
336 TCollection_AsciiString aRes;
338 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
339 if ( !aLabel.IsNull() )
341 Handle(TDataStd_AsciiString) anAsciiStr;
342 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
343 aRes = anAsciiStr->Get();
349 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
350 const bool theIsUpdate )
352 bool anIsAltitudesInverted = IsAltitudesInverted();
353 if ( anIsAltitudesInverted == theIsInverted )
356 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
363 // Update altitude points
364 AltitudePoints anAltitudePoints = GetAltitudePoints();
365 if ( anAltitudePoints.IsEmpty() )
368 AltitudePoints::Iterator anIter( anAltitudePoints );
369 for ( ; anIter.More(); anIter.Next() )
371 AltitudePoint& aPoint = anIter.ChangeValue();
372 aPoint.SetZ( aPoint.Z() * -1 );
375 SetAltitudePoints( anAltitudePoints );
378 bool HYDROData_Bathymetry::IsAltitudesInverted() const
382 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
383 if ( !aLabel.IsNull() )
385 Handle(TDataStd_Integer) anIntVal;
386 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
387 aRes = (bool)anIntVal->Get();
393 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
395 // Try to open the file
396 QFile aFile( theFileName.ToCString() );
397 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
402 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
404 AltitudePoints aPoints;
406 // Try to import the file
407 if ( aFileSuf == "xyz" )
408 aRes = importFromXYZFile( aFile, aPoints );
409 else if ( aFileSuf == "asc" )
410 aRes = importFromASCFile( aFile, aPoints );
416 // Convert from global to local CS
417 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
418 AltitudePoints::Iterator anIter( aPoints );
419 for ( ; anIter.More(); anIter.Next() )
421 AltitudePoint& aPoint = anIter.ChangeValue();
422 aDoc->Transform( aPoint, true );
427 // Update file path and altitude points of this Bathymetry
428 SetFilePath( theFileName );
429 SetAltitudePoints( aPoints );
432 return aRes && !aPoints.IsEmpty();
435 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
436 AltitudePoints& thePoints ) const
438 if ( !theFile.isOpen() )
441 // Strings in file is written as:
442 // 1. X(float) Y(float) Z(float)
443 // 2. X(float) Y(float) Z(float)
451 bool anIsAltitudesInverted = IsAltitudesInverted();
452 while ( !theFile.atEnd() )
454 QString aLine = theFile.readLine().simplified();
455 if ( aLine.isEmpty() )
458 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
459 if ( aValues.length() < 3 )
462 AltitudePoint aPoint;
464 QString anX = aValues.value( 0 );
465 QString anY = aValues.value( 1 );
466 QString aZ = aValues.value( 2 );
468 bool isXOk = false, isYOk = false, isZOk = false;
470 aPoint.SetX( anX.toDouble( &isXOk ) );
471 aPoint.SetY( anY.toDouble( &isYOk ) );
472 aPoint.SetZ( aZ.toDouble( &isZOk ) );
474 if ( !isXOk || !isYOk || !isZOk )
477 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
478 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
479 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
482 // Invert the z value if requested
483 if ( anIsAltitudesInverted )
484 aPoint.SetZ( -aPoint.Z() );
486 thePoints.Append( aPoint );
491 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
492 aTimer.Show( stream );
498 bool HYDROData_Bathymetry::importFromASCFile( QFile& theFile,
499 AltitudePoints& thePoints ) const
501 if ( !theFile.isOpen() )
505 QStringList aStrList;
514 aLine = theFile.readLine().simplified();
515 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
516 if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
518 aNCols = aStrList[1].toInt();
520 aLine = theFile.readLine().simplified();
521 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
522 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
524 aNRows = aStrList[1].toInt();
526 aLine = theFile.readLine().simplified();
527 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
528 if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
530 anXllCorner = aStrList[1].toDouble();
532 aLine = theFile.readLine().simplified();
533 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
534 if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
536 anYllCorner = aStrList[1].toDouble();
538 aLine = theFile.readLine().simplified();
539 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
540 if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
542 aCellSize = aStrList[1].toDouble();
544 aLine = theFile.readLine().simplified();
545 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
546 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
548 aNoDataValue = aStrList[1].toDouble();
550 bool anIsAltitudesInverted = IsAltitudesInverted();
554 while ( !theFile.atEnd() )
556 aLine = theFile.readLine().simplified();
557 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
559 aStrLength = aStrList.length();
560 if ( aStrLength == 0 )
563 if ( aStrLength != aNRows )
566 for (int j = 0; j < aNCols; j++)
568 if (aStrList[j].toDouble() != aNoDataValue)
570 AltitudePoint aPoint;
571 aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
572 aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
573 aPoint.SetZ(aStrList[j].toDouble());
575 if ( anIsAltitudesInverted )
576 aPoint.SetZ( -aPoint.Z() );
578 thePoints.Append(aPoint);
590 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
592 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
593 Handle_HYDROData_PolylineXY aResult =
594 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
596 if( aResult.IsNull() )
600 QString aPolylinePref = GetName() + "_Boundary";
601 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
602 aResult->SetName( aPolylineName );
604 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
606 AltitudePoints aPoints = GetAltitudePoints();
608 AltitudePoints::Iterator anIter( aPoints );
609 for ( ; anIter.More(); anIter.Next() )
611 const AltitudePoint& aPoint = anIter.Value();
613 double x = aPoint.X(), y = aPoint.Y();
614 if( isFirst || x<Xmin )
616 if( isFirst || x>Xmax )
618 if( isFirst || y<Ymin )
620 if( isFirst || y>Ymax )
625 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
626 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
627 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
628 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
629 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
631 aResult->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
638 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
640 gp_XYZ aDelta( theDx, theDy, 0 );
641 AltitudePoints aPoints = GetAltitudePoints();
642 AltitudePoints::Iterator anIter( aPoints );
643 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
645 AltitudePoint& aPoint = anIter.ChangeValue();
648 SetAltitudePoints( aPoints );