Salome HOME
#refs 522 (very draft) //import only
[modules/hydro.git] / src / HYDROData / test_HYDROData_BSplineOperation.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include<test_HYDROData_BSplineOperation.h>
24
25 #include <HYDROData_BSplineOperation.h>
26 #include <gp_Pnt.hxx>
27 #include <QTransform>
28
29 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
30
31 void test_HYDROData_BSplineOperation::testCurve()
32 {
33   // prepare points: function of sin(x)
34   NCollection_Sequence<gp_XYZ> aPoints;
35   for ( double x = 0; x < 6.28; x += 0.1 )
36   {
37     gp_XYZ aPoint( x, sin( x ), 0.0 );
38     aPoints.Append( aPoint );
39   }
40   // compute BSpline
41   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
42   
43   Handle(Geom_BSplineCurve) aBS = aBSpline.Curve();
44   CPPUNIT_ASSERT( !aBS.IsNull() );
45   CPPUNIT_ASSERT( !aBS->IsClosed() );
46   CPPUNIT_ASSERT_EQUAL( aBS->Continuity(), GeomAbs_C2 );
47
48   // check that values of BSpline are not far from original "sin" function
49   // in all points of the curve
50   for ( double x = 0; x < 6.29; x += 0.001 )
51   {
52     double aDiff = aBS->Value( x ).Y() - sin( aBS->Value( x ).X() );
53     if ( aDiff < 0 ) aDiff = -aDiff;
54     CPPUNIT_ASSERT( aDiff < 3.e-6 ); // this number is found manually
55   }
56 }
57
58 void test_HYDROData_BSplineOperation::testPath()
59 {
60   // prepare points: function of sin(x)
61   static const double aScale = 10000000.;
62
63   NCollection_Sequence<gp_XYZ> aPoints;
64   for ( double x = 0; x < 6.28; x += 0.1 )
65   {
66     gp_XYZ aPoint( x * aScale, sin( x ) * aScale, 0.0 );
67     aPoints.Append( aPoint );
68   }
69
70   // convert to QPainterPath
71   HYDROData_BSplineOperation aBSpline( aPoints, false, LOCAL_SELECTION_TOLERANCE );
72
73   CPPUNIT_ASSERT( !aBSpline.Curve().IsNull() );
74
75   QPainterPath aPath;
76   aBSpline.ComputePath( aPath );
77   CPPUNIT_ASSERT( !aPath.isEmpty() );
78   
79   /*
80   QImage aPic(1300, 600, QImage::Format_RGB32);
81   QPainter aPainter(&aPic);
82   aPainter.setBrush(QBrush(Qt::white));
83   aPainter.drawPath(aPath);
84   aPic.save("pic.bmp");
85   */
86      
87   // check that values of Path are not far from original "sin" function
88   // in all points of the curve
89   QList<QPolygonF> aPolyF = aPath.toSubpathPolygons( QTransform() );
90   QList<QPolygonF>::iterator aFIter = aPolyF.begin();
91   for(; aFIter != aPolyF.end();aFIter++) {
92     QPolygon aPoly = aFIter->toPolygon();
93     QPolygon::iterator aPoints = aPoly.begin();
94     for(; aPoints != aPoly.end(); aPoints++) {
95       double aDiff = aPoints->y() / aScale - sin(aPoints->x() / aScale);
96       if (aDiff < 0) aDiff = -aDiff;
97       CPPUNIT_ASSERT(aDiff < 4.e-6); // this number is found manually
98     }
99   }
100 }