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;
38 extern QString TMP_DIR;
40 bool test_HYDROData_Profile::createTestFile( const QString& theFileName,
41 const bool theIsParametric )
43 QFile aTmpFile( theFileName );
44 if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
47 if ( theIsParametric )
49 QTextStream anOutStream( &aTmpFile );
51 anOutStream << "0 182.15 \n";
52 anOutStream << "4 181.95 \n";
53 anOutStream << "10.18 181.63 \n";
54 anOutStream << "14.75 179.27 \n";
55 anOutStream << "19.75 178.87 \n";
59 anOutStream << "-5 50 \n";
60 anOutStream << "0 15 \n";
61 anOutStream << "10.1 10 \n";
62 anOutStream << "20 20 \n";
63 anOutStream << "250 0.005 \n";
67 QTextStream anOutStream( &aTmpFile );
69 anOutStream << "1040499.17 6788618.13 182.15 \n";
70 anOutStream << "1040503.12 6788618.79 181.95 \n";
71 anOutStream << "1040509.21 6788619.81 181.63 \n";
72 anOutStream << "1040513.72 6788620.56 179.27 \n";
73 anOutStream << "1040518.65 6788621.38 178.87 \n";
81 void test_HYDROData_Profile::testFileImport()
83 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
85 QString aParamFileName = TMP_DIR + QDir::separator() + "parametric.pa";
86 QString aGeorefFileName = TMP_DIR + QDir::separator() + "georef.pa";
87 if ( !createTestFile( aParamFileName, true ) || !createTestFile( aGeorefFileName, false ) )
88 return; // No file has been created
90 TCollection_AsciiString aFileName( aParamFileName.toStdString().c_str() );
92 NCollection_Sequence<int> aBadProfilesList;
93 CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, true ) );
95 int aProfileCount = 0;
96 HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
97 for ( ; aDocIter.More(); aDocIter.Next() )
99 Handle(HYDROData_Profile) aProfile =
100 Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
101 if ( aProfile.IsNull() )
104 CPPUNIT_ASSERT( aProfile->IsValid() == false );
105 CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
110 CPPUNIT_ASSERT( aProfileCount == 2 );
112 Handle(HYDROData_Profile) aGeorefProfile =
113 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
115 aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
116 bool notEmpty = false;
117 CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName, true, ¬Empty ) );
118 CPPUNIT_ASSERT( notEmpty );
120 // Check validity of imported profile
121 CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
123 CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
125 aGeorefProfile->Update();
126 CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
128 HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
129 CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
131 HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
132 CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.X(), 1040509.21, EPS );
133 CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Y(), 6788619.81, EPS );
134 CPPUNIT_ASSERT_DOUBLES_EQUAL( aProfilePoint.Z(), 181.63, EPS );
140 void test_HYDROData_Profile::testCopy()
142 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
144 Handle(HYDROData_Profile) aProfile1 =
145 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
147 QString aFileName = TMP_DIR + QDir::separator() + "georef.pa";
149 bool anIsFileCreated = createTestFile( aFileName, false );
151 if ( anIsFileCreated )
153 bool notEmpty = false;
154 TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
155 CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName, true, ¬Empty ) );
156 CPPUNIT_ASSERT( notEmpty );
158 CPPUNIT_ASSERT( aProfile1->IsValid() );
159 CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
162 Handle(HYDROData_Profile) aProfile2 =
163 Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
165 aProfile1->CopyTo( aProfile2, false );
167 if ( anIsFileCreated )
169 CPPUNIT_ASSERT( aProfile2->IsValid() );
170 CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
176 void operator << ( std::ostream& s, const gp_XYZ& p )
178 s << "(" << p.X() << "; " << p.Y() << "; " << p.Z() << ") ";
181 bool operator == ( const gp_XYZ& p1, const gp_XYZ& p2 )
183 return fabs(p1.X()-p2.X())<EPS && fabs(p1.Y()-p2.Y())<EPS && fabs(p1.Z()-p2.Z())<EPS;
186 void test_HYDROData_Profile::testProjection()
188 std::string aPath = ( REF_DATA_PATH+"/profiles1.xyz" ).toStdString();
190 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
192 TCollection_AsciiString aFileName( aPath.c_str() );
193 NCollection_Sequence<int> aBadProfilesList;
194 CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, false ) );
195 CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName, aBadProfilesList, true ) );
197 HYDROData_Iterator it( aDoc, KIND_PROFILE );
198 CPPUNIT_ASSERT( it.More() );
199 Handle(HYDROData_Profile) p1 = Handle(HYDROData_Profile)::DownCast( it.Current() ); it.Next();
200 CPPUNIT_ASSERT( it.More() );
201 Handle(HYDROData_Profile) p2 = Handle(HYDROData_Profile)::DownCast( it.Current() ); it.Next();
202 CPPUNIT_ASSERT( !it.More() );
204 CPPUNIT_ASSERT_EQUAL( QString( "Profile_1" ), p1->GetName() );
205 CPPUNIT_ASSERT_EQUAL( QString( "Profile_2" ), p2->GetName() );
207 HYDROData_Profile::ProfilePoints pp1 = p1->GetProfilePoints();
208 int low1 = pp1.Lower(), up1 = pp1.Upper();
209 CPPUNIT_ASSERT_EQUAL( 1, low1 );
210 CPPUNIT_ASSERT_EQUAL( 6, up1 );
211 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 1.0, 2.0, 5.0 ), pp1.Value( 1 ) );
212 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.04019, 4.0838, 4.0 ), pp1.Value( 2 ) );
213 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.9601, 5.9202, 3.0 ), pp1.Value( 3 ) );
214 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.08026, 8.16052, 3.0 ), pp1.Value( 4 ) );
215 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.9202, 9.84041, 4.0 ), pp1.Value( 5 ) );
216 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 6.0, 12.0, 5.0 ), pp1.Value( 6 ) );
218 HYDROData_Profile::ProfilePoints pp2 = p2->GetProfilePoints();
219 int low2 = pp2.Lower(), up2 = pp2.Upper();
220 CPPUNIT_ASSERT_EQUAL( 1, low2 );
221 CPPUNIT_ASSERT_EQUAL( 6, up2 );
222 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 1.0, 2.0, 5.0 ), pp2.Value( 1 ) );
223 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.04019, 4.0838, 4.0 ), pp2.Value( 2 ) );
224 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 2.9601, 5.9202, 3.0 ), pp2.Value( 3 ) );
225 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.08, 8.16, 3.0 ), pp2.Value( 4 ) );
226 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 4.92, 9.84, 4.0 ), pp2.Value( 5 ) );
227 CPPUNIT_ASSERT_EQUAL( gp_XYZ( 6.0, 12.0, 5.0 ), pp2.Value( 6 ) );