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