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