~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.1/src/qwt_clipper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
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 <qrect.h>
 
11
#include "qwt_math.h"
 
12
#include "qwt_clipper.h"
 
13
 
 
14
static inline QwtDoubleRect boundingRect(const QwtPolygonF &polygon)
 
15
{
 
16
#if QT_VERSION < 0x040000
 
17
    if (polygon.isEmpty())
 
18
        return QwtDoubleRect(0, 0, 0, 0);
 
19
 
 
20
    register const QwtDoublePoint *pd = polygon.data();
 
21
 
 
22
    double minx, maxx, miny, maxy;
 
23
    minx = maxx = pd->x();
 
24
    miny = maxy = pd->y();
 
25
    pd++;
 
26
 
 
27
    for (uint i = 1; i < polygon.size(); i++, pd++) 
 
28
    {
 
29
        if (pd->x() < minx)
 
30
            minx = pd->x();
 
31
        else if (pd->x() > maxx)
 
32
            maxx = pd->x();
 
33
        if (pd->y() < miny)
 
34
            miny = pd->y();
 
35
        else if (pd->y() > maxy)
 
36
            maxy = pd->y();
 
37
    }
 
38
    return QwtDoubleRect(minx, miny, maxx - minx, maxy - miny);
 
39
#else
 
40
    return polygon.boundingRect();
 
41
#endif
 
42
}
 
43
 
 
44
enum Edge 
 
45
 
46
    Left, 
 
47
    Top, 
 
48
    Right, 
 
49
    Bottom, 
 
50
    NEdges 
 
51
};
 
52
 
 
53
class QwtPolygonClipper: public QRect
 
54
{
 
55
public:
 
56
    QwtPolygonClipper(const QRect &r);
 
57
 
 
58
    QwtPolygon clipPolygon(const QwtPolygon &) const;
 
59
 
 
60
private:
 
61
    void clipEdge(Edge, const QwtPolygon &, QwtPolygon &) const;
 
62
    bool insideEdge(const QPoint &, Edge edge) const;
 
63
    QPoint intersectEdge(const QPoint &p1,
 
64
        const QPoint &p2, Edge edge) const;
 
65
 
 
66
    void addPoint(QwtPolygon &, uint pos, const QPoint &point) const;
 
67
};
 
68
 
 
69
class QwtPolygonClipperF: public QwtDoubleRect
 
70
{
 
71
public:
 
72
    QwtPolygonClipperF(const QwtDoubleRect &r);
 
73
    QwtPolygonF clipPolygon(const QwtPolygonF &) const;
 
74
 
 
75
private:
 
76
    void clipEdge(Edge, const QwtPolygonF &, QwtPolygonF &) const;
 
77
    bool insideEdge(const QwtDoublePoint &, Edge edge) const;
 
78
    QwtDoublePoint intersectEdge(const QwtDoublePoint &p1,
 
79
        const QwtDoublePoint &p2, Edge edge) const;
 
80
 
 
81
    void addPoint(QwtPolygonF &, uint pos, const QwtDoublePoint &point) const;
 
82
};
 
83
 
 
84
#if QT_VERSION >= 0x040000
 
85
class QwtCircleClipper: public QwtDoubleRect
 
86
{
 
87
public:
 
88
    QwtCircleClipper(const QwtDoubleRect &r);
 
89
    QwtArray<QwtDoubleInterval> clipCircle(
 
90
        const QwtDoublePoint &, double radius) const;
 
91
 
 
92
private:
 
93
    QList<QwtDoublePoint> cuttingPoints(
 
94
        Edge, const QwtDoublePoint &pos, double radius) const;
 
95
    double toAngle(const QwtDoublePoint &, const QwtDoublePoint &) const;
 
96
};
 
97
#endif
 
98
 
 
99
QwtPolygonClipper::QwtPolygonClipper(const QRect &r): 
 
100
    QRect(r) 
 
101
{
 
102
}
 
103
 
 
104
inline void QwtPolygonClipper::addPoint(
 
105
    QwtPolygon &pa, uint pos, const QPoint &point) const
 
106
{
 
107
    if ( uint(pa.size()) <= pos ) 
 
108
        pa.resize(pos + 5);
 
109
 
 
110
    pa.setPoint(pos, point);
 
111
}
 
112
 
 
113
//! Sutherland-Hodgman polygon clipping
 
114
QwtPolygon QwtPolygonClipper::clipPolygon(const QwtPolygon &pa) const
 
115
{
 
116
    if ( contains( pa.boundingRect() ) )
 
117
        return pa;
 
118
 
 
119
    QwtPolygon cpa(pa.size());
 
120
 
 
121
    clipEdge((Edge)0, pa, cpa);
 
122
 
 
123
    for ( uint edge = 1; edge < NEdges; edge++ ) 
 
124
    {
 
125
        const QwtPolygon rpa = cpa;
 
126
#if QT_VERSION < 0x040000
 
127
        cpa.detach();
 
128
#endif
 
129
        clipEdge((Edge)edge, rpa, cpa);
 
130
    }
 
131
 
 
132
    return cpa;
 
133
}
 
