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