2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Tool.h"
8 #include <TDataStd_RealArray.hxx>
14 #include <QStringList>
16 #define INVALID_ALTITUDE_VALUE -9999.0
19 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_Object)
20 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_Object)
22 HYDROData_Bathymetry::HYDROData_Bathymetry()
26 HYDROData_Bathymetry::~HYDROData_Bathymetry()
30 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
32 RemoveAltitudePoints();
34 if ( thePoints.isEmpty() )
38 Handle(TDataStd_RealArray) aCoordsArray =
39 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
41 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
42 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
43 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
45 const AltitudePoint& aPoint = *aListItBeg;
47 aCoordsArray->SetValue( i * 3, aPoint.X() );
48 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
49 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
53 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
55 AltitudePoints aPoints;
57 Handle(TDataStd_RealArray) aCoordsArray;
58 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
61 int aLowerIdx = aCoordsArray->Lower();
62 int anUpperIdx = aCoordsArray->Upper();
63 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
69 aPoint.SetX( aCoordsArray->Value( i++ ) );
70 aPoint.SetY( aCoordsArray->Value( i++ ) );
71 aPoint.SetZ( aCoordsArray->Value( i++ ) );
79 void HYDROData_Bathymetry::RemoveAltitudePoints()
81 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
82 aLab.ForgetAllAttributes();
85 void interpolateAltitudeForPoints( const gp_XY& thePoint,
86 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
87 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
88 HYDROData_Bathymetry::AltitudePoint& theResPoint,
89 const bool& theIsVertical )
91 double aCoordX = thePoint.X();
92 double aCoordY = thePoint.Y();
96 aCoordX = theFirstPoint.X();
98 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
100 // Recalculate X coordinate by equation of line from two points
101 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
102 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
107 aCoordY = theFirstPoint.Y();
109 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
111 // Recalculate y by equation of line from two points
112 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
113 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
117 theResPoint.SetX( aCoordX );
118 theResPoint.SetY( aCoordY );
120 // Calculate coefficient for interpolation
121 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
122 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
124 double aInterCoeff = 0;
126 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
129 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
130 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
132 // Calculate interpolated value
133 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
135 theResPoint.SetZ( aResVal );
138 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
140 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
141 return GetAltitudeForPoint( aGpPoint );
144 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
146 double aResAltitude = -9999.90;
148 AltitudePoints anAltitudePoints = GetAltitudePoints();
149 if ( anAltitudePoints.isEmpty() )
152 QPolygonF aBoundingRect;
155 // [ 0 (top-left) ] [ 1 (top-right) ]
157 // [ 2 (bot-left) ] [ 3 (bot-right) ]
158 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
159 AltitudePoint( DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
160 AltitudePoint( -DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ),
161 AltitudePoint( DBL_MAX, DBL_MAX, INVALID_ALTITUDE_VALUE ) };
163 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
164 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
165 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
167 const AltitudePoint& aPoint = *aListItBeg;
169 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
170 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
172 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
174 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
176 aResAltitude = aPoint.Z();
179 else if ( aDeltaY < 0 ) // top side
182 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
183 aBounds[ 0 ] = aPoint;
184 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
185 aBounds[ 1 ] = aPoint;
190 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
191 aBounds[ 2 ] = aPoint;
192 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
193 aBounds[ 3 ] = aPoint;
196 else if ( aDeltaX < 0 ) // left side
198 if ( ValuesEquals( aDeltaY, 0.0 ) )
201 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
202 aBounds[ 0 ] = aPoint;
203 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
204 aBounds[ 2 ] = aPoint;
206 else if ( aDeltaY < 0 )
209 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
210 aBounds[ 0 ] = aPoint;
214 // bottom left corner
215 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
216 aBounds[ 2 ] = aPoint;
221 if ( ValuesEquals( aDeltaY, 0.0 ) )
224 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
225 aBounds[ 1 ] = aPoint;
226 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
227 aBounds[ 3 ] = aPoint;
229 else if ( aDeltaY < 0 )
232 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
233 aBounds[ 1 ] = aPoint;
237 // bottom right corner
238 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
239 aBounds[ 3 ] = aPoint;
243 // Update bounding rectangle of our global grid
244 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
247 // Check if requested point is inside of our bounding rectangle
248 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
249 return INVALID_ALTITUDE_VALUE;
251 // Calculate result altitude for point
252 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
254 // At first we merge top and bottom borders
255 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
256 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
258 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
259 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
261 AltitudePoint aResPoint( aFirstPoint );
263 // At last we merge left and right borders
264 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
265 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
267 aResAltitude = aResPoint.Z();
272 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
274 // Try to open the file
275 QFile aFile( theFileName );
276 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
281 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
283 AltitudePoints aPoints;
285 // Try to import the file
286 if ( aFileSuf == "xyz" )
287 aRes = importFromXYZFile( aFile, aPoints );
294 // Update altitude points of this Bathymetry
295 SetAltitudePoints( aPoints );
298 return aRes && !aPoints.isEmpty();
301 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
302 AltitudePoints& thePoints )
304 if ( !theFile.isOpen() )
307 // Strings in file is written as:
308 // 1. X(float) Y(float) Z(float)
309 // 2. X(float) Y(float) Z(float)
312 while ( !theFile.atEnd() )
314 QString aLine = theFile.readLine().simplified();
315 if ( aLine.isEmpty() )
318 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
319 if ( aValues.length() < 3 )
322 AltitudePoint aPoint;
324 QString anX = aValues.value( 0 );
325 QString anY = aValues.value( 1 );
326 QString aZ = aValues.value( 2 );
328 bool isXOk = false, isYOk = false, isZOk = false;
330 aPoint.SetX( anX.toDouble( &isXOk ) );
331 aPoint.SetY( anY.toDouble( &isYOk ) );
332 aPoint.SetZ( aZ.toDouble( &isZOk ) );
334 if ( !isXOk || !isYOk || !isZOk )