Salome HOME
Bug #487: dump/load script - problem with obstacle.
[modules/hydro.git] / src / HYDROData / test_HYDROData_BSplineOperation.cxx
1 #include<test_HYDROData_BSplineOperation.h>
2
3 #include <HYDROData_BSplineOperation.h>
4 #include <gp_Pnt.hxx>
5 #include <QTransform>
6
7 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
8
9 void test_HYDROData_BSplineOperation::testCurve()
10 {
11   // prepare points: function of sin(x)
12   NCollection_Sequence<gp_XYZ> aPoints;
13   for ( double x = 0; x < 6.28; x += 0.1 )
14   {
15     gp_XYZ aPoint( x, sin( x ), 0.0 );
16     aPoints.Append( aPoint );
17   }
18   // compute BSpline
19   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
20   
21   Handle(Geom_BSplineCurve) aBS = aBSpline.Curve();
22   CPPUNIT_ASSERT( !aBS.IsNull() );
23   CPPUNIT_ASSERT( !aBS->IsClosed() );
24   CPPUNIT_ASSERT_EQUAL( aBS->Continuity(), GeomAbs_C2 );
25
26   // check that values of BSpline are not far from original "sin" function
27   // in all points of the curve
28   for ( double x = 0; x < 6.29; x += 0.001 )
29   {
30     double aDiff = aBS->Value( x ).Y() - sin( aBS->Value( x ).X() );
31     if ( aDiff < 0 ) aDiff = -aDiff;
32     CPPUNIT_ASSERT( aDiff < 3.e-6 ); // this number is found manually
33   }
34 }
35
36 void test_HYDROData_BSplineOperation::testPath()
37 {
38   // prepare points: function of sin(x)
39   static const double aScale = 10000000.;
40
41   NCollection_Sequence<gp_XYZ> aPoints;
42   for ( double x = 0; x < 6.28; x += 0.1 )
43   {
44     gp_XYZ aPoint( x * aScale, sin( x ) * aScale, 0.0 );
45     aPoints.Append( aPoint );
46   }
47
48   // convert to QPainterPath
49   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
50
51   CPPUNIT_ASSERT( !aBSpline.Curve().IsNull() );
52
53   QPainterPath aPath;
54   aBSpline.ComputePath( aPath );
55   CPPUNIT_ASSERT( !aPath.isEmpty() );
56   
57   /*
58   QImage aPic(1300, 600, QImage::Format_RGB32);
59   QPainter aPainter(&aPic);
60   aPainter.setBrush(QBrush(Qt::white));
61   aPainter.drawPath(aPath);
62   aPic.save("pic.bmp");
63   */
64      
65   // check that values of Path are not far from original "sin" function
66   // in all points of the curve
67   QList<QPolygonF> aPolyF = aPath.toSubpathPolygons( QTransform() );
68   QList<QPolygonF>::iterator aFIter = aPolyF.begin();
69   for(; aFIter != aPolyF.end();aFIter++) {
70     QPolygon aPoly = aFIter->toPolygon();
71     QPolygon::iterator aPoints = aPoly.begin();
72     for(; aPoints != aPoly.end(); aPoints++) {
73       double aDiff = aPoints->y() / aScale - sin(aPoints->x() / aScale);
74       if (aDiff < 0) aDiff = -aDiff;
75       CPPUNIT_ASSERT(aDiff < 4.e-6); // this number is found manually
76     }
77   }
78 }