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>
20 #include <OSD_Timer.hxx>
23 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
26 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
27 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
29 HYDROData_Bathymetry::HYDROData_Bathymetry()
30 : HYDROData_IAltitudeObject()
34 HYDROData_Bathymetry::~HYDROData_Bathymetry()
38 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
43 if ( aDocument.IsNull() )
46 QString aDocName = aDocument->GetDocPyName();
47 QString aBathymetryName = GetName();
49 aResList << QString( "%1 = %2.CreateObject( %3 );" )
50 .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
51 aResList << QString( "%1.SetName( \"%2\" );" )
52 .arg( aBathymetryName ).arg( aBathymetryName );
54 QString aFilePath = GetFilePath();
55 if ( !aFilePath.isEmpty() )
57 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
58 .arg( aBathymetryName ).arg( aFilePath );
62 // TODO : bathymetry is composed from other bathymetry(ies)
68 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
70 RemoveAltitudePoints();
72 if ( thePoints.isEmpty() )
76 Handle(TDataStd_RealArray) aCoordsArray =
77 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
79 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
80 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
81 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
83 const AltitudePoint& aPoint = *aListItBeg;
85 aCoordsArray->SetValue( i * 3, aPoint.X() );
86 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
87 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
93 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
95 AltitudePoints aPoints;
97 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
98 if ( aLabel.IsNull() )
101 Handle(TDataStd_RealArray) aCoordsArray;
102 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
105 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
110 AltitudePoint aPoint;
111 aPoint.SetX( aCoordsArray->Value( i++ ) );
112 aPoint.SetY( aCoordsArray->Value( i++ ) );
113 aPoint.SetZ( aCoordsArray->Value( i++ ) );
121 void HYDROData_Bathymetry::RemoveAltitudePoints()
123 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
124 if ( !aLabel.IsNull() )
126 aLabel.ForgetAllAttributes();
131 void interpolateAltitudeForPoints( const gp_XY& thePoint,
132 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
133 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
134 HYDROData_Bathymetry::AltitudePoint& theResPoint,
135 const bool& theIsVertical )
137 double aCoordX = thePoint.X();
138 double aCoordY = thePoint.Y();
142 aCoordX = theFirstPoint.X();
144 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
146 // Recalculate X coordinate by equation of line from two points
147 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
148 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
153 aCoordY = theFirstPoint.Y();
155 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
157 // Recalculate y by equation of line from two points
158 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
159 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
163 theResPoint.SetX( aCoordX );
164 theResPoint.SetY( aCoordY );
166 // Calculate coefficient for interpolation
167 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
168 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
170 double aInterCoeff = 0;
172 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
175 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
176 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
178 // Calculate interpolated value
179 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
181 theResPoint.SetZ( aResVal );
184 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
186 double aResAltitude = GetInvalidAltitude();
188 AltitudePoints anAltitudePoints = GetAltitudePoints();
189 if ( anAltitudePoints.isEmpty() )
192 QPolygonF aBoundingRect;
195 // [ 0 (top-left) ] [ 1 (top-right) ]
197 // [ 2 (bot-left) ] [ 3 (bot-right) ]
198 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
199 AltitudePoint( DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
200 AltitudePoint( -DBL_MAX, DBL_MAX, GetInvalidAltitude() ),
201 AltitudePoint( DBL_MAX, DBL_MAX, GetInvalidAltitude() ) };
203 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
204 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
205 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
207 const AltitudePoint& aPoint = *aListItBeg;
209 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
210 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
212 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
214 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
216 aResAltitude = aPoint.Z();
219 else if ( aDeltaY < 0 ) // top side
222 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
223 aBounds[ 0 ] = aPoint;
224 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
225 aBounds[ 1 ] = aPoint;
230 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
231 aBounds[ 2 ] = aPoint;
232 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
233 aBounds[ 3 ] = aPoint;
236 else if ( aDeltaX < 0 ) // left side
238 if ( ValuesEquals( aDeltaY, 0.0 ) )
241 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
242 aBounds[ 0 ] = aPoint;
243 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
244 aBounds[ 2 ] = aPoint;
246 else if ( aDeltaY < 0 )
249 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
250 aBounds[ 0 ] = aPoint;
254 // bottom left corner
255 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
256 aBounds[ 2 ] = aPoint;
261 if ( ValuesEquals( aDeltaY, 0.0 ) )
264 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
265 aBounds[ 1 ] = aPoint;
266 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
267 aBounds[ 3 ] = aPoint;
269 else if ( aDeltaY < 0 )
272 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
273 aBounds[ 1 ] = aPoint;
277 // bottom right corner
278 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
279 aBounds[ 3 ] = aPoint;
283 // Update bounding rectangle of our global grid
284 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
287 // Check if requested point is inside of our bounding rectangle
288 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
291 // Calculate result altitude for point
292 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
294 // At first we merge top and bottom borders
295 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
296 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
298 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
299 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
301 AltitudePoint aResPoint( aFirstPoint );
303 // At last we merge left and right borders
304 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
305 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
307 aResAltitude = aResPoint.Z();
312 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
314 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
315 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
318 QString HYDROData_Bathymetry::GetFilePath() const
322 Handle(TDataStd_AsciiString) anAsciiStr;
323 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
324 aRes = QString( anAsciiStr->Get().ToCString() );
329 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
331 // Try to open the file
332 QFile aFile( theFileName );
333 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
338 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
340 AltitudePoints aPoints;
342 // Try to import the file
343 if ( aFileSuf == "xyz" )
344 aRes = importFromXYZFile( aFile, aPoints );
351 // Update file path and altitude points of this Bathymetry
352 SetFilePath( theFileName );
353 SetAltitudePoints( aPoints );
356 return aRes && !aPoints.isEmpty();
359 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
360 AltitudePoints& thePoints )
362 if ( !theFile.isOpen() )
365 // Strings in file is written as:
366 // 1. X(float) Y(float) Z(float)
367 // 2. X(float) Y(float) Z(float)
375 while ( !theFile.atEnd() )
377 QString aLine = theFile.readLine().simplified();
378 if ( aLine.isEmpty() )
381 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
382 if ( aValues.length() < 3 )
385 AltitudePoint aPoint;
387 QString anX = aValues.value( 0 );
388 QString anY = aValues.value( 1 );
389 QString aZ = aValues.value( 2 );
391 bool isXOk = false, isYOk = false, isZOk = false;
393 // We automaticaly convert the z value to a negative number
394 aPoint.SetX( anX.toDouble( &isXOk ) );
395 aPoint.SetY( anY.toDouble( &isYOk ) );
396 aPoint.SetZ( -aZ.toDouble( &isZOk ) );
398 if ( !isXOk || !isYOk || !isZOk )
406 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
407 aTimer.Show( stream );