Salome HOME
copyrights in HYDRO files are updated
[modules/hydro.git] / src / HYDROData / test_HYDROData_Profile.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <test_HYDROData_Profile.h>
24
25 #include <HYDROData_Document.h>
26 #include <HYDROData_Tool.h>
27 #include <HYDROData_Profile.h>
28 #include <HYDROData_Iterator.h>
29
30 #include <TopoDS_Shape.hxx>
31
32 #include <gp_XY.hxx>
33 #include <gp_XYZ.hxx>
34
35 #include <QDir>
36 #include <QFile>
37 #include <QTextStream>
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   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName ) );
92
93   int aProfileCount = 0;
94   HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
95   for ( ; aDocIter.More(); aDocIter.Next() )
96   {
97     Handle(HYDROData_Profile) aProfile = 
98       Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
99     if ( aProfile.IsNull() )
100       continue;
101     
102     CPPUNIT_ASSERT( aProfile->IsValid() == false );
103     CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
104     
105     aProfileCount++;
106   }
107
108   CPPUNIT_ASSERT( aProfileCount == 2 );
109
110   Handle(HYDROData_Profile) aGeorefProfile = 
111     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
112
113   aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
114   CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName ) );
115
116   // Check validity of imported profile
117   CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
118
119   CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
120
121   aGeorefProfile->UpdateShape3D();
122   CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
123
124   HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
125   CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
126
127   HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
128   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.X(), 1040509.21 ) );
129   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Y(), 6788619.81 ) );
130   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Z(), 181.63 ) );
131
132   aDoc->Close();
133 }
134
135
136 void test_HYDROData_Profile::testCopy()
137 {
138   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
139   
140   Handle(HYDROData_Profile) aProfile1 = 
141     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
142
143   QString aFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
144
145   bool anIsFileCreated = createTestFile( aFileName, false );
146   
147   if ( anIsFileCreated )
148   {
149     TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
150     CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName ) );
151
152     CPPUNIT_ASSERT( aProfile1->IsValid() );
153     CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
154   }
155
156   Handle(HYDROData_Profile) aProfile2 = 
157     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
158
159   aProfile1->CopyTo( aProfile2 );
160
161   if ( anIsFileCreated )
162   {
163     CPPUNIT_ASSERT( aProfile2->IsValid() );
164     CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
165   }
166
167   aDoc->Close();
168 }