forked from OpenModelica/OpenModelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwt_spline_pleasing.cpp
351 lines (278 loc) · 9.09 KB
/
qwt_spline_pleasing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_spline_pleasing.h"
#include "qwt_spline_parametrization.h"
#include <qpainterpath.h>
static inline double qwtChordalLength( const QPointF& point1, const QPointF& point2 )
{
const double dx = point2.x() - point1.x();
const double dy = point2.y() - point1.y();
return std::sqrt( dx * dx + dy * dy );
}
template< class Param >
static QPointF qwtVector( Param param,
const QPointF& p1, const QPointF& p2 )
{
return ( p2 - p1 ) / param( p1, p2 );
}
template< class Param >
static QPointF qwtVectorCardinal( Param param,
const QPointF& p1, const QPointF& p2, const QPointF& p3 )
{
const double t1 = param( p1, p2 );
const double t2 = param( p2, p3 );
return t2 * ( p3 - p1 ) / ( t1 + t2 );
}
namespace QwtSplinePleasingP
{
struct Tension
{
double t1;
double t2;
};
struct param
{
param( const QwtSplineParametrization* p ):
parameter( p )
{
}
inline double operator()( const QPointF& p1, const QPointF& p2 ) const
{
return parameter->valueIncrement( p1, p2 );
}
const QwtSplineParametrization* parameter;
};
struct paramUniform
{
inline double operator()( const QPointF& p1, const QPointF& p2 ) const
{
return QwtSplineParametrization::valueIncrementUniform( p1, p2 );
}
};
class PathStore
{
public:
inline void init( int )
{
}
inline void start( const QPointF& p0 )
{
path.moveTo( p0 );
}
inline void addCubic( const QPointF& cp1,
const QPointF& cp2, const QPointF& p2 )
{
path.cubicTo( cp1, cp2, p2 );
}
QPainterPath path;
};
class ControlPointsStore
{
public:
inline ControlPointsStore():
m_cp( NULL )
{
}
inline void init( int size )
{
controlPoints.resize( size );
m_cp = controlPoints.data();
}
inline void start( const QPointF& )
{
}
inline void addCubic( const QPointF& cp1,
const QPointF& cp2, const QPointF& )
{
QLineF& l = *m_cp++;
l.setPoints( cp1, cp2 );
}
QVector< QLineF > controlPoints;
private:
QLineF* m_cp;
};
}
static inline QwtSplinePleasingP::Tension qwtTensionPleasing(
double d13, double d23, double d24,
const QPointF& p1, const QPointF& p2,
const QPointF& p3, const QPointF& p4 )
{
QwtSplinePleasingP::Tension tension;
const bool b1 = ( d13 / 3.0 ) < d23;
const bool b2 = ( d24 / 3.0 ) < d23;
if ( b1 )
{
if ( b2 )
{
tension.t1 = ( p1 != p2 ) ? ( 1.0 / 3.0 ) : ( 2.0 / 3.0 );
tension.t2 = ( p3 != p4 ) ? ( 1.0 / 3.0 ) : ( 2.0 / 3.0 );
}
else
{
tension.t1 = tension.t2 = d23 / d24;
}
}
else
{
if ( b2 )
{
tension.t1 = tension.t2 = d23 / d13;
}
else
{
tension.t1 = d23 / d13;
tension.t2 = d23 / d24;
}
}
return tension;
}
template< class SplineStore, class Param >
static SplineStore qwtSplinePathPleasing( const QPolygonF& points,
bool isClosed, Param param )
{
using namespace QwtSplinePleasingP;
const int size = points.size();
const QPointF* p = points.constData();
SplineStore store;
store.init( isClosed ? size : size - 1 );
store.start( p[0] );
double d13;
QPointF vec1;
if ( isClosed )
{
d13 = qwtChordalLength(p[0], p[2]);
const Tension t0 = qwtTensionPleasing(
qwtChordalLength( p[size - 1], p[1]), qwtChordalLength(p[0], p[1]),
d13, p[size - 1], p[0], p[1], p[2] );
const QPointF vec0 = qwtVectorCardinal< Param >( param, p[size - 1], p[0], p[1] );
vec1 = qwtVectorCardinal< Param >( param, p[0], p[1], p[2] );
store.addCubic( p[0] + vec0 * t0.t1, p[1] - vec1 * t0.t2, p[1] );
}
else
{
d13 = qwtChordalLength(p[0], p[2]);
const Tension t0 = qwtTensionPleasing(
qwtChordalLength( p[0], p[1]), qwtChordalLength(p[0], p[1]),
d13, p[0], p[0], p[1], p[2] );
const QPointF vec0 = 0.5 * qwtVector< Param >( param, p[0], p[1] );
vec1 = qwtVectorCardinal< Param >( param, p[0], p[1], p[2] );
store.addCubic( p[0] + vec0 * t0.t1, p[1] - vec1 * t0.t2, p[1] );
}
for ( int i = 1; i < size - 2; i++ )
{
const double d23 = qwtChordalLength( p[i], p[i + 1] );
const double d24 = qwtChordalLength( p[i], p[i + 2] );
const QPointF vec2 = qwtVectorCardinal< Param >( param, p[i], p[i + 1], p[i + 2] );
const Tension t = qwtTensionPleasing(
d13, d23, d24, p[i - 1], p[i], p[i + 1], p[i + 2] );
store.addCubic( p[i] + vec1 * t.t1, p[i + 1] - vec2 * t.t2, p[i + 1] );
d13 = d24;
vec1 = vec2;
}
if ( isClosed )
{
const double d24 = qwtChordalLength( p[size - 2], p[0] );
const Tension tn = qwtTensionPleasing(
d13, qwtChordalLength( p[size - 2], p[size - 1] ), d24,
p[size - 3], p[size - 2], p[size - 1], p[0] );
const QPointF vec2 = qwtVectorCardinal< Param >( param, p[size - 2], p[size - 1], p[0] );
store.addCubic( p[size - 2] + vec1 * tn.t1, p[size - 1] - vec2 * tn.t2, p[size - 1] );
const double d34 = qwtChordalLength( p[size - 1], p[0] );
const double d35 = qwtChordalLength( p[size - 1], p[1] );
const Tension tc = qwtTensionPleasing( d24, d34, d35, p[size - 2], p[size - 1], p[0], p[1] );
const QPointF vec3 = qwtVectorCardinal< Param >( param, p[size - 1], p[0], p[1] );
store.addCubic( p[size - 1] + vec2 * tc.t1, p[0] - vec3 * tc.t2, p[0] );
}
else
{
const double d24 = qwtChordalLength( p[size - 2], p[size - 1] );
const Tension tn = qwtTensionPleasing(
d13, qwtChordalLength( p[size - 2], p[size - 1] ), d24,
p[size - 3], p[size - 2], p[size - 1], p[size - 1] );
const QPointF vec2 = 0.5 * qwtVector< Param >( param, p[size - 2], p[size - 1] );
store.addCubic( p[size - 2] + vec1 * tn.t1, p[size - 1] - vec2 * tn.t2, p[size - 1] );
}
return store;
}
/*!
\brief Constructor
The default setting is a non closing spline with uniform parametrization.
( QwtSplineParametrization::ParameterUniform ).
\sa QwtSpline::setParametrization(), QwtSpline::setBoundaryType()
*/
QwtSplinePleasing::QwtSplinePleasing()
{
setParametrization( QwtSplineParametrization::ParameterUniform );
}
//! Destructor
QwtSplinePleasing::~QwtSplinePleasing()
{
}
//! \return 2
uint QwtSplinePleasing::locality() const
{
return 2;
}
/*!
\brief Interpolate a curve with Bezier curves
Interpolates a polygon piecewise with cubic Bezier curves
and returns them as QPainterPath.
\param points Control points
\return QPainterPath Painter path, that can be rendered by QPainter
*/
QPainterPath QwtSplinePleasing::painterPath( const QPolygonF& points ) const
{
const int size = points.size();
if ( size <= 2 )
return QwtSplineG1::painterPath( points );
const bool isClosing = ( boundaryType() == QwtSpline::ClosedPolygon );
using namespace QwtSplinePleasingP;
PathStore store;
if ( parametrization()->type() == QwtSplineParametrization::ParameterUniform )
{
store = qwtSplinePathPleasing< PathStore >( points,
isClosing, paramUniform() );
}
else
{
store = qwtSplinePathPleasing< PathStore >( points,
isClosing, param( parametrization() ) );
}
if ( isClosing )
store.path.closeSubpath();
return store.path;
}
/*!
\brief Interpolate a curve with Bezier curves
Interpolates a polygon piecewise with cubic Bezier curves
and returns the 2 control points of each curve as QLineF.
\param points Control points
\return Control points of the interpolating Bezier curves
*/
QVector< QLineF > QwtSplinePleasing::bezierControlLines(
const QPolygonF& points ) const
{
const int size = points.size();
if ( size <= 2 )
return QVector< QLineF >();
const bool isClosing = ( boundaryType() == QwtSpline::ClosedPolygon );
using namespace QwtSplinePleasingP;
ControlPointsStore store;
if ( parametrization()->type() == QwtSplineParametrization::ParameterUniform )
{
store = qwtSplinePathPleasing< ControlPointsStore >( points,
isClosing, paramUniform() );
}
else
{
store = qwtSplinePathPleasing< ControlPointsStore >( points,
isClosing, param( parametrization() ) );
}
return store.controlPoints;
}