Salome HOME
refs #561: the draft data model for Strickler table
[modules/hydro.git] / src / HYDROData / test_HYDROData_Bathymetry.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_HYDROData_Bathymetry.h>
20
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_Bathymetry.h>
24
25 #include <gp_XY.hxx>
26 #include <gp_XYZ.hxx>
27
28 #include <QDir>
29 #include <QFile>
30 #include <QTextStream>
31
32 #include <gp_Pnt2d.hxx>
33
34 void generateOne( QTextStream& theStream,
35                   double theFirstX, double theFirstY,
36                   double theLastX, double theLastY )
37 {
38   const int aNbPoints = 50;
39
40   double aComDist = gp_Pnt2d( theFirstX, theFirstY ).Distance( gp_Pnt2d( theLastX, theLastY ) );
41   double aStep = aComDist / (double)aNbPoints;
42
43   double aStepZ = -5;
44   double aCurDist = 0.0;
45   for ( int i = 0; i <= aNbPoints; ++i )
46   {
47     double aRatio = ( aCurDist / ( aComDist - aCurDist ) );
48
49     double anX = ( theFirstX + aRatio * theLastX ) / ( 1 + aRatio );
50     double anY = ( theFirstY + aRatio * theLastY ) / ( 1 + aRatio );
51     double aZ = aStepZ * aStepZ + sin( (float)rand() );
52     theStream << anX << " " << anY << " " << aZ << " \n";
53
54     aCurDist += aStep;
55     aStepZ += 0.2;
56   }
57 }
58
59 bool test_HYDROData_Bathymetry::createTestFile( const QString& theFileName )
60 {
61   QFile aTmpFile( theFileName );
62   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
63     return false;
64
65   {
66     QTextStream anOutStream( &aTmpFile );
67
68     srand( 4587 );
69     generateOne( anOutStream, 0, 5, 0, -5 );
70     generateOne( anOutStream, 10, 5, 10, -5 );
71     generateOne( anOutStream, 20, 5, 20, -5 );
72     generateOne( anOutStream, 30, 5, 30, -5 );
73     generateOne( anOutStream, 40, 5, 40, -5 );
74     generateOne( anOutStream, 45.833271, 6.703101, 50.85397, -3.794724 );
75     generateOne( anOutStream, 50.85397, 11.267372, 56.787525, 2.367043 );
76     generateOne( anOutStream, 52.67968, 16.516285, 62.721077, 10.354518 );
77     generateOne( anOutStream, 55.190029, 23.819118, 65.23143, 17.657352 );
78     generateOne( anOutStream, 58.385021, 31.806595, 68.198204, 23.819118 );
79     generateOne( anOutStream, 65.916069, 38.653004, 75.044609, 27.470537 );
80     generateOne( anOutStream, 76.870323, 41.847992, 82.575661, 28.38339 );
81     generateOne( anOutStream, 92.845276, 40.022282, 91.932419, 26.557682 );
82     generateOne( anOutStream, 106.081657, 35.686226, 98.778824, 21.99341 );
83     generateOne( anOutStream, 119.774475, 29.980886, 106.994514, 14.91879 );
84     generateOne( anOutStream, 133.239075, 26.785896, 115.666634, 5.790246 );
85     generateOne( anOutStream, 150.811523, 20.167702, 125.47982, -4.707579 );
86     generateOne( anOutStream, 163.591476, 13.777717, 135.977631, -12.92327 );
87     generateOne( anOutStream, 183.446045, 11.267367, 151.496155, -24.105736 );
88     generateOne( anOutStream, 197.367081, 8.98523100, 172.720016, -31.180355 );
89     generateOne( anOutStream, 214.026672, 9.669872, 197.823502, -38.483189 );
90     generateOne( anOutStream, 235.706985, 9.89809100, 220.873108, -47.155304 );
91     generateOne( anOutStream, 266.744019, 9.213447, 248.715134, -58.565987 );
92     generateOne( anOutStream, 307.366028, 10.354514, 278.611145, -67.922737 );
93     generateOne( anOutStream, 342.510925, 7.159524, 316.951019, -84.81053 );
94     generateOne( anOutStream, 384.659393, -4.55408, 350.913635, -100.02983 );
95     generateOne( anOutStream, 443.097107, -21.015415, 391.243927, -118.960365 );
96     generateOne( anOutStream, 500.711792, -34.184483, 440.62793, -147.7677 );
97     generateOne( anOutStream, 547.626587, -52.291954, 482.604309, -169.990509 );
98     generateOne( anOutStream, 589.603027, -68.753288, 534.45752, -193.8594 );
99     generateOne( anOutStream, 633.225525, -97.560631, 569.026306, -206.205444 );
100     generateOne( anOutStream, 673.555786, -120.606499, 601.948975, -224.312912 );
101     generateOne( anOutStream, 719.647522, -148.590775, 654.625305, -248.181854 );
102     generateOne( anOutStream, 770.677673, -183.982635, 708.947693, -278.635315 );
103     generateOne( anOutStream, 824.177002, -225.959045, 742.69342, -314.027191 );
104     generateOne( anOutStream, 855.937317, -260.368958, 793.05932, -341.853638 );
105     generateOne( anOutStream, 884.168213, -294.37439, 838.6137700, -355.969116 );
106     generateOne( anOutStream, 915.607178, -327.096588, 875.185669, -366.234894 );
107     generateOne( anOutStream, 940.630066, -348.269775, 903.416565, -381.633575 );
108     generateOne( anOutStream, 963.728088, -370.726166, 936.780396, -397.032257 );
109     generateOne( anOutStream, 983.618042, -390.61615, 959.878418, -411.789337 );
110     generateOne( anOutStream, 999.658325, -407.939667, 978.485107, -425.904785 );
111     generateOne( anOutStream, 1018.265076, -422.696716, 996.450256, -443.228302 );
112     generateOne( anOutStream, 1040.079834, -441.303467, 1017.623413, -459.9102175 );
113     generateOne( anOutStream, 1055.478516, -456.060547, 1034.946899, -475.9505 );
114     generateOne( anOutStream, 1074.085327, -474.025665, 1058.044922, -493.274017 );
115   }
116
117   aTmpFile.close();
118
119   return true;
120 }
121
122 void test_HYDROData_Bathymetry::testFileImport()
123 {
124   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
125
126   Handle(HYDROData_Bathymetry) aBathymetry = 
127     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
128
129   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
130   if ( !createTestFile( aFileName ) )
131     return; // No file has been created
132
133   CPPUNIT_ASSERT( aBathymetry->ImportFromFile( aFileName.toStdString().c_str() ) );
134
135   HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry->GetAltitudePoints();
136   CPPUNIT_ASSERT( anAltitudePoints.Length() == 20 );
137
138   gp_XY aTestPoint( 1, 1 );
139   double anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
140   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 10.0 ) );
141
142   aTestPoint = gp_XY( 0.5, 0.5 );
143   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
144   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 5.0 ) );
145
146   aTestPoint = gp_XY( 1.5, 1 );
147   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
148   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 10.0 ) );
149
150   aTestPoint = gp_XY( 1.5, 0.7 );
151   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
152   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 7.0 ) );
153
154   aTestPoint = gp_XY( 1.5, -0.7 );
155   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
156   CPPUNIT_ASSERT( ValuesEquals( anAltitude, HYDROData_Bathymetry::GetInvalidAltitude() ) );
157
158   aTestPoint = gp_XY( 2, 3.5 );
159   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
160   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 35.0 ) );
161
162   aDoc->Close();
163 }
164
165
166 void test_HYDROData_Bathymetry::testCopy()
167 {
168   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
169   
170   Handle(HYDROData_Bathymetry) aBathymetry1 = 
171     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
172
173   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
174
175   bool anIsFileCreated = createTestFile( aFileName );
176   
177   if ( anIsFileCreated )
178   {
179     CPPUNIT_ASSERT( aBathymetry1->ImportFromFile( aFileName.toStdString().c_str() ) );
180
181     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry1->GetAltitudePoints();
182     CPPUNIT_ASSERT( anAltitudePoints.Length() == 20 );
183   }
184
185   Handle(HYDROData_Bathymetry) aBathymetry2 = 
186     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
187
188   aBathymetry1->CopyTo( aBathymetry2 );
189
190   if ( anIsFileCreated )
191   {
192     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry2->GetAltitudePoints();
193     CPPUNIT_ASSERT( anAltitudePoints.Length() == 20 );
194   }
195
196   aDoc->Close();
197 }