134
 
 
135
bool QwtPolygonClipper::insideEdge(const QPoint &p, Edge edge) const
 
136
{
 
137
    switch(edge) 
 
138
    {
 
139
        case Left:
 
140
            return p.x() > left();
 
141
        case Top:
 
142
            return p.y() > top();
 
143
        case Right:
 
144
            return p.x() < right();
 
145
        case Bottom:
 
146
            return p.y() < bottom();
 
147
        default:
 
148
            break;
 
149
    }
 
150
 
 
151
    return false;
 
152
}
 
153
 
 
154
QPoint QwtPolygonClipper::intersectEdge(const QPoint &p1, 
 
155
    const QPoint &p2, Edge edge ) const
 
156
{
 
157
    int x=0, y=0;
 
158
    double m = 0;
 
159
 
 
160
    const double dy = p2.y() - p1.y();
 
161
    const double dx = p2.x() - p1.x();
 
162
 
 
163
    switch ( edge ) 
 
164
    {
 
165
        case Left:
 
166
            x = left();
 
167
            m = double(qwtAbs(p1.x() - x)) / qwtAbs(dx);
 
168
            y = p1.y() + int(dy * m);
 
169
            break;
 
170
        case Top:
 
171
            y = top();
 
172
            m = double(qwtAbs(p1.y() - y)) / qwtAbs(dy);
 
173
            x = p1.x() + int(dx * m);
 
174
            break;
 
175
        case Right:
 
176
            x = right();
 
177
            m = double(qwtAbs(p1.x() - x)) / qwtAbs(dx);
 
178
            y = p1.y() + int(dy * m);
 
179
            break;
 
180
        case Bottom:
 
181
            y = bottom();
 
182
            m = double(qwtAbs(p1.y() - y)) / qwtAbs(dy);
 
183
            x = p1.x() + int(dx * m);
 
184
            break;
 
185
        default:
 
186
            break;
 
187
    }
 
188
 
 
189
    return QPoint(x,y);
 
190
}
 
191
 
 
192
void QwtPolygonClipper::clipEdge(Edge edge, 
 
193
    const QwtPolygon &pa, QwtPolygon &cpa) const
 
194
{
 
195
    if ( pa.count() == 0 )
 
196
    {
 
197
        cpa.resize(0);
 
198
        return;
 
199
    }
 
200
 
 
201
    unsigned int count = 0;
 
202
 
 
203
    QPoint p1 = pa.point(0);
 
204
    if ( insideEdge(p1, edge) )
 
205
        addPoint(cpa, count++, p1);
 
206
 
 
207
    const uint nPoints = pa.size();
 
208
    for ( uint i = 1; i < nPoints; i++ )
 
209
    {
 
210
        const QPoint p2 = pa.point(i);
 
211
        if ( insideEdge(p2, edge) )
 
212
        {
 
213
            if ( insideEdge(p1, edge) )
 
214
                addPoint(cpa, count++, p2);
 
215
            else
 
216
            {
 
217
                addPoint(cpa, count++, intersectEdge(p1, p2, edge));
 
218
                addPoint(cpa, count++, p2);
 
219
            }
 
220
        }
 
221
        else
 
222
        {
 
223
            if ( insideEdge(p1, edge) )
 
224
                addPoint(cpa, count++, intersectEdge(p1, p2, edge));
 
225
        }
 
226
        p1 = p2;
 
227
    }
 
228
    cpa.resize(count);
 
229
}
 
230
 
 
231
QwtPolygonClipperF::QwtPolygonClipperF(const QwtDoubleRect &r): 
 
232
    QwtDoubleRect(r) 
 
233
{
 
234
}
 
235
 
 
236
inline void QwtPolygonClipperF::addPoint(QwtPolygonF &pa, uint pos, const QwtDoublePoint &point) const
 
237
{
 
238
    if ( uint(pa.size()) <= pos ) 
 
239
        pa.resize(pos + 5);
 
240
 
 
241
    pa[(int)pos] = point;
 
242
}
 
243
 
 
244
//! Sutherland-Hodgman polygon clipping
 
245
QwtPolygonF QwtPolygonClipperF::clipPolygon(const QwtPolygonF &pa) const
 
246
{
 
247
    if ( contains( ::boundingRect(pa) ) )
 
248
        return pa;
 
249
 
 
250
    QwtPolygonF cpa(pa.size());
 
251
 
 
252
    clipEdge((Edge)0, pa, cpa);
 
253
 
 
254
    for ( uint edge = 1; edge < NEdges; edge++ ) 
 
255
    {
 
256
        const QwtPolygonF rpa = cpa;
 
257
#if QT_VERSION < 0x040000
 
258
        cpa.detach();
 
259
#endif
 
260
        clipEdge((Edge)edge, rpa, cpa);
 
261
    }
 
262
 
 
263
    return cpa;
 
264
}
 
