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"
27 #include <TDataStd_RealArray.hxx>
28 #include <TDataStd_AsciiString.hxx>
29 #include <TDataStd_Integer.hxx>
36 #include <QStringList>
42 #include <OSD_Timer.hxx>
45 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
46 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
48 HYDROData_Bathymetry::HYDROData_Bathymetry()
49 : HYDROData_IAltitudeObject()
53 HYDROData_Bathymetry::~HYDROData_Bathymetry()
57 QStringList HYDROData_Bathymetry::DumpToPython( const QString& thePyScriptPath,
58 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(bool IsConvertToGlobal) 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 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
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 if( IsConvertToGlobal )
125 aDoc->Transform( aPoint, false );
126 aPoints.Append( aPoint );
132 void HYDROData_Bathymetry::RemoveAltitudePoints()
134 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
135 if ( !aLabel.IsNull() )
137 aLabel.ForgetAllAttributes();
142 void interpolateAltitudeForPoints( const gp_XY& thePoint,
143 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
144 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
145 HYDROData_Bathymetry::AltitudePoint& theResPoint,
146 const bool& theIsVertical )
148 double aCoordX = thePoint.X();
149 double aCoordY = thePoint.Y();
153 aCoordX = theFirstPoint.X();
155 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
157 // Recalculate X coordinate by equation of line from two points
158 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
159 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
164 aCoordY = theFirstPoint.Y();
166 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
168 // Recalculate y by equation of line from two points
169 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
170 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
174 theResPoint.SetX( aCoordX );
175 theResPoint.SetY( aCoordY );
177 // Calculate coefficient for interpolation
178 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
179 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
181 double aInterCoeff = 0;
183 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
186 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
187 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
189 // Calculate interpolated value
190 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
192 theResPoint.SetZ( aResVal );
195 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
197 double anInvalidAltitude = GetInvalidAltitude();
198 double aResAltitude = anInvalidAltitude;
200 AltitudePoints anAltitudePoints = GetAltitudePoints();
201 if ( anAltitudePoints.IsEmpty() )
204 QPolygonF aBoundingRect;
207 // [ 0 (top-left) ] [ 1 (top-right) ]
209 // [ 2 (bot-left) ] [ 3 (bot-right) ]
210 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
211 AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
212 AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
213 AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
215 AltitudePoints::Iterator anIter( anAltitudePoints );
216 for ( ; anIter.More(); anIter.Next() )
218 const AltitudePoint& aPoint = anIter.Value();
220 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
221 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
223 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
225 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
227 aResAltitude = aPoint.Z();
230 else if ( aDeltaY < 0 ) // top side
233 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
234 aBounds[ 0 ] = aPoint;
235 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
236 aBounds[ 1 ] = aPoint;
241 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
242 aBounds[ 2 ] = aPoint;
243 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
244 aBounds[ 3 ] = aPoint;
247 else if ( aDeltaX < 0 ) // left side
249 if ( ValuesEquals( aDeltaY, 0.0 ) )
252 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
253 aBounds[ 0 ] = aPoint;
254 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
255 aBounds[ 2 ] = aPoint;
257 else if ( aDeltaY < 0 )
260 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
261 aBounds[ 0 ] = aPoint;
265 // bottom left corner
266 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
267 aBounds[ 2 ] = aPoint;
272 if ( ValuesEquals( aDeltaY, 0.0 ) )
275 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
276 aBounds[ 1 ] = aPoint;
277 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
278 aBounds[ 3 ] = aPoint;
280 else if ( aDeltaY < 0 )
283 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
284 aBounds[ 1 ] = aPoint;
288 // bottom right corner
289 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
290 aBounds[ 3 ] = aPoint;
294 // Update bounding rectangle of our global grid
295 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
298 const double LIMIT = 1E300;
299 if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
300 fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
301 fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
302 fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
303 return anInvalidAltitude;
306 // Check if requested point is inside of our bounding rectangle
307 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
310 // Calculate result altitude for point
311 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
313 // At first we merge top and bottom borders
314 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
315 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
317 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
318 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
320 AltitudePoint aResPoint( aFirstPoint );
322 // At last we merge left and right borders
323 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
324 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
326 aResAltitude = aResPoint.Z();
331 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
333 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
336 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
338 TCollection_AsciiString aRes;
340 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
341 if ( !aLabel.IsNull() )
343 Handle(TDataStd_AsciiString) anAsciiStr;
344 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
345 aRes = anAsciiStr->Get();
351 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
352 const bool theIsUpdate )
354 bool anIsAltitudesInverted = IsAltitudesInverted();
355 if ( anIsAltitudesInverted == theIsInverted )
358 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
365 // Update altitude points
366 AltitudePoints anAltitudePoints = GetAltitudePoints();
367 if ( anAltitudePoints.IsEmpty() )
370 AltitudePoints::Iterator anIter( anAltitudePoints );
371 for ( ; anIter.More(); anIter.Next() )
373 AltitudePoint& aPoint = anIter.ChangeValue();
374 aPoint.SetZ( aPoint.Z() * -1 );
377 SetAltitudePoints( anAltitudePoints );
380 bool HYDROData_Bathymetry::IsAltitudesInverted() const
384 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
385 if ( !aLabel.IsNull() )
387 Handle(TDataStd_Integer) anIntVal;
388 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
389 aRes = (bool)anIntVal->Get();
395 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
397 // Try to open the file
398 QFile aFile( theFileName.ToCString() );
399 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
404 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
406 AltitudePoints aPoints;
408 // Try to import the file
409 if ( aFileSuf == "xyz" )
410 aRes = importFromXYZFile( aFile, aPoints );
411 else if ( aFileSuf == "asc" )
412 aRes = importFromASCFile( aFile, aPoints );
418 // Convert from global to local CS
419 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
420 AltitudePoints::Iterator anIter( aPoints );
421 for ( ; anIter.More(); anIter.Next() )
423 AltitudePoint& aPoint = anIter.ChangeValue();
424 aDoc->Transform( aPoint, true );
429 // Update file path and altitude points of this Bathymetry
430 SetFilePath( theFileName );
431 SetAltitudePoints( aPoints );
434 return aRes && !aPoints.IsEmpty();
437 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
438 AltitudePoints& thePoints ) const
440 if ( !theFile.isOpen() )
443 // Strings in file is written as:
444 // 1. X(float) Y(float) Z(float)
445 // 2. X(float) Y(float) Z(float)
453 bool anIsAltitudesInverted = IsAltitudesInverted();
454 while ( !theFile.atEnd() )
456 QString aLine = theFile.readLine().simplified();
457 if ( aLine.isEmpty() )
460 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
461 if ( aValues.length() < 3 )
464 AltitudePoint aPoint;
466 QString anX = aValues.value( 0 );
467 QString anY = aValues.value( 1 );
468 QString aZ = aValues.value( 2 );
470 bool isXOk = false, isYOk = false, isZOk = false;
472 aPoint.SetX( anX.toDouble( &isXOk ) );
473 aPoint.SetY( anY.toDouble( &isYOk ) );
474 aPoint.SetZ( aZ.toDouble( &isZOk ) );
476 if ( !isXOk || !isYOk || !isZOk )
479 if ( HYDROData_Tool::IsNan( aPoint.X() ) || HYDROData_Tool::IsInf( aPoint.X() ) ||
480 HYDROData_Tool::IsNan( aPoint.Y() ) || HYDROData_Tool::IsInf( aPoint.Y() ) ||
481 HYDROData_Tool::IsNan( aPoint.Z() ) || HYDROData_Tool::IsInf( aPoint.Z() ) )
484 // Invert the z value if requested
485 if ( anIsAltitudesInverted )
486 aPoint.SetZ( -aPoint.Z() );
488 thePoints.Append( aPoint );
493 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
494 aTimer.Show( stream );
500 bool HYDROData_Bathymetry::importFromASCFile( QFile& theFile,
501 AltitudePoints& thePoints ) const
503 if ( !theFile.isOpen() )
507 QStringList aStrList;
516 aLine = theFile.readLine().simplified();
517 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
518 if ( aStrList.length() != 2 && aStrList[0].toLower() != "ncols" )
520 aNCols = aStrList[1].toInt();
522 aLine = theFile.readLine().simplified();
523 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
524 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nrows" )
526 aNRows = aStrList[1].toInt();
528 aLine = theFile.readLine().simplified();
529 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
530 if ( aStrList.length() != 2 && aStrList[0].toLower() != "xllcorner" )
532 anXllCorner = aStrList[1].toDouble();
534 aLine = theFile.readLine().simplified();
535 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
536 if ( aStrList.length() != 2 && aStrList[0].toLower() != "yllcorner" )
538 anYllCorner = aStrList[1].toDouble();
540 aLine = theFile.readLine().simplified();
541 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
542 if ( aStrList.length() != 2 && aStrList[0].toLower() != "cellsize" )
544 aCellSize = aStrList[1].toDouble();
546 aLine = theFile.readLine().simplified();
547 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
548 if ( aStrList.length() != 2 && aStrList[0].toLower() != "nodata_value" )
550 aNoDataValue = aStrList[1].toDouble();
552 bool anIsAltitudesInverted = IsAltitudesInverted();
556 while ( !theFile.atEnd() )
558 aLine = theFile.readLine().simplified();
559 aStrList = aLine.split( ' ', QString::SkipEmptyParts );
561 aStrLength = aStrList.length();
562 if ( aStrLength == 0 )
565 if ( aStrLength != aNRows )
568 for (int j = 0; j < aNCols; j++)
570 if (aStrList[j].toDouble() != aNoDataValue)
572 AltitudePoint aPoint;
573 aPoint.SetX(anXllCorner + aCellSize*(j + 0.5));
574 aPoint.SetY(anYllCorner + aCellSize*(aNRows - i + 0.5));
575 aPoint.SetZ(aStrList[j].toDouble());
577 if ( anIsAltitudesInverted )
578 aPoint.SetZ( -aPoint.Z() );
580 thePoints.Append(aPoint);
592 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
594 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
595 Handle_HYDROData_PolylineXY aResult =
596 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
598 if( aResult.IsNull() )
602 QString aPolylinePref = GetName() + "_Boundary";
603 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
604 aResult->SetName( aPolylineName );
606 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
608 AltitudePoints aPoints = GetAltitudePoints();
610 AltitudePoints::Iterator anIter( aPoints );
611 for ( ; anIter.More(); anIter.Next() )
613 const AltitudePoint& aPoint = anIter.Value();
615 double x = aPoint.X(), y = aPoint.Y();
616 if( isFirst || x<Xmin )
618 if( isFirst || x>Xmax )
620 if( isFirst || y<Ymin )
622 if( isFirst || y>Ymax )
627 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
628 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
629 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
630 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
631 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
633 aResult->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
640 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
642 gp_XYZ aDelta( theDx, theDy, 0 );
643 AltitudePoints aPoints = GetAltitudePoints();
644 AltitudePoints::Iterator anIter( aPoints );
645 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
647 AltitudePoint& aPoint = anIter.ChangeValue();
650 SetAltitudePoints( aPoints );