Salome HOME
49872918376602d09366ab05fa63f86edf4c9681
[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 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   NCollection_Sequence<int> aBadProfilesList;
88   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList ) );
89
90   int aProfileCount = 0;
91   HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
92   for ( ; aDocIter.More(); aDocIter.Next() )
93   {
94     Handle(HYDROData_Profile) aProfile = 
95       Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
96     if ( aProfile.IsNull() )
97       continue;
98     
99     CPPUNIT_ASSERT( aProfile->IsValid() == false );
100     CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
101     
102     aProfileCount++;
103   }
104
105   CPPUNIT_ASSERT( aProfileCount == 2 );
106
107   Handle(HYDROData_Profile) aGeorefProfile = 
108     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
109
110   aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
111   CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName ) );
112
113   // Check validity of imported profile
114   CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
115
116   CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
117
118   aGeorefProfile->Update();
119   CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
120
121   HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
122   CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
123
124   HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
125   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.X(), 1040509.21 ) );
126   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Y(), 6788619.81 ) );
127   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Z(), 181.63 ) );
128
129   aDoc->Close();
130 }
131
132
133 void test_HYDROData_Profile::testCopy()
134 {
135   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
136   
137   Handle(HYDROData_Profile) aProfile1 = 
138     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
139
140   QString aFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
141
142   bool anIsFileCreated = createTestFile( aFileName, false );
143   
144   if ( anIsFileCreated )
145   {
146     TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
147     CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName ) );
148
149     CPPUNIT_ASSERT( aProfile1->IsValid() );
150     CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
151   }
152
153   Handle(HYDROData_Profile) aProfile2 = 
154     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
155
156   aProfile1->CopyTo( aProfile2, false );
157
158   if ( anIsFileCreated )
159   {
160     CPPUNIT_ASSERT( aProfile2->IsValid() );
161     CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
162   }
163
164   aDoc->Close();
165 }