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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <test_HYDROData_Profile.h>
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_Profile.h>
24 #include <HYDROData_Iterator.h>
26 #include <TopoDS_Shape.hxx>
33 #include <QTextStream>
36 const double EPS = 1E-2;
37 extern QString REF_DATA_PATH;
39 bool test_HYDROData_Profile::createTestFile( const QString& theFileName,
40 const bool theIsParametric )
42 QFile aTmpFile( theFileName );
43 if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
46 if ( theIsParametric )
48 QTextStream anOutStream( &aTmpFile );
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";
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";
66 QTextStream anOutStream( &aTmpFile );
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";
80 void test_HYDROData_Profile::testFileImport()
82 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
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
89 TCollection_AsciiString aFileName( aParamFileName.toStdString().c_str() );
91 NCollection_Sequence<int> aBadProfilesList;
92 CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, true ) );
94 int aProfileCount = 0;
95 HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
96 for ( ; aDocIter.More(); aDocIter.Next() )
98 Handle(HYDROData_Profile) aProfile =
99 Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
100 if ( aProfile.IsNull() )
103 CPPUNIT_ASSERT( aProfile->IsValid() == false );
104 CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
109 CPPUNIT_ASSERT( aProfileCount == 2 );
111 Handle(HYDROData_Profile) aGeorefProfile =
112 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
114 aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
115 bool notEmpty = false;
116 CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName, true, ¬Empty ) );
117 CPPUNIT_ASSERT( notEmpty );
119 // Check validity of imported profile
120 CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
122 CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
124 aGeorefProfile->Update();
125 CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
127 HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
128 CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
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 );
139 void test_HYDROData_Profile::testCopy()
141 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
143 Handle(HYDROData_Profile) aProfile1 =
144 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
146 QString aFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
148 bool anIsFileCreated = createTestFile( aFileName, false );
150 if ( anIsFileCreated )
152 bool notEmpty = false;
153 TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
154 CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName, true, ¬Empty ) );
155 CPPUNIT_ASSERT( notEmpty );
157 CPPUNIT_ASSERT( aProfile1->IsValid() );
158 CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
161 Handle(HYDROData_Profile) aProfile2 =
162 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
164 aProfile1->CopyTo( aProfile2, false );
166 if ( anIsFileCreated )
168 CPPUNIT_ASSERT( aProfile2->IsValid() );
169 CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
175 void operator << ( std::ostream& s, const gp_XYZ& p )
177 s << "(" << p.X() << "; " << p.Y() << "; " << p.Z() << ") ";
180 bool operator == ( const gp_XYZ& p1, const gp_XYZ& p2 )
182 return fabs(p1.X()-p2.X())<EPS && fabs(p1.Y()-p2.Y())<EPS && fabs(p1.Z()-p2.Z())<EPS;
185 void test_HYDROData_Profile::testProjection()
187 std::string aPath = ( REF_DATA_PATH+"/profiles1.xyz" ).toStdString();
189 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
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 ) );
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() );
203 CPPUNIT_ASSERT_EQUAL( QString( "Profile_1" ), p1->GetName() );
204 CPPUNIT_ASSERT_EQUAL( QString( "Profile_2" ), p2->GetName() );
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 ) );
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 ) );