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