Salome HOME
Merge remote-tracking branch 'origin/BR_LCM_COMP' into BR_LAND_COVER_MAP
[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
35 const double EPS = 1E-2;
36
37 bool test_HYDROData_Profile::createTestFile( const QString& theFileName,
38                                              const bool     theIsParametric )
39 {
40   QFile aTmpFile( theFileName );
41   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
42     return false;
43
44   if ( theIsParametric )
45   {
46     QTextStream anOutStream( &aTmpFile );
47     
48     anOutStream << "0      182.15 \n";
49     anOutStream << "4      181.95 \n";
50     anOutStream << "10.18  181.63 \n";
51     anOutStream << "14.75  179.27 \n";
52     anOutStream << "19.75  178.87 \n";
53
54     anOutStream << "\n";
55
56     anOutStream << "-5   50    \n";
57     anOutStream << "0    15    \n";
58     anOutStream << "10.1 10    \n";
59     anOutStream << "20   20    \n";
60     anOutStream << "250  0.005 \n";
61   }
62   else
63   {
64     QTextStream anOutStream( &aTmpFile );
65
66     anOutStream << "1040499.17 6788618.13 182.15 \n";
67     anOutStream << "1040503.12 6788618.79 181.95 \n";
68     anOutStream << "1040509.21 6788619.81 181.63 \n";
69     anOutStream << "1040513.72 6788620.56 179.27 \n";
70     anOutStream << "1040518.65 6788621.38 178.87 \n";
71   }
72
73   aTmpFile.close();
74
75   return true;
76 }
77
78 void test_HYDROData_Profile::testFileImport()
79 {
80   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
81
82   QString aParamFileName = QDir::tempPath() + QDir::separator() + "parametric.pa";
83   QString aGeorefFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
84   if ( !createTestFile( aParamFileName, true ) || !createTestFile( aGeorefFileName, false ) )
85     return; // No file has been created
86
87   TCollection_AsciiString aFileName( aParamFileName.toStdString().c_str() );
88
89   NCollection_Sequence<int> aBadProfilesList;
90   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList ) );
91
92   int aProfileCount = 0;
93   HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
94   for ( ; aDocIter.More(); aDocIter.Next() )
95   {
96     Handle(HYDROData_Profile) aProfile = 
97       Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
98     if ( aProfile.IsNull() )
99       continue;
100     
101     CPPUNIT_ASSERT( aProfile->IsValid() == false );
102     CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
103     
104     aProfileCount++;
105   }
106
107   CPPUNIT_ASSERT( aProfileCount == 2 );
108
109   Handle(HYDROData_Profile) aGeorefProfile = 
110     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
111
112   aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
113   CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName ) );
114
115   // Check validity of imported profile
116   CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
117
118   CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
119
120   aGeorefProfile->Update();
121   CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
122
123   HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
124   CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
125
126   HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
127   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.X(), 1040509.21, EPS );
128   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Y(), 6788619.81, EPS );
129   CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Z(), 181.63, EPS );
130
131   aDoc->Close();
132 }
133
134
135 void test_HYDROData_Profile::testCopy()
136 {
137   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
138   
139   Handle(HYDROData_Profile) aProfile1 = 
140     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
141
142   QString aFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
143
144   bool anIsFileCreated = createTestFile( aFileName, false );
145   
146   if ( anIsFileCreated )
147   {
148     TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
149     CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName ) );
150
151     CPPUNIT_ASSERT( aProfile1->IsValid() );
152     CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
153   }
154
155   Handle(HYDROData_Profile) aProfile2 = 
156     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
157
158   aProfile1->CopyTo( aProfile2, false );
159
160   if ( anIsFileCreated )
161   {
162     CPPUNIT_ASSERT( aProfile2->IsValid() );
163     CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
164   }
165
166   aDoc->Close();
167 }