source: ntrip/trunk/BNC/qwt/qwt_bezier.cpp

Last change on this file was 11158, checked in by stuerze, 5 weeks ago

deprecated functions exchanged libs updated

File size: 6.5 KB
RevLine 
[11158]1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_bezier.h"
11#include "qwt_math.h"
12
13#include <qpolygon.h>
14#include <qstack.h>
15
16namespace
17{
18 class BezierData
19 {
20 public:
21 inline BezierData()
22 {
23 // default constructor with uninitialized points
24 }
25
26 inline BezierData( const QPointF& p1, const QPointF& cp1,
27 const QPointF& cp2, const QPointF& p2 ):
28 m_x1( p1.x() ),
29 m_y1( p1.y() ),
30 m_cx1( cp1.x() ),
31 m_cy1( cp1.y() ),
32 m_cx2( cp2.x() ),
33 m_cy2( cp2.y() ),
34 m_x2( p2.x() ),
35 m_y2( p2.y() )
36 {
37 }
38
39 static inline double minFlatness( double tolerance )
40 {
41 // we can simplify the tolerance criterion check in
42 // the subdivision loop, by precalculating some
43 // flatness value.
44
45 return 16 * ( tolerance * tolerance );
46 }
47
48 inline double flatness() const
49 {
50 // algo by Roger Willcocks ( http://www.rops.org )
51
52 const double ux = 3.0 * m_cx1 - 2.0 * m_x1 - m_x2;
53 const double uy = 3.0 * m_cy1 - 2.0 * m_y1 - m_y2;
54 const double vx = 3.0 * m_cx2 - 2.0 * m_x2 - m_x1;
55 const double vy = 3.0 * m_cy2 - 2.0 * m_y2 - m_y1;
56
57 const double ux2 = ux * ux;
58 const double uy2 = uy * uy;
59
60 const double vx2 = vx * vx;
61 const double vy2 = vy * vy;
62
63 return qwtMaxF( ux2, vx2 ) + qwtMaxF( uy2, vy2 );
64 }
65
66 inline BezierData subdivided()
67 {
68 BezierData bz;
69
70 const double c1 = midValue( m_cx1, m_cx2 );
71
72 bz.m_cx1 = midValue( m_x1, m_cx1 );
73 m_cx2 = midValue( m_cx2, m_x2 );
74 bz.m_x1 = m_x1;
75 bz.m_cx2 = midValue( bz.m_cx1, c1 );
76 m_cx1 = midValue( c1, m_cx2 );
77 bz.m_x2 = m_x1 = midValue( bz.m_cx2, m_cx1 );
78
79 const double c2 = midValue( m_cy1, m_cy2 );
80
81 bz.m_cy1 = midValue( m_y1, m_cy1 );
82 m_cy2 = midValue( m_cy2, m_y2 );
83 bz.m_y1 = m_y1;
84 bz.m_cy2 = midValue( bz.m_cy1, c2 );
85 m_cy1 = midValue( m_cy2, c2 );
86 bz.m_y2 = m_y1 = midValue( bz.m_cy2, m_cy1 );
87
88 return bz;
89 }
90
91 inline QPointF p2() const
92 {
93 return QPointF( m_x2, m_y2 );
94 }
95
96 private:
97 inline double midValue( double v1, double v2 )
98 {
99 return 0.5 * ( v1 + v2 );
100 }
101
102 double m_x1, m_y1;
103 double m_cx1, m_cy1;
104 double m_cx2, m_cy2;
105 double m_x2, m_y2;
106 };
107}
108
109/*!
110 \brief Constructor
111
112 \param tolerance Termination criterion for the subdivision
113 \sa setTolerance()
114 */
115
116QwtBezier::QwtBezier( double tolerance )
117 : m_tolerance( qwtMaxF( tolerance, 0.0 ) )
118 , m_flatness( BezierData::minFlatness( m_tolerance ) )
119{
120}
121
122//! Destructor
123QwtBezier::~QwtBezier()
124{
125}
126
127/*!
128 Set the tolerance
129
130 The tolerance is a measurement for the flatness of a curve.
131 A curve with a flatness below the tolerance is considered as being flat
132 terminating the subdivision algorithm.
133
134 When interpolating a Bezier curve to render it as a sequence of lines
135 to some sort of raster ( f.e to screen ) a value of 0.5 of the pixel size
136 is a good value for the tolerance.
137
138 \param tolerance Termination criterion for the subdivision
139 \sa tolerance()
140 */
141void QwtBezier::setTolerance( double tolerance )
142{
143 m_tolerance = qwtMaxF( tolerance, 0.0 );
144 m_flatness = BezierData::minFlatness( m_tolerance );
145}
146
147/*!
148 \brief Interpolate a Bézier curve by a polygon
149
150 \param p1 Start point
151 \param cp1 First control point
152 \param cp2 Second control point
153 \param p2 End point
154
155 \return Interpolating polygon
156 */
157QPolygonF QwtBezier::toPolygon( const QPointF& p1,
158 const QPointF& cp1, const QPointF& cp2, const QPointF& p2 ) const
159{
160 QPolygonF polygon;
161
162 if ( m_flatness > 0.0 )
163 {
164 // a flatness of 0.0 is not achievable
165 appendToPolygon( p1, cp1, cp2, p2, polygon );
166 }
167
168 return polygon;
169}
170
171/*!
172 \brief Interpolate a Bézier curve by a polygon
173
174 appendToPolygon() is tailored for cumulating points from a sequence
175 of bezier curves like being created by a spline interpolation.
176
177 \param p1 Start point
178 \param cp1 First control point
179 \param cp2 Second control point
180 \param p2 End point
181 \param polygon Polygon, where the interpolating points are added
182
183 \note If the last point of the incoming polygon matches p1 it won't be
184 inserted a second time.
185 */
186void QwtBezier::appendToPolygon( const QPointF& p1, const QPointF& cp1,
187 const QPointF& cp2, const QPointF& p2, QPolygonF& polygon ) const
188{
189 if ( m_flatness <= 0.0 )
190 {
191 // a flatness of 0.0 is not achievable
192 return;
193 }
194
195 if ( polygon.isEmpty() || polygon.last() != p1 )
196 polygon += p1;
197
198 // to avoid deep stacks we convert the recursive algo
199 // to something iterative, where the parameters of the
200 // recursive class are pushed to a stack instead
201
202 QStack< BezierData > stack;
203 stack.push( BezierData( p1, cp1, cp2, p2 ) );
204
205 while( true )
206 {
207 BezierData& bz = stack.top();
208
209 if ( bz.flatness() < m_flatness )
210 {
211 if ( stack.size() == 1 )
212 {
213 polygon += p2;
214 return;
215 }
216
217 polygon += bz.p2();
218 stack.pop();
219 }
220 else
221 {
222 stack.push( bz.subdivided() );
223 }
224 }
225
226}
227
228/*!
229 Find a point on a Bézier Curve
230
231 \param p1 Start point
232 \param cp1 First control point
233 \param cp2 Second control point
234 \param p2 End point
235 \param t Parameter value, something between [0,1]
236
237 \return Point on the curve
238 */
239QPointF QwtBezier::pointAt( const QPointF& p1,
240 const QPointF& cp1, const QPointF& cp2, const QPointF& p2, double t )
241{
242 const double d1 = 3.0 * t;
243 const double d2 = 3.0 * t * t;
244 const double d3 = t * t * t;
245 const double s = 1.0 - t;
246
247 const double x = ( ( s * p1.x() + d1 * cp1.x() ) * s + d2 * cp2.x() ) * s + d3 * p2.x();
248 const double y = ( ( s * p1.y() + d1 * cp1.y() ) * s + d2 * cp2.y() ) * s + d3 * p2.y();
249
250 return QPointF( x, y );
251}
252
Note: See TracBrowser for help on using the repository browser.