Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into BR_1427
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_Profile.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_Profile.h>
20
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_Profile.h>
24 #include <HYDROData_Iterator.h>
25
26 #include <TopoDS_Shape.hxx>
27
28 #include <gp_XY.hxx>
29 #include <gp_XYZ.hxx>
30
31 #include <QDir>
32 #include <QFile>
33 #include <QTextStream>
34 #include <QString>
35
36 const double EPS = 1E-2;
37 extern QString REF_DATA_PATH;
38 extern QString TMP_DIR;
39
40 bool test_HYDROData_Profile::createTestFile( const QString& theFileName,
41                                              const bool     theIsParametric )
42 {
43   QFile aTmpFile( theFileName );
44   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
45     return false;
46
47   if ( theIsParametric )
48   {
49     QTextStream anOutStream( &aTmpFile );
50     
51     anOutStream << "0      182.15 \n";
52     anOutStream << "4      181.95 \n";
53     anOutStream << "10.18  181.63 \n";
54     anOutStream << "14.75  179.27 \n";
55     anOutStream << "19.75  178.87 \n";
56
57     anOutStream << "\n";
58
59     anOutStream << "-5   50    \n";
60     anOutStream << "0    15    \n";
61     anOutStream << "10.1 10    \n";
62     anOutStream << "20   20    \n";
63     anOutStream << "250  0.005 \n";
64   }
65   else
66   {
67     QTextStream anOutStream( &aTmpFile );
68
69     anOutStream << "1040499.17 6788618.13 182.15 \n";
70     anOutStream << "1040503.12 6788618.79 181.95 \n";
71     anOutStream << "1040509.21 6788619.81 181.63 \n";
72     anOutStream << "1040513.72 6788620.56 179.27 \n";
73     anOutStream << "1040518.65 6788621.38 178.87 \n";
74   }
75
76   aTmpFile.close();
77
78   return true;
79 }
80
81 void test_HYDROData_Profile::testFileImport()
82 {
83   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
84
85   QString aParamFileName = TMP_DIR + QDir::separator() + "parametric.pa";
86   QString aGeorefFileName = TMP_DIR + QDir::separator() + "georef.pa";
87   if ( !createTestFile( aParamFileName, true ) || !createTestFile( aGeorefFileName, false ) )
88     return; // No file has been created
89
90   TCollection_AsciiString aFileName( aParamFileName.toStdString().c_str() );
91
92   NCollection_Sequence<int> aBadProfilesList;
93   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, true ) );
94
95   int aProfileCount = 0;
96   HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
97   for ( ; aDocIter.More(); aDocIter.Next() )
98   {
99     Handle(HYDROData_Profile) aProfile = 
100       Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
101     if ( aProfile.IsNull() )
102       continue;
103     
104     CPPUNIT_ASSERT( aProfile->IsValid() == false );
105     CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
106     
107     aProfileCount++;
108   }
109
110   CPPUNIT_ASSERT( aProfileCount == 2 );
111
112   Handle(HYDROData_Profile) aGeorefProfile = 
113     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
114
115   aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
116   bool notEmpty = false;
117   CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName, true, &notEmpty ) );
118   CPPUNIT_ASSERT( notEmpty );
119
120   // Check validity of imported profile
121   CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
122
123   CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
124
125   aGeorefProfile->Update();
126   CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
127
128   HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
129   CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
130
131   HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
132   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.X(), 1040509.21, EPS );
133   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Y(), 6788619.81, EPS );
134   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Z(), 181.63, EPS );
135
136   aDoc->Close();
137 }
138
139
140 void test_HYDROData_Profile::testCopy()
141 {
142   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
143   
144   Handle(HYDROData_Profile) aProfile1 = 
145     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
146
147   QString aFileName = TMP_DIR + QDir::separator() + "georef.pa";
148
149   bool anIsFileCreated = createTestFile( aFileName, false );
150   
151   if ( anIsFileCreated )
152   {
153     bool notEmpty = false;
154     TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
155     CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName, true, &notEmpty ) );
156     CPPUNIT_ASSERT( notEmpty );
157
158     CPPUNIT_ASSERT( aProfile1->IsValid() );
159     CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
160   }
161
162   Handle(HYDROData_Profile) aProfile2 = 
163     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
164
165   aProfile1->CopyTo( aProfile2, false );
166
167   if ( anIsFileCreated )
168   {
169     CPPUNIT_ASSERT( aProfile2->IsValid() );
170     CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
171   }
172
173   aDoc->Close();
174 }
175
176 void operator << ( std::ostream& s, const gp_XYZ& p )
177 {
178   s << "(" << p.X() << "; " << p.Y() << "; " << p.Z() << ") ";
179 }
180
181 bool operator == ( const gp_XYZ& p1, const gp_XYZ& p2 )
182 {
183   return fabs(p1.X()-p2.X())<EPS && fabs(p1.Y()-p2.Y())<EPS && fabs(p1.Z()-p2.Z())<EPS;
184 }
185
186 void test_HYDROData_Profile::testProjection()
187 {
188   std::string aPath = ( REF_DATA_PATH+"/profiles1.xyz" ).toStdString();
189
190   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
191
192   TCollection_AsciiString aFileName( aPath.c_str() );
193   NCollection_Sequence<int> aBadProfilesList;
194   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, false ) );
195   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, true  ) );
196
197   HYDROData_Iterator it( aDoc, KIND_PROFILE );
198   CPPUNIT_ASSERT( it.More() );
199   Handle(HYDROData_Profile) p1 = Handle(HYDROData_Profile)::DownCast( it.Current() ); it.Next();
200   CPPUNIT_ASSERT( it.More() );
201   Handle(HYDROData_Profile) p2 = Handle(HYDROData_Profile)::DownCast( it.Current() ); it.Next();
202   CPPUNIT_ASSERT( !it.More() );
203
204   CPPUNIT_ASSERT_EQUAL( QString( "Profile_1" ), p1->GetName() );
205   CPPUNIT_ASSERT_EQUAL( QString( "Profile_2" ), p2->GetName() );
206
207   HYDROData_Profile::ProfilePoints pp1 = p1->GetProfilePoints();
208   int low1 = pp1.Lower(), up1 = pp1.Upper();
209   CPPUNIT_ASSERT_EQUAL( 1, low1 );
210   CPPUNIT_ASSERT_EQUAL( 6, up1 );
211   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 1.0,      2.0,     5.0 ), pp1.Value( 1 ) );
212   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.04019,  4.0838,  4.0 ), pp1.Value( 2 ) );
213   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.9601,   5.9202,  3.0 ), pp1.Value( 3 ) );
214   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.08026,  8.16052, 3.0 ), pp1.Value( 4 ) );
215   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.9202,   9.84041, 4.0 ), pp1.Value( 5 ) );
216   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 6.0,     12.0,     5.0 ), pp1.Value( 6 ) );
217
218   HYDROData_Profile::ProfilePoints pp2 = p2->GetProfilePoints();
219   int low2 = pp2.Lower(), up2 = pp2.Upper();
220   CPPUNIT_ASSERT_EQUAL( 1, low2 );
221   CPPUNIT_ASSERT_EQUAL( 6, up2 );
222   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 1.0,      2.0,     5.0 ), pp2.Value( 1 ) );
223   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.04019,  4.0838,  4.0 ), pp2.Value( 2 ) );
224   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.9601,   5.9202,  3.0 ), pp2.Value( 3 ) );
225   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.08,     8.16,    3.0 ), pp2.Value( 4 ) );
226   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.92,     9.84,    4.0 ), pp2.Value( 5 ) );
227   CPPUNIT_ASSERT_EQUAL( gp_XYZ( 6.0,     12.0,     5.0 ), pp2.Value( 6 ) );
228
229   aDoc->Close();
230 }