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>
11 #include <TDataStd_Integer.hxx>
17 #include <QStringList>
21 #include <OSD_Timer.hxx>
24 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
27 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
28 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
30 HYDROData_Bathymetry::HYDROData_Bathymetry()
31 : HYDROData_IAltitudeObject()
35 HYDROData_Bathymetry::~HYDROData_Bathymetry()
39 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
43 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
44 if ( aDocument.IsNull() )
47 QString aDocName = aDocument->GetDocPyName();
48 QString aBathymetryName = GetName();
50 aResList << QString( "%1 = %2.CreateObject( %3 );" )
51 .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
52 aResList << QString( "%1.SetName( \"%2\" );" )
53 .arg( aBathymetryName ).arg( aBathymetryName );
55 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
56 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
58 QString aFilePath = GetFilePath();
59 if ( !aFilePath.isEmpty() )
61 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
62 .arg( aBathymetryName ).arg( aFilePath );
66 // TODO : bathymetry is composed from other bathymetry(ies)
72 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
74 RemoveAltitudePoints();
76 if ( thePoints.isEmpty() )
80 Handle(TDataStd_RealArray) aCoordsArray =
81 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
83 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
84 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
85 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
87 const AltitudePoint& aPoint = *aListItBeg;
89 aCoordsArray->SetValue( i * 3, aPoint.X() );
90 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
91 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
97 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
99 AltitudePoints aPoints;
101 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
102 if ( aLabel.IsNull() )
105 Handle(TDataStd_RealArray) aCoordsArray;
106 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
109 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
114 AltitudePoint aPoint;
115 aPoint.SetX( aCoordsArray->Value( i++ ) );
116 aPoint.SetY( aCoordsArray->Value( i++ ) );
117 aPoint.SetZ( aCoordsArray->Value( i++ ) );
125 void HYDROData_Bathymetry::RemoveAltitudePoints()
127 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
128 if ( !aLabel.IsNull() )
130 aLabel.ForgetAllAttributes();
135 void interpolateAltitudeForPoints( const gp_XY& thePoint,
136 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
137 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
138 HYDROData_Bathymetry::AltitudePoint& theResPoint,
139 const bool& theIsVertical )
141 double aCoordX = thePoint.X();
142 double aCoordY = thePoint.Y();
146 aCoordX = theFirstPoint.X();
148 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
150 // Recalculate X coordinate by equation of line from two points
151 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
152 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
157 aCoordY = theFirstPoint.Y();
159 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
161 // Recalculate y by equation of line from two points
162 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
163 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
167 theResPoint.SetX( aCoordX );
168 theResPoint.SetY( aCoordY );
170 // Calculate coefficient for interpolation
171 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
172 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
174 double aInterCoeff = 0;
176 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
179 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
180 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
182 // Calculate interpolated value
183 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
185 theResPoint.SetZ( aResVal );
188 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
190 double aResAltitude = GetInvalidAltitude();
192 AltitudePoints anAltitudePoints = GetAltitudePoints();
193 if ( anAltitudePoints.isEmpty() )
196 QPolygonF aBoundingRect;
199 // [ 0 (top-left) ] [ 1 (top-right) ]
201 // [ 2 (bot-left) ] [ 3 (bot-right) ]
202 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
203 AltitudePoint( DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
204 AltitudePoint( -DBL_MAX, DBL_MAX, GetInvalidAltitude() ),
205 AltitudePoint( DBL_MAX, DBL_MAX, GetInvalidAltitude() ) };
207 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
208 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
209 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
211 const AltitudePoint& aPoint = *aListItBeg;
213 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
214 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
216 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
218 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
220 aResAltitude = aPoint.Z();
223 else if ( aDeltaY < 0 ) // top side
226 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
227 aBounds[ 0 ] = aPoint;
228 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
229 aBounds[ 1 ] = aPoint;
234 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
235 aBounds[ 2 ] = aPoint;
236 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
237 aBounds[ 3 ] = aPoint;
240 else if ( aDeltaX < 0 ) // left side
242 if ( ValuesEquals( aDeltaY, 0.0 ) )
245 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
246 aBounds[ 0 ] = aPoint;
247 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
248 aBounds[ 2 ] = aPoint;
250 else if ( aDeltaY < 0 )
253 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
254 aBounds[ 0 ] = aPoint;
258 // bottom left corner
259 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
260 aBounds[ 2 ] = aPoint;
265 if ( ValuesEquals( aDeltaY, 0.0 ) )
268 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
269 aBounds[ 1 ] = aPoint;
270 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
271 aBounds[ 3 ] = aPoint;
273 else if ( aDeltaY < 0 )
276 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
277 aBounds[ 1 ] = aPoint;
281 // bottom right corner
282 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
283 aBounds[ 3 ] = aPoint;
287 // Update bounding rectangle of our global grid
288 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
291 // Check if requested point is inside of our bounding rectangle
292 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
295 // Calculate result altitude for point
296 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
298 // At first we merge top and bottom borders
299 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
300 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
302 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
303 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
305 AltitudePoint aResPoint( aFirstPoint );
307 // At last we merge left and right borders
308 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
309 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
311 aResAltitude = aResPoint.Z();
316 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
318 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
319 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
322 QString HYDROData_Bathymetry::GetFilePath() const
326 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
327 if ( !aLabel.IsNull() )
329 Handle(TDataStd_AsciiString) anAsciiStr;
330 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
331 aRes = QString( anAsciiStr->Get().ToCString() );
337 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
338 const bool theIsUpdate )
340 bool anIsAltitudesInverted = IsAltitudesInverted();
341 if ( anIsAltitudesInverted == theIsInverted )
344 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
351 // Update altitude points
352 AltitudePoints anAltitudePoints = GetAltitudePoints();
353 if ( anAltitudePoints.isEmpty() )
356 AltitudePoints::iterator aListItBeg = anAltitudePoints.begin();
357 AltitudePoints::iterator aListItEnd = anAltitudePoints.end();
358 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
360 AltitudePoint& aPoint = *aListItBeg;
361 aPoint.SetZ( aPoint.Z() * -1 );
364 SetAltitudePoints( anAltitudePoints );
367 bool HYDROData_Bathymetry::IsAltitudesInverted() const
371 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
372 if ( !aLabel.IsNull() )
374 Handle(TDataStd_Integer) anIntVal;
375 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
376 aRes = (bool)anIntVal->Get();
382 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
384 // Try to open the file
385 QFile aFile( theFileName );
386 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
391 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
393 AltitudePoints aPoints;
395 // Try to import the file
396 if ( aFileSuf == "xyz" )
397 aRes = importFromXYZFile( aFile, aPoints );
404 // Update file path and altitude points of this Bathymetry
405 SetFilePath( theFileName );
406 SetAltitudePoints( aPoints );
409 return aRes && !aPoints.isEmpty();
412 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
413 AltitudePoints& thePoints ) const
415 if ( !theFile.isOpen() )
418 // Strings in file is written as:
419 // 1. X(float) Y(float) Z(float)
420 // 2. X(float) Y(float) Z(float)
428 bool anIsAltitudesInverted = IsAltitudesInverted();
429 while ( !theFile.atEnd() )
431 QString aLine = theFile.readLine().simplified();
432 if ( aLine.isEmpty() )
435 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
436 if ( aValues.length() < 3 )
439 AltitudePoint aPoint;
441 QString anX = aValues.value( 0 );
442 QString anY = aValues.value( 1 );
443 QString aZ = aValues.value( 2 );
445 bool isXOk = false, isYOk = false, isZOk = false;
447 aPoint.SetX( anX.toDouble( &isXOk ) );
448 aPoint.SetY( anY.toDouble( &isYOk ) );
449 aPoint.SetZ( aZ.toDouble( &isZOk ) );
451 if ( !isXOk || !isYOk || !isZOk )
454 // Invert the z value if requested
455 if ( anIsAltitudesInverted )
456 aPoint.SetZ( -aPoint.Z() );
463 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
464 aTimer.Show( stream );