265
 
 
266
bool QwtPolygonClipperF::insideEdge(const QwtDoublePoint &p, Edge edge) const
 
267
{
 
268
    switch(edge) 
 
269
    {
 
270
        case Left:
 
271
            return p.x() > left();
 
272
        case Top:
 
273
            return p.y() > top();
 
274
        case Right:
 
275
            return p.x() < right();
 
276
        case Bottom:
 
277
            return p.y() < bottom();
 
278
        default:
 
279
            break;
 
280
    }
 
281
 
 
282
    return false;
 
283
}
 
284
 
 
285
QwtDoublePoint QwtPolygonClipperF::intersectEdge(const QwtDoublePoint &p1, 
 
286
    const QwtDoublePoint &p2, Edge edge ) const
 
287
{
 
288
    double x=0.0, y=0.0;
 
289
    double m = 0;
 
290
 
 
291
    const double dy = p2.y() - p1.y();
 
292
    const double dx = p2.x() - p1.x();
 
293
 
 
294
    switch ( edge ) 
 
295
    {
 
296
        case Left:
 
297
            x = left();
 
298
            m = double(qwtAbs(p1.x() - x)) / qwtAbs(dx);
 
299
            y = p1.y() + int(dy * m);
 
300
            break;
 
301
        case Top:
 
302
            y = top();
 
303
            m = double(qwtAbs(p1.y() - y)) / qwtAbs(dy);
 
304
            x = p1.x() + int(dx * m);
 
305
            break;
 
306
        case Right:
 
307
            x = right();
 
308
            m = double(qwtAbs(p1.x() - x)) / qwtAbs(dx);
 
309
            y = p1.y() + int(dy * m);
 
310
            break;
 
311
        case Bottom:
 
312
            y = bottom();
 
313
            m = double(qwtAbs(p1.y() - y)) / qwtAbs(dy);
 
314
            x = p1.x() + int(dx * m);
 
315
            break;
 
316
        default:
 
317
            break;
 
318
    }
 
319
 
 
320
    return QwtDoublePoint(x,y);
 
321
}
 
322
 
 
323
void QwtPolygonClipperF::clipEdge(Edge edge, 
 
324
    const QwtPolygonF &pa, QwtPolygonF &cpa) const
 
325
{
 
326
    if ( pa.count() == 0 )
 
327
    {
 
328
        cpa.resize(0);
 
329
        return;
 
330
    }
 
331
 
 
332
    unsigned int count = 0;
 
333
 
 
334
    QwtDoublePoint p1 = pa[0];
 
335
    if ( insideEdge(p1, edge) )
 
336
        addPoint(cpa, count++, p1);
 
337
 
 
338
    const uint nPoints = pa.size();
 
339
    for ( uint i = 1; i < nPoints; i++ )
 
340
    {
 
341
        const QwtDoublePoint p2 = pa[(int)i];
 
342
        if ( insideEdge(p2, edge) )
 
343
        {
 
344
            if ( insideEdge(p1, edge) )
 
345
                addPoint(cpa, count++, p2);
 
346
            else
 
347
            {
 
348
                addPoint(cpa, count++, intersectEdge(p1, p2, edge));
 
349
                addPoint(cpa, count++, p2);
 
350
            }
 
351
        }
 
352
        else
 
353
        {
 
354
            if ( insideEdge(p1, edge) )
 
355
                addPoint(cpa, count++, intersectEdge(p1, p2, edge));
 
356
        }
 
357
        p1 = p2;
 
358
    }
 
359
    cpa.resize(count);
 
360
}
 
361
 
 
362
#if QT_VERSION >= 0x040000
 
363
 
 
364
QwtCircleClipper::QwtCircleClipper(const QwtDoubleRect &r):
 
365
    QwtDoubleRect(r)
 
366
{
 
367
}
 
368
 
 
369
QwtArray<QwtDoubleInterval> QwtCircleClipper::clipCircle(
 
370
    const QwtDoublePoint &pos, double radius) const
 
371
{
 
372
    QList<QwtDoublePoint> points;
 
373
    for ( int edge = 0; edge < NEdges; edge++ )
 
374
        points += cuttingPoints((Edge)edge, pos, radius);
 
375
 
 
376
    QwtArray<QwtDoubleInterval> intv;
 
377
    if ( points.size() <= 0 )
 
378
    {
 
379
        QwtDoubleRect cRect(0, 0, 2 * radius, 2* radius);
 
380
        cRect.moveCenter(pos);
 
381
        if ( contains(cRect) )
 
382
            intv += QwtDoubleInterval(0.0, 2 * M_PI);
 
383
    }
 
384
    else
 
385
    {
 
386
        QList<double> angles;
 
387
        for ( int i = 0; i < points.size(); i++ )
 
388
            angles += toAngle(pos, points[i]);
 
389
        qSort(angles);
 
390
 
 
391
        const int in = contains(qwtPolar2Pos(pos, radius, 
 
392
            angles[0] + (angles[1] - angles[0]) / 2));
 
393
        if ( in )
 
394
        {
 
395
            for ( int i = 0; i < angles.size() - 1; i += 2)
 
396
                intv += QwtDoubleInterval(angles[i], angles[i+1]);
 
397
        }
 
398
        else
 
399
        {
 
400
            for ( int i = 1; i < angles.size() - 1; i += 2)
 
401
                intv += QwtDoubleInterval(angles[i], angles[i+1]);
 
402
            intv += QwtDoubleInterval(angles.last(), angles.first());
 
403
        }
 
404
    }
 
405
 
 
406
    return intv;
 
407
}
 
408
 
 
409
double QwtCircleClipper::toAngle(
 
410
    const QwtDoublePoint &from, const QwtDoublePoint &to) const
 
411
{
 
412
    if ( from.x() == to.x() )
 
413
        return from.y() <= to.y() ? M_PI / 2.0 : 3 * M_PI / 2.0;
 
414
 
 
415
    const double m = qwtAbs((to.y() - from.y()) / (to.x() - from.x()) );
 
416
 
 
417
    double angle = ::atan(m);
 
418
    if ( to.x() > from.x() )
 
419
    {   
 
420
        if ( to.y() > from.y() )
 
421
            angle = 2 * M_PI - angle;
 
422
    }
 
423
    else
 
424
    {
 
425
        if ( to.y() > from.y() )
 
426
            angle = M_PI + angle;
 
427
        else
 
428
            angle = M_PI - angle;
 
429
    }
 
430
 
 
431
    return angle;
 
432
}
 
433
 
 
434
QList<QwtDoublePoint> QwtCircleClipper::cuttingPoints(
 
435
    Edge edge, const QwtDoublePoint &pos, double radius) const
 
436
{
 
437
    QList<QwtDoublePoint> points;
 
438
 
 
439
    if ( edge == Left || edge == Right )
 
440
    {
 
441
        const double x = (edge == Left) ? left() : right();
 
442
        if ( qwtAbs(pos.x() - x) < radius )
 
443
        {
 
444
            const double off = ::sqrt(qwtSqr(radius) - qwtSqr(pos.x() - x));
 
445
            const double y1 = pos.y() + off;
 
446
            if ( y1 >= top() && y1 <= bottom() )
 
447
                points += QwtDoublePoint(x, y1);
 
448
            const double y2 = pos.y() - off;
 
449
            if ( y2 >= top() && y2 <= bottom() )
 
450
                points += QwtDoublePoint(x, y2);
 
451
        }
 
452
    }
 
453
    else
 
454
    {
 
455
        const double y = (edge == Top) ? top() : bottom();
 
456
        if ( qwtAbs(pos.y() - y) < radius )
 
457
        {
 
458
            const double off = ::sqrt(qwtSqr(radius) - qwtSqr(pos.y() - y));
 
459
            const double x1 = pos.x() + off;
 
460
            if ( x1 >= left() && x1 <= right() )
 
461
                points += QwtDoublePoint(x1, y);
 
462
            const double x2 = pos.x() - off;
 
463
            if ( x2 >= left() && x2 <= right() )
 
464
                points += QwtDoublePoint(x2, y);
 
465
        }
 
466
    }
 
467
    return points;
 
468
}
 
469
#endif
 
470
    
 
471
QwtPolygon QwtClipper::clipPolygon(
 
472
    const QRect &clipRect, const QwtPolygon &polygon)
 
473
{
 
474
    QwtPolygonClipper clipper(clipRect);
 
475
    return clipper.clipPolygon(polygon);
 
476
}
 
477
 
 
478
QwtPolygonF QwtClipper::clipPolygonF(
 
479
    const QwtDoubleRect &clipRect, const QwtPolygonF &polygon)
 
480
{
 
481
    QwtPolygonClipperF clipper(clipRect);
 
482
    return clipper.clipPolygon(polygon);
 
483
}
 
484
 
 
485
#if QT_VERSION >= 0x040000
 
486
QwtArray<QwtDoubleInterval> QwtClipper::clipCircle(
 
487
    const QwtDoubleRect &clipRect, 
 
488
    const QwtDoublePoint &center, double radius)
 
489
{
 
490
    QwtCircleClipper clipper(clipRect);
 
491
    return clipper.clipCircle(center, radius);
 
492
}
 
493
#endif