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