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 double HYDROData_Bathymetry::GetInvalidAltitude()
32 return INVALID_ALTITUDE_VALUE;
35 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
37 RemoveAltitudePoints();
39 if ( thePoints.isEmpty() )
43 Handle(TDataStd_RealArray) aCoordsArray =
44 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
46 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
47 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
48 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
50 const AltitudePoint& aPoint = *aListItBeg;
52 aCoordsArray->SetValue( i * 3, aPoint.X() );
53 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
54 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
58 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
60 AltitudePoints aPoints;
62 Handle(TDataStd_RealArray) aCoordsArray;
63 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
66 int aLowerIdx = aCoordsArray->Lower();
67 int anUpperIdx = aCoordsArray->Upper();
68 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
74 aPoint.SetX( aCoordsArray->Value( i++ ) );
75 aPoint.SetY( aCoordsArray->Value( i++ ) );
76 aPoint.SetZ( aCoordsArray->Value( i++ ) );
84 void HYDROData_Bathymetry::RemoveAltitudePoints()
86 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
87 aLab.ForgetAllAttributes();
90 void interpolateAltitudeForPoints( const gp_XY& thePoint,
91 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
92 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
93 HYDROData_Bathymetry::AltitudePoint& theResPoint,
94 const bool& theIsVertical )
96 double aCoordX = thePoint.X();
97 double aCoordY = thePoint.Y();
101 aCoordX = theFirstPoint.X();
103 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
105 // Recalculate X coordinate by equation of line from two points
106 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
107 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
112 aCoordY = theFirstPoint.Y();
114 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
116 // Recalculate y by equation of line from two points
117 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
118 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
122 theResPoint.SetX( aCoordX );
123 theResPoint.SetY( aCoordY );
125 // Calculate coefficient for interpolation
126 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
127 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
129 double aInterCoeff = 0;
131 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
134 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
135 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
137 // Calculate interpolated value
138 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
140 theResPoint.SetZ( aResVal );
143 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
145 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
146 return GetAltitudeForPoint( aGpPoint );
149 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
151 double aResAltitude = -9999.90;
153 AltitudePoints anAltitudePoints = GetAltitudePoints();
154 if ( anAltitudePoints.isEmpty() )
157 QPolygonF aBoundingRect;
160 // [ 0 (top-left) ] [ 1 (top-right) ]
162 // [ 2 (bot-left) ] [ 3 (bot-right) ]
163 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, INVALID_ALTITUDE_VALUE ),
164 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 ) };
168 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
169 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
170 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
172 const AltitudePoint& aPoint = *aListItBeg;
174 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
175 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
177 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
179 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
181 aResAltitude = aPoint.Z();
184 else if ( aDeltaY < 0 ) // top side
187 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
188 aBounds[ 0 ] = aPoint;
189 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
190 aBounds[ 1 ] = aPoint;
195 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
196 aBounds[ 2 ] = aPoint;
197 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
198 aBounds[ 3 ] = aPoint;
201 else if ( aDeltaX < 0 ) // left side
203 if ( ValuesEquals( aDeltaY, 0.0 ) )
206 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
207 aBounds[ 0 ] = aPoint;
208 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
209 aBounds[ 2 ] = aPoint;
211 else if ( aDeltaY < 0 )
214 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
215 aBounds[ 0 ] = aPoint;
219 // bottom left corner
220 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
221 aBounds[ 2 ] = aPoint;
226 if ( ValuesEquals( aDeltaY, 0.0 ) )
229 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
230 aBounds[ 1 ] = aPoint;
231 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
232 aBounds[ 3 ] = aPoint;
234 else if ( aDeltaY < 0 )
237 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
238 aBounds[ 1 ] = aPoint;
242 // bottom right corner
243 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
244 aBounds[ 3 ] = aPoint;
248 // Update bounding rectangle of our global grid
249 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
252 // Check if requested point is inside of our bounding rectangle
253 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
254 return INVALID_ALTITUDE_VALUE;
256 // Calculate result altitude for point
257 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
259 // At first we merge top and bottom borders
260 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
261 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
263 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
264 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
266 AltitudePoint aResPoint( aFirstPoint );
268 // At last we merge left and right borders
269 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
270 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
272 aResAltitude = aResPoint.Z();
277 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
279 // Try to open the file
280 QFile aFile( theFileName );
281 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
286 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
288 AltitudePoints aPoints;
290 // Try to import the file
291 if ( aFileSuf == "xyz" )
292 aRes = importFromXYZFile( aFile, aPoints );
299 // Update altitude points of this Bathymetry
300 SetAltitudePoints( aPoints );
303 return aRes && !aPoints.isEmpty();
306 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
307 AltitudePoints& thePoints )
309 if ( !theFile.isOpen() )
312 // Strings in file is written as:
313 // 1. X(float) Y(float) Z(float)
314 // 2. X(float) Y(float) Z(float)
317 while ( !theFile.atEnd() )
319 QString aLine = theFile.readLine().simplified();
320 if ( aLine.isEmpty() )
323 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
324 if ( aValues.length() < 3 )
327 AltitudePoint aPoint;
329 QString anX = aValues.value( 0 );
330 QString anY = aValues.value( 1 );
331 QString aZ = aValues.value( 2 );
333 bool isXOk = false, isYOk = false, isZOk = false;
335 aPoint.SetX( anX.toDouble( &isXOk ) );
336 aPoint.SetY( anY.toDouble( &isYOk ) );
337 aPoint.SetZ( aZ.toDouble( &isZOk ) );
339 if ( !isXOk || !isYOk || !isZOk )