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