Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_spline.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_spline.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_spline.cpp
r8127 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 9 9 10 10 #include "qwt_spline.h" 11 #include "qwt_math.h" 11 #include "qwt_spline_parametrization.h" 12 #include "qwt_spline_polynomial.h" 13 #include "qwt_bezier.h" 14 15 #include <qpainterpath.h> 16 17 namespace QwtSplineC1P 18 { 19 struct param 20 { 21 param( const QwtSplineParametrization* p ): 22 parameter( p ) 23 { 24 } 25 26 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 27 { 28 return parameter->valueIncrement( p1, p2 ); 29 } 30 31 const QwtSplineParametrization* parameter; 32 }; 33 34 struct paramY 35 { 36 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 37 { 38 return QwtSplineParametrization::valueIncrementY( p1, p2 ); 39 } 40 }; 41 42 struct paramUniform 43 { 44 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 45 { 46 return QwtSplineParametrization::valueIncrementUniform( p1, p2 ); 47 } 48 }; 49 50 struct paramCentripetal 51 { 52 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 53 { 54 return QwtSplineParametrization::valueIncrementCentripetal( p1, p2 ); 55 } 56 }; 57 58 struct paramChordal 59 { 60 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 61 { 62 return QwtSplineParametrization::valueIncrementChordal( p1, p2 ); 63 } 64 }; 65 66 struct paramManhattan 67 { 68 inline double operator()( const QPointF& p1, const QPointF& p2 ) const 69 { 70 return QwtSplineParametrization::valueIncrementManhattan( p1, p2 ); 71 } 72 }; 73 74 class PathStore 75 { 76 public: 77 inline void init( int size ) 78 { 79 Q_UNUSED(size); 80 } 81 82 inline void start( double x1, double y1 ) 83 { 84 path.moveTo( x1, y1 ); 85 } 86 87 inline void addCubic( double cx1, double cy1, 88 double cx2, double cy2, double x2, double y2 ) 89 { 90 path.cubicTo( cx1, cy1, cx2, cy2, x2, y2 ); 91 } 92 93 inline void end() 94 { 95 path.closeSubpath(); 96 } 97 98 QPainterPath path; 99 }; 100 101 class ControlPointsStore 102 { 103 public: 104 inline ControlPointsStore(): 105 m_cp( NULL ) 106 { 107 } 108 109 inline void init( int size ) 110 { 111 controlPoints.resize( size ); 112 m_cp = controlPoints.data(); 113 } 114 115 inline void start( double x1, double y1 ) 116 { 117 Q_UNUSED( x1 ); 118 Q_UNUSED( y1 ); 119 } 120 121 inline void addCubic( double cx1, double cy1, 122 double cx2, double cy2, double x2, double y2 ) 123 { 124 Q_UNUSED( x2 ); 125 Q_UNUSED( y2 ); 126 127 QLineF& l = *m_cp++; 128 l.setLine( cx1, cy1, cx2, cy2 ); 129 } 130 131 inline void end() 132 { 133 } 134 135 QVector< QLineF > controlPoints; 136 137 private: 138 QLineF* m_cp; 139 }; 140 141 double slopeBoundary( int boundaryCondition, double boundaryValue, 142 const QPointF& p1, const QPointF& p2, double slope1 ) 143 { 144 const double dx = p2.x() - p1.x(); 145 const double dy = p2.y() - p1.y(); 146 147 double m = 0.0; 148 149 switch( boundaryCondition ) 150 { 151 case QwtSpline::Clamped1: 152 { 153 m = boundaryValue; 154 break; 155 } 156 case QwtSpline::Clamped2: 157 { 158 const double c2 = 0.5 * boundaryValue; 159 const double c1 = slope1; 160 161 m = 0.5 * ( 3.0 * dy / dx - c1 - c2 * dx ); 162 break; 163 } 164 case QwtSpline::Clamped3: 165 { 166 const double c3 = boundaryValue / 6.0; 167 m = c3 * dx * dx + 2 * dy / dx - slope1; 168 break; 169 } 170 case QwtSpline::LinearRunout: 171 { 172 const double s = dy / dx; 173 const double r = qBound( 0.0, boundaryValue, 1.0 ); 174 175 m = s - r * ( s - slope1 ); 176 break; 177 } 178 default: 179 { 180 m = dy / dx; // something 181 } 182 } 183 184 return m; 185 } 186 } 187 188 template< class SplineStore > 189 static inline SplineStore qwtSplineC1PathParamX( 190 const QwtSplineC1* spline, const QPolygonF& points ) 191 { 192 const int n = points.size(); 193 194 const QVector< double > m = spline->slopes( points ); 195 if ( m.size() != n ) 196 return SplineStore(); 197 198 const QPointF* pd = points.constData(); 199 const double* md = m.constData(); 200 201 SplineStore store; 202 store.init( m.size() - 1 ); 203 store.start( pd[0].x(), pd[0].y() ); 204 205 for ( int i = 0; i < n - 1; i++ ) 206 { 207 const double dx3 = ( pd[i + 1].x() - pd[i].x() ) / 3.0; 208 209 store.addCubic( pd[i].x() + dx3, pd[i].y() + md[i] * dx3, 210 pd[i + 1].x() - dx3, pd[i + 1].y() - md[i + 1] * dx3, 211 pd[i + 1].x(), pd[i + 1].y() ); 212 } 213 214 return store; 215 } 216 217 template< class SplineStore > 218 static inline SplineStore qwtSplineC1PathParamY( 219 const QwtSplineC1* spline, const QPolygonF& points ) 220 { 221 const int n = points.size(); 222 223 QPolygonF pointsFlipped( n ); 224 for ( int i = 0; i < n; i++ ) 225 { 226 pointsFlipped[i].setX( points[i].y() ); 227 pointsFlipped[i].setY( points[i].x() ); 228 } 229 230 const QVector< double > m = spline->slopes( pointsFlipped ); 231 if ( m.size() != n ) 232 return SplineStore(); 233 234 const QPointF* pd = pointsFlipped.constData(); 235 const double* md = m.constData(); 236 237 SplineStore store; 238 store.init( m.size() - 1 ); 239 store.start( pd[0].y(), pd[0].x() ); 240 241 QVector< QLineF > lines( n ); 242 for ( int i = 0; i < n - 1; i++ ) 243 { 244 const double dx3 = ( pd[i + 1].x() - pd[i].x() ) / 3.0; 245 246 store.addCubic( pd[i].y() + md[i] * dx3, pd[i].x() + dx3, 247 pd[i + 1].y() - md[i + 1] * dx3, pd[i + 1].x() - dx3, 248 pd[i + 1].y(), pd[i + 1].x() ); 249 } 250 251 return store; 252 } 253 254 template< class SplineStore, class Param > 255 static inline SplineStore qwtSplineC1PathParametric( 256 const QwtSplineC1* spline, const QPolygonF& points, Param param ) 257 { 258 const bool isClosing = ( spline->boundaryType() == QwtSpline::ClosedPolygon ); 259 const int n = points.size(); 260 261 QPolygonF pointsX, pointsY; 262 pointsX.resize( isClosing ? n + 1 : n ); 263 pointsY.resize( isClosing ? n + 1 : n ); 264 265 QPointF* px = pointsX.data(); 266 QPointF* py = pointsY.data(); 267 const QPointF* p = points.constData(); 268 269 double t = 0.0; 270 271 px[0].rx() = py[0].rx() = t; 272 px[0].ry() = p[0].x(); 273 py[0].ry() = p[0].y(); 274 275 int numParamPoints = 1; 276 for ( int i = 1; i < n; i++ ) 277 { 278 const double td = param( points[i - 1], points[i] ); 279 if ( td > 0.0 ) 280 { 281 t += td; 282 283 px[numParamPoints].rx() = py[numParamPoints].rx() = t; 284 285 px[numParamPoints].ry() = p[i].x(); 286 py[numParamPoints].ry() = p[i].y(); 287 288 numParamPoints++; 289 } 290 } 291 292 if ( isClosing ) 293 { 294 const double td = param( points[n - 1], points[0] ); 295 296 if ( td > 0.0 ) 297 { 298 t += td; 299 300 px[numParamPoints].rx() = py[numParamPoints].rx() = t; 301 302 px[numParamPoints].ry() = p[0].x(); 303 py[numParamPoints].ry() = p[0].y(); 304 305 numParamPoints++; 306 } 307 } 308 309 if ( pointsX.size() != numParamPoints ) 310 { 311 pointsX.resize( numParamPoints ); 312 pointsY.resize( numParamPoints ); 313 } 314 315 const QVector< double > slopesX = spline->slopes( pointsX ); 316 const QVector< double > slopesY = spline->slopes( pointsY ); 317 318 const double* mx = slopesX.constData(); 319 const double* my = slopesY.constData(); 320 321 // we don't need it anymore 322 pointsX.clear(); 323 pointsY.clear(); 324 325 SplineStore store; 326 store.init( isClosing ? n : n - 1 ); 327 store.start( points[0].x(), points[0].y() ); 328 329 int j = 0; 330 331 for ( int i = 0; i < n - 1; i++ ) 332 { 333 const QPointF& p1 = p[i]; 334 const QPointF& p2 = p[i + 1]; 335 336 const double td = param( p1, p2 ); 337 338 if ( td != 0.0 ) 339 { 340 const double t3 = td / 3.0; 341 342 const double cx1 = p1.x() + mx[j] * t3; 343 const double cy1 = p1.y() + my[j] * t3; 344 345 const double cx2 = p2.x() - mx[j + 1] * t3; 346 const double cy2 = p2.y() - my[j + 1] * t3; 347 348 store.addCubic( cx1, cy1, cx2, cy2, p2.x(), p2.y() ); 349 350 j++; 351 } 352 else 353 { 354 // setting control points to the ends 355 store.addCubic( p1.x(), p1.y(), p2.x(), p2.y(), p2.x(), p2.y() ); 356 } 357 } 358 359 if ( isClosing ) 360 { 361 const QPointF& p1 = p[n - 1]; 362 const QPointF& p2 = p[0]; 363 364 const double td = param( p1, p2 ); 365 366 if ( td != 0.0 ) 367 { 368 const double t3 = td / 3.0; 369 370 const double cx1 = p1.x() + mx[j] * t3; 371 const double cy1 = p1.y() + my[j] * t3; 372 373 const double cx2 = p2.x() - mx[0] * t3; 374 const double cy2 = p2.y() - my[0] * t3; 375 376 store.addCubic( cx1, cy1, cx2, cy2, p2.x(), p2.y() ); 377 } 378 else 379 { 380 store.addCubic( p1.x(), p1.y(), p2.x(), p2.y(), p2.x(), p2.y() ); 381 } 382 383 store.end(); 384 } 385 386 return store; 387 } 388 389 template< QwtSplinePolynomial toPolynomial( const QPointF&, double, const QPointF&, double ) > 390 static QPolygonF qwtPolygonParametric( double distance, 391 const QPolygonF& points, const QVector< double >& values, bool withNodes ) 392 { 393 QPolygonF fittedPoints; 394 395 const QPointF* p = points.constData(); 396 const double* v = values.constData(); 397 398 fittedPoints += p[0]; 399 double t = distance; 400 401 const int n = points.size(); 402 403 for ( int i = 0; i < n - 1; i++ ) 404 { 405 const QPointF& p1 = p[i]; 406 const QPointF& p2 = p[i + 1]; 407 408 const QwtSplinePolynomial polynomial = toPolynomial( p1, v[i], p2, v[i + 1] ); 409 410 const double l = p2.x() - p1.x(); 411 412 while ( t < l ) 413 { 414 fittedPoints += QPointF( p1.x() + t, p1.y() + polynomial.valueAt( t ) ); 415 t += distance; 416 } 417 418 if ( withNodes ) 419 { 420 if ( qFuzzyCompare( fittedPoints.last().x(), p2.x() ) ) 421 fittedPoints.last() = p2; 422 else 423 fittedPoints += p2; 424 } 425 else 426 { 427 t -= l; 428 } 429 } 430 431 return fittedPoints; 432 } 12 433 13 434 class QwtSpline::PrivateData 14 435 { 15 public: 16 PrivateData(): 17 splineType( QwtSpline::Natural ) 18 { 19 } 20 21 QwtSpline::SplineType splineType; 22 23 // coefficient vectors 24 QVector<double> a; 25 QVector<double> b; 26 QVector<double> c; 27 28 // control points 29 QPolygonF points; 436 public: 437 PrivateData() 438 : boundaryType( QwtSpline::ConditionalBoundaries ) 439 { 440 parametrization = new QwtSplineParametrization( 441 QwtSplineParametrization::ParameterChordal ); 442 443 // parabolic runout at both ends 444 445 boundaryConditions[0].type = QwtSpline::Clamped3; 446 boundaryConditions[0].value = 0.0; 447 448 boundaryConditions[1].type = QwtSpline::Clamped3; 449 boundaryConditions[1].value = 0.0; 450 } 451 452 ~PrivateData() 453 { 454 delete parametrization; 455 } 456 457 QwtSplineParametrization* parametrization; 458 QwtSpline::BoundaryType boundaryType; 459 460 struct 461 { 462 int type; 463 double value; 464 465 } boundaryConditions[2]; 30 466 }; 31 467 32 static int lookup( double x, const QPolygonF &values ) 33 { 34 #if 0 35 //qLowerBound/qHigherBound ??? 36 #endif 37 int i1; 38 const int size = values.size(); 39 40 if ( x <= values[0].x() ) 41 i1 = 0; 42 else if ( x >= values[size - 2].x() ) 43 i1 = size - 2; 44 else 45 { 46 i1 = 0; 47 int i2 = size - 2; 48 int i3 = 0; 49 50 while ( i2 - i1 > 1 ) 51 { 52 i3 = i1 + ( ( i2 - i1 ) >> 1 ); 53 54 if ( values[i3].x() > x ) 55 i2 = i3; 56 else 57 i1 = i3; 58 } 59 } 60 return i1; 61 } 62 63 //! Constructor 468 /*! 469 \fn QPainterPath QwtSpline::painterPath( const QPolygonF &points ) const 470 471 Approximates a polygon piecewise with cubic Bezier curves 472 and returns them as QPainterPath. 473 474 \param points Control points 475 \return Painter path, that can be rendered by QPainter 476 477 \sa polygon(), QwtBezier 478 */ 479 480 /*! 481 \brief Interpolate a curve by a polygon 482 483 Interpolates a polygon piecewise with Bezier curves 484 interpolating them in a 2nd pass by polygons. 485 486 The interpolation is based on "Piecewise Linear Approximation of Bézier Curves" 487 by Roger Willcocks ( http://www.rops.org ) 488 489 \param points Control points 490 \param tolerance Maximum for the accepted error of the approximation 491 492 \return polygon approximating the interpolating polynomials 493 494 \sa bezierControlLines(), QwtBezier 495 */ 496 QPolygonF QwtSpline::polygon( const QPolygonF& points, double tolerance ) const 497 { 498 if ( tolerance <= 0.0 ) 499 return QPolygonF(); 500 501 const QPainterPath path = painterPath( points ); 502 const int n = path.elementCount(); 503 if ( n == 0 ) 504 return QPolygonF(); 505 506 const QPainterPath::Element el = path.elementAt( 0 ); 507 if ( el.type != QPainterPath::MoveToElement ) 508 return QPolygonF(); 509 510 QPointF p1( el.x, el.y ); 511 512 QPolygonF polygon; 513 QwtBezier bezier( tolerance ); 514 515 for ( int i = 1; i < n; i += 3 ) 516 { 517 const QPainterPath::Element el1 = path.elementAt( i ); 518 const QPainterPath::Element el2 = path.elementAt( i + 1 ); 519 const QPainterPath::Element el3 = path.elementAt( i + 2 ); 520 521 const QPointF cp1( el1.x, el1.y ); 522 const QPointF cp2( el2.x, el2.y ); 523 const QPointF p2( el3.x, el3.y ); 524 525 bezier.appendToPolygon( p1, cp1, cp2, p2, polygon ); 526 527 p1 = p2; 528 } 529 530 return polygon; 531 } 532 533 /*! 534 \brief Constructor 535 536 The default setting is a non closing spline with chordal parametrization 537 538 \sa setParametrization(), setBoundaryType() 539 */ 64 540 QwtSpline::QwtSpline() 65 541 { 66 d_data = new PrivateData; 67 } 68 69 /*! 70 Copy constructor 71 \param other Spline used for initialization 72 */ 73 QwtSpline::QwtSpline( const QwtSpline& other ) 74 { 75 d_data = new PrivateData( *other.d_data ); 76 } 77 78 /*! 79 Assignment operator 80 \param other Spline used for initialization 81 \return *this 82 */ 83 QwtSpline &QwtSpline::operator=( const QwtSpline & other ) 84 { 85 *d_data = *other.d_data; 86 return *this; 542 m_data = new PrivateData; 87 543 } 88 544 … … 90 546 QwtSpline::~QwtSpline() 91 547 { 92 delete d_data; 93 } 94 95 /*! 96 Select the algorithm used for calculating the spline 97 98 \param splineType Spline type 99 \sa splineType() 100 */ 101 void QwtSpline::setSplineType( SplineType splineType ) 102 { 103 d_data->splineType = splineType; 104 } 105 106 /*! 107 \return the spline type 108 \sa setSplineType() 109 */ 110 QwtSpline::SplineType QwtSpline::splineType() const 111 { 112 return d_data->splineType; 113 } 114 115 /*! 116 \brief Calculate the spline coefficients 117 118 Depending on the value of \a periodic, this function 119 will determine the coefficients for a natural or a periodic 120 spline and store them internally. 121 122 \param points Points 123 \return true if successful 124 \warning The sequence of x (but not y) values has to be strictly monotone 125 increasing, which means <code>points[i].x() < points[i+1].x()</code>. 126 If this is not the case, the function will return false 127 */ 128 bool QwtSpline::setPoints( const QPolygonF& points ) 129 { 130 const int size = points.size(); 131 if ( size <= 2 ) 132 { 133 reset(); 134 return false; 135 } 136 137 d_data->points = points; 138 139 d_data->a.resize( size - 1 ); 140 d_data->b.resize( size - 1 ); 141 d_data->c.resize( size - 1 ); 142 143 bool ok; 144 if ( d_data->splineType == Periodic ) 145 ok = buildPeriodicSpline( points ); 146 else 147 ok = buildNaturalSpline( points ); 148 149 if ( !ok ) 150 reset(); 151 152 return ok; 153 } 154 155 /*! 156 \return Points, that have been by setPoints() 157 */ 158 QPolygonF QwtSpline::points() const 159 { 160 return d_data->points; 161 } 162 163 //! \return A coefficients 164 const QVector<double> &QwtSpline::coefficientsA() const 165 { 166 return d_data->a; 167 } 168 169 //! \return B coefficients 170 const QVector<double> &QwtSpline::coefficientsB() const 171 { 172 return d_data->b; 173 } 174 175 //! \return C coefficients 176 const QVector<double> &QwtSpline::coefficientsC() const 177 { 178 return d_data->c; 179 } 180 181 182 //! Free allocated memory and set size to 0 183 void QwtSpline::reset() 184 { 185 d_data->a.resize( 0 ); 186 d_data->b.resize( 0 ); 187 d_data->c.resize( 0 ); 188 d_data->points.resize( 0 ); 189 } 190 191 //! True if valid 192 bool QwtSpline::isValid() const 193 { 194 return d_data->a.size() > 0; 195 } 196 197 /*! 198 Calculate the interpolated function value corresponding 199 to a given argument x. 200 201 \param x Coordinate 202 \return Interpolated coordinate 203 */ 204 double QwtSpline::value( double x ) const 205 { 206 if ( d_data->a.size() == 0 ) 548 delete m_data; 549 } 550 551 /*! 552 The locality of an spline interpolation identifies how many adjacent 553 polynomials are affected, when changing the position of one point. 554 555 A locality of 'n' means, that changing the coordinates of a point 556 has an effect on 'n' leading and 'n' following polynomials. 557 Those polynomials can be calculated from a local subpolygon. 558 559 A value of 0 means, that the interpolation is not local and any modification 560 of the polygon requires to recalculate all polynomials ( f.e cubic splines ). 561 562 \return Order of locality 563 */ 564 uint QwtSpline::locality() const 565 { 566 return 0; 567 } 568 569 /*! 570 Define the parametrization for a parametric spline approximation 571 The default setting is a chordal parametrization. 572 573 \param type Type of parametrization, usually one of QwtSplineParametrization::Type 574 \sa parametrization() 575 */ 576 void QwtSpline::setParametrization( int type ) 577 { 578 if ( m_data->parametrization->type() != type ) 579 { 580 delete m_data->parametrization; 581 m_data->parametrization = new QwtSplineParametrization( type ); 582 } 583 } 584 585 /*! 586 Define the parametrization for a parametric spline approximation 587 The default setting is a chordal parametrization. 588 589 \param parametrization Parametrization 590 \sa parametrization() 591 */ 592 void QwtSpline::setParametrization( QwtSplineParametrization* parametrization ) 593 { 594 if ( ( parametrization != NULL ) && ( m_data->parametrization != parametrization ) ) 595 { 596 delete m_data->parametrization; 597 m_data->parametrization = parametrization; 598 } 599 } 600 601 /*! 602 \return parametrization 603 \sa setParametrization() 604 */ 605 const QwtSplineParametrization* QwtSpline::parametrization() const 606 { 607 return m_data->parametrization; 608 } 609 610 /*! 611 Define the boundary type for the endpoints of the approximating 612 spline. 613 614 \param boundaryType Boundary type 615 \sa boundaryType() 616 */ 617 void QwtSpline::setBoundaryType( BoundaryType boundaryType ) 618 { 619 m_data->boundaryType = boundaryType; 620 } 621 622 /*! 623 \return Boundary type 624 \sa setBoundaryType() 625 */ 626 QwtSpline::BoundaryType QwtSpline::boundaryType() const 627 { 628 return m_data->boundaryType; 629 } 630 631 /*! 632 \brief Define the condition for an endpoint of the spline 633 634 \param position At the beginning or the end of the spline 635 \param condition Condition 636 637 \sa BoundaryCondition, QwtSplineC2::BoundaryCondition, boundaryCondition() 638 */ 639 void QwtSpline::setBoundaryCondition( BoundaryPosition position, int condition ) 640 { 641 if ( ( position == QwtSpline::AtBeginning ) || ( position == QwtSpline::AtEnd ) ) 642 m_data->boundaryConditions[position].type = condition; 643 } 644 645 /*! 646 \return Condition for an endpoint of the spline 647 \param position At the beginning or the end of the spline 648 649 \sa setBoundaryCondition(), boundaryValue(), setBoundaryConditions() 650 */ 651 int QwtSpline::boundaryCondition( BoundaryPosition position ) const 652 { 653 if ( ( position == QwtSpline::AtBeginning ) || ( position == QwtSpline::AtEnd ) ) 654 return m_data->boundaryConditions[position].type; 655 656 return m_data->boundaryConditions[0].type; // should never happen 657 } 658 659 /*! 660 \brief Define the boundary value 661 662 The boundary value is an parameter used in combination with 663 the boundary condition. Its meaning depends on the condition. 664 665 \param position At the beginning or the end of the spline 666 \param value Value used for the condition at the end point 667 668 \sa boundaryValue(), setBoundaryCondition() 669 */ 670 void QwtSpline::setBoundaryValue( BoundaryPosition position, double value ) 671 { 672 if ( ( position == QwtSpline::AtBeginning ) || ( position == QwtSpline::AtEnd ) ) 673 m_data->boundaryConditions[position].value = value; 674 } 675 676 /*! 677 \return Boundary value 678 \param position At the beginning or the end of the spline 679 680 \sa setBoundaryValue(), boundaryCondition() 681 */ 682 double QwtSpline::boundaryValue( BoundaryPosition position ) const 683 { 684 if ( ( position == QwtSpline::AtBeginning ) || ( position == QwtSpline::AtEnd ) ) 685 return m_data->boundaryConditions[position].value; 686 687 return m_data->boundaryConditions[0].value; // should never happen 688 } 689 690 /*! 691 \brief Define the condition at the endpoints of a spline 692 693 \param condition Condition 694 \param valueBegin Used for the condition at the beginning of te spline 695 \param valueEnd Used for the condition at the end of te spline 696 697 \sa BoundaryCondition, QwtSplineC2::BoundaryCondition, 698 testBoundaryCondition(), setBoundaryValue() 699 */ 700 void QwtSpline::setBoundaryConditions( 701 int condition, double valueBegin, double valueEnd ) 702 { 703 setBoundaryCondition( QwtSpline::AtBeginning, condition ); 704 setBoundaryValue( QwtSpline::AtBeginning, valueBegin ); 705 706 setBoundaryCondition( QwtSpline::AtEnd, condition ); 707 setBoundaryValue( QwtSpline::AtEnd, valueEnd ); 708 } 709 710 //! \brief Constructor 711 QwtSplineInterpolating::QwtSplineInterpolating() 712 { 713 } 714 715 //! Destructor 716 QwtSplineInterpolating::~QwtSplineInterpolating() 717 { 718 } 719 720 /*! \fn QVector<QLineF> QwtSplineInterpolating::bezierControlLines( const QPolygonF &points ) const 721 722 \brief Interpolate a curve with Bezier curves 723 724 Interpolates a polygon piecewise with cubic Bezier curves 725 and returns the 2 control points of each curve as QLineF. 726 727 \param points Control points 728 \return Control points of the interpolating Bezier curves 729 */ 730 731 /*! 732 \brief Interpolate a curve with Bezier curves 733 734 Interpolates a polygon piecewise with cubic Bezier curves 735 and returns them as QPainterPath. 736 737 The implementation calculates the Bezier control lines first 738 and converts them into painter path elements in an additional loop. 739 740 \param points Control points 741 \return Painter path, that can be rendered by QPainter 742 743 \note Derived spline classes might overload painterPath() to avoid 744 the extra loops for converting results into a QPainterPath 745 746 \sa bezierControlLines() 747 */ 748 QPainterPath QwtSplineInterpolating::painterPath( const QPolygonF& points ) const 749 { 750 const int n = points.size(); 751 752 QPainterPath path; 753 if ( n == 0 ) 754 return path; 755 756 if ( n == 1 ) 757 { 758 path.moveTo( points[0] ); 759 return path; 760 } 761 762 if ( n == 2 ) 763 { 764 path.addPolygon( points ); 765 return path; 766 } 767 768 const QVector< QLineF > controlLines = bezierControlLines( points ); 769 if ( controlLines.size() < n - 1 ) 770 return path; 771 772 const QPointF* p = points.constData(); 773 const QLineF* l = controlLines.constData(); 774 775 path.moveTo( p[0] ); 776 for ( int i = 0; i < n - 1; i++ ) 777 path.cubicTo( l[i].p1(), l[i].p2(), p[i + 1] ); 778 779 if ( ( boundaryType() == QwtSpline::ClosedPolygon ) 780 && ( controlLines.size() >= n ) ) 781 { 782 path.cubicTo( l[n - 1].p1(), l[n - 1].p2(), p[0] ); 783 path.closeSubpath(); 784 } 785 786 return path; 787 } 788 789 /*! 790 \brief Interpolate a curve by a polygon 791 792 Interpolates a polygon piecewise with Bezier curves 793 approximating them by polygons. 794 795 The approximation is based on "Piecewise Linear Approximation of Bézier Curves" 796 by Roger Willcocks ( http://www.rops.org ) 797 798 \param points Control points 799 \param tolerance Maximum for the accepted error of the approximation 800 801 \return polygon approximating the interpolating polynomials 802 803 \sa bezierControlLines(), QwtSplineBezier::toPolygon() 804 */ 805 QPolygonF QwtSplineInterpolating::polygon( 806 const QPolygonF& points, double tolerance ) const 807 { 808 if ( tolerance <= 0.0 ) 809 return QPolygonF(); 810 811 const QVector< QLineF > controlLines = bezierControlLines( points ); 812 if ( controlLines.isEmpty() ) 813 return QPolygonF(); 814 815 const bool isClosed = boundaryType() == QwtSpline::ClosedPolygon; 816 817 QwtBezier bezier( tolerance ); 818 819 const QPointF* p = points.constData(); 820 const QLineF* cl = controlLines.constData(); 821 822 const int n = controlLines.size(); 823 824 QPolygonF polygon; 825 826 for ( int i = 0; i < n - 1; i++ ) 827 { 828 const QLineF& l = cl[i]; 829 bezier.appendToPolygon( p[i], l.p1(), l.p2(), p[i + 1], polygon ); 830 } 831 832 const QPointF& pn = isClosed ? p[0] : p[n]; 833 const QLineF& l = cl[n - 1]; 834 835 bezier.appendToPolygon( p[n - 1], l.p1(), l.p2(), pn, polygon ); 836 837 return polygon; 838 } 839 840 /*! 841 \brief Find an interpolated polygon with "equidistant" points 842 843 When withNodes is disabled all points of the resulting polygon 844 will be equidistant according to the parametrization. 845 846 When withNodes is enabled the resulting polygon will also include 847 the control points and the interpolated points are always aligned to 848 the control point before ( points[i] + i * distance ). 849 850 The implementation calculates bezier curves first and calculates 851 the interpolated points in a second run. 852 853 \param points Control nodes of the spline 854 \param distance Distance between 2 points according 855 to the parametrization 856 \param withNodes When true, also add the control 857 nodes ( even if not being equidistant ) 858 859 \return Interpolating polygon 860 861 \sa bezierControlLines() 862 */ 863 QPolygonF QwtSplineInterpolating::equidistantPolygon( const QPolygonF& points, 864 double distance, bool withNodes ) const 865 { 866 if ( distance <= 0.0 ) 867 return QPolygonF(); 868 869 const int n = points.size(); 870 if ( n <= 1 ) 871 return points; 872 873 if ( n == 2 ) 874 { 875 // TODO 876 return points; 877 } 878 879 QPolygonF path; 880 881 const QVector< QLineF > controlLines = bezierControlLines( points ); 882 883 if ( controlLines.size() < n - 1 ) 884 return path; 885 886 path += points.first(); 887 double t = distance; 888 889 const QPointF* p = points.constData(); 890 const QLineF* cl = controlLines.constData(); 891 892 const QwtSplineParametrization* param = parametrization(); 893 894 for ( int i = 0; i < n - 1; i++ ) 895 { 896 const double l = param->valueIncrement( p[i], p[i + 1] ); 897 898 while ( t < l ) 899 { 900 path += QwtBezier::pointAt( p[i], cl[i].p1(), 901 cl[i].p2(), p[i + 1], t / l ); 902 903 t += distance; 904 } 905 906 if ( withNodes ) 907 { 908 if ( qFuzzyCompare( path.last().x(), p[i + 1].x() ) ) 909 path.last() = p[i + 1]; 910 else 911 path += p[i + 1]; 912 913 t = distance; 914 } 915 else 916 { 917 t -= l; 918 } 919 } 920 921 if ( ( boundaryType() == QwtSpline::ClosedPolygon ) 922 && ( controlLines.size() >= n ) ) 923 { 924 const double l = param->valueIncrement( p[n - 1], p[0] ); 925 926 while ( t < l ) 927 { 928 path += QwtBezier::pointAt( p[n - 1], cl[n - 1].p1(), 929 cl[n - 1].p2(), p[0], t / l ); 930 931 t += distance; 932 } 933 934 if ( qFuzzyCompare( path.last().x(), p[0].x() ) ) 935 path.last() = p[0]; 936 else 937 path += p[0]; 938 } 939 940 return path; 941 } 942 943 //! Constructor 944 QwtSplineG1::QwtSplineG1() 945 { 946 } 947 948 //! Destructor 949 QwtSplineG1::~QwtSplineG1() 950 { 951 } 952 953 /*! 954 \brief Constructor 955 956 The default setting is a non closing spline with no parametrization 957 ( QwtSplineParametrization::ParameterX ). 958 959 \sa QwtSpline::setParametrization(), 960 QwtSpline::setBoundaryType() 961 */ 962 QwtSplineC1::QwtSplineC1() 963 { 964 setParametrization( QwtSplineParametrization::ParameterX ); 965 } 966 967 //! Destructor 968 QwtSplineC1::~QwtSplineC1() 969 { 970 } 971 972 /*! 973 \param points Control points 974 \param slopeNext Value of the first derivative at the second point 975 976 \return value of the first derivative at the first point 977 \sa slopeAtEnd(), QwtSpline::boundaryCondition(), QwtSpline::boundaryValue() 978 */ 979 double QwtSplineC1::slopeAtBeginning( const QPolygonF& points, double slopeNext ) const 980 { 981 if ( points.size() < 2 ) 207 982 return 0.0; 208 983 209 const int i = lookup( x, d_data->points ); 210 211 const double delta = x - d_data->points[i].x(); 212 return( ( ( ( d_data->a[i] * delta ) + d_data->b[i] ) 213 * delta + d_data->c[i] ) * delta + d_data->points[i].y() ); 214 } 215 216 /*! 217 \brief Determines the coefficients for a natural spline 218 \return true if successful 219 */ 220 bool QwtSpline::buildNaturalSpline( const QPolygonF &points ) 221 { 222 int i; 223 224 const QPointF *p = points.data(); 225 const int size = points.size(); 226 227 double *a = d_data->a.data(); 228 double *b = d_data->b.data(); 229 double *c = d_data->c.data(); 230 231 // set up tridiagonal equation system; use coefficient 232 // vectors as temporary buffers 233 QVector<double> h( size - 1 ); 234 for ( i = 0; i < size - 1; i++ ) 235 { 236 h[i] = p[i+1].x() - p[i].x(); 237 if ( h[i] <= 0 ) 238 return false; 239 } 240 241 QVector<double> d( size - 1 ); 242 double dy1 = ( p[1].y() - p[0].y() ) / h[0]; 243 for ( i = 1; i < size - 1; i++ ) 244 { 245 b[i] = c[i] = h[i]; 246 a[i] = 2.0 * ( h[i-1] + h[i] ); 247 248 const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i]; 249 d[i] = 6.0 * ( dy1 - dy2 ); 250 dy1 = dy2; 251 } 252 253 // 254 // solve it 255 // 256 257 // L-U Factorization 258 for ( i = 1; i < size - 2; i++ ) 259 { 260 c[i] /= a[i]; 261 a[i+1] -= b[i] * c[i]; 262 } 263 264 // forward elimination 265 QVector<double> s( size ); 266 s[1] = d[1]; 267 for ( i = 2; i < size - 1; i++ ) 268 s[i] = d[i] - c[i-1] * s[i-1]; 269 270 // backward elimination 271 s[size - 2] = - s[size - 2] / a[size - 2]; 272 for ( i = size - 3; i > 0; i-- ) 273 s[i] = - ( s[i] + b[i] * s[i+1] ) / a[i]; 274 s[size - 1] = s[0] = 0.0; 275 276 // 277 // Finally, determine the spline coefficients 278 // 279 for ( i = 0; i < size - 1; i++ ) 280 { 281 a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] ); 282 b[i] = 0.5 * s[i]; 283 c[i] = ( p[i+1].y() - p[i].y() ) / h[i] 284 - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0; 285 } 286 287 return true; 288 } 289 290 /*! 291 \brief Determines the coefficients for a periodic spline 292 \return true if successful 293 */ 294 bool QwtSpline::buildPeriodicSpline( const QPolygonF &points ) 295 { 296 int i; 297 298 const QPointF *p = points.data(); 299 const int size = points.size(); 300 301 double *a = d_data->a.data(); 302 double *b = d_data->b.data(); 303 double *c = d_data->c.data(); 304 305 QVector<double> d( size - 1 ); 306 QVector<double> h( size - 1 ); 307 QVector<double> s( size ); 308 309 // 310 // setup equation system; use coefficient 311 // vectors as temporary buffers 312 // 313 for ( i = 0; i < size - 1; i++ ) 314 { 315 h[i] = p[i+1].x() - p[i].x(); 316 if ( h[i] <= 0.0 ) 317 return false; 318 } 319 320 const int imax = size - 2; 321 double htmp = h[imax]; 322 double dy1 = ( p[0].y() - p[imax].y() ) / htmp; 323 for ( i = 0; i <= imax; i++ ) 324 { 325 b[i] = c[i] = h[i]; 326 a[i] = 2.0 * ( htmp + h[i] ); 327 const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i]; 328 d[i] = 6.0 * ( dy1 - dy2 ); 329 dy1 = dy2; 330 htmp = h[i]; 331 } 332 333 // 334 // solve it 335 // 336 337 // L-U Factorization 338 a[0] = qSqrt( a[0] ); 339 c[0] = h[imax] / a[0]; 340 double sum = 0; 341 342 for ( i = 0; i < imax - 1; i++ ) 343 { 344 b[i] /= a[i]; 345 if ( i > 0 ) 346 c[i] = - c[i-1] * b[i-1] / a[i]; 347 a[i+1] = qSqrt( a[i+1] - qwtSqr( b[i] ) ); 348 sum += qwtSqr( c[i] ); 349 } 350 b[imax-1] = ( b[imax-1] - c[imax-2] * b[imax-2] ) / a[imax-1]; 351 a[imax] = qSqrt( a[imax] - qwtSqr( b[imax-1] ) - sum ); 352 353 354 // forward elimination 355 s[0] = d[0] / a[0]; 356 sum = 0; 357 for ( i = 1; i < imax; i++ ) 358 { 359 s[i] = ( d[i] - b[i-1] * s[i-1] ) / a[i]; 360 sum += c[i-1] * s[i-1]; 361 } 362 s[imax] = ( d[imax] - b[imax-1] * s[imax-1] - sum ) / a[imax]; 363 364 365 // backward elimination 366 s[imax] = - s[imax] / a[imax]; 367 s[imax-1] = -( s[imax-1] + b[imax-1] * s[imax] ) / a[imax-1]; 368 for ( i = imax - 2; i >= 0; i-- ) 369 s[i] = - ( s[i] + b[i] * s[i+1] + c[i] * s[imax] ) / a[i]; 370 371 // 372 // Finally, determine the spline coefficients 373 // 374 s[size-1] = s[0]; 375 for ( i = 0; i < size - 1; i++ ) 376 { 377 a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] ); 378 b[i] = 0.5 * s[i]; 379 c[i] = ( p[i+1].y() - p[i].y() ) 380 / h[i] - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0; 381 } 382 383 return true; 384 } 984 return QwtSplineC1P::slopeBoundary( 985 boundaryCondition( QwtSpline::AtBeginning ), 986 boundaryValue( QwtSpline::AtBeginning ), 987 points[0], points[1], slopeNext ); 988 } 989 990 /*! 991 \param points Control points 992 \param slopeBefore Value of the first derivative at the point before the last one 993 994 \return value of the first derivative at the last point 995 \sa slopeAtBeginning(), QwtSpline::boundaryCondition(), QwtSpline::boundaryValue() 996 */ 997 double QwtSplineC1::slopeAtEnd( const QPolygonF& points, double slopeBefore ) const 998 { 999 const int n = points.size(); 1000 1001 const QPointF p1( points[n - 1].x(), -points[n - 1].y() ); 1002 const QPointF p2( points[n - 2].x(), -points[n - 2].y() ); 1003 1004 const int condition = boundaryCondition( QwtSpline::AtEnd ); 1005 1006 double value = boundaryValue( QwtSpline::AtEnd ); 1007 if ( condition != QwtSpline::LinearRunout ) 1008 { 1009 // beside LinearRunout the boundaryValue is a slope or curvature 1010 // and needs to be inverted too 1011 value = -value; 1012 } 1013 1014 const double slope = QwtSplineC1P::slopeBoundary( condition, value, p1, p2, -slopeBefore ); 1015 return -slope; 1016 } 1017 1018 /*! \fn QVector<double> QwtSplineC1::slopes( const QPolygonF &points ) const 1019 1020 \brief Find the first derivative at the control points 1021 1022 \param points Control nodes of the spline 1023 \return Vector with the values of the 2nd derivate at the control points 1024 1025 \note The x coordinates need to be increasing or decreasing 1026 */ 1027 1028 /*! 1029 \brief Calculate an interpolated painter path 1030 1031 Interpolates a polygon piecewise into cubic Bezier curves 1032 and returns them as QPainterPath. 1033 1034 The implementation calculates the slopes at the control points 1035 and converts them into painter path elements in an additional loop. 1036 1037 \param points Control points 1038 \return QPainterPath Painter path, that can be rendered by QPainter 1039 1040 \note Derived spline classes might overload painterPath() to avoid 1041 the extra loops for converting results into a QPainterPath 1042 */ 1043 QPainterPath QwtSplineC1::painterPath( const QPolygonF& points ) const 1044 { 1045 const int n = points.size(); 1046 if ( n <= 2 ) 1047 return QwtSplineInterpolating::painterPath( points ); 1048 1049 using namespace QwtSplineC1P; 1050 1051 PathStore store; 1052 switch( parametrization()->type() ) 1053 { 1054 case QwtSplineParametrization::ParameterX: 1055 { 1056 store = qwtSplineC1PathParamX< PathStore >( this, points ); 1057 break; 1058 } 1059 case QwtSplineParametrization::ParameterY: 1060 { 1061 store = qwtSplineC1PathParamY< PathStore >( this, points ); 1062 break; 1063 } 1064 case QwtSplineParametrization::ParameterUniform: 1065 { 1066 store = qwtSplineC1PathParametric< PathStore >( 1067 this, points, paramUniform() ); 1068 break; 1069 } 1070 case QwtSplineParametrization::ParameterCentripetal: 1071 { 1072 store = qwtSplineC1PathParametric< PathStore >( 1073 this, points, paramCentripetal() ); 1074 break; 1075 } 1076 case QwtSplineParametrization::ParameterChordal: 1077 { 1078 store = qwtSplineC1PathParametric< PathStore >( 1079 this, points, paramChordal() ); 1080 break; 1081 } 1082 default: 1083 { 1084 store = qwtSplineC1PathParametric< PathStore >( 1085 this, points, param( parametrization() ) ); 1086 } 1087 } 1088 1089 return store.path; 1090 } 1091 1092 /*! 1093 \brief Interpolate a curve with Bezier curves 1094 1095 Interpolates a polygon piecewise with cubic Bezier curves 1096 and returns the 2 control points of each curve as QLineF. 1097 1098 \param points Control points 1099 \return Control points of the interpolating Bezier curves 1100 */ 1101 QVector< QLineF > QwtSplineC1::bezierControlLines( const QPolygonF& points ) const 1102 { 1103 using namespace QwtSplineC1P; 1104 1105 const int n = points.size(); 1106 if ( n <= 2 ) 1107 return QVector< QLineF >(); 1108 1109 ControlPointsStore store; 1110 switch( parametrization()->type() ) 1111 { 1112 case QwtSplineParametrization::ParameterX: 1113 { 1114 store = qwtSplineC1PathParamX< ControlPointsStore >( this, points ); 1115 break; 1116 } 1117 case QwtSplineParametrization::ParameterY: 1118 { 1119 store = qwtSplineC1PathParamY< ControlPointsStore >( this, points ); 1120 break; 1121 } 1122 case QwtSplineParametrization::ParameterUniform: 1123 { 1124 store = qwtSplineC1PathParametric< ControlPointsStore >( 1125 this, points, paramUniform() ); 1126 break; 1127 } 1128 case QwtSplineParametrization::ParameterCentripetal: 1129 { 1130 store = qwtSplineC1PathParametric< ControlPointsStore >( 1131 this, points, paramCentripetal() ); 1132 break; 1133 } 1134 case QwtSplineParametrization::ParameterChordal: 1135 { 1136 store = qwtSplineC1PathParametric< ControlPointsStore >( 1137 this, points, paramChordal() ); 1138 break; 1139 } 1140 default: 1141 { 1142 store = qwtSplineC1PathParametric< ControlPointsStore >( 1143 this, points, param( parametrization() ) ); 1144 } 1145 } 1146 1147 return store.controlPoints; 1148 } 1149 1150 /*! 1151 \brief Find an interpolated polygon with "equidistant" points 1152 1153 The implementation is optimzed for non parametric curves 1154 ( QwtSplineParametrization::ParameterX ) and falls back to 1155 QwtSpline::equidistantPolygon() otherwise. 1156 1157 \param points Control nodes of the spline 1158 \param distance Distance between 2 points according 1159 to the parametrization 1160 \param withNodes When true, also add the control 1161 nodes ( even if not being equidistant ) 1162 1163 \return Interpolating polygon 1164 1165 \sa QwtSpline::equidistantPolygon() 1166 */ 1167 QPolygonF QwtSplineC1::equidistantPolygon( const QPolygonF& points, 1168 double distance, bool withNodes ) const 1169 { 1170 if ( parametrization()->type() == QwtSplineParametrization::ParameterX ) 1171 { 1172 if ( points.size() > 2 ) 1173 { 1174 const QVector< double > m = slopes( points ); 1175 if ( m.size() != points.size() ) 1176 return QPolygonF(); 1177 1178 return qwtPolygonParametric< QwtSplinePolynomial::fromSlopes >( 1179 distance, points, m, withNodes ); 1180 } 1181 } 1182 1183 return QwtSplineInterpolating::equidistantPolygon( points, distance, withNodes ); 1184 } 1185 1186 /*! 1187 \brief Calculate the interpolating polynomials for a non parametric spline 1188 1189 C1 spline interpolations are based on finding values for the first 1190 derivates at the control points. The interpolating polynomials can 1191 be calculated from the the first derivates using QwtSplinePolynomial::fromSlopes(). 1192 1193 The default implementation is a two pass calculation. In derived classes it 1194 might be overloaded by a one pass implementation. 1195 1196 \param points Control points 1197 \return Interpolating polynomials 1198 1199 \note The x coordinates need to be increasing or decreasing 1200 */ 1201 QVector< QwtSplinePolynomial > QwtSplineC1::polynomials( 1202 const QPolygonF& points ) const 1203 { 1204 QVector< QwtSplinePolynomial > polynomials; 1205 1206 const QVector< double > m = slopes( points ); 1207 if ( m.size() < 2 ) 1208 return polynomials; 1209 1210 polynomials.reserve( m.size() - 1 ); 1211 for ( int i = 1; i < m.size(); i++ ) 1212 { 1213 polynomials += QwtSplinePolynomial::fromSlopes( 1214 points[i - 1], m[i - 1], points[i], m[i] ); 1215 } 1216 1217 return polynomials; 1218 } 1219 1220 /*! 1221 \brief Constructor 1222 1223 The default setting is a non closing spline with no parametrization 1224 ( QwtSplineParametrization::ParameterX ). 1225 1226 \sa QwtSpline::setParametrization(), QwtSpline::setBoundaryType() 1227 */ 1228 QwtSplineC2::QwtSplineC2() 1229 { 1230 } 1231 1232 //! Destructor 1233 QwtSplineC2::~QwtSplineC2() 1234 { 1235 } 1236 1237 /*! 1238 \brief Interpolate a curve with Bezier curves 1239 1240 Interpolates a polygon piecewise with cubic Bezier curves 1241 and returns them as QPainterPath. 1242 1243 \param points Control points 1244 \return Painter path, that can be rendered by QPainter 1245 1246 \note The implementation simply calls QwtSplineC1::painterPath(), but is 1247 intended to be replaced by a one pass calculation some day. 1248 */ 1249 QPainterPath QwtSplineC2::painterPath( const QPolygonF& points ) const 1250 { 1251 // could be implemented from curvatures without the extra 1252 // loop for calculating the slopes vector. TODO ... 1253 1254 return QwtSplineC1::painterPath( points ); 1255 } 1256 1257 /*! 1258 \brief Interpolate a curve with Bezier curves 1259 1260 Interpolates a polygon piecewise with cubic Bezier curves 1261 and returns the 2 control points of each curve as QLineF. 1262 1263 \param points Control points 1264 \return Control points of the interpolating Bezier curves 1265 1266 \note The implementation simply calls QwtSplineC1::bezierControlLines(), 1267 but is intended to be replaced by a more efficient implementation 1268 that builds the polynomials by the curvatures some day. 1269 */ 1270 QVector< QLineF > QwtSplineC2::bezierControlLines( const QPolygonF& points ) const 1271 { 1272 // could be implemented from curvatures without the extra 1273 // loop for calculating the slopes vector. TODO ... 1274 1275 return QwtSplineC1::bezierControlLines( points ); 1276 } 1277 1278 /*! 1279 \brief Find an interpolated polygon with "equidistant" points 1280 1281 The implementation is optimzed for non parametric curves 1282 ( QwtSplineParametrization::ParameterX ) and falls back to 1283 QwtSpline::equidistantPolygon() otherwise. 1284 1285 \param points Control nodes of the spline 1286 \param distance Distance between 2 points according 1287 to the parametrization 1288 \param withNodes When true, also add the control 1289 nodes ( even if not being equidistant ) 1290 1291 \return Interpolating polygon 1292 1293 \sa QwtSpline::equidistantPolygon() 1294 */ 1295 QPolygonF QwtSplineC2::equidistantPolygon( const QPolygonF& points, 1296 double distance, bool withNodes ) const 1297 { 1298 if ( parametrization()->type() == QwtSplineParametrization::ParameterX ) 1299 { 1300 if ( points.size() > 2 ) 1301 { 1302 const QVector< double > cv = curvatures( points ); 1303 if ( cv.size() != points.size() ) 1304 return QPolygonF(); 1305 1306 return qwtPolygonParametric< QwtSplinePolynomial::fromCurvatures >( 1307 distance, points, cv, withNodes ); 1308 } 1309 } 1310 1311 return QwtSplineInterpolating::equidistantPolygon( points, distance, withNodes ); 1312 } 1313 1314 /*! \fn QVector<double> QwtSplineC2::curvatures( const QPolygonF &points ) const 1315 1316 \brief Find the second derivative at the control points 1317 1318 \param points Control nodes of the spline 1319 \return Vector with the values of the 2nd derivate at the control points 1320 1321 \sa slopes() 1322 \note The x coordinates need to be increasing or decreasing 1323 */ 1324 1325 /*! 1326 \brief Find the first derivative at the control points 1327 1328 An implementation calculating the 2nd derivatives and then building 1329 the slopes in a 2nd loop. QwtSplineCubic overloads it with a more 1330 performant implementation doing it in one loop. 1331 1332 \param points Control nodes of the spline 1333 \return Vector with the values of the 1nd derivate at the control points 1334 1335 \sa curvatures() 1336 1337 \note The x coordinates need to be increasing or decreasing 1338 */ 1339 QVector< double > QwtSplineC2::slopes( const QPolygonF& points ) const 1340 { 1341 const QVector< double > curvatures = this->curvatures( points ); 1342 if ( curvatures.size() < 2 ) 1343 return QVector< double >(); 1344 1345 QVector< double > slopes( curvatures.size() ); 1346 1347 const double* cv = curvatures.constData(); 1348 double* m = slopes.data(); 1349 1350 const int n = points.size(); 1351 const QPointF* p = points.constData(); 1352 1353 QwtSplinePolynomial polynomial; 1354 1355 for ( int i = 0; i < n - 1; i++ ) 1356 { 1357 polynomial = QwtSplinePolynomial::fromCurvatures( p[i], cv[i], p[i + 1], cv[i + 1] ); 1358 m[i] = polynomial.c1; 1359 } 1360 1361 m[n - 1] = polynomial.slopeAt( p[n - 1].x() - p[n - 2].x() ); 1362 1363 return slopes; 1364 } 1365 1366 /*! 1367 \brief Calculate the interpolating polynomials for a non parametric spline 1368 1369 C2 spline interpolations are based on finding values for the second 1370 derivates of f at the control points. The interpolating polynomials can 1371 be calculated from the the second derivates using QwtSplinePolynomial::fromCurvatures. 1372 1373 The default implementation is a 2 pass calculation. In derived classes it 1374 might be overloaded by a one pass implementation. 1375 1376 \param points Control points 1377 \return Interpolating polynomials 1378 1379 \note The x coordinates need to be increasing or decreasing 1380 */ 1381 QVector< QwtSplinePolynomial > QwtSplineC2::polynomials( const QPolygonF& points ) const 1382 { 1383 QVector< QwtSplinePolynomial > polynomials; 1384 1385 const QVector< double > curvatures = this->curvatures( points ); 1386 if ( curvatures.size() < 2 ) 1387 return polynomials; 1388 1389 const QPointF* p = points.constData(); 1390 const double* cv = curvatures.constData(); 1391 const int n = curvatures.size(); 1392 polynomials.reserve( n - 1 ); 1393 1394 for ( int i = 1; i < n; i++ ) 1395 { 1396 polynomials += QwtSplinePolynomial::fromCurvatures( 1397 p[i - 1], cv[i - 1], p[i], cv[i] ); 1398 } 1399 1400 return polynomials; 1401 }
Note:
See TracChangeset
for help on using the changeset viewer.
