2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Tool.h"
8 #include <TDataStd_RealArray.hxx>
9 #include <TDataStd_AsciiString.hxx>
15 #include <QStringList>
17 #define INVALID_ALTITUDE_VALUE -9999.0
20 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_Object)
21 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_Object)
23 HYDROData_Bathymetry::HYDROData_Bathymetry()
27 HYDROData_Bathymetry::~HYDROData_Bathymetry()
31 double HYDROData_Bathymetry::GetInvalidAltitude()
33 return INVALID_ALTITUDE_VALUE;
36 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
38 RemoveAltitudePoints();
40 if ( thePoints.isEmpty() )
44 Handle(TDataStd_RealArray) aCoordsArray =
45 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
47 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
48 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
49 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
51 const AltitudePoint& aPoint = *aListItBeg;
53 aCoordsArray->SetValue( i * 3, aPoint.X() );
54 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
55 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
59 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
61 AltitudePoints aPoints;
63 Handle(TDataStd_RealArray) aCoordsArray;
64 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
67 int aLowerIdx = aCoordsArray->Lower();
68 int anUpperIdx = aCoordsArray->Upper();
69 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
75 aPoint.SetX( aCoordsArray->Value( i++ ) );
76 aPoint.SetY( aCoordsArray->Value( i++ ) );
77 aPoint.SetZ( aCoordsArray->Value( i++ ) );
85 void HYDROData_Bathymetry::RemoveAltitudePoints()
87 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
88 aLab.ForgetAllAttributes();
91 void interpolateAltitudeForPoints( const gp_XY& thePoint,
92 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
93 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
94 HYDROData_Bathymetry::AltitudePoint& theResPoint,
95 const bool& theIsVertical )
97 double aCoordX = thePoint.X();
98 double aCoordY = thePoint.Y();
102 aCoordX = theFirstPoint.X();
104 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
106 // Recalculate X coordinate by equation of line from two points
107 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
108 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
113 aCoordY = theFirstPoint.Y();
115 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
117 // Recalculate y by equation of line from two points
118 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
119 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
123 theResPoint.SetX( aCoordX );
124 theResPoint.SetY( aCoordY );
126 // Calculate coefficient for interpolation
127 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
128 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
130 double aInterCoeff = 0;
132 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
135 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
136 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
138 // Calculate interpolated value
139 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
141 theResPoint.SetZ( aResVal );
144 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
146 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
147 return GetAltitudeForPoint( aGpPoint );
150 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
152 double aResAltitude = -9999.90;
154 AltitudePoints anAltitudePoints = GetAltitudePoints();
155 if ( anAltitudePoints.isEmpty() )
158 QPolygonF aBoundingRect;
161 // [ 0 (top-left) ] [ 1 (top-right) ]
163 // [ 2 (bot-left) ] [ 3 (bot-right) ]
164 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
165 AltitudePoint( DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
166 AltitudePoint( -DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ),
167 AltitudePoint( DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ) };
169 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
170 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
171 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
173 const AltitudePoint& aPoint = *aListItBeg;
175 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
176 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
178 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
180 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
182 aResAltitude = aPoint.Z();
185 else if ( aDeltaY < 0 ) // top side
188 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
189 aBounds[ 0 ] = aPoint;
190 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
191 aBounds[ 1 ] = aPoint;
196 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
197 aBounds[ 2 ] = aPoint;
198 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
199 aBounds[ 3 ] = aPoint;
202 else if ( aDeltaX < 0 ) // left side
204 if ( ValuesEquals( aDeltaY, 0.0 ) )
207 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
208 aBounds[ 0 ] = aPoint;
209 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
210 aBounds[ 2 ] = aPoint;
212 else if ( aDeltaY < 0 )
215 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
216 aBounds[ 0 ] = aPoint;
220 // bottom left corner
221 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
222 aBounds[ 2 ] = aPoint;
227 if ( ValuesEquals( aDeltaY, 0.0 ) )
230 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
231 aBounds[ 1 ] = aPoint;
232 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
233 aBounds[ 3 ] = aPoint;
235 else if ( aDeltaY < 0 )
238 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
239 aBounds[ 1 ] = aPoint;
243 // bottom right corner
244 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
245 aBounds[ 3 ] = aPoint;
249 // Update bounding rectangle of our global grid
250 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
253 // Check if requested point is inside of our bounding rectangle
254 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
255 return INVALID_ALTITUDE_VALUE;
257 // Calculate result altitude for point
258 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
260 // At first we merge top and bottom borders
261 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
262 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
264 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
265 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
267 AltitudePoint aResPoint( aFirstPoint );
269 // At last we merge left and right borders
270 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
271 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
273 aResAltitude = aResPoint.Z();
278 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
280 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
281 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
284 QString HYDROData_Bathymetry::GetFilePath() const
288 Handle(TDataStd_AsciiString) anAsciiStr;
289 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
290 aRes = QString( anAsciiStr->Get().ToCString() );
295 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
297 // Try to open the file
298 QFile aFile( theFileName );
299 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
304 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
306 AltitudePoints aPoints;
308 // Try to import the file
309 if ( aFileSuf == "xyz" )
310 aRes = importFromXYZFile( aFile, aPoints );
317 // Update file path and altitude points of this Bathymetry
318 SetFilePath( theFileName );
319 SetAltitudePoints( aPoints );
322 return aRes && !aPoints.isEmpty();
325 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
326 AltitudePoints& thePoints )
328 if ( !theFile.isOpen() )
331 // Strings in file is written as:
332 // 1. X(float) Y(float) Z(float)
333 // 2. X(float) Y(float) Z(float)
336 while ( !theFile.atEnd() )
338 QString aLine = theFile.readLine().simplified();
339 if ( aLine.isEmpty() )
342 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
343 if ( aValues.length() < 3 )
346 AltitudePoint aPoint;
348 QString anX = aValues.value( 0 );
349 QString anY = aValues.value( 1 );
350 QString aZ = aValues.value( 2 );
352 bool isXOk = false, isYOk = false, isZOk = false;
354 aPoint.SetX( anX.toDouble( &isXOk ) );
355 aPoint.SetY( anY.toDouble( &isYOk ) );
356 aPoint.SetZ( aZ.toDouble( &isZOk ) );
358 if ( !isXOk || !isYOk || !isZOk )