Salome HOME
Merge branch 'BR_v14_rc' into BR_SINUSX_FORMAT
[modules/hydro.git] / src / HYDROData / test_HYDROData_BSplineOperation.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_BSplineOperation.h>
20
21 #include <HYDROData_BSplineOperation.h>
22 #include <gp_Pnt.hxx>
23 #include <QTransform>
24
25 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
26
27 void test_HYDROData_BSplineOperation::testCurve()
28 {
29   // prepare points: function of sin(x)
30   NCollection_Sequence<gp_XYZ> aPoints;
31   for ( double x = 0; x < 6.28; x += 0.1 )
32   {
33     gp_XYZ aPoint( x, sin( x ), 0.0 );
34     aPoints.Append( aPoint );
35   }
36   // compute BSpline
37   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
38   
39   Handle(Geom_BSplineCurve) aBS = aBSpline.Curve();
40   CPPUNIT_ASSERT( !aBS.IsNull() );
41   CPPUNIT_ASSERT( !aBS->IsClosed() );
42   CPPUNIT_ASSERT_EQUAL( aBS->Continuity(), GeomAbs_C2 );
43
44   // check that values of BSpline are not far from original "sin" function
45   // in all points of the curve
46   for ( double x = 0; x < 6.29; x += 0.001 )
47   {
48     double aDiff = aBS->Value( x ).Y() - sin( aBS->Value( x ).X() );
49     if ( aDiff < 0 ) aDiff = -aDiff;
50     CPPUNIT_ASSERT( aDiff < 3.e-6 ); // this number is found manually
51   }
52 }
53
54 void test_HYDROData_BSplineOperation::testPath()
55 {
56   // prepare points: function of sin(x)
57   static const double aScale = 10000000.;
58
59   NCollection_Sequence<gp_XYZ> aPoints;
60   for ( double x = 0; x < 6.28; x += 0.1 )
61   {
62     gp_XYZ aPoint( x * aScale, sin( x ) * aScale, 0.0 );
63     aPoints.Append( aPoint );
64   }
65
66   // convert to QPainterPath
67   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
68
69   CPPUNIT_ASSERT( !aBSpline.Curve().IsNull() );
70
71   QPainterPath aPath;
72   aBSpline.ComputePath( aPath );
73   CPPUNIT_ASSERT( !aPath.isEmpty() );
74   
75   /*
76   QImage aPic(1300, 600, QImage::Format_RGB32);
77   QPainter aPainter(&aPic);
78   aPainter.setBrush(QBrush(Qt::white));
79   aPainter.drawPath(aPath);
80   aPic.save("pic.bmp");
81   */
82      
83   // check that values of Path are not far from original "sin" function
84   // in all points of the curve
85   QList<QPolygonF> aPolyF = aPath.toSubpathPolygons( QTransform() );
86   QList<QPolygonF>::iterator aFIter = aPolyF.begin();
87   for(; aFIter != aPolyF.end();aFIter++) {
88     QPolygon aPoly = aFIter->toPolygon();
89     QPolygon::iterator aPoints = aPoly.begin();
90     for(; aPoints != aPoly.end(); aPoints++) {
91       double aDiff = aPoints->y() / aScale - sin(aPoints->x() / aScale);
92       if (aDiff < 0) aDiff = -aDiff;
93       CPPUNIT_ASSERT(aDiff < 4.e-6); // this number is found manually
94     }
95   }
96 }