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