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