1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_Bathymetry.h"
24 #include "HYDROData_Document.h"
25 #include "HYDROData_Tool.h"
26 #include "HYDROData_PolylineXY.h"
28 #include <boost/math/special_functions/fpclassify.hpp>
33 #include <TDataStd_RealArray.hxx>
34 #include <TDataStd_AsciiString.hxx>
35 #include <TDataStd_Integer.hxx>
41 #include <QStringList>
47 #include <OSD_Timer.hxx>
50 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
51 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
53 HYDROData_Bathymetry::HYDROData_Bathymetry()
54 : HYDROData_IAltitudeObject()
58 HYDROData_Bathymetry::~HYDROData_Bathymetry()
62 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
64 QStringList aResList = dumpObjectCreation( theTreatedObjects );
65 QString aBathymetryName = GetObjPyName();
67 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
68 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
70 TCollection_AsciiString aFilePath = GetFilePath();
71 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
72 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
74 aResList << QString( "" );
75 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
76 aResList << QString( "" );
81 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
83 RemoveAltitudePoints();
85 if ( thePoints.IsEmpty() )
89 Handle(TDataStd_RealArray) aCoordsArray =
90 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
92 AltitudePoints::Iterator anIter( thePoints );
93 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
95 const AltitudePoint& aPoint = anIter.Value();
97 aCoordsArray->SetValue( i * 3, aPoint.X() );
98 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
99 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
105 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
107 AltitudePoints aPoints;
109 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
110 if ( aLabel.IsNull() )
113 Handle(TDataStd_RealArray) aCoordsArray;
114 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
117 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
122 AltitudePoint aPoint;
123 aPoint.SetX( aCoordsArray->Value( i++ ) );
124 aPoint.SetY( aCoordsArray->Value( i++ ) );
125 aPoint.SetZ( aCoordsArray->Value( i++ ) );
127 aPoints.Append( aPoint );
133 void HYDROData_Bathymetry::RemoveAltitudePoints()
135 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
136 if ( !aLabel.IsNull() )
138 aLabel.ForgetAllAttributes();
143 void interpolateAltitudeForPoints( const gp_XY& thePoint,
144 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
145 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
146 HYDROData_Bathymetry::AltitudePoint& theResPoint,
147 const bool& theIsVertical )
149 double aCoordX = thePoint.X();
150 double aCoordY = thePoint.Y();
154 aCoordX = theFirstPoint.X();
156 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
158 // Recalculate X coordinate by equation of line from two points
159 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
160 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
165 aCoordY = theFirstPoint.Y();
167 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
169 // Recalculate y by equation of line from two points
170 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
171 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
175 theResPoint.SetX( aCoordX );
176 theResPoint.SetY( aCoordY );
178 // Calculate coefficient for interpolation
179 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
180 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
182 double aInterCoeff = 0;
184 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
187 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
188 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
190 // Calculate interpolated value
191 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
193 theResPoint.SetZ( aResVal );
196 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
198 double anInvalidAltitude = GetInvalidAltitude();
199 double aResAltitude = anInvalidAltitude;
201 AltitudePoints anAltitudePoints = GetAltitudePoints();
202 if ( anAltitudePoints.IsEmpty() )
205 QPolygonF aBoundingRect;
208 // [ 0 (top-left) ] [ 1 (top-right) ]
210 // [ 2 (bot-left) ] [ 3 (bot-right) ]
211 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
212 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
213 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
214 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
216 AltitudePoints::Iterator anIter( anAltitudePoints );
217 for ( ; anIter.More(); anIter.Next() )
219 const AltitudePoint& aPoint = anIter.Value();
221 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
222 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
224 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
226 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
228 aResAltitude = aPoint.Z();
231 else if ( aDeltaY < 0 ) // top side
234 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
235 aBounds[ 0 ] = aPoint;
236 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
237 aBounds[ 1 ] = aPoint;
242 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
243 aBounds[ 2 ] = aPoint;
244 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
245 aBounds[ 3 ] = aPoint;
248 else if ( aDeltaX < 0 ) // left side
250 if ( ValuesEquals( aDeltaY, 0.0 ) )
253 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
254 aBounds[ 0 ] = aPoint;
255 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
256 aBounds[ 2 ] = aPoint;
258 else if ( aDeltaY < 0 )
261 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
262 aBounds[ 0 ] = aPoint;
266 // bottom left corner
267 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
268 aBounds[ 2 ] = aPoint;
273 if ( ValuesEquals( aDeltaY, 0.0 ) )
276 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
277 aBounds[ 1 ] = aPoint;
278 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
279 aBounds[ 3 ] = aPoint;
281 else if ( aDeltaY < 0 )
284 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
285 aBounds[ 1 ] = aPoint;
289 // bottom right corner
290 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
291 aBounds[ 3 ] = aPoint;
295 // Update bounding rectangle of our global grid
296 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
299 const double LIMIT = 1E300;
300 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
301 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
302 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
303 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
304 return anInvalidAltitude;
307 // Check if requested point is inside of our bounding rectangle
308 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
311 // Calculate result altitude for point
312 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
314 // At first we merge top and bottom borders
315 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
316 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
318 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
319 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
321 AltitudePoint aResPoint( aFirstPoint );
323 // At last we merge left and right borders
324 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
325 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
327 aResAltitude = aResPoint.Z();
332 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
334 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
337 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
339 TCollection_AsciiString aRes;
341 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
342 if ( !aLabel.IsNull() )
344 Handle(TDataStd_AsciiString) anAsciiStr;
345 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
346 aRes = anAsciiStr->Get();
352 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
353 const bool theIsUpdate )
355 bool anIsAltitudesInverted = IsAltitudesInverted();
356 if ( anIsAltitudesInverted == theIsInverted )
359 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
366 // Update altitude points
367 AltitudePoints anAltitudePoints = GetAltitudePoints();
368 if ( anAltitudePoints.IsEmpty() )
371 AltitudePoints::Iterator anIter( anAltitudePoints );
372 for ( ; anIter.More(); anIter.Next() )
374 AltitudePoint& aPoint = anIter.ChangeValue();
375 aPoint.SetZ( aPoint.Z() * -1 );
378 SetAltitudePoints( anAltitudePoints );
381 bool HYDROData_Bathymetry::IsAltitudesInverted() const
385 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
386 if ( !aLabel.IsNull() )
388 Handle(TDataStd_Integer) anIntVal;
389 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
390 aRes = (bool)anIntVal->Get();
396 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
398 // Try to open the file
399 QFile aFile( theFileName.ToCString() );
400 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
405 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
407 AltitudePoints aPoints;
409 // Try to import the file
410 if ( aFileSuf == "xyz" )
411 aRes = importFromXYZFile( aFile, aPoints );
412 else if ( aFileSuf == "asc" )
413 aRes = importFromASCFile( aFile, aPoints );
419 // Convert from global to local CS
420 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
421 AltitudePoints::Iterator anIter( aPoints );
422 for ( ; anIter.More(); anIter.Next() )
424 AltitudePoint& aPoint = anIter.ChangeValue();
425 aDoc->Transform( aPoint, true );
430 // Update file path and altitude points of this Bathymetry
431 SetFilePath( theFileName );
432 SetAltitudePoints( aPoints );
435 return aRes && !aPoints.IsEmpty();
438 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
439 AltitudePoints& thePoints ) const
441 if ( !theFile.isOpen() )
444 // Strings in file is written as:
445 // 1. X(float) Y(float) Z(float)
446 // 2. X(float) Y(float) Z(float)
454 bool anIsAltitudesInverted = IsAltitudesInverted();
455 while ( !theFile.atEnd() )
457 QString aLine = theFile.readLine().simplified();
458 if ( aLine.isEmpty() )
461 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
462 if ( aValues.length() < 3 )
465 AltitudePoint aPoint;
467 QString anX = aValues.value( 0 );
468 QString anY = aValues.value( 1 );
469 QString aZ = aValues.value( 2 );
471 bool isXOk = false, isYOk = false, isZOk = false;
473 aPoint.SetX( anX.toDouble( &isXOk ) );
474 aPoint.SetY( anY.toDouble( &isYOk ) );
475 aPoint.SetZ( aZ.toDouble( &isZOk ) );
477 if ( !isXOk || !isYOk || !isZOk )
480 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
481 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
482 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
485 // Invert the z value if requested
486 if ( anIsAltitudesInverted )
487 aPoint.SetZ( -aPoint.Z() );
489 thePoints.Append( aPoint );
494 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
495 aTimer.Show( stream );
501 bool HYDROData_Bathymetry::importFromASCFile( QFile& theFile,
502 AltitudePoints& thePoints ) const
504 if ( !theFile.isOpen() )
508 QStringList aStrList;
517 aLine = theFile.readLine().simplified();
518 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
519 if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
521 aNCols = aStrList[1].toInt();
523 aLine = theFile.readLine().simplified();
524 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
525 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
527 aNRows = aStrList[1].toInt();
529 aLine = theFile.readLine().simplified();
530 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
531 if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
533 anXllCorner = aStrList[1].toDouble();
535 aLine = theFile.readLine().simplified();
536 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
537 if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
539 anYllCorner = aStrList[1].toDouble();
541 aLine = theFile.readLine().simplified();
542 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
543 if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
545 aCellSize = aStrList[1].toDouble();
547 aLine = theFile.readLine().simplified();
548 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
549 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
551 aNoDataValue = aStrList[1].toDouble();
553 bool anIsAltitudesInverted = IsAltitudesInverted();
557 while ( !theFile.atEnd() )
559 aLine = theFile.readLine().simplified();
560 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
562 aStrLength = aStrList.length();
563 if ( aStrLength == 0 )
566 if ( aStrLength != aNRows )
569 for (int j = 0; j < aNCols; j++)
571 if (aStrList[j].toDouble() != aNoDataValue)
573 AltitudePoint aPoint;
574 aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
575 aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
576 aPoint.SetZ(aStrList[j].toDouble());
578 if ( anIsAltitudesInverted )
579 aPoint.SetZ( -aPoint.Z() );
581 thePoints.Append(aPoint);
593 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
595 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
596 Handle_HYDROData_PolylineXY aResult =
597 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
599 if( aResult.IsNull() )
603 QString aPolylinePref = GetName() + "_Boundary";
604 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
605 aResult->SetName( aPolylineName );
607 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
609 AltitudePoints aPoints = GetAltitudePoints();
611 AltitudePoints::Iterator anIter( aPoints );
612 for ( ; anIter.More(); anIter.Next() )
614 const AltitudePoint& aPoint = anIter.Value();
616 double x = aPoint.X(), y = aPoint.Y();
617 if( isFirst || x<Xmin )
619 if( isFirst || x>Xmax )
621 if( isFirst || y<Ymin )
623 if( isFirst || y>Ymax )
628 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
629 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
630 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
631 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
632 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
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 );