Salome HOME
for DTM
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_Stream.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_Stream.h>
20 #include <HYDROGUI_StreamDlg.h>
21 #include <HYDROData_Document.h>
22 #include <HYDROData_DTM.h>
23 #include <HYDROData_Stream.h>
24 #include <HYDROData_IPolyline.h>
25 #include <HYDROData_Profile.h>
26 #include <HYDROData_PolylineXY.h>
27 #include <HYDROData_Iterator.h>
28 #include <AIS_InteractiveContext.hxx>
29 #include <TestViewer.h>
30 #include <QApplication>
31
32 extern QString REF_DATA_PATH;
33 NCollection_Sequence<HYDROData_IPolyline::Point> points2;
34 const double EPS = 1E-3;
35
36 void test_HYDROData_Stream::setUp()
37 {
38   points2.Clear();
39   points2.Append( gp_XY( 0.0, 5.0 ) );
40   points2.Append( gp_XY( 1.0, 1.0 ) );
41   points2.Append( gp_XY( 1.5, 0.0 ) );
42   points2.Append( gp_XY( 4.0, 4.0 ) );
43 }
44
45 void test_HYDROData_Stream::tearDown()
46 {
47 }
48
49 void test_HYDROData_Stream::test_dialog()
50 {
51   HYDROGUI_StreamDlg* aDlg = new HYDROGUI_StreamDlg( 0, "stream" );
52   aDlg->show();
53   qApp->processEvents();
54
55   QImage aStreamDlgImage = QPixmap::grabWidget( aDlg ).toImage();
56   CPPUNIT_ASSERT_IMAGES2( &aStreamDlgImage, "StreamDlg" );
57
58   aDlg->setDDZ( 12.34 );
59   CPPUNIT_ASSERT_DOUBLES_EQUAL( 12.34, aDlg->getDDZ(), EPS );
60
61   aDlg->setSpatialStep( 56.78 );
62   CPPUNIT_ASSERT_DOUBLES_EQUAL( 56.78, aDlg->getSpatialStep(), EPS );
63
64   delete aDlg;
65 }
66
67 void test_HYDROData_Stream::test_alt_object()
68 {
69   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
70
71   Handle(HYDROData_Stream) aStream = 
72     Handle(HYDROData_Stream)::DownCast( aDoc->CreateObject( KIND_STREAM ) );
73
74   CPPUNIT_ASSERT_EQUAL( false, (bool)aStream.IsNull() );
75   CPPUNIT_ASSERT_EQUAL( false, (bool)aStream->GetAltitudeObject().IsNull() );
76   CPPUNIT_ASSERT_EQUAL( KIND_DTM, aStream->getAltitudeObjectType() );
77
78   Handle(HYDROData_DTM) aDTM = 
79     Handle(HYDROData_DTM)::DownCast( aStream->GetAltitudeObject() );
80   CPPUNIT_ASSERT_EQUAL( false, (bool)aDTM.IsNull() );
81
82   aDoc->Close();
83 }
84
85 void test_HYDROData_Stream::test_params_sync()
86 {
87   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
88
89   Handle(HYDROData_Stream) aStream = 
90     Handle(HYDROData_Stream)::DownCast( aDoc->CreateObject( KIND_STREAM ) );
91   Handle(HYDROData_DTM) aDTM = 
92     Handle(HYDROData_DTM)::DownCast( aStream->GetAltitudeObject() );
93   CPPUNIT_ASSERT_EQUAL( false, (bool)aDTM.IsNull() );
94
95   Handle(HYDROData_Profile) aProfile1 = 
96     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
97
98   Handle(HYDROData_Profile) aProfile2 = 
99     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
100
101   aProfile1->SetParametricPoints( points2 );
102   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
103   aProfile1->SetLeftPoint( gp_XY( 10, 10 ) );
104   aProfile1->SetRightPoint( gp_XY( 20, 0 ) );
105
106   aProfile2->SetParametricPoints( points2 );
107   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
108   aProfile2->SetLeftPoint( gp_XY( 50, 0 ) );
109   aProfile2->SetRightPoint( gp_XY( 60, 10 ) );
110
111   HYDROData_SequenceOfObjects profiles;
112   profiles.Append( aProfile1 );
113   profiles.Append( aProfile2 );
114
115   aStream->SetProfiles( profiles, false );
116   aStream->SetDDZ( 3.14 );
117   aStream->SetSpatialStep( 4.14 );
118
119   CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.14, aStream->GetDDZ(), EPS );
120   CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.14, aDTM->GetDDZ(), EPS );
121   CPPUNIT_ASSERT_DOUBLES_EQUAL( 4.14, aStream->GetSpatialStep(), EPS );
122   CPPUNIT_ASSERT_DOUBLES_EQUAL( 4.14, aDTM->GetSpatialStep(), EPS );
123
124   HYDROData_SequenceOfObjects profiles1 = aStream->GetProfiles();
125   CPPUNIT_ASSERT_EQUAL( 2, profiles1.Size() );
126   CPPUNIT_ASSERT( profiles.Value(1)->Label() == profiles1.Value(1)->Label() );
127   CPPUNIT_ASSERT( profiles.Value(2)->Label() == profiles1.Value(2)->Label() );
128
129   HYDROData_SequenceOfObjects profiles2 = aDTM->GetProfiles();
130   CPPUNIT_ASSERT_EQUAL( 2, profiles2.Size() );
131   CPPUNIT_ASSERT( profiles.Value(1)->Label() == profiles2.Value(1)->Label() );
132   CPPUNIT_ASSERT( profiles.Value(2)->Label() == profiles2.Value(2)->Label() );
133
134   aDoc->Close();
135 }
136
137 void test_HYDROData_Stream::test_dump()
138 {
139   // Case 1. Without hydraulic axis
140   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
141
142   Handle(HYDROData_Stream) aStream1 = 
143     Handle(HYDROData_Stream)::DownCast( aDoc->CreateObject( KIND_STREAM ) );
144   aStream1->SetName( "stream_1" );
145
146   Handle(HYDROData_Profile) aProfile1 = 
147     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
148   aProfile1->SetName( "p1" );
149
150   Handle(HYDROData_Profile) aProfile2 = 
151     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
152   aProfile2->SetName( "p2" );
153
154   aProfile1->SetParametricPoints( points2 );
155   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
156   aProfile1->SetLeftPoint( gp_XY( 10, 10 ) );
157   aProfile1->SetRightPoint( gp_XY( 20, 0 ) );
158
159   aProfile2->SetParametricPoints( points2 );
160   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
161   aProfile2->SetLeftPoint( gp_XY( 50, 0 ) );
162   aProfile2->SetRightPoint( gp_XY( 60, 10 ) );
163
164   HYDROData_SequenceOfObjects profiles;
165   profiles.Append( aProfile1 );
166   profiles.Append( aProfile2 );
167
168   aStream1->SetProfiles( profiles, false );
169   aStream1->SetDDZ( 0.2 );
170   aStream1->SetSpatialStep( 3.0 );
171
172   MapOfTreatedObjects objs;
173   objs["p1"] = aProfile1;
174   objs["p2"] = aProfile2;
175
176   QStringList aScript1 = aStream1->DumpToPython( "", objs );
177   CPPUNIT_ASSERT_EQUAL( 10, aScript1.size() );
178   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1 = hydro_doc.CreateObject( KIND_STREAM )" ), aScript1[0].toStdString() );
179   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.SetName( \"stream_1\" )" ),                 aScript1[1].toStdString() );
180   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript1[2].toStdString() );
181   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.AddProfile( p1 )" ),                        aScript1[3].toStdString() );
182   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.AddProfile( p2 )" ),                        aScript1[4].toStdString() );
183   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.SetDDZ( 0.200 )" ),                         aScript1[5].toStdString() );
184   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.SetSpatialStep( 3.000 )" ),                 aScript1[6].toStdString() );
185   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript1[7].toStdString() );
186   CPPUNIT_ASSERT_EQUAL( std::string( "stream_1.Update()" ),                                aScript1[8].toStdString() );
187   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript1[9].toStdString() );
188
189   // Case 2. With hydraulic axis
190
191   Handle(HYDROData_Stream) aStream2 = 
192     Handle(HYDROData_Stream)::DownCast( aDoc->CreateObject( KIND_STREAM ) );
193   aStream2->SetName( "stream_2" );
194
195   Handle(HYDROData_PolylineXY) anHAxis = 
196     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
197   anHAxis->SetName( "axis" );
198
199   aStream2->SetProfiles( profiles, false );
200   aStream2->SetDDZ( 0.2 );
201   aStream2->SetSpatialStep( 3.0 );
202   aStream2->SetReferenceObject( anHAxis, HYDROData_Stream::DataTag_HydraulicAxis );
203
204   objs.clear();
205   objs["p1"] = aProfile1;
206   objs["p2"] = aProfile2;
207   objs["axis"] = anHAxis;
208
209   QStringList aScript2 = aStream2->DumpToPython( "", objs );
210   CPPUNIT_ASSERT_EQUAL( 11, aScript2.size() );
211   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2 = hydro_doc.CreateObject( KIND_STREAM )" ), aScript2[0].toStdString() );
212   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.SetName( \"stream_2\" )" ),                 aScript2[1].toStdString() );
213   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript2[2].toStdString() );
214   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.SetHydraulicAxis( axis )" ),                aScript2[3].toStdString() );
215   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.AddProfile( p1 )" ),                        aScript2[4].toStdString() );
216   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.AddProfile( p2 )" ),                        aScript2[5].toStdString() );
217   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.SetDDZ( 0.200 )" ),                         aScript2[6].toStdString() );
218   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.SetSpatialStep( 3.000 )" ),                 aScript2[7].toStdString() );
219   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript2[8].toStdString() );
220   CPPUNIT_ASSERT_EQUAL( std::string( "stream_2.Update()" ),                                aScript2[9].toStdString() );
221   CPPUNIT_ASSERT_EQUAL( std::string( "" ),                                                 aScript2[10].toStdString() );
222
223   aDoc->Close();
224 }
225
226 void test_HYDROData_Stream::test_presentation()
227 {
228   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
229   
230   TCollection_AsciiString fname = REF_DATA_PATH.toLatin1().data();
231   fname += "/Profiles.xyz";
232   NCollection_Sequence<int> bad_ids;
233
234   int aSize = HYDROData_Profile::ImportFromFile( aDoc, fname, bad_ids, true );
235   
236   CPPUNIT_ASSERT_EQUAL( 0, bad_ids.Size() );
237   CPPUNIT_ASSERT_EQUAL( 46, aSize );
238
239   HYDROData_SequenceOfObjects profiles;
240   HYDROData_Iterator it( aDoc, KIND_PROFILE );
241   for( int i=0; it.More(); it.Next(), i++ )
242   {
243     if( i>=31 && i<=32 )
244     {
245       it.Current()->Update();
246       profiles.Append( Handle(HYDROData_Profile)::DownCast( it.Current() ) );
247     }
248   }
249
250   Handle(HYDROData_Stream) aStream = 
251     Handle(HYDROData_Stream)::DownCast( aDoc->CreateObject( KIND_STREAM ) );
252
253   aStream->SetProfiles( profiles, false );
254   aStream->SetDDZ( 0.2 );
255   aStream->SetSpatialStep( 10 );
256   aStream->Update();
257
258   TopoDS_Shape aPrs3d = aStream->GetShape3D();
259   TopoDS_Shape aPrs2d = aStream->GetTopShape();
260
261   TestViewer::show( aPrs2d, 0, true, "stream_dtm_2d" );
262   CPPUNIT_ASSERT_IMAGES;
263
264   TestViewer::eraseAll( true );
265   TestViewer::show( aPrs3d, 0, true, "stream_dtm_3d" );
266   CPPUNIT_ASSERT_IMAGES
267
268   aDoc->Close();
269 }
270