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 PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
24 HYDROData_Bathymetry::HYDROData_Bathymetry()
25 : HYDROData_IAltitudeObject()
29 HYDROData_Bathymetry::~HYDROData_Bathymetry()
33 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
37 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
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 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
65 RemoveAltitudePoints();
67 if ( thePoints.isEmpty() )
71 Handle(TDataStd_RealArray) aCoordsArray =
72 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
74 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
75 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
76 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
78 const AltitudePoint& aPoint = *aListItBeg;
80 aCoordsArray->SetValue( i * 3, aPoint.X() );
81 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
82 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
86 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
88 AltitudePoints aPoints;
90 Handle(TDataStd_RealArray) aCoordsArray;
91 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
94 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
100 aPoint.SetX( aCoordsArray->Value( i++ ) );
101 aPoint.SetY( aCoordsArray->Value( i++ ) );
102 aPoint.SetZ( aCoordsArray->Value( i++ ) );
110 void HYDROData_Bathymetry::RemoveAltitudePoints()
112 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
113 aLab.ForgetAllAttributes();
116 void interpolateAltitudeForPoints( const gp_XY& thePoint,
117 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
118 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
119 HYDROData_Bathymetry::AltitudePoint& theResPoint,
120 const bool& theIsVertical )
122 double aCoordX = thePoint.X();
123 double aCoordY = thePoint.Y();
127 aCoordX = theFirstPoint.X();
129 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
131 // Recalculate X coordinate by equation of line from two points
132 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
133 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
138 aCoordY = theFirstPoint.Y();
140 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
142 // Recalculate y by equation of line from two points
143 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
144 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
148 theResPoint.SetX( aCoordX );
149 theResPoint.SetY( aCoordY );
151 // Calculate coefficient for interpolation
152 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
153 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
155 double aInterCoeff = 0;
157 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
160 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
161 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
163 // Calculate interpolated value
164 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
166 theResPoint.SetZ( aResVal );
169 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
171 double aResAltitude = GetInvalidAltitude();
173 AltitudePoints anAltitudePoints = GetAltitudePoints();
174 if ( anAltitudePoints.isEmpty() )
177 QPolygonF aBoundingRect;
180 // [ 0 (top-left) ] [ 1 (top-right) ]
182 // [ 2 (bot-left) ] [ 3 (bot-right) ]
183 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
184 AltitudePoint( DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
185 AltitudePoint( -DBL_MAX, DBL_MAX, GetInvalidAltitude() ),
186 AltitudePoint( DBL_MAX, DBL_MAX, GetInvalidAltitude() ) };
188 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
189 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
190 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
192 const AltitudePoint& aPoint = *aListItBeg;
194 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
195 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
197 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
199 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
201 aResAltitude = aPoint.Z();
204 else if ( aDeltaY < 0 ) // top side
207 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
208 aBounds[ 0 ] = aPoint;
209 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
210 aBounds[ 1 ] = aPoint;
215 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
216 aBounds[ 2 ] = aPoint;
217 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
218 aBounds[ 3 ] = aPoint;
221 else if ( aDeltaX < 0 ) // left side
223 if ( ValuesEquals( aDeltaY, 0.0 ) )
226 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
227 aBounds[ 0 ] = aPoint;
228 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
229 aBounds[ 2 ] = aPoint;
231 else if ( aDeltaY < 0 )
234 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
235 aBounds[ 0 ] = aPoint;
239 // bottom left corner
240 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
241 aBounds[ 2 ] = aPoint;
246 if ( ValuesEquals( aDeltaY, 0.0 ) )
249 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
250 aBounds[ 1 ] = aPoint;
251 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
252 aBounds[ 3 ] = aPoint;
254 else if ( aDeltaY < 0 )
257 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
258 aBounds[ 1 ] = aPoint;
262 // bottom right corner
263 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
264 aBounds[ 3 ] = aPoint;
268 // Update bounding rectangle of our global grid
269 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
272 // Check if requested point is inside of our bounding rectangle
273 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
276 // Calculate result altitude for point
277 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
279 // At first we merge top and bottom borders
280 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
281 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
283 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
284 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
286 AltitudePoint aResPoint( aFirstPoint );
288 // At last we merge left and right borders
289 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
290 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
292 aResAltitude = aResPoint.Z();
297 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
299 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
300 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
303 QString HYDROData_Bathymetry::GetFilePath() const
307 Handle(TDataStd_AsciiString) anAsciiStr;
308 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
309 aRes = QString( anAsciiStr->Get().ToCString() );
314 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
316 // Try to open the file
317 QFile aFile( theFileName );
318 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
323 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
325 AltitudePoints aPoints;
327 // Try to import the file
328 if ( aFileSuf == "xyz" )
329 aRes = importFromXYZFile( aFile, aPoints );
336 // Update file path and altitude points of this Bathymetry
337 SetFilePath( theFileName );
338 SetAltitudePoints( aPoints );
341 return aRes && !aPoints.isEmpty();
344 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
345 AltitudePoints& thePoints )
347 if ( !theFile.isOpen() )
350 // Strings in file is written as:
351 // 1. X(float) Y(float) Z(float)
352 // 2. X(float) Y(float) Z(float)
355 while ( !theFile.atEnd() )
357 QString aLine = theFile.readLine().simplified();
358 if ( aLine.isEmpty() )
361 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
362 if ( aValues.length() < 3 )
365 AltitudePoint aPoint;
367 QString anX = aValues.value( 0 );
368 QString anY = aValues.value( 1 );
369 QString aZ = aValues.value( 2 );
371 bool isXOk = false, isYOk = false, isZOk = false;
373 // We automaticaly convert the z value to a negative number
374 aPoint.SetX( anX.toDouble( &isXOk ) );
375 aPoint.SetY( anY.toDouble( &isYOk ) );
376 aPoint.SetZ( -aZ.toDouble( &isZOk ) );
378 if ( !isXOk || !isYOk || !isZOk )