2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
9 #include <TDataStd_RealArray.hxx>
10 #include <TDataStd_AsciiString.hxx>
16 #include <QStringList>
18 #define INVALID_ALTITUDE_VALUE -9999.0
19 #define PYTHON_BATHYMETRY_ID "4"
22 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_Object)
23 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_Object)
25 HYDROData_Bathymetry::HYDROData_Bathymetry()
29 HYDROData_Bathymetry::~HYDROData_Bathymetry()
33 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
37 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
38 if ( aDocument.IsNull() )
41 QString aDocName = aDocument->GetDocPyName();
42 QString aBathymetryName = GetName();
44 aResList << QString( "%1 = %2.CreateObject( %3 );" )
45 .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
46 aResList << QString( "%1.SetName( \"%2\" );" )
47 .arg( aBathymetryName ).arg( aBathymetryName );
49 QString aFilePath = GetFilePath();
50 if ( !aFilePath.isEmpty() )
52 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
53 .arg( aBathymetryName ).arg( aFilePath );
57 // TODO : bathymetry is composed from other bathymetry(ies)
63 double HYDROData_Bathymetry::GetInvalidAltitude()
65 return INVALID_ALTITUDE_VALUE;
68 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
70 RemoveAltitudePoints();
72 if ( thePoints.isEmpty() )
76 Handle(TDataStd_RealArray) aCoordsArray =
77 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
79 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
80 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
81 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
83 const AltitudePoint& aPoint = *aListItBeg;
85 aCoordsArray->SetValue( i * 3, aPoint.X() );
86 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
87 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
91 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
93 AltitudePoints aPoints;
95 Handle(TDataStd_RealArray) aCoordsArray;
96 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
99 int aLowerIdx = aCoordsArray->Lower();
100 int anUpperIdx = aCoordsArray->Upper();
101 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
106 AltitudePoint aPoint;
107 aPoint.SetX( aCoordsArray->Value( i++ ) );
108 aPoint.SetY( aCoordsArray->Value( i++ ) );
109 aPoint.SetZ( aCoordsArray->Value( i++ ) );
117 void HYDROData_Bathymetry::RemoveAltitudePoints()
119 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
120 aLab.ForgetAllAttributes();
123 void interpolateAltitudeForPoints( const gp_XY& thePoint,
124 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
125 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
126 HYDROData_Bathymetry::AltitudePoint& theResPoint,
127 const bool& theIsVertical )
129 double aCoordX = thePoint.X();
130 double aCoordY = thePoint.Y();
134 aCoordX = theFirstPoint.X();
136 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
138 // Recalculate X coordinate by equation of line from two points
139 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
140 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
145 aCoordY = theFirstPoint.Y();
147 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
149 // Recalculate y by equation of line from two points
150 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
151 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
155 theResPoint.SetX( aCoordX );
156 theResPoint.SetY( aCoordY );
158 // Calculate coefficient for interpolation
159 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
160 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
162 double aInterCoeff = 0;
164 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
167 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
168 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
170 // Calculate interpolated value
171 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
173 theResPoint.SetZ( aResVal );
176 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
178 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
179 return GetAltitudeForPoint( aGpPoint );
182 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
184 double aResAltitude = -9999.90;
186 AltitudePoints anAltitudePoints = GetAltitudePoints();
187 if ( anAltitudePoints.isEmpty() )
190 QPolygonF aBoundingRect;
193 // [ 0 (top-left) ] [ 1 (top-right) ]
195 // [ 2 (bot-left) ] [ 3 (bot-right) ]
196 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
197 AltitudePoint( DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
198 AltitudePoint( -DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ),
199 AltitudePoint( DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ) };
201 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
202 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
203 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
205 const AltitudePoint& aPoint = *aListItBeg;
207 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
208 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
210 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
212 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
214 aResAltitude = aPoint.Z();
217 else if ( aDeltaY < 0 ) // top side
220 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
221 aBounds[ 0 ] = aPoint;
222 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
223 aBounds[ 1 ] = aPoint;
228 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
229 aBounds[ 2 ] = aPoint;
230 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
231 aBounds[ 3 ] = aPoint;
234 else if ( aDeltaX < 0 ) // left side
236 if ( ValuesEquals( aDeltaY, 0.0 ) )
239 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
240 aBounds[ 0 ] = aPoint;
241 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
242 aBounds[ 2 ] = aPoint;
244 else if ( aDeltaY < 0 )
247 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
248 aBounds[ 0 ] = aPoint;
252 // bottom left corner
253 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
254 aBounds[ 2 ] = aPoint;
259 if ( ValuesEquals( aDeltaY, 0.0 ) )
262 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
263 aBounds[ 1 ] = aPoint;
264 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
265 aBounds[ 3 ] = aPoint;
267 else if ( aDeltaY < 0 )
270 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
271 aBounds[ 1 ] = aPoint;
275 // bottom right corner
276 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
277 aBounds[ 3 ] = aPoint;
281 // Update bounding rectangle of our global grid
282 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
285 // Check if requested point is inside of our bounding rectangle
286 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
287 return INVALID_ALTITUDE_VALUE;
289 // Calculate result altitude for point
290 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
292 // At first we merge top and bottom borders
293 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
294 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
296 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
297 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
299 AltitudePoint aResPoint( aFirstPoint );
301 // At last we merge left and right borders
302 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
303 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
305 aResAltitude = aResPoint.Z();
310 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
312 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
313 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
316 QString HYDROData_Bathymetry::GetFilePath() const
320 Handle(TDataStd_AsciiString) anAsciiStr;
321 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
322 aRes = QString( anAsciiStr->Get().ToCString() );
327 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
329 // Try to open the file
330 QFile aFile( theFileName );
331 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
336 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
338 AltitudePoints aPoints;
340 // Try to import the file
341 if ( aFileSuf == "xyz" )
342 aRes = importFromXYZFile( aFile, aPoints );
349 // Update file path and altitude points of this Bathymetry
350 SetFilePath( theFileName );
351 SetAltitudePoints( aPoints );
354 return aRes && !aPoints.isEmpty();
357 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
358 AltitudePoints& thePoints )
360 if ( !theFile.isOpen() )
363 // Strings in file is written as:
364 // 1. X(float) Y(float) Z(float)
365 // 2. X(float) Y(float) Z(float)
368 while ( !theFile.atEnd() )
370 QString aLine = theFile.readLine().simplified();
371 if ( aLine.isEmpty() )
374 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
375 if ( aValues.length() < 3 )
378 AltitudePoint aPoint;
380 QString anX = aValues.value( 0 );
381 QString anY = aValues.value( 1 );
382 QString aZ = aValues.value( 2 );
384 bool isXOk = false, isYOk = false, isZOk = false;
386 aPoint.SetX( anX.toDouble( &isXOk ) );
387 aPoint.SetY( anY.toDouble( &isYOk ) );
388 aPoint.SetZ( aZ.toDouble( &isZOk ) );
390 if ( !isXOk || !isYOk || !isZOk )