Salome HOME
tests configuration for Linux and YAMM
[modules/hydro.git] / src / HYDRO_tests / test_HYDROGUI_BathymetryPrs.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_HYDROGUI_BathymetryPrs.h>
20 #include <HYDROGUI_ShapeBathymetry.h>
21 #include <TestViewer.h>
22 #include <AIS_ColorScale.hxx>
23 #include <OCCViewer_ViewWindow.h>
24 #include <OCCViewer_ViewFrame.h>
25 #include <OCCViewer_ViewPort3d.h>
26 #include <QTest>
27
28 extern QString REF_DATA_PATH;
29 const double EPS = 1E-3;
30
31 void test_HYDROGUI_BathymetryPrs::importTestBath( const Handle(HYDROData_Document)& theDoc )
32 {
33   myBath = Handle(HYDROData_Bathymetry)::DownCast( theDoc->CreateObject( KIND_BATHYMETRY ) );
34
35   QString fname = (REF_DATA_PATH + "/bathy.xyz");
36   CPPUNIT_ASSERT( myBath->ImportFromFiles( QStringList() << fname ) );
37
38   HYDROData_Bathymetry::AltitudePoints anAltitudePoints = myBath->GetAltitudePoints();
39   CPPUNIT_ASSERT_EQUAL( 14781, (int)anAltitudePoints.size() );
40 }
41
42 void test_HYDROGUI_BathymetryPrs::createBathPrs()
43 {
44   myBathPrs = new HYDROGUI_ShapeBathymetry( 0, TestViewer::context(), myBath );
45   myBathPrs->Build();
46   myBathPrs->getAISObjects()[0]->SetAutoHilight( Standard_False );
47
48   double min, max;
49   myBathPrs->GetRange( min, max );
50   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
51   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
52 }
53
54 void test_HYDROGUI_BathymetryPrs::updateColors()
55 {
56   double min, max;
57   myBathPrs->GetRange( min, max );
58
59   TestViewer::colorScale()->SetRange( min, max );
60   myBathPrs->UpdateWithColorScale( TestViewer::colorScale() );
61 }
62
63
64 void test_HYDROGUI_BathymetryPrs::test_presentation()
65 {
66   TestViewer::eraseAll( true );
67   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
68   
69   importTestBath( aDoc );
70   TestViewer::showColorScale( false );
71
72   createBathPrs();
73   updateColors();
74
75   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
76   CPPUNIT_ASSERT( !bprs.IsNull() );
77
78   TestViewer::show( bprs, 0, 0, true, "bathy_prs" );
79   CPPUNIT_ASSERT_IMAGES
80   //QTest::qWait( 50000 );
81
82   aDoc->Close();
83   TestViewer::eraseAll( true );
84 }
85
86 void select( int x1, int y1, int x2, int y2 )
87 {
88   QPoint p1( x1, y1 ), p2( x2, y2 );
89   QWidget* w = TestViewer::viewWindow()->getViewPort();
90   QTest::mousePress( w, Qt::LeftButton, Qt::NoModifier, p1 );
91   QTest::mouseEvent( QTest::MouseMove, w, Qt::LeftButton, Qt::NoModifier, p2 );
92   QTest::mouseRelease( w, Qt::LeftButton, Qt::NoModifier, p2 );
93   qApp->processEvents();
94 }
95
96 void test_HYDROGUI_BathymetryPrs::test_selection()
97 {
98   TestViewer::eraseAll( true );
99   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
100
101   importTestBath( aDoc );
102   TestViewer::showColorScale( false );
103
104   createBathPrs();
105   updateColors();
106
107   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
108   CPPUNIT_ASSERT( !bprs.IsNull() );
109
110   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
111   CPPUNIT_ASSERT_IMAGES;
112
113   select( 150, 5, 350, 140 );
114   TestViewer::setKey( "bathy_selection" );
115   CPPUNIT_ASSERT_IMAGES
116
117   select( 5, 5, 6, 6 );
118   TestViewer::setKey( "bathy_prs" );
119   CPPUNIT_ASSERT_IMAGES
120
121   //QTest::qWait( 50000 );
122
123   aDoc->Close();
124   TestViewer::eraseAll( true );
125 }
126
127 void test_HYDROGUI_BathymetryPrs::test_rescale_by_selection()
128 {
129   TestViewer::eraseAll( true );
130   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
131
132   importTestBath( aDoc );
133   TestViewer::showColorScale( false );
134
135   createBathPrs();
136   updateColors();
137
138   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
139   CPPUNIT_ASSERT( !bprs.IsNull() );
140
141   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
142   CPPUNIT_ASSERT_IMAGES;
143
144   // 1. Rescale by selection
145   select( 150, 100, 350, 125 );
146   myBathPrs->RescaleBySelection();
147   updateColors(); // In HYDRO GUI it should be done by displayer
148
149   //QTest::qWait( 50000 );
150
151   double min, max;
152   myBathPrs->GetRange( min, max );
153   CPPUNIT_ASSERT_DOUBLES_EQUAL( 177.78, min, EPS );
154   CPPUNIT_ASSERT_DOUBLES_EQUAL( 182.70, max, EPS );
155
156   TestViewer::colorScale()->GetRange( min, max );
157   CPPUNIT_ASSERT_DOUBLES_EQUAL( 177.78, min, EPS );
158   CPPUNIT_ASSERT_DOUBLES_EQUAL( 182.70, max, EPS );
159
160   TestViewer::setKey( "bathy_rescaled_selection" );
161   CPPUNIT_ASSERT_IMAGES;
162
163   //QTest::qWait( 50000 );
164
165   // 2. User rescale
166   myBathPrs->Rescale( 180, 181 );
167   updateColors(); // In HYDRO GUI it should be done by displayer
168
169   myBathPrs->GetRange( min, max );
170   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
171   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
172
173   TestViewer::colorScale()->GetRange( min, max );
174   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
175   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
176
177   TestViewer::setKey( "bathy_rescaled_user" );
178   CPPUNIT_ASSERT_IMAGES;
179
180   //QTest::qWait( 50000 );
181
182   aDoc->Close();
183   TestViewer::eraseAll( true );
184 }
185
186 void test_HYDROGUI_BathymetryPrs::test_rescale_by_visible()
187 {
188   TestViewer::eraseAll( true );
189   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
190
191   importTestBath( aDoc );
192   TestViewer::showColorScale( false );
193
194   createBathPrs();
195   updateColors();
196
197   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
198   CPPUNIT_ASSERT( !bprs.IsNull() );
199
200   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
201   CPPUNIT_ASSERT_IMAGES;
202
203   // Change aspect to see a part of bathymetry
204   OCCViewer_ViewPort3d* vp = TestViewer::viewWindow()->getViewPort();
205   Handle(V3d_View) v = vp->getView();
206   v->SetProj( 1, 1, 1 );
207   vp->fitRect( QRect( 500, 380, 100, 70 ) );
208   qApp->processEvents();
209
210   // Rescale to the visible part of bathymetry
211   myBathPrs->RescaleByVisible( TestViewer::viewWindow() );
212   updateColors(); // In HYDRO GUI it should be done by displayer
213   vp->fitAll(); // Necessary to see the whole presentation
214
215   // Check parameters
216   double min, max;
217   myBathPrs->GetRange( min, max );
218   CPPUNIT_ASSERT_DOUBLES_EQUAL( 176.18, min, EPS );
219   CPPUNIT_ASSERT_DOUBLES_EQUAL( 179.55, max, EPS );
220
221   TestViewer::colorScale()->GetRange( min, max );
222   CPPUNIT_ASSERT_DOUBLES_EQUAL( 176.18, min, EPS );
223   CPPUNIT_ASSERT_DOUBLES_EQUAL( 179.55, max, EPS );
224
225   TestViewer::setKey( "bathy_rescaled_visible" );
226   CPPUNIT_ASSERT_IMAGES;
227
228   //QTest::qWait( 50000 );
229   aDoc->Close();
230   TestViewer::eraseAll( true );
231 }
232
233 void test_HYDROGUI_BathymetryPrs::test_text_presentation()
234 {
235   TestViewer::eraseAll( true );
236   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
237
238   importTestBath( aDoc );
239   TestViewer::showColorScale( false );
240
241   createBathPrs();
242   updateColors();
243
244   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
245   CPPUNIT_ASSERT( !bprs.IsNull() );
246
247   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
248   CPPUNIT_ASSERT_IMAGES;
249
250   // Assign text labels
251   int x1 = 150, y1 = 100, x2 = 200, y2 = 150;
252   select( x1, y1, x2, y2 );
253   myBathPrs->TextLabels( true );
254   OCCViewer_ViewPort3d* vp = TestViewer::viewWindow()->getViewPort();
255   vp->fitRect( QRect( x1, y1, x2-x1, y2-y1 ) );
256
257   qApp->processEvents();
258
259   TestViewer::setKey( "bathy_text_labels" );
260   CPPUNIT_ASSERT_IMAGES;
261
262   // Disable text labels
263   myBathPrs->TextLabels( false );
264   vp->fitAll();
265   qApp->processEvents();
266   TestViewer::setKey( "bathy_prs" );
267   CPPUNIT_ASSERT_IMAGES;
268
269   // Special case: flag=false + non-empty selection
270   select( x1, y1, x2, y2 );
271   myBathPrs->TextLabels( false );
272   vp->fitAll();
273   qApp->processEvents();
274   TestViewer::setKey( "bathy_prs" );
275   CPPUNIT_ASSERT_IMAGES;
276
277
278   //QTest::qWait( 50000 );
279
280   aDoc->Close();
281   TestViewer::eraseAll( true );
282 }
283
284 void test_HYDROGUI_BathymetryPrs::test_rescale_default()
285 {
286   TestViewer::eraseAll( true );
287   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
288
289   importTestBath( aDoc );
290   TestViewer::showColorScale( false );
291
292   createBathPrs();
293   updateColors();
294
295   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
296   CPPUNIT_ASSERT( !bprs.IsNull() );
297
298   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
299   CPPUNIT_ASSERT_IMAGES;
300
301   double min, max;
302
303   // 1. User rescale
304   myBathPrs->Rescale( 180, 181 );
305   updateColors(); // In HYDRO GUI it should be done by displayer
306
307   myBathPrs->GetRange( min, max );
308   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
309   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
310
311   TestViewer::colorScale()->GetRange( min, max );
312   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
313   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
314
315   TestViewer::setKey( "bathy_rescaled_user" );
316   CPPUNIT_ASSERT_IMAGES;
317
318   //QTest::qWait( 50000 );
319
320   // 2. Default rescale
321   myBathPrs->RescaleDefault();
322   updateColors(); // In HYDRO GUI it should be done by displayer
323
324   myBathPrs->GetRange( min, max );
325   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
326   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
327
328   TestViewer::colorScale()->GetRange( min, max );
329   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
330   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
331
332   TestViewer::setKey( "bathy_prs" );
333   CPPUNIT_ASSERT_IMAGES;
334
335   //QTest::qWait( 50000 );
336
337   aDoc->Close();
338   TestViewer::eraseAll( true );
339 }
340
341 void test_HYDROGUI_BathymetryPrs::test_fit_on_selected()
342 {
343   TestViewer::eraseAll( true );
344   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
345
346   importTestBath( aDoc );
347   TestViewer::showColorScale( false );
348
349   createBathPrs();
350   updateColors();
351
352   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
353   CPPUNIT_ASSERT( !bprs.IsNull() );
354
355   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
356   CPPUNIT_ASSERT_IMAGES;
357
358   // Fit selected points on bathymetry
359   int x1 = 100, y1 = 50, x2 = 250, y2 = 200;
360   select( x1, y1, x2, y2 );
361   TestViewer::viewWindow()->onFitSelection();
362   TestViewer::setKey( "bathy_prs_fit_selected" );
363   CPPUNIT_ASSERT_IMAGES;
364
365   //QTest::qWait( 50000 );
366
367   aDoc->Close();
368   TestViewer::eraseAll( true );
369 }