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 );
47 QString aFilePath = GetFilePath();
48 if ( !aFilePath.isEmpty() )
50 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
51 .arg( aBathymetryName ).arg( aFilePath );
55 // TODO : bathymetry is composed from other bathymetry(ies)
61 double HYDROData_Bathymetry::GetInvalidAltitude()
63 return INVALID_ALTITUDE_VALUE;
66 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
68 RemoveAltitudePoints();
70 if ( thePoints.isEmpty() )
74 Handle(TDataStd_RealArray) aCoordsArray =
75 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
77 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
78 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
79 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
81 const AltitudePoint& aPoint = *aListItBeg;
83 aCoordsArray->SetValue( i * 3, aPoint.X() );
84 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
85 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
89 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
91 AltitudePoints aPoints;
93 Handle(TDataStd_RealArray) aCoordsArray;
94 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
97 int aLowerIdx = aCoordsArray->Lower();
98 int anUpperIdx = aCoordsArray->Upper();
99 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
104 AltitudePoint aPoint;
105 aPoint.SetX( aCoordsArray->Value( i++ ) );
106 aPoint.SetY( aCoordsArray->Value( i++ ) );
107 aPoint.SetZ( aCoordsArray->Value( i++ ) );
115 void HYDROData_Bathymetry::RemoveAltitudePoints()
117 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
118 aLab.ForgetAllAttributes();
121 void interpolateAltitudeForPoints( const gp_XY& thePoint,
122 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
123 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
124 HYDROData_Bathymetry::AltitudePoint& theResPoint,
125 const bool& theIsVertical )
127 double aCoordX = thePoint.X();
128 double aCoordY = thePoint.Y();
132 aCoordX = theFirstPoint.X();
134 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
136 // Recalculate X coordinate by equation of line from two points
137 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
138 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
143 aCoordY = theFirstPoint.Y();
145 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
147 // Recalculate y by equation of line from two points
148 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
149 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
153 theResPoint.SetX( aCoordX );
154 theResPoint.SetY( aCoordY );
156 // Calculate coefficient for interpolation
157 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
158 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
160 double aInterCoeff = 0;
162 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
165 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
166 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
168 // Calculate interpolated value
169 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
171 theResPoint.SetZ( aResVal );
174 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
176 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
177 return GetAltitudeForPoint( aGpPoint );
180 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
182 double aResAltitude = -9999.90;
184 AltitudePoints anAltitudePoints = GetAltitudePoints();
185 if ( anAltitudePoints.isEmpty() )
188 QPolygonF aBoundingRect;
191 // [ 0 (top-left) ] [ 1 (top-right) ]
193 // [ 2 (bot-left) ] [ 3 (bot-right) ]
194 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
195 AltitudePoint( DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
196 AltitudePoint( -DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ),
197 AltitudePoint( DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ) };
199 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
200 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
201 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
203 const AltitudePoint& aPoint = *aListItBeg;
205 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
206 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
208 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
210 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
212 aResAltitude = aPoint.Z();
215 else if ( aDeltaY < 0 ) // top side
218 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
219 aBounds[ 0 ] = aPoint;
220 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
221 aBounds[ 1 ] = aPoint;
226 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
227 aBounds[ 2 ] = aPoint;
228 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
229 aBounds[ 3 ] = aPoint;
232 else if ( aDeltaX < 0 ) // left side
234 if ( ValuesEquals( aDeltaY, 0.0 ) )
237 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
238 aBounds[ 0 ] = aPoint;
239 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
240 aBounds[ 2 ] = aPoint;
242 else if ( aDeltaY < 0 )
245 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
246 aBounds[ 0 ] = aPoint;
250 // bottom left corner
251 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
252 aBounds[ 2 ] = aPoint;
257 if ( ValuesEquals( aDeltaY, 0.0 ) )
260 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
261 aBounds[ 1 ] = aPoint;
262 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
263 aBounds[ 3 ] = aPoint;
265 else if ( aDeltaY < 0 )
268 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
269 aBounds[ 1 ] = aPoint;
273 // bottom right corner
274 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
275 aBounds[ 3 ] = aPoint;
279 // Update bounding rectangle of our global grid
280 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
283 // Check if requested point is inside of our bounding rectangle
284 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
285 return INVALID_ALTITUDE_VALUE;
287 // Calculate result altitude for point
288 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
290 // At first we merge top and bottom borders
291 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
292 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
294 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
295 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
297 AltitudePoint aResPoint( aFirstPoint );
299 // At last we merge left and right borders
300 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
301 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
303 aResAltitude = aResPoint.Z();
308 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
310 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
311 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
314 QString HYDROData_Bathymetry::GetFilePath() const
318 Handle(TDataStd_AsciiString) anAsciiStr;
319 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
320 aRes = QString( anAsciiStr->Get().ToCString() );
325 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
327 // Try to open the file
328 QFile aFile( theFileName );
329 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
334 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
336 AltitudePoints aPoints;
338 // Try to import the file
339 if ( aFileSuf == "xyz" )
340 aRes = importFromXYZFile( aFile, aPoints );
347 // Update file path and altitude points of this Bathymetry
348 SetFilePath( theFileName );
349 SetAltitudePoints( aPoints );
352 return aRes && !aPoints.isEmpty();
355 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
356 AltitudePoints& thePoints )
358 if ( !theFile.isOpen() )
361 // Strings in file is written as:
362 // 1. X(float) Y(float) Z(float)
363 // 2. X(float) Y(float) Z(float)
366 while ( !theFile.atEnd() )
368 QString aLine = theFile.readLine().simplified();
369 if ( aLine.isEmpty() )
372 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
373 if ( aValues.length() < 3 )
376 AltitudePoint aPoint;
378 QString anX = aValues.value( 0 );
379 QString anY = aValues.value( 1 );
380 QString aZ = aValues.value( 2 );
382 bool isXOk = false, isYOk = false, isZOk = false;
384 aPoint.SetX( anX.toDouble( &isXOk ) );
385 aPoint.SetY( anY.toDouble( &isYOk ) );
386 aPoint.SetZ( aZ.toDouble( &isZOk ) );
388 if ( !isXOk || !isYOk || !isZOk )