Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into BR_1427
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_DTM.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_DTM.h>
20 #include <HYDROData_Document.h>
21 #include <HYDROData_Profile.h>
22 #include <HYDROData_DTM.h>
23 #include <HYDROData_Iterator.h>
24 #include <HYDROData_CalculationCase.h>
25 #include <Geom2d_Curve.hxx>
26 #include <Geom2d_BSplineCurve.hxx>
27 #include <gp_XY.hxx>
28 #include <gp_Pnt2d.hxx>
29 #include <TColgp_Array1OfPnt2d.hxx>
30 #include <TestViewer.h>
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_PointCloud.hxx>
33 #include <HYDROGUI_ShapeBathymetry.h>
34 #include <AIS_ColorScale.hxx>
35 #include <QGraphicsItem>
36 #include <QGraphicsScene>
37 #include <QGraphicsView>
38 #include <QPixmap>
39 #include <QApplication>
40 #include <QTest>
41
42 const double EPS = 1E-3;
43
44 extern QString REF_DATA_PATH;
45 NCollection_Sequence<HYDROData_IPolyline::Point> points;
46
47 class DTM_item : public QGraphicsItem
48 {
49 public:
50   DTM_item( HYDROGUI_ShapeBathymetry* theDTM, double d )
51   {
52     Handle(AIS_PointCloud) pc = Handle(AIS_PointCloud)::DownCast( theDTM->getAISObjects()[0] );
53     Handle(Graphic3d_ArrayOfPoints) pp = pc->GetPoints();
54     myBB = QRectF();
55     double xmin, xmax, ymin, ymax;
56     myD = d;
57
58     int n = pp->VertexNumber();
59     
60     for( int i=1; i<=n; i++ )
61     {
62       gp_Pnt pnt = pp->Vertice( i );
63       Quantity_Color col = pp->VertexColor( i );
64
65       int r = col.Red()*255;
66       int g = col.Green()*255;
67       int b = col.Blue()*255;
68       int val = ( r << 16 ) + ( g << 8 ) + b;
69       double x = pnt.X();
70       double y = -pnt.Y();
71       QPointF aPnt( x, y );
72       myPoints[val].append( aPnt );
73       if( i==1 )
74       {
75         xmin = x;
76         xmax = x;
77         ymin = y;
78         ymax = y;
79       }
80       else
81       {
82         if( x>xmax )
83           xmax = x;
84         if( x<xmin )
85           xmin = x;
86         if( y>ymax )
87           ymax = y;
88         if( y<ymin )
89           ymin = y;
90       }
91     }
92
93     xmin -= 10;
94     xmax += 10;
95     ymin -= 10;
96     ymax += 10;
97     myBB.setRect( xmin, ymin, xmax-xmin, ymax-ymin );
98   }
99
100   virtual QRectF boundingRect() const
101   {
102     return myBB;
103   }
104
105   virtual void paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidget* )
106   {
107     QMap<int, QList<QPointF> >::const_iterator it = myPoints.begin(), last = myPoints.end();
108     for( ; it!=last; it++ )
109     {
110       int r = ( it.key() >> 16 ) % 256;
111       int g = ( it.key() >> 8  ) % 256;
112       int b = ( it.key() >> 0  ) % 256;
113       QColor aColor( r, g, b );
114       QBrush aBrush( aColor );
115       foreach( QPointF pnt, it.value() )
116       {
117         QRectF r( pnt.x()-myD/2, pnt.y()-myD/2, myD, myD );
118         p->fillRect( r, aBrush );
119       }
120     }
121   }
122
123 private:
124   QRectF myBB;
125   double myD;
126   QMap<int, QList<QPointF> > myPoints;
127 };
128
129 QImage draw_DTM( HYDROGUI_ShapeBathymetry* theDTM, double theD, int theWidth, int theHeight )
130 {
131   QGraphicsScene aScene;
132   QGraphicsView aView;
133   DTM_item anItem( theDTM, theD );
134
135   aView.setScene( &aScene );
136   aView.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
137   aView.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
138   aScene.addItem( &anItem );
139
140   aView.resize( theWidth, theHeight );
141   QRectF bb = anItem.boundingRect();
142   aView.fitInView( bb, Qt::KeepAspectRatio );
143   QApplication::processEvents();
144
145   QPixmap aPixmap = QPixmap::grabWidget( &aView );
146   return aPixmap.toImage();
147 }
148
149
150 void test_HYDROData_DTM::setUp()
151 {
152   points.Clear();
153   points.Append( gp_XY( 0.0, 5.0 ) );
154   points.Append( gp_XY( 1.0, 1.0 ) );
155   points.Append( gp_XY( 1.5, 0.0 ) );
156   points.Append( gp_XY( 4.0, 4.0 ) );
157 }
158
159 void test_HYDROData_DTM::test_creation()
160 {
161   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
162
163   Handle(HYDROData_DTM) DTM = 
164     Handle(HYDROData_DTM)::DownCast( aDoc->CreateObject( KIND_DTM ) );
165
166   CPPUNIT_ASSERT_EQUAL( false, (bool)DTM.IsNull() );
167    
168   aDoc->Close();
169 }
170
171 void test_HYDROData_DTM::test_hydraulic_axis()
172 {
173   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
174
175   Handle(HYDROData_Profile) aProfile1 = 
176     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
177
178   Handle(HYDROData_Profile) aProfile2 = 
179     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
180
181   Handle(HYDROData_Profile) aProfile3 = 
182     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
183
184   aProfile1->SetParametricPoints( points );
185   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
186   aProfile1->SetLeftPoint( gp_XY( 10, 10 ) );
187   aProfile1->SetRightPoint( gp_XY( 20, 0 ) );
188
189   aProfile2->SetParametricPoints( points );
190   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
191   aProfile2->SetLeftPoint( gp_XY( 50, 0 ) );
192   aProfile2->SetRightPoint( gp_XY( 60, 10 ) );
193
194   aProfile3->SetParametricPoints( points );
195   aProfile3->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
196   aProfile3->SetLeftPoint( gp_XY( 200, 50 ) );
197   aProfile3->SetRightPoint( gp_XY( 210, 40 ) );
198
199   std::vector<double> distances;
200   std::vector<Handle(HYDROData_Profile)> profiles;
201   profiles.push_back( aProfile1 );
202   profiles.push_back( aProfile2 );
203   profiles.push_back( aProfile3 );
204
205   Handle(Geom2d_BSplineCurve) HA = HYDROData_DTM::CreateHydraulicAxis( profiles, distances );
206   CPPUNIT_ASSERT_EQUAL( false, (bool)HA.IsNull() );
207   CPPUNIT_ASSERT_EQUAL( 3, (int)distances.size() );
208
209   CPPUNIT_ASSERT_DOUBLES_EQUAL(   0.0,   distances[0], EPS );
210   CPPUNIT_ASSERT_DOUBLES_EQUAL(  44.859, distances[1], EPS );
211   CPPUNIT_ASSERT_DOUBLES_EQUAL( 207.661, distances[2], EPS );
212
213   gp_Pnt2d p;
214   gp_Vec2d q;
215   HA->D1( 0, p, q );
216   CPPUNIT_ASSERT_DOUBLES_EQUAL( 13.75, p.X(), EPS );
217   CPPUNIT_ASSERT_DOUBLES_EQUAL( 6.25, p.Y(), EPS );
218   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.532, q.X(), EPS );
219   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.532, q.Y(), EPS );
220
221   aDoc->Close();
222 }
223
224 void test_HYDROData_DTM::test_profile_conversion_to_2d()
225 {
226   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
227
228   Handle(HYDROData_Profile) aProfile1 = 
229     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
230
231   Handle(HYDROData_Profile) aProfile2 = 
232     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
233
234   aProfile1->SetParametricPoints( points );
235   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
236   aProfile1->SetLeftPoint( gp_XY( 10, 10 ) );
237   aProfile1->SetRightPoint( gp_XY( 20, 20 ) );
238
239   aProfile2->SetParametricPoints( points );
240   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_SPLINE );
241   aProfile2->SetLeftPoint( gp_XY( 10, 10 ) );
242   aProfile2->SetRightPoint( gp_XY( 20, 20 ) );
243
244   double aUMin1 = std::numeric_limits<double>::max(),
245          aUMax1 = -aUMin1,
246          aUMin2 = aUMin1,
247          aUMax2 = aUMax1;
248   gp_Vec2d aProfileDir;
249   std::vector<Handle(Geom2d_Curve)> curves1 = HYDROData_DTM::ProfileToParametric( aProfile1, aUMin1, aUMax1, aProfileDir );
250   std::vector<Handle(Geom2d_Curve)> curves2 = HYDROData_DTM::ProfileToParametric( aProfile2, aUMin2, aUMax2, aProfileDir );
251
252   gp_Pnt2d aFirst, aLast;
253   CPPUNIT_ASSERT_EQUAL( 3, (int)curves1.size() );
254   CPPUNIT_ASSERT_DOUBLES_EQUAL( -5.303, aUMin1, EPS );
255   CPPUNIT_ASSERT_DOUBLES_EQUAL(  8.839, aUMax1, EPS );
256   curves1[0]->D0( curves1[0]->FirstParameter(), aFirst );
257   curves1[0]->D0( curves1[0]->LastParameter(), aLast );
258   CPPUNIT_ASSERT_DOUBLES_EQUAL( -5.303, aFirst.X(), EPS );
259   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   aFirst.Y(), EPS );
260   CPPUNIT_ASSERT_DOUBLES_EQUAL( -1.768, aLast.X(),  EPS );
261   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.0,   aLast.Y(),  EPS );
262   curves1[1]->D0( curves1[1]->FirstParameter(), aFirst );
263   curves1[1]->D0( curves1[1]->LastParameter(), aLast );
264   CPPUNIT_ASSERT_DOUBLES_EQUAL( -1.768, aFirst.X(), EPS );
265   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.0,   aFirst.Y(), EPS );
266   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aLast.X(),  EPS );
267   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aLast.Y(),  EPS );
268   curves1[2]->D0( curves1[2]->FirstParameter(), aFirst );
269   curves1[2]->D0( curves1[2]->LastParameter(), aLast );
270   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aFirst.X(), EPS );
271   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aFirst.Y(), EPS );
272   CPPUNIT_ASSERT_DOUBLES_EQUAL(  8.839, aLast.X(),  EPS );
273   CPPUNIT_ASSERT_DOUBLES_EQUAL(  4.0,   aLast.Y(),  EPS );
274
275   CPPUNIT_ASSERT_EQUAL( 1, (int)curves2.size() );
276   CPPUNIT_ASSERT_DOUBLES_EQUAL( -5.303, aUMin2, EPS );
277   CPPUNIT_ASSERT_DOUBLES_EQUAL(  8.839, aUMax2, EPS );
278   Handle(Geom2d_BSplineCurve) aBSpline = Handle(Geom2d_BSplineCurve)::DownCast( curves2[0] );
279   CPPUNIT_ASSERT_EQUAL( false, (bool)aBSpline.IsNull() );
280   const TColgp_Array1OfPnt2d& poles = aBSpline->Poles();
281   CPPUNIT_ASSERT_EQUAL( 1, (int)poles.Lower() );
282   CPPUNIT_ASSERT_EQUAL( 8, (int)poles.Upper() );
283   CPPUNIT_ASSERT_DOUBLES_EQUAL( -5.303, poles.Value( 1 ).X(), EPS );
284   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   poles.Value( 1 ).Y(), EPS );
285   CPPUNIT_ASSERT_DOUBLES_EQUAL( -4.125, poles.Value( 2 ).X(), EPS );
286   CPPUNIT_ASSERT_DOUBLES_EQUAL(  3.667, poles.Value( 2 ).Y(), EPS );
287   CPPUNIT_ASSERT_DOUBLES_EQUAL( -3.150, poles.Value( 3 ).X(), EPS );
288   CPPUNIT_ASSERT_DOUBLES_EQUAL(  2.120, poles.Value( 3 ).Y(), EPS );
289   CPPUNIT_ASSERT_DOUBLES_EQUAL( -1.242, poles.Value( 4 ).X(), EPS );
290   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.574, poles.Value( 4 ).Y(), EPS );
291
292   aDoc->Close();
293 }
294
295 void test_HYDROData_DTM::test_profile_properties()
296 {
297   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
298
299   Handle(HYDROData_Profile) aProfile = 
300     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
301
302   aProfile->SetParametricPoints( points );
303   aProfile->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
304   aProfile->SetLeftPoint( gp_XY( 10, 10 ) );
305   aProfile->SetRightPoint( gp_XY( 20, 25 ) );
306
307   gp_Pnt lp;
308   gp_Vec2d dd;
309   double zmin, zmax;
310   HYDROData_DTM::GetProperties( aProfile, lp, dd, zmin, zmax );
311   CPPUNIT_ASSERT_DOUBLES_EQUAL( 13.75, lp.X(), EPS );
312   CPPUNIT_ASSERT_DOUBLES_EQUAL( 15.625, lp.Y(), EPS );
313   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, lp.Z(), EPS );
314   CPPUNIT_ASSERT_DOUBLES_EQUAL( 10, dd.X(), EPS );
315   CPPUNIT_ASSERT_DOUBLES_EQUAL( 15, dd.Y(), EPS );
316   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, zmin, EPS );
317   CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0, zmax, EPS );
318
319  /* HYDROData_DTM::GetProperties( aProfile, lp, dd, true, zmin, zmax );
320   CPPUNIT_ASSERT_DOUBLES_EQUAL( 13.75, lp.X(), EPS );
321   CPPUNIT_ASSERT_DOUBLES_EQUAL( 15.625, lp.Y(), EPS );
322   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, lp.Z(), EPS );
323   CPPUNIT_ASSERT_DOUBLES_EQUAL( -15, dd.X(), EPS );
324   CPPUNIT_ASSERT_DOUBLES_EQUAL( 10, dd.Y(), EPS );
325   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, zmin, EPS );
326   CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0, zmax, EPS );*/
327
328   aDoc->Close();
329 }
330
331 void test_HYDROData_DTM::test_profile_discretization_polyline()
332 {
333   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
334
335   Handle(HYDROData_Profile) aProfile = 
336     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
337
338   aProfile->SetParametricPoints( points );
339   aProfile->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
340   aProfile->SetLeftPoint( gp_XY( 10, 10 ) );
341   aProfile->SetRightPoint( gp_XY( 20, 20 ) );
342
343   HYDROData_DTM::CurveUZ aMid( 0.0, gp_Vec2d(), 0, 0 ), aWid( 0.0, gp_Vec2d(), 0, 0 );
344   int dummy = 0;
345   HYDROData_DTM::ProfileDiscretization( aProfile, 0.0, 0.0, 5.0, 5.0, 0.5, aMid, aWid, dummy );
346   CPPUNIT_ASSERT_EQUAL( 11, (int)aMid.size() );
347   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aMid[0].U, EPS );
348   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aMid[0].Z, EPS );
349   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.11,  aMid[1].U, EPS );
350   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.5,   aMid[1].Z, EPS );
351   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.215, aMid[5].U, EPS );
352   CPPUNIT_ASSERT_DOUBLES_EQUAL(  2.5,   aMid[5].Z, EPS );
353   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.768, aMid[10].U, EPS );
354   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   aMid[10].Z, EPS );
355
356   CPPUNIT_ASSERT_EQUAL( 11, (int)aWid.size() );
357   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aWid[0].U, EPS );
358   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aWid[0].Z, EPS );
359   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.989, aWid[1].U, EPS );
360   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.5,   aWid[1].Z, EPS );
361   CPPUNIT_ASSERT_DOUBLES_EQUAL(  8.618, aWid[5].U, EPS );
362   CPPUNIT_ASSERT_DOUBLES_EQUAL(  2.5,   aWid[5].Z, EPS );
363   CPPUNIT_ASSERT_DOUBLES_EQUAL( 14.142, aWid[10].U, EPS );
364   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   aWid[10].Z, EPS );
365
366   aDoc->Close();
367 }
368
369 void test_HYDROData_DTM::test_profile_discretization_spline()
370 {
371 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
372
373   Handle(HYDROData_Profile) aProfile = 
374     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
375
376   aProfile->SetParametricPoints( points );
377   aProfile->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_SPLINE );
378   aProfile->SetLeftPoint( gp_XY( 10, 10 ) );
379   aProfile->SetRightPoint( gp_XY( 20, 20 ) );
380
381   HYDROData_DTM::CurveUZ aMid( 0.0, gp_Vec2d(), 0, 0 ), aWid( 0.0, gp_Vec2d(), 0, 0 );
382   int dummy = 0 ;
383   HYDROData_DTM::ProfileDiscretization( aProfile, 0.0, 0.0, 5.0, 5.0, 0.5, aMid, aWid, dummy );
384   CPPUNIT_ASSERT_EQUAL( 11, (int)aMid.size() );
385   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.242, aMid[0].U, EPS );
386   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aMid[0].Z, EPS );
387   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.755, aMid[1].U, EPS );
388   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.5,   aMid[1].Z, EPS );
389   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.473, aMid[5].U, EPS );
390   CPPUNIT_ASSERT_DOUBLES_EQUAL(  2.5,   aMid[5].Z, EPS );
391   CPPUNIT_ASSERT_DOUBLES_EQUAL(  1.768, aMid[10].U, EPS );
392   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   aMid[10].Z, EPS );
393
394   CPPUNIT_ASSERT_EQUAL( 11, (int)aWid.size() );
395   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.484, aWid[0].U, EPS );
396   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.0,   aWid[0].Z, EPS );
397   CPPUNIT_ASSERT_DOUBLES_EQUAL(  3.809, aWid[1].U, EPS );
398   CPPUNIT_ASSERT_DOUBLES_EQUAL(  0.5,   aWid[1].Z, EPS );
399   CPPUNIT_ASSERT_DOUBLES_EQUAL(  9.472, aWid[5].U, EPS );
400   CPPUNIT_ASSERT_DOUBLES_EQUAL(  2.5,   aWid[5].Z, EPS );
401   CPPUNIT_ASSERT_DOUBLES_EQUAL( 14.142, aWid[10].U, EPS );
402   CPPUNIT_ASSERT_DOUBLES_EQUAL(  5.0,   aWid[10].Z, EPS );
403
404   aDoc->Close();
405 }
406
407 void operator << ( std::ostream& s, const HYDROData_DTM::PointUZ& p )
408 {
409   s << "(" << p.U << "; " << p.Z << ") ";
410 }
411
412 void operator << ( std::ostream& s, const HYDROData_DTM::AltitudePoint& p )
413 {
414   s << "(" << p.X << "; " << p.Y << "; " << p.Z << ") ";
415 }
416
417 bool operator == ( const HYDROData_DTM::PointUZ& p1, const HYDROData_DTM::PointUZ& p2 )
418 {
419   return fabs(p1.U-p2.U)<EPS && fabs(p1.Z-p2.Z)<EPS;
420 }
421
422 bool operator == ( const HYDROData_DTM::AltitudePoint& p1, const HYDROData_DTM::AltitudePoint& p2 )
423 {
424   return fabs(p1.X-p2.X)<EPS && fabs(p1.Y-p2.Y)<EPS && fabs(p1.Z-p2.Z)<EPS;
425 }
426
427 void operator << ( std::ostream& s, const HYDROData_DTM::CurveUZ& c )
428 {
429   size_t n = c.size();
430   for( size_t i=0; i<n; i++ )
431     s << c[i];
432 }
433
434 void test_HYDROData_DTM::test_curves_interpolation()
435 {
436   HYDROData_DTM::CurveUZ A(1.0, gp_Vec2d(), 0, 0), B(2.0, gp_Vec2d(), 0, 0);
437   A.push_back( HYDROData_DTM::PointUZ( 0, 0 ) );
438   A.push_back( HYDROData_DTM::PointUZ( 1, 1 ) );
439   A.push_back( HYDROData_DTM::PointUZ( 2, 2 ) );
440   B.push_back( HYDROData_DTM::PointUZ( 10, 0 ) );
441   B.push_back( HYDROData_DTM::PointUZ( 15, 1 ) );
442   B.push_back( HYDROData_DTM::PointUZ( 20, 2 ) );
443
444   std::vector<HYDROData_DTM::CurveUZ> i1;
445   HYDROData_DTM::Interpolate( A, B, 1, i1, false );
446
447   CPPUNIT_ASSERT_EQUAL( 2, (int)i1.size() );
448   CPPUNIT_ASSERT_EQUAL( A, i1[0] );
449   CPPUNIT_ASSERT_EQUAL( 3, (int)i1[1].size() );
450   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 5, 0 ), i1[1][0] );
451   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 8, 1 ), i1[1][1] );
452   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 11, 2 ), i1[1][2] );
453
454   std::vector<HYDROData_DTM::CurveUZ> i2;
455   HYDROData_DTM::Interpolate( A, B, 1, i2, true );
456
457   CPPUNIT_ASSERT_EQUAL( 3, (int)i2.size() );
458   CPPUNIT_ASSERT_EQUAL( A, i2[0] );
459   CPPUNIT_ASSERT_EQUAL( 3, (int)i2[1].size() );
460   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 5, 0 ), i2[1][0] );
461   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 8, 1 ), i2[1][1] );
462   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 11, 2 ), i2[1][2] );
463   CPPUNIT_ASSERT_EQUAL( B, i2[2] );
464
465   std::vector<HYDROData_DTM::CurveUZ> i3;
466   HYDROData_DTM::Interpolate( A, B, 3, i3, false );
467
468   CPPUNIT_ASSERT_EQUAL( 4, (int)i3.size() );
469   CPPUNIT_ASSERT_EQUAL( A, i3[0] );
470   CPPUNIT_ASSERT_EQUAL( 3, (int)i3[1].size() );
471   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 2.5, 0 ), i3[1][0] );
472   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 4.5, 1 ), i3[1][1] );
473   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::PointUZ( 6.5, 2 ), i3[1][2] );
474 }
475
476 void test_HYDROData_DTM::test_curve_to_3d()
477 {
478   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
479
480   Handle(HYDROData_Profile) aProfile1 = 
481     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
482
483   Handle(HYDROData_Profile) aProfile2 = 
484     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
485
486   aProfile1->SetParametricPoints( points );
487   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
488   aProfile1->SetLeftPoint( gp_XY( 20, 0 ) );
489   aProfile1->SetRightPoint( gp_XY( 10, 10 ) );
490
491   aProfile2->SetParametricPoints( points );
492   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_POLYLINE );
493   aProfile2->SetLeftPoint( gp_XY( 100, 0 ) );
494   aProfile2->SetRightPoint( gp_XY( 110, 0 ) );
495
496   std::vector<double> distances;
497   std::vector<Handle(HYDROData_Profile)> profiles;
498   profiles.push_back( aProfile1 );
499   profiles.push_back( aProfile2 );
500
501   Handle(Geom2d_BSplineCurve) HA = HYDROData_DTM::CreateHydraulicAxis( profiles, distances );
502   HYDROData_DTM::AltitudePoints points;
503   HYDROData_DTM::CurveUZ mid( 5.0, gp_Vec2d(-10,10), 0, 10 );
504   mid.push_back( HYDROData_DTM::PointUZ( 0, 5 ) );
505   mid.push_back( HYDROData_DTM::PointUZ( 1, 6 ) );
506   HYDROData_DTM::CurveUZ wid( 5.0, gp_Vec2d(-10,10), 0, 10 );
507   wid.push_back( HYDROData_DTM::PointUZ( 2, 5 ) );
508   wid.push_back( HYDROData_DTM::PointUZ( 6, 6 ) );
509   HYDROData_DTM::CurveTo3D( HA, mid, wid, points );
510
511   CPPUNIT_ASSERT_EQUAL( 4, (int)points.size() );
512   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::AltitudePoint( 21.588, 5.419, 6.0 ), points[0] );
513   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::AltitudePoint( 20.881, 6.126, 5.0 ), points[1] );
514   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::AltitudePoint( 19.466, 7.541, 5.0 ), points[2] );
515   CPPUNIT_ASSERT_EQUAL( HYDROData_DTM::AltitudePoint( 17.345, 9.662, 6.0 ), points[3] );
516
517   aDoc->Close();
518 }
519
520 void test_HYDROData_DTM::test_presentation()
521 {
522   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
523
524   Handle(HYDROData_DTM) DTM = Handle(HYDROData_DTM)::DownCast( aDoc->CreateObject( KIND_DTM ) );
525
526   Handle(HYDROData_Profile) aProfile1 = 
527     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
528
529   Handle(HYDROData_Profile) aProfile2 = 
530     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
531
532   aProfile1->SetParametricPoints( points );
533   aProfile1->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_SPLINE );
534   aProfile1->SetLeftPoint( gp_XY( 10, 10 ) );
535   aProfile1->SetRightPoint( gp_XY( 20, 0 ) );
536
537   aProfile2->SetParametricPoints( points );
538   aProfile2->GetProfileUZ()->SetSectionType( 0, HYDROData_IPolyline::SECTION_SPLINE );
539   aProfile2->SetLeftPoint( gp_XY( 110, 10 ) );
540   aProfile2->SetRightPoint( gp_XY( 100, 0 ) );
541
542   HYDROData_SequenceOfObjects seq;
543   seq.Append( aProfile1 );
544   seq.Append( aProfile2 );
545   DTM->SetProfiles( seq );
546   DTM->SetDDZ( 0.1 );
547   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.1, DTM->GetDDZ(), EPS );
548   DTM->SetSpatialStep( 1.0 );
549   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, DTM->GetSpatialStep(), EPS );
550   DTM->Update();
551   
552   CPPUNIT_ASSERT_EQUAL( 9108, (int)DTM->GetAltitudePoints().size() ); 
553
554   Handle(AIS_InteractiveContext) aContext = TestViewer::context();
555   HYDROGUI_ShapeBathymetry* aBathPrs = new HYDROGUI_ShapeBathymetry( 0, aContext, DTM );
556   aBathPrs->update( true, false );
557   aBathPrs->RescaleDefault();
558
559   bool ColorScaleIsDisp = TestViewer::ColorScaleIsDisplayed();
560   TestViewer::showColorScale( true );
561   Handle(AIS_ColorScale) aCS = TestViewer::colorScale();
562   aCS->SetMin( 0.0 );
563   aCS->SetMax( 5.0 );
564   aCS->SetNumberOfIntervals( 10 );
565   aBathPrs->UpdateWithColorScale( aCS );
566   //QTest::qWait(50000);
567   QImage aDTMPrs = draw_DTM( aBathPrs, 0.5, 600, 600 );
568   CPPUNIT_ASSERT_IMAGES3( &aDTMPrs, "DTM_1", false );
569   delete aBathPrs;
570   TestViewer::showColorScale( ColorScaleIsDisp );
571
572   aDoc->Close();
573 }
574
575 void test_HYDROData_DTM::test_garonne()
576 {
577   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
578   
579   TCollection_AsciiString fname = REF_DATA_PATH.toLatin1().data();
580   fname += "/Profiles.xyz";
581   NCollection_Sequence<int> bad_ids;
582
583   int aSize = HYDROData_Profile::ImportFromFile( aDoc, fname, bad_ids, true );
584   
585   CPPUNIT_ASSERT_EQUAL( 0, bad_ids.Size() );
586   CPPUNIT_ASSERT_EQUAL( 46, aSize );
587
588   HYDROData_SequenceOfObjects profiles;
589   HYDROData_Iterator it( aDoc, KIND_PROFILE );
590   for( int i=0; it.More(); it.Next(), i++ )
591   {
592     if( i>=25 && i<=35 )
593       profiles.Append( Handle(HYDROData_Profile)::DownCast( it.Current() ) );
594   }
595
596   CPPUNIT_ASSERT_EQUAL( 11, (int)profiles.Size() );
597
598   Handle(HYDROData_DTM) DTM = Handle(HYDROData_DTM)::DownCast( aDoc->CreateObject( KIND_DTM ) );
599   DTM->SetProfiles( profiles );
600   DTM->SetDDZ( 0.1 );
601   DTM->SetSpatialStep( 1.0 );
602   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.1, DTM->GetDDZ(), EPS );
603   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, DTM->GetSpatialStep(), EPS );
604   DTM->Update();
605   
606   CPPUNIT_ASSERT_EQUAL( 281898, (int)DTM->GetAltitudePoints().size() ); 
607   
608   Handle(AIS_InteractiveContext) aContext = TestViewer::context();
609   HYDROGUI_ShapeBathymetry* aBathPrs = new HYDROGUI_ShapeBathymetry( 0, aContext, DTM );
610   aBathPrs->update( true, false );
611   aBathPrs->RescaleDefault();
612   
613   bool ColorScaleIsDisp = TestViewer::ColorScaleIsDisplayed();
614
615   TestViewer::showColorScale( true );
616   Handle(AIS_ColorScale) aCS = TestViewer::colorScale();
617   aCS->SetMin( 0.0 );
618   aCS->SetMax( 25.0 );
619   aCS->SetNumberOfIntervals( 30 );
620   aBathPrs->UpdateWithColorScale( aCS );
621   
622   QImage aDTMPrs = draw_DTM( aBathPrs, 0.5, 600, 600 );
623   CPPUNIT_ASSERT_IMAGES3( &aDTMPrs, "DTM_2", false );
624   TestViewer::showColorScale( ColorScaleIsDisp );
625   delete aBathPrs;
626   aDoc->Close();
627 }
628
629 void test_HYDROData_DTM::test_classifier_1()
630 {
631   TCollection_AsciiString fname = REF_DATA_PATH.toLatin1().data();
632   fname += "/pb_1066.cbf";
633   CPPUNIT_ASSERT_EQUAL( (int)DocError_OK, (int)HYDROData_Document::Load( fname.ToCString(), 1 ) );
634
635   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
636   
637   Handle(HYDROData_CalculationCase) aCase = 
638     Handle(HYDROData_CalculationCase)::DownCast( aDoc->FindObjectByName( "Case_1" ) );
639   CPPUNIT_ASSERT_EQUAL( false, aCase.IsNull() ); 
640   std::vector<gp_XY> points;
641   points.push_back( gp_XY( 43.4842, 3.33176  ) );
642   points.push_back( gp_XY( -125.777, 2.24728 ) );
643   points.push_back( gp_XY( -60.1628, 168.262 ) );
644   points.push_back( gp_XY( 21.8055587645, 154.699344457 ) );
645   points.push_back( gp_XY( -84.4764138524, 79.2606012276 ) );
646   points.push_back( gp_XY( -73.4132070504, 69.7096313266 ) );
647
648   std::vector<double> values = aCase->GetStricklerCoefficientForPoints( points, 0.0, true );
649   CPPUNIT_ASSERT_EQUAL( 6, (int)values.size() );
650   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0123, values[0], EPS );
651   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0123, values[1], EPS );
652   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0221, values[2], EPS );
653   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0221, values[3], EPS );
654   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0221, values[4], EPS );
655   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0221, values[5], EPS );
656
657   std::vector<int> types = aCase->GetStricklerTypeForPoints( points );
658   CPPUNIT_ASSERT_EQUAL( 6, (int)types.size() );
659   CPPUNIT_ASSERT_EQUAL( 123, types[0] );
660   CPPUNIT_ASSERT_EQUAL( 123, types[1] );
661   CPPUNIT_ASSERT_EQUAL( 221, types[2] );
662   CPPUNIT_ASSERT_EQUAL( 221, types[3] );
663   CPPUNIT_ASSERT_EQUAL( 221, types[4] );
664   CPPUNIT_ASSERT_EQUAL( 221, types[5] );
665
666   aDoc->Close();
667 }
668