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