Salome HOME
automatic tests stabilization
[modules/hydro.git] / src / HYDRO_tests / 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 const double EPS = 1E-4;
35
36 void generateOne( QTextStream& theStream,
37                   double theFirstX, double theFirstY,
38                   double theLastX, double theLastY )
39 {
40   const int aNbPoints = 50;
41   double aStepZ = -5;
42   const double dt = 1.0 / aNbPoints;
43   
44   for( double t=0.0; t<=1.0; t += dt )
45   {
46     double anX = theFirstX * (1-t) + theLastX * t;
47     double anY = theFirstY * (1-t) + theLastY * t;
48     double aZ = aStepZ * aStepZ + sin( (float)rand() );
49     theStream << anX << " " << anY << " " << aZ << " \n";
50
51     aStepZ += 0.2;
52   }
53 }
54
55 bool test_HYDROData_Bathymetry::createTestFile( const QString& theFileName )
56 {
57   QFile aTmpFile( theFileName );
58   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
59     return false;
60
61   {
62     QTextStream anOutStream( &aTmpFile );
63
64     srand( 4587 );
65     generateOne( anOutStream, 0, 5, 0, -5 );
66     generateOne( anOutStream, 10, 5, 10, -5 );
67     generateOne( anOutStream, 20, 5, 20, -5 );
68     generateOne( anOutStream, 30, 5, 30, -5 );
69     generateOne( anOutStream, 40, 5, 40, -5 );
70     generateOne( anOutStream, 45.833271, 6.703101, 50.85397, -3.794724 );
71     generateOne( anOutStream, 50.85397, 11.267372, 56.787525, 2.367043 );
72     generateOne( anOutStream, 52.67968, 16.516285, 62.721077, 10.354518 );
73     generateOne( anOutStream, 55.190029, 23.819118, 65.23143, 17.657352 );
74     generateOne( anOutStream, 58.385021, 31.806595, 68.198204, 23.819118 );
75     generateOne( anOutStream, 65.916069, 38.653004, 75.044609, 27.470537 );
76     generateOne( anOutStream, 76.870323, 41.847992, 82.575661, 28.38339 );
77     generateOne( anOutStream, 92.845276, 40.022282, 91.932419, 26.557682 );
78     generateOne( anOutStream, 106.081657, 35.686226, 98.778824, 21.99341 );
79     generateOne( anOutStream, 119.774475, 29.980886, 106.994514, 14.91879 );
80     generateOne( anOutStream, 133.239075, 26.785896, 115.666634, 5.790246 );
81     generateOne( anOutStream, 150.811523, 20.167702, 125.47982, -4.707579 );
82     generateOne( anOutStream, 163.591476, 13.777717, 135.977631, -12.92327 );
83     generateOne( anOutStream, 183.446045, 11.267367, 151.496155, -24.105736 );
84     generateOne( anOutStream, 197.367081, 8.98523100, 172.720016, -31.180355 );
85     generateOne( anOutStream, 214.026672, 9.669872, 197.823502, -38.483189 );
86     generateOne( anOutStream, 235.706985, 9.89809100, 220.873108, -47.155304 );
87     generateOne( anOutStream, 266.744019, 9.213447, 248.715134, -58.565987 );
88     generateOne( anOutStream, 307.366028, 10.354514, 278.611145, -67.922737 );
89     generateOne( anOutStream, 342.510925, 7.159524, 316.951019, -84.81053 );
90     generateOne( anOutStream, 384.659393, -4.55408, 350.913635, -100.02983 );
91     generateOne( anOutStream, 443.097107, -21.015415, 391.243927, -118.960365 );
92     generateOne( anOutStream, 500.711792, -34.184483, 440.62793, -147.7677 );
93     generateOne( anOutStream, 547.626587, -52.291954, 482.604309, -169.990509 );
94     generateOne( anOutStream, 589.603027, -68.753288, 534.45752, -193.8594 );
95     generateOne( anOutStream, 633.225525, -97.560631, 569.026306, -206.205444 );
96     generateOne( anOutStream, 673.555786, -120.606499, 601.948975, -224.312912 );
97     generateOne( anOutStream, 719.647522, -148.590775, 654.625305, -248.181854 );
98     generateOne( anOutStream, 770.677673, -183.982635, 708.947693, -278.635315 );
99     generateOne( anOutStream, 824.177002, -225.959045, 742.69342, -314.027191 );
100     generateOne( anOutStream, 855.937317, -260.368958, 793.05932, -341.853638 );
101     generateOne( anOutStream, 884.168213, -294.37439, 838.6137700, -355.969116 );
102     generateOne( anOutStream, 915.607178, -327.096588, 875.185669, -366.234894 );
103     generateOne( anOutStream, 940.630066, -348.269775, 903.416565, -381.633575 );
104     generateOne( anOutStream, 963.728088, -370.726166, 936.780396, -397.032257 );
105     generateOne( anOutStream, 983.618042, -390.61615, 959.878418, -411.789337 );
106     generateOne( anOutStream, 999.658325, -407.939667, 978.485107, -425.904785 );
107     generateOne( anOutStream, 1018.265076, -422.696716, 996.450256, -443.228302 );
108     generateOne( anOutStream, 1040.079834, -441.303467, 1017.623413, -459.9102175 );
109     generateOne( anOutStream, 1055.478516, -456.060547, 1034.946899, -475.9505 );
110     generateOne( anOutStream, 1074.085327, -474.025665, 1058.044922, -493.274017 );
111   }
112
113   aTmpFile.close();
114
115   return true;
116 }
117
118 void test_HYDROData_Bathymetry::testFileImport()
119 {
120   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
121
122   Handle(HYDROData_Bathymetry) aBathymetry = 
123     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
124
125   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
126   if ( !createTestFile( aFileName ) )
127     return; // No file has been created
128
129   CPPUNIT_ASSERT( aBathymetry->ImportFromFile( aFileName.toStdString().c_str() ) );
130
131   HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry->GetAltitudePoints();
132   CPPUNIT_ASSERT_EQUAL( 2300, anAltitudePoints.Length() );
133
134   gp_XY aTestPoint( 1, 1 );
135   double anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
136   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9755, anAltitude, EPS );
137
138   aTestPoint = gp_XY( 0.5, 0.5 );
139   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
140   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0147, anAltitude, EPS );
141
142   aTestPoint = gp_XY( 1.5, 1 );
143   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
144   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9534, anAltitude, EPS );
145
146   aTestPoint = gp_XY( 1.5, 0.7 );
147   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
148   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5032, anAltitude, EPS );
149
150   aTestPoint = gp_XY( 1.5, -0.7 );
151   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
152   CPPUNIT_ASSERT_DOUBLES_EQUAL( 6.3088, anAltitude, EPS );
153
154   aTestPoint = gp_XY( 2, 3.5 );
155   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
156   CPPUNIT_ASSERT_DOUBLES_EQUAL( 11.090, anAltitude, EPS );
157
158   aDoc->Close();
159 }
160
161
162 void test_HYDROData_Bathymetry::testCopy()
163 {
164   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
165   
166   Handle(HYDROData_Bathymetry) aBathymetry1 = 
167     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
168
169   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
170
171   bool anIsFileCreated = createTestFile( aFileName );
172   
173   if ( anIsFileCreated )
174   {
175     CPPUNIT_ASSERT( aBathymetry1->ImportFromFile( aFileName.toStdString().c_str() ) );
176
177     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry1->GetAltitudePoints();
178     CPPUNIT_ASSERT_EQUAL( 2300, anAltitudePoints.Length() );
179   }
180
181   Handle(HYDROData_Bathymetry) aBathymetry2 = 
182     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
183
184   aBathymetry1->CopyTo( aBathymetry2, true );
185
186   if ( anIsFileCreated )
187   {
188     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry2->GetAltitudePoints();
189     CPPUNIT_ASSERT_EQUAL( 2300, anAltitudePoints.Length() );
190   }
191
192   aDoc->Close();
193 }