2 #include "HYDROData_Bathymetry.h"
7 #include <TDataStd_RealArray.hxx>
12 #include <QStringList>
14 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_Object)
15 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_Object)
17 HYDROData_Bathymetry::HYDROData_Bathymetry()
21 HYDROData_Bathymetry::~HYDROData_Bathymetry()
25 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
27 RemoveAltitudePoints();
29 if ( thePoints.isEmpty() )
33 Handle(TDataStd_RealArray) aCoordsArray =
34 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
36 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
37 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
38 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
40 const AltitudePoint& aPoint = *aListItBeg;
42 aCoordsArray->SetValue( i * 3, aPoint.X() );
43 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
44 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
48 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
50 AltitudePoints aPoints;
52 Handle(TDataStd_RealArray) aCoordsArray;
53 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
56 int aLowerIdx = aCoordsArray->Lower();
57 int anUpperIdx = aCoordsArray->Upper();
58 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
64 aPoint.SetX( aCoordsArray->Value( i++ ) );
65 aPoint.SetY( aCoordsArray->Value( i++ ) );
66 aPoint.SetZ( aCoordsArray->Value( i++ ) );
74 void HYDROData_Bathymetry::RemoveAltitudePoints()
76 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
77 aLab.ForgetAllAttributes();
80 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
82 gp_XY aGpPoint( thePoint.x(), thePoint.y() );
83 return GetAltitudeForPoint( aGpPoint );
86 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
88 double aResAltitude = -9999.90;
90 AltitudePoints anAltitudePoints = GetAltitudePoints();
97 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
99 // Try to open the file
100 QFile aFile( theFileName );
101 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
106 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
108 AltitudePoints aPoints;
110 // Try to import the file
111 if ( aFileSuf == "xyz" )
112 aRes = importFromXYZFile( aFile, aPoints );
119 // Update altitude points of this Bathymetry
120 SetAltitudePoints( aPoints );
123 return aRes && !aPoints.isEmpty();
126 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
127 AltitudePoints& thePoints )
129 if ( !theFile.isOpen() )
132 // Strings in file is written as:
133 // 1. X(float) Y(float) Z(float)
134 // 2. X(float) Y(float) Z(float)
137 while ( !theFile.atEnd() )
139 QString aLine = theFile.readLine();
140 if ( aLine.isEmpty() )
143 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
144 if ( aValues.length() < 3 )
147 AltitudePoint aPoint;
149 QString anX = aValues.value( 0 );
150 QString anY = aValues.value( 1 );
151 QString aZ = aValues.value( 2 );
153 bool isXOk = false, isYOk = false, isZOk = false;
155 aPoint.SetX( anX.toDouble( &isXOk ) );
156 aPoint.SetY( anY.toDouble( &isYOk ) );
157 aPoint.SetZ( aZ.toDouble( &isZOk ) );
159 if ( !isXOk || !isYOk || !isZOk )