Salome HOME
First implimentation of Bathymetry. Import of Bathymetries from fiels (Feature #8).
[modules/hydro.git] / src / HYDROData / HYDROData_Bathymetry.cxx
1
2 #include "HYDROData_Bathymetry.h"
3
4 #include <gp_XY.hxx>
5 #include <gp_XYZ.hxx>
6
7 #include <TDataStd_RealArray.hxx>
8
9 #include <QFile>
10 #include <QFileInfo>
11 #include <QPointF>
12 #include <QStringList>
13
14 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_Object)
15 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_Object)
16
17 HYDROData_Bathymetry::HYDROData_Bathymetry()
18 {
19 }
20
21 HYDROData_Bathymetry::~HYDROData_Bathymetry()
22 {
23 }
24
25 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
26 {
27   RemoveAltitudePoints();
28
29   if ( thePoints.isEmpty() )
30     return;
31
32   // Save coordinates
33   Handle(TDataStd_RealArray) aCoordsArray = 
34     TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
35
36   AltitudePoints::const_iterator aListItBeg =  thePoints.constBegin();
37   AltitudePoints::const_iterator aListItEnd =  thePoints.constEnd();
38   for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
39   {
40     const AltitudePoint& aPoint = *aListItBeg;
41
42     aCoordsArray->SetValue( i * 3, aPoint.X() );
43     aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
44     aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
45   }
46 }
47
48 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
49 {
50   AltitudePoints aPoints;
51
52   Handle(TDataStd_RealArray) aCoordsArray;
53   if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
54     return aPoints;
55
56   int aLowerIdx = aCoordsArray->Lower();
57   int anUpperIdx = aCoordsArray->Upper();
58   for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
59   {
60     if ( i + 3 > n )
61       break;
62
63     AltitudePoint aPoint;
64     aPoint.SetX( aCoordsArray->Value( i++ ) );
65     aPoint.SetY( aCoordsArray->Value( i++ ) );
66     aPoint.SetZ( aCoordsArray->Value( i++ ) );
67
68     aPoints << aPoint;
69   }
70
71   return aPoints;
72 }
73
74 void HYDROData_Bathymetry::RemoveAltitudePoints()
75 {
76   TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
77   aLab.ForgetAllAttributes();
78 }
79
80 double HYDROData_Bathymetry::GetAltitudeForPoint( const QPointF& thePoint ) const
81 {
82   gp_XY aGpPoint( thePoint.x(), thePoint.y() );
83   return GetAltitudeForPoint( aGpPoint );
84 }
85
86 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
87 {
88   double aResAltitude = -9999.90;
89   
90   AltitudePoints anAltitudePoints = GetAltitudePoints();
91
92   // TODO : implement
93
94   return aResAltitude;
95 }
96
97 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
98 {
99   // Try to open the file
100   QFile aFile( theFileName );
101   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
102     return false;
103
104   bool aRes = false;
105
106   QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
107
108   AltitudePoints aPoints;
109
110   // Try to import the file
111   if ( aFileSuf == "xyz" )
112     aRes = importFromXYZFile( aFile, aPoints );
113     
114   // Close the file
115   aFile.close();
116
117   if ( aRes )
118   {
119     // Update altitude points of this Bathymetry
120     SetAltitudePoints( aPoints );
121   }
122
123   return aRes && !aPoints.isEmpty();
124 }
125
126 bool HYDROData_Bathymetry::importFromXYZFile( QFile&          theFile,
127                                               AltitudePoints& thePoints )
128 {
129   if ( !theFile.isOpen() )
130     return false;
131
132   // Strings in file is written as:
133   //  1. X(float) Y(float) Z(float)
134   //  2. X(float) Y(float) Z(float)
135   //  ...
136
137   while ( !theFile.atEnd() )
138   {
139     QString aLine = theFile.readLine();
140     if ( aLine.isEmpty() )
141       continue;
142
143     QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
144     if ( aValues.length() < 3 )
145       return false;
146
147     AltitudePoint aPoint;
148     
149     QString anX = aValues.value( 0 );
150     QString anY = aValues.value( 1 );
151     QString aZ  = aValues.value( 2 );
152
153     bool isXOk = false, isYOk = false, isZOk = false;
154
155     aPoint.SetX( anX.toDouble( &isXOk ) );
156     aPoint.SetY( anY.toDouble( &isYOk ) );
157     aPoint.SetZ(  aZ.toDouble( &isZOk ) );
158
159     if ( !isXOk || !isYOk || !isZOk )
160       return false;
161
162     thePoints << aPoint;
163   }
164
165   return true;
166 }
167
168
169
170