| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qtessellator_p.h"
|
|---|
| 43 |
|
|---|
| 44 | #include <QRect>
|
|---|
| 45 | #include <QList>
|
|---|
| 46 | #include <QDebug>
|
|---|
| 47 |
|
|---|
| 48 | #include <qmath.h>
|
|---|
| 49 | #include <limits.h>
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_NAMESPACE
|
|---|
| 52 |
|
|---|
| 53 | //#define DEBUG
|
|---|
| 54 | #ifdef DEBUG
|
|---|
| 55 | #define QDEBUG qDebug
|
|---|
| 56 | #else
|
|---|
| 57 | #define QDEBUG if (1); else qDebug
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | static const bool emit_clever = true;
|
|---|
| 61 | static const bool mark_clever = false;
|
|---|
| 62 |
|
|---|
| 63 | enum VertexFlags {
|
|---|
| 64 | LineBeforeStarts = 0x1,
|
|---|
| 65 | LineBeforeEnds = 0x2,
|
|---|
| 66 | LineBeforeHorizontal = 0x4,
|
|---|
| 67 | LineAfterStarts = 0x8,
|
|---|
| 68 | LineAfterEnds = 0x10,
|
|---|
| 69 | LineAfterHorizontal = 0x20
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | class QTessellatorPrivate {
|
|---|
| 75 | public:
|
|---|
| 76 | struct Vertices;
|
|---|
| 77 |
|
|---|
| 78 | QTessellatorPrivate() {}
|
|---|
| 79 |
|
|---|
| 80 | QRectF collectAndSortVertices(const QPointF *points, int *maxActiveEdges);
|
|---|
| 81 | void cancelCoincidingEdges();
|
|---|
| 82 |
|
|---|
| 83 | void emitEdges(QTessellator *tessellator);
|
|---|
| 84 | void processIntersections();
|
|---|
| 85 | void removeEdges();
|
|---|
| 86 | void addEdges();
|
|---|
| 87 | void addIntersections();
|
|---|
| 88 |
|
|---|
| 89 | struct Vertex : public QTessellator::Vertex
|
|---|
| 90 | {
|
|---|
| 91 | int flags;
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | struct Intersection
|
|---|
| 95 | {
|
|---|
| 96 | Q27Dot5 y;
|
|---|
| 97 | int edge;
|
|---|
| 98 | bool operator <(const Intersection &other) const {
|
|---|
| 99 | if (y != other.y)
|
|---|
| 100 | return y < other.y;
|
|---|
| 101 | return edge < other.edge;
|
|---|
| 102 | }
|
|---|
| 103 | };
|
|---|
| 104 | struct IntersectionLink
|
|---|
| 105 | {
|
|---|
| 106 | int next;
|
|---|
| 107 | int prev;
|
|---|
| 108 | };
|
|---|
| 109 | typedef QMap<Intersection, IntersectionLink> Intersections;
|
|---|
| 110 |
|
|---|
| 111 | struct Edge {
|
|---|
| 112 | Edge(const Vertices &v, int _edge);
|
|---|
| 113 | int edge;
|
|---|
| 114 | const Vertex *v0;
|
|---|
| 115 | const Vertex *v1;
|
|---|
| 116 | Q27Dot5 y_left;
|
|---|
| 117 | Q27Dot5 y_right;
|
|---|
| 118 | signed int winding : 8;
|
|---|
| 119 | bool mark;
|
|---|
| 120 | bool free;
|
|---|
| 121 | bool intersect_left;
|
|---|
| 122 | bool intersect_right;
|
|---|
| 123 | bool isLeftOf(const Edge &other, Q27Dot5 y) const;
|
|---|
| 124 | Q27Dot5 positionAt(Q27Dot5 y) const;
|
|---|
| 125 | bool intersect(const Edge &other, Q27Dot5 *y, bool *det_positive) const;
|
|---|
| 126 |
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | class EdgeSorter
|
|---|
| 130 | {
|
|---|
| 131 | public:
|
|---|
| 132 | EdgeSorter(int _y) : y(_y) {}
|
|---|
| 133 | bool operator() (const Edge *e1, const Edge *e2);
|
|---|
| 134 | int y;
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | class Scanline {
|
|---|
| 138 | public:
|
|---|
| 139 | Scanline();
|
|---|
| 140 | ~Scanline();
|
|---|
| 141 |
|
|---|
| 142 | void init(int maxActiveEdges);
|
|---|
| 143 | void done();
|
|---|
| 144 |
|
|---|
| 145 | int findEdgePosition(Q27Dot5 x, Q27Dot5 y) const;
|
|---|
| 146 | int findEdgePosition(const Edge &e) const;
|
|---|
| 147 | int findEdge(int edge) const;
|
|---|
| 148 | void clearMarks();
|
|---|
| 149 |
|
|---|
| 150 | void swap(int p1, int p2) {
|
|---|
| 151 | Edge *tmp = edges[p1];
|
|---|
| 152 | edges[p1] = edges[p2];
|
|---|
| 153 | edges[p2] = tmp;
|
|---|
| 154 | }
|
|---|
| 155 | void insert(int pos, const Edge &e);
|
|---|
| 156 | void removeAt(int pos);
|
|---|
| 157 | void markEdges(int pos1, int pos2);
|
|---|
| 158 |
|
|---|
| 159 | void prepareLine();
|
|---|
| 160 | void lineDone();
|
|---|
| 161 |
|
|---|
| 162 | Edge **old;
|
|---|
| 163 | int old_size;
|
|---|
| 164 |
|
|---|
| 165 | Edge **edges;
|
|---|
| 166 | int size;
|
|---|
| 167 |
|
|---|
| 168 | private:
|
|---|
| 169 | Edge *edge_table;
|
|---|
| 170 | int first_unused;
|
|---|
| 171 | int max_edges;
|
|---|
| 172 | enum { default_alloc = 32 };
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | struct Vertices {
|
|---|
| 176 | enum { default_alloc = 128 };
|
|---|
| 177 | Vertices();
|
|---|
| 178 | ~Vertices();
|
|---|
| 179 | void init(int maxVertices);
|
|---|
| 180 | void done();
|
|---|
| 181 | Vertex *storage;
|
|---|
| 182 | Vertex **sorted;
|
|---|
| 183 |
|
|---|
| 184 | Vertex *operator[] (int i) { return storage + i; }
|
|---|
| 185 | const Vertex *operator[] (int i) const { return storage + i; }
|
|---|
| 186 | int position(const Vertex *v) const {
|
|---|
| 187 | return v - storage;
|
|---|
| 188 | }
|
|---|
| 189 | Vertex *next(Vertex *v) {
|
|---|
| 190 | ++v;
|
|---|
| 191 | if (v == storage + nPoints)
|
|---|
| 192 | v = storage;
|
|---|
| 193 | return v;
|
|---|
| 194 | }
|
|---|
| 195 | const Vertex *next(const Vertex *v) const {
|
|---|
| 196 | ++v;
|
|---|
| 197 | if (v == storage + nPoints)
|
|---|
| 198 | v = storage;
|
|---|
| 199 | return v;
|
|---|
| 200 | }
|
|---|
| 201 | int nextPos(const Vertex *v) const {
|
|---|
| 202 | ++v;
|
|---|
| 203 | if (v == storage + nPoints)
|
|---|
| 204 | return 0;
|
|---|
| 205 | return v - storage;
|
|---|
| 206 | }
|
|---|
| 207 | Vertex *prev(Vertex *v) {
|
|---|
| 208 | if (v == storage)
|
|---|
| 209 | v = storage + nPoints;
|
|---|
| 210 | --v;
|
|---|
| 211 | return v;
|
|---|
| 212 | }
|
|---|
| 213 | const Vertex *prev(const Vertex *v) const {
|
|---|
| 214 | if (v == storage)
|
|---|
| 215 | v = storage + nPoints;
|
|---|
| 216 | --v;
|
|---|
| 217 | return v;
|
|---|
| 218 | }
|
|---|
| 219 | int prevPos(const Vertex *v) const {
|
|---|
| 220 | if (v == storage)
|
|---|
| 221 | v = storage + nPoints;
|
|---|
| 222 | --v;
|
|---|
| 223 | return v - storage;
|
|---|
| 224 | }
|
|---|
| 225 | int nPoints;
|
|---|
| 226 | int allocated;
|
|---|
| 227 | };
|
|---|
| 228 | Vertices vertices;
|
|---|
| 229 | Intersections intersections;
|
|---|
| 230 | Scanline scanline;
|
|---|
| 231 | bool winding;
|
|---|
| 232 | Q27Dot5 y;
|
|---|
| 233 | int currentVertex;
|
|---|
| 234 |
|
|---|
| 235 | private:
|
|---|
| 236 | void addIntersection(const Edge *e1, const Edge *e2);
|
|---|
| 237 | bool edgeInChain(Intersection i, int edge);
|
|---|
| 238 | };
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | QTessellatorPrivate::Edge::Edge(const QTessellatorPrivate::Vertices &vertices, int edge)
|
|---|
| 242 | {
|
|---|
| 243 | this->edge = edge;
|
|---|
| 244 | intersect_left = intersect_right = true;
|
|---|
| 245 | mark = false;
|
|---|
| 246 | free = false;
|
|---|
| 247 |
|
|---|
| 248 | v0 = vertices[edge];
|
|---|
| 249 | v1 = vertices.next(v0);
|
|---|
| 250 |
|
|---|
| 251 | Q_ASSERT(v0->y != v1->y);
|
|---|
| 252 |
|
|---|
| 253 | if (v0->y > v1->y) {
|
|---|
| 254 | qSwap(v0, v1);
|
|---|
| 255 | winding = -1;
|
|---|
| 256 | } else {
|
|---|
| 257 | winding = 1;
|
|---|
| 258 | }
|
|---|
| 259 | y_left = y_right = v0->y;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | // This is basically the algorithm from graphics gems. The algorithm
|
|---|
| 263 | // is cubic in the coordinates at one place. Since we use 64bit
|
|---|
| 264 | // integers, this implies, that the allowed range for our coordinates
|
|---|
| 265 | // is limited to 21 bits. With 5 bits behind the decimal, this
|
|---|
| 266 | // implies that differences in coordaintes can range from 2*SHORT_MIN
|
|---|
| 267 | // to 2*SHORT_MAX, giving us efficiently a coordinate system from
|
|---|
| 268 | // SHORT_MIN to SHORT_MAX.
|
|---|
| 269 | //
|
|---|
| 270 |
|
|---|
| 271 | // WARNING: It's absolutely critical that the intersect() and isLeftOf() methods use
|
|---|
| 272 | // exactly the same algorithm to calulate yi. It's also important to be sure the algorithms
|
|---|
| 273 | // are transitive (ie. the conditions below are true for all input data):
|
|---|
| 274 | //
|
|---|
| 275 | // a.intersect(b) == b.intersect(a)
|
|---|
| 276 | // a.isLeftOf(b) != b.isLeftOf(a)
|
|---|
| 277 | //
|
|---|
| 278 | // This is tricky to get right, so be very careful when changing anything in here!
|
|---|
| 279 |
|
|---|
| 280 | static inline bool sameSign(qint64 a, qint64 b) {
|
|---|
| 281 | return (((qint64) ((quint64) a ^ (quint64) b)) >= 0 );
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | bool QTessellatorPrivate::Edge::intersect(const Edge &other, Q27Dot5 *y, bool *det_positive) const
|
|---|
| 285 | {
|
|---|
| 286 | qint64 a1 = v1->y - v0->y;
|
|---|
| 287 | qint64 b1 = v0->x - v1->x;
|
|---|
| 288 |
|
|---|
| 289 | qint64 a2 = other.v1->y - other.v0->y;
|
|---|
| 290 | qint64 b2 = other.v0->x - other.v1->x;
|
|---|
| 291 |
|
|---|
| 292 | qint64 det = a1 * b2 - a2 * b1;
|
|---|
| 293 | if (det == 0)
|
|---|
| 294 | return false;
|
|---|
| 295 |
|
|---|
| 296 | qint64 c1 = qint64(v1->x) * v0->y - qint64(v0->x) * v1->y;
|
|---|
| 297 |
|
|---|
| 298 | qint64 r3 = a1 * other.v0->x + b1 * other.v0->y + c1;
|
|---|
| 299 | qint64 r4 = a1 * other.v1->x + b1 * other.v1->y + c1;
|
|---|
| 300 |
|
|---|
| 301 | // Check signs of r3 and r4. If both point 3 and point 4 lie on
|
|---|
| 302 | // same side of line 1, the line segments do not intersect.
|
|---|
| 303 | QDEBUG() << " " << r3 << r4;
|
|---|
| 304 | if (r3 != 0 && r4 != 0 && sameSign( r3, r4 ))
|
|---|
| 305 | return false;
|
|---|
| 306 |
|
|---|
| 307 | qint64 c2 = qint64(other.v1->x) * other.v0->y - qint64(other.v0->x) * other.v1->y;
|
|---|
| 308 |
|
|---|
| 309 | qint64 r1 = a2 * v0->x + b2 * v0->y + c2;
|
|---|
| 310 | qint64 r2 = a2 * v1->x + b2 * v1->y + c2;
|
|---|
| 311 |
|
|---|
| 312 | // Check signs of r1 and r2. If both point 1 and point 2 lie
|
|---|
| 313 | // on same side of second line segment, the line segments do not intersect.
|
|---|
| 314 | QDEBUG() << " " << r1 << r2;
|
|---|
| 315 | if (r1 != 0 && r2 != 0 && sameSign( r1, r2 ))
|
|---|
| 316 | return false;
|
|---|
| 317 |
|
|---|
| 318 | // The det/2 is to get rounding instead of truncating. It
|
|---|
| 319 | // is added or subtracted to the numerator, depending upon the
|
|---|
| 320 | // sign of the numerator.
|
|---|
| 321 | qint64 offset = det < 0 ? -det : det;
|
|---|
| 322 | offset >>= 1;
|
|---|
| 323 |
|
|---|
| 324 | qint64 num = a2 * c1 - a1 * c2;
|
|---|
| 325 | *y = ( num < 0 ? num - offset : num + offset ) / det;
|
|---|
| 326 |
|
|---|
| 327 | *det_positive = (det > 0);
|
|---|
| 328 |
|
|---|
| 329 | return true;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | #undef SAME_SIGNS
|
|---|
| 333 |
|
|---|
| 334 | bool QTessellatorPrivate::Edge::isLeftOf(const Edge &other, Q27Dot5 y) const
|
|---|
| 335 | {
|
|---|
| 336 | // QDEBUG() << "isLeftOf" << edge << other.edge << y;
|
|---|
| 337 | qint64 a1 = v1->y - v0->y;
|
|---|
| 338 | qint64 b1 = v0->x - v1->x;
|
|---|
| 339 | qint64 a2 = other.v1->y - other.v0->y;
|
|---|
| 340 | qint64 b2 = other.v0->x - other.v1->x;
|
|---|
| 341 |
|
|---|
| 342 | qint64 c2 = qint64(other.v1->x) * other.v0->y - qint64(other.v0->x) * other.v1->y;
|
|---|
| 343 |
|
|---|
| 344 | qint64 det = a1 * b2 - a2 * b1;
|
|---|
| 345 | if (det == 0) {
|
|---|
| 346 | // lines are parallel. Only need to check side of one point
|
|---|
| 347 | // fixed ordering for coincident edges
|
|---|
| 348 | qint64 r1 = a2 * v0->x + b2 * v0->y + c2;
|
|---|
| 349 | // QDEBUG() << "det = 0" << r1;
|
|---|
| 350 | if (r1 == 0)
|
|---|
| 351 | return edge < other.edge;
|
|---|
| 352 | return (r1 < 0);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | // not parallel, need to find the y coordinate of the intersection point
|
|---|
| 356 | qint64 c1 = qint64(v1->x) * v0->y - qint64(v0->x) * v1->y;
|
|---|
| 357 |
|
|---|
| 358 | qint64 offset = det < 0 ? -det : det;
|
|---|
| 359 | offset >>= 1;
|
|---|
| 360 |
|
|---|
| 361 | qint64 num = a2 * c1 - a1 * c2;
|
|---|
| 362 | qint64 yi = ( num < 0 ? num - offset : num + offset ) / det;
|
|---|
| 363 | // QDEBUG() << " num=" << num << "offset=" << offset << "det=" << det;
|
|---|
| 364 |
|
|---|
| 365 | return ((yi > y) ^ (det < 0));
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | static inline bool compareVertex(const QTessellatorPrivate::Vertex *p1,
|
|---|
| 369 | const QTessellatorPrivate::Vertex *p2)
|
|---|
| 370 | {
|
|---|
| 371 | if (p1->y == p2->y) {
|
|---|
| 372 | if (p1->x == p2->x)
|
|---|
| 373 | return p1 < p2;
|
|---|
| 374 | return p1->x < p2->x;
|
|---|
| 375 | }
|
|---|
| 376 | return p1->y < p2->y;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | Q27Dot5 QTessellatorPrivate::Edge::positionAt(Q27Dot5 y) const
|
|---|
| 380 | {
|
|---|
| 381 | if (y == v0->y)
|
|---|
| 382 | return v0->x;
|
|---|
| 383 | else if (y == v1->y)
|
|---|
| 384 | return v1->x;
|
|---|
| 385 |
|
|---|
| 386 | qint64 d = v1->x - v0->x;
|
|---|
| 387 | return (v0->x + d*(y - v0->y)/(v1->y-v0->y));
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | bool QTessellatorPrivate::EdgeSorter::operator() (const Edge *e1, const Edge *e2)
|
|---|
| 391 | {
|
|---|
| 392 | return e1->isLeftOf(*e2, y);
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 | QTessellatorPrivate::Scanline::Scanline()
|
|---|
| 397 | {
|
|---|
| 398 | edges = 0;
|
|---|
| 399 | edge_table = 0;
|
|---|
| 400 | old = 0;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | void QTessellatorPrivate::Scanline::init(int maxActiveEdges)
|
|---|
| 404 | {
|
|---|
| 405 | maxActiveEdges *= 2;
|
|---|
| 406 | if (!edges || maxActiveEdges > default_alloc) {
|
|---|
| 407 | max_edges = maxActiveEdges;
|
|---|
| 408 | int s = qMax(maxActiveEdges + 1, default_alloc + 1);
|
|---|
| 409 | edges = (Edge **)realloc(edges, s*sizeof(Edge *));
|
|---|
| 410 | edge_table = (Edge *)realloc(edge_table, s*sizeof(Edge));
|
|---|
| 411 | old = (Edge **)realloc(old, s*sizeof(Edge *));
|
|---|
| 412 | }
|
|---|
| 413 | size = 0;
|
|---|
| 414 | old_size = 0;
|
|---|
| 415 | first_unused = 0;
|
|---|
| 416 | for (int i = 0; i < maxActiveEdges; ++i)
|
|---|
| 417 | edge_table[i].edge = i+1;
|
|---|
| 418 | edge_table[maxActiveEdges].edge = -1;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | void QTessellatorPrivate::Scanline::done()
|
|---|
| 422 | {
|
|---|
| 423 | if (max_edges > default_alloc) {
|
|---|
| 424 | free(edges);
|
|---|
| 425 | free(old);
|
|---|
| 426 | free(edge_table);
|
|---|
| 427 | edges = 0;
|
|---|
| 428 | old = 0;
|
|---|
| 429 | edge_table = 0;
|
|---|
| 430 | }
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | QTessellatorPrivate::Scanline::~Scanline()
|
|---|
| 434 | {
|
|---|
| 435 | free(edges);
|
|---|
| 436 | free(old);
|
|---|
| 437 | free(edge_table);
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | int QTessellatorPrivate::Scanline::findEdgePosition(Q27Dot5 x, Q27Dot5 y) const
|
|---|
| 441 | {
|
|---|
| 442 | int min = 0;
|
|---|
| 443 | int max = size - 1;
|
|---|
| 444 | while (min < max) {
|
|---|
| 445 | int pos = min + ((max - min + 1) >> 1);
|
|---|
| 446 | Q27Dot5 ax = edges[pos]->positionAt(y);
|
|---|
| 447 | if (ax > x) {
|
|---|
| 448 | max = pos - 1;
|
|---|
| 449 | } else {
|
|---|
| 450 | min = pos;
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | return min;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | int QTessellatorPrivate::Scanline::findEdgePosition(const Edge &e) const
|
|---|
| 457 | {
|
|---|
| 458 | // qDebug() << ">> findEdgePosition";
|
|---|
| 459 | int min = 0;
|
|---|
| 460 | int max = size;
|
|---|
| 461 | while (min < max) {
|
|---|
| 462 | int pos = min + ((max - min) >> 1);
|
|---|
| 463 | // qDebug() << " " << min << max << pos << edges[pos]->isLeftOf(e, e.y0);
|
|---|
| 464 | if (edges[pos]->isLeftOf(e, e.v0->y)) {
|
|---|
| 465 | min = pos + 1;
|
|---|
| 466 | } else {
|
|---|
| 467 | max = pos;
|
|---|
| 468 | }
|
|---|
| 469 | }
|
|---|
| 470 | // qDebug() << "<< findEdgePosition got" << min;
|
|---|
| 471 | return min;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | int QTessellatorPrivate::Scanline::findEdge(int edge) const
|
|---|
| 475 | {
|
|---|
| 476 | for (int i = 0; i < size; ++i) {
|
|---|
| 477 | int item_edge = edges[i]->edge;
|
|---|
| 478 | if (item_edge == edge)
|
|---|
| 479 | return i;
|
|---|
| 480 | }
|
|---|
| 481 | //Q_ASSERT(false);
|
|---|
| 482 | return -1;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | void QTessellatorPrivate::Scanline::clearMarks()
|
|---|
| 486 | {
|
|---|
| 487 | for (int i = 0; i < size; ++i) {
|
|---|
| 488 | edges[i]->mark = false;
|
|---|
| 489 | edges[i]->intersect_left = false;
|
|---|
| 490 | edges[i]->intersect_right = false;
|
|---|
| 491 | }
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | void QTessellatorPrivate::Scanline::prepareLine()
|
|---|
| 495 | {
|
|---|
| 496 | Edge **end = edges + size;
|
|---|
| 497 | Edge **e = edges;
|
|---|
| 498 | Edge **o = old;
|
|---|
| 499 | while (e < end) {
|
|---|
| 500 | *o = *e;
|
|---|
| 501 | ++o;
|
|---|
| 502 | ++e;
|
|---|
| 503 | }
|
|---|
| 504 | old_size = size;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | void QTessellatorPrivate::Scanline::lineDone()
|
|---|
| 508 | {
|
|---|
| 509 | Edge **end = old + old_size;
|
|---|
| 510 | Edge **e = old;
|
|---|
| 511 | while (e < end) {
|
|---|
| 512 | if ((*e)->free) {
|
|---|
| 513 | (*e)->edge = first_unused;
|
|---|
| 514 | first_unused = (*e - edge_table);
|
|---|
| 515 | }
|
|---|
| 516 | ++e;
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | void QTessellatorPrivate::Scanline::insert(int pos, const Edge &e)
|
|---|
| 521 | {
|
|---|
| 522 | Edge *edge = edge_table + first_unused;
|
|---|
| 523 | first_unused = edge->edge;
|
|---|
| 524 | Q_ASSERT(first_unused != -1);
|
|---|
| 525 | *edge = e;
|
|---|
| 526 | memmove(edges + pos + 1, edges + pos, (size - pos)*sizeof(Edge *));
|
|---|
| 527 | edges[pos] = edge;
|
|---|
| 528 | ++size;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | void QTessellatorPrivate::Scanline::removeAt(int pos)
|
|---|
| 532 | {
|
|---|
| 533 | Edge *e = edges[pos];
|
|---|
| 534 | e->free = true;
|
|---|
| 535 | --size;
|
|---|
| 536 | memmove(edges + pos, edges + pos + 1, (size - pos)*sizeof(Edge *));
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | void QTessellatorPrivate::Scanline::markEdges(int pos1, int pos2)
|
|---|
| 540 | {
|
|---|
| 541 | if (pos2 < pos1)
|
|---|
| 542 | return;
|
|---|
| 543 |
|
|---|
| 544 | for (int i = pos1; i <= pos2; ++i)
|
|---|
| 545 | edges[i]->mark = true;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 | QTessellatorPrivate::Vertices::Vertices()
|
|---|
| 550 | {
|
|---|
| 551 | storage = 0;
|
|---|
| 552 | sorted = 0;
|
|---|
| 553 | allocated = 0;
|
|---|
| 554 | nPoints = 0;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | QTessellatorPrivate::Vertices::~Vertices()
|
|---|
| 558 | {
|
|---|
| 559 | if (storage) {
|
|---|
| 560 | free(storage);
|
|---|
| 561 | free(sorted);
|
|---|
| 562 | }
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | void QTessellatorPrivate::Vertices::init(int maxVertices)
|
|---|
| 566 | {
|
|---|
| 567 | if (!storage || maxVertices > allocated) {
|
|---|
| 568 | int size = qMax((int)default_alloc, maxVertices);
|
|---|
| 569 | storage = (Vertex *)realloc(storage, size*sizeof(Vertex));
|
|---|
| 570 | sorted = (Vertex **)realloc(sorted, size*sizeof(Vertex *));
|
|---|
| 571 | allocated = maxVertices;
|
|---|
| 572 | }
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | void QTessellatorPrivate::Vertices::done()
|
|---|
| 576 | {
|
|---|
| 577 | if (allocated > default_alloc) {
|
|---|
| 578 | free(storage);
|
|---|
| 579 | free(sorted);
|
|---|
| 580 | storage = 0;
|
|---|
| 581 | sorted = 0;
|
|---|
| 582 | allocated = 0;
|
|---|
| 583 | }
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 | static inline void fillTrapezoid(Q27Dot5 y1, Q27Dot5 y2, int left, int right,
|
|---|
| 589 | const QTessellatorPrivate::Vertices &vertices,
|
|---|
| 590 | QTessellator::Trapezoid *trap)
|
|---|
| 591 | {
|
|---|
| 592 | trap->top = y1;
|
|---|
| 593 | trap->bottom = y2;
|
|---|
| 594 | const QTessellatorPrivate::Vertex *v = vertices[left];
|
|---|
| 595 | trap->topLeft = v;
|
|---|
| 596 | trap->bottomLeft = vertices.next(v);
|
|---|
| 597 | if (trap->topLeft->y > trap->bottomLeft->y)
|
|---|
| 598 | qSwap(trap->topLeft,trap->bottomLeft);
|
|---|
| 599 | v = vertices[right];
|
|---|
| 600 | trap->topRight = v;
|
|---|
| 601 | trap->bottomRight = vertices.next(v);
|
|---|
| 602 | if (trap->topRight->y > trap->bottomRight->y)
|
|---|
| 603 | qSwap(trap->topRight, trap->bottomRight);
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | QRectF QTessellatorPrivate::collectAndSortVertices(const QPointF *points, int *maxActiveEdges)
|
|---|
| 607 | {
|
|---|
| 608 | *maxActiveEdges = 0;
|
|---|
| 609 | Vertex *v = vertices.storage;
|
|---|
| 610 | Vertex **vv = vertices.sorted;
|
|---|
| 611 |
|
|---|
| 612 | qreal xmin(points[0].x());
|
|---|
| 613 | qreal xmax(points[0].x());
|
|---|
| 614 | qreal ymin(points[0].y());
|
|---|
| 615 | qreal ymax(points[0].y());
|
|---|
| 616 |
|
|---|
| 617 | // collect vertex data
|
|---|
| 618 | Q27Dot5 y_prev = FloatToQ27Dot5(points[vertices.nPoints-1].y());
|
|---|
| 619 | Q27Dot5 x_next = FloatToQ27Dot5(points[0].x());
|
|---|
| 620 | Q27Dot5 y_next = FloatToQ27Dot5(points[0].y());
|
|---|
| 621 | int j = 0;
|
|---|
| 622 | int i = 0;
|
|---|
| 623 | while (i < vertices.nPoints) {
|
|---|
| 624 | Q27Dot5 y_curr = y_next;
|
|---|
| 625 |
|
|---|
| 626 | *vv = v;
|
|---|
| 627 |
|
|---|
| 628 | v->x = x_next;
|
|---|
| 629 | v->y = y_next;
|
|---|
| 630 | v->flags = 0;
|
|---|
| 631 |
|
|---|
| 632 | next_point:
|
|---|
| 633 |
|
|---|
| 634 | xmin = qMin(xmin, points[i+1].x());
|
|---|
| 635 | xmax = qMax(xmax, points[i+1].x());
|
|---|
| 636 | ymin = qMin(ymin, points[i+1].y());
|
|---|
| 637 | ymax = qMax(ymax, points[i+1].y());
|
|---|
| 638 |
|
|---|
| 639 | y_next = FloatToQ27Dot5(points[i+1].y());
|
|---|
| 640 | x_next = FloatToQ27Dot5(points[i+1].x());
|
|---|
| 641 |
|
|---|
| 642 | // skip vertices on top of each other
|
|---|
| 643 | if (v->x == x_next && v->y == y_next) {
|
|---|
| 644 | ++i;
|
|---|
| 645 | if (i < vertices.nPoints)
|
|---|
| 646 | goto next_point;
|
|---|
| 647 | Vertex *v0 = vertices.storage;
|
|---|
| 648 | v0->flags &= ~(LineBeforeStarts|LineBeforeEnds|LineBeforeHorizontal);
|
|---|
| 649 | if (y_prev < y_curr)
|
|---|
| 650 | v0->flags |= LineBeforeEnds;
|
|---|
| 651 | else if (y_prev > y_curr)
|
|---|
| 652 | v0->flags |= LineBeforeStarts;
|
|---|
| 653 | else
|
|---|
| 654 | v0->flags |= LineBeforeHorizontal;
|
|---|
| 655 | if ((v0->flags & (LineBeforeStarts|LineAfterStarts))
|
|---|
| 656 | && !(v0->flags & (LineAfterEnds|LineBeforeEnds)))
|
|---|
| 657 | *maxActiveEdges += 2;
|
|---|
| 658 | break;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | if (y_prev < y_curr)
|
|---|
| 662 | v->flags |= LineBeforeEnds;
|
|---|
| 663 | else if (y_prev > y_curr)
|
|---|
| 664 | v->flags |= LineBeforeStarts;
|
|---|
| 665 | else
|
|---|
| 666 | v->flags |= LineBeforeHorizontal;
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 | if (y_curr < y_next)
|
|---|
| 670 | v->flags |= LineAfterStarts;
|
|---|
| 671 | else if (y_curr > y_next)
|
|---|
| 672 | v->flags |= LineAfterEnds;
|
|---|
| 673 | else
|
|---|
| 674 | v->flags |= LineAfterHorizontal;
|
|---|
| 675 | // ### could probably get better limit by looping over sorted list and counting down on ending edges
|
|---|
| 676 | if ((v->flags & (LineBeforeStarts|LineAfterStarts))
|
|---|
| 677 | && !(v->flags & (LineAfterEnds|LineBeforeEnds)))
|
|---|
| 678 | *maxActiveEdges += 2;
|
|---|
| 679 | y_prev = y_curr;
|
|---|
| 680 | ++v;
|
|---|
| 681 | ++vv;
|
|---|
| 682 | ++j;
|
|---|
| 683 | ++i;
|
|---|
| 684 | }
|
|---|
| 685 | vertices.nPoints = j;
|
|---|
| 686 |
|
|---|
| 687 | QDEBUG() << "maxActiveEdges=" << *maxActiveEdges;
|
|---|
| 688 | vv = vertices.sorted;
|
|---|
| 689 | qSort(vv, vv + vertices.nPoints, compareVertex);
|
|---|
| 690 |
|
|---|
| 691 | return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | struct QCoincidingEdge {
|
|---|
| 695 | QTessellatorPrivate::Vertex *start;
|
|---|
| 696 | QTessellatorPrivate::Vertex *end;
|
|---|
| 697 | bool used;
|
|---|
| 698 | bool before;
|
|---|
| 699 |
|
|---|
| 700 | inline bool operator<(const QCoincidingEdge &e2) const
|
|---|
| 701 | {
|
|---|
| 702 | return end->y == e2.end->y ? end->x < e2.end->x : end->y < e2.end->y;
|
|---|
| 703 | }
|
|---|
| 704 | };
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 | static void cancelEdges(QCoincidingEdge &e1, QCoincidingEdge &e2)
|
|---|
| 708 | {
|
|---|
| 709 | if (e1.before) {
|
|---|
| 710 | e1.start->flags &= ~(LineBeforeStarts|LineBeforeHorizontal);
|
|---|
| 711 | e1.end->flags &= ~(LineAfterEnds|LineAfterHorizontal);
|
|---|
| 712 | } else {
|
|---|
| 713 | e1.start->flags &= ~(LineAfterStarts|LineAfterHorizontal);
|
|---|
| 714 | e1.end->flags &= ~(LineBeforeEnds|LineBeforeHorizontal);
|
|---|
| 715 | }
|
|---|
| 716 | if (e2.before) {
|
|---|
| 717 | e2.start->flags &= ~(LineBeforeStarts|LineBeforeHorizontal);
|
|---|
| 718 | e2.end->flags &= ~(LineAfterEnds|LineAfterHorizontal);
|
|---|
| 719 | } else {
|
|---|
| 720 | e2.start->flags &= ~(LineAfterStarts|LineAfterHorizontal);
|
|---|
| 721 | e2.end->flags &= ~(LineBeforeEnds|LineBeforeHorizontal);
|
|---|
| 722 | }
|
|---|
| 723 | e1.used = e2.used = true;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | void QTessellatorPrivate::cancelCoincidingEdges()
|
|---|
| 727 | {
|
|---|
| 728 | Vertex **vv = vertices.sorted;
|
|---|
| 729 |
|
|---|
| 730 | QCoincidingEdge *tl = 0;
|
|---|
| 731 | int tlSize = 0;
|
|---|
| 732 |
|
|---|
| 733 | for (int i = 0; i < vertices.nPoints - 1; ++i) {
|
|---|
| 734 | Vertex *v = vv[i];
|
|---|
| 735 | int testListSize = 0;
|
|---|
| 736 | while (i < vertices.nPoints - 1) {
|
|---|
| 737 | Vertex *n = vv[i];
|
|---|
| 738 | if (v->x != n->x || v->y != n->y)
|
|---|
| 739 | break;
|
|---|
| 740 |
|
|---|
| 741 | if (testListSize > tlSize - 2) {
|
|---|
| 742 | tlSize = qMax(tlSize*2, 16);
|
|---|
| 743 | tl = (QCoincidingEdge *)realloc(tl, tlSize*sizeof(QCoincidingEdge));
|
|---|
| 744 | }
|
|---|
| 745 | if (n->flags & (LineBeforeStarts|LineBeforeHorizontal)) {
|
|---|
| 746 | tl[testListSize].start = n;
|
|---|
| 747 | tl[testListSize].end = vertices.prev(n);
|
|---|
| 748 | tl[testListSize].used = false;
|
|---|
| 749 | tl[testListSize].before = true;
|
|---|
| 750 | ++testListSize;
|
|---|
| 751 | }
|
|---|
| 752 | if (n->flags & (LineAfterStarts|LineAfterHorizontal)) {
|
|---|
| 753 | tl[testListSize].start = n;
|
|---|
| 754 | tl[testListSize].end = vertices.next(n);
|
|---|
| 755 | tl[testListSize].used = false;
|
|---|
| 756 | tl[testListSize].before = false;
|
|---|
| 757 | ++testListSize;
|
|---|
| 758 | }
|
|---|
| 759 | ++i;
|
|---|
| 760 | }
|
|---|
| 761 | if (!testListSize)
|
|---|
| 762 | continue;
|
|---|
| 763 |
|
|---|
| 764 | qSort(tl, tl + testListSize);
|
|---|
| 765 |
|
|---|
| 766 | for (int j = 0; j < testListSize; ++j) {
|
|---|
| 767 | if (tl[j].used)
|
|---|
| 768 | continue;
|
|---|
| 769 |
|
|---|
| 770 | for (int k = j + 1; k < testListSize; ++k) {
|
|---|
| 771 | if (tl[j].end->x != tl[k].end->x
|
|---|
| 772 | || tl[j].end->y != tl[k].end->y
|
|---|
| 773 | || tl[k].used)
|
|---|
| 774 | break;
|
|---|
| 775 |
|
|---|
| 776 | if (!winding || tl[j].before != tl[k].before) {
|
|---|
| 777 | cancelEdges(tl[j], tl[k]);
|
|---|
| 778 | break;
|
|---|
| 779 | }
|
|---|
| 780 | ++k;
|
|---|
| 781 | }
|
|---|
| 782 | ++j;
|
|---|
| 783 | }
|
|---|
| 784 | }
|
|---|
| 785 | free(tl);
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 | void QTessellatorPrivate::emitEdges(QTessellator *tessellator)
|
|---|
| 790 | {
|
|---|
| 791 | //QDEBUG() << "TRAPS:";
|
|---|
| 792 | if (!scanline.old_size)
|
|---|
| 793 | return;
|
|---|
| 794 |
|
|---|
| 795 | // emit edges
|
|---|
| 796 | if (winding) {
|
|---|
| 797 | // winding fill rule
|
|---|
| 798 | int w = 0;
|
|---|
| 799 |
|
|---|
| 800 | scanline.old[0]->y_left = y;
|
|---|
| 801 |
|
|---|
| 802 | for (int i = 0; i < scanline.old_size - 1; ++i) {
|
|---|
| 803 | Edge *left = scanline.old[i];
|
|---|
| 804 | Edge *right = scanline.old[i+1];
|
|---|
| 805 | w += left->winding;
|
|---|
| 806 | // qDebug() << "i=" << i << "edge->winding=" << left->winding << "winding=" << winding;
|
|---|
| 807 | if (w == 0) {
|
|---|
| 808 | left->y_right = y;
|
|---|
| 809 | right->y_left = y;
|
|---|
| 810 | } else if (!emit_clever || left->mark || right->mark) {
|
|---|
| 811 | Q27Dot5 top = qMax(left->y_right, right->y_left);
|
|---|
| 812 | if (top != y) {
|
|---|
| 813 | QTessellator::Trapezoid trap;
|
|---|
| 814 | fillTrapezoid(top, y, left->edge, right->edge, vertices, &trap);
|
|---|
| 815 | tessellator->addTrap(trap);
|
|---|
| 816 | // QDEBUG() << " top=" << Q27Dot5ToDouble(top) << "left=" << left->edge << "right=" << right->edge;
|
|---|
| 817 | }
|
|---|
| 818 | right->y_left = y;
|
|---|
| 819 | left->y_right = y;
|
|---|
| 820 | }
|
|---|
| 821 | left->mark = false;
|
|---|
| 822 | }
|
|---|
| 823 | if (scanline.old[scanline.old_size - 1]->mark) {
|
|---|
| 824 | scanline.old[scanline.old_size - 1]->y_right = y;
|
|---|
| 825 | scanline.old[scanline.old_size - 1]->mark = false;
|
|---|
| 826 | }
|
|---|
| 827 | } else {
|
|---|
| 828 | // odd-even fill rule
|
|---|
| 829 | for (int i = 0; i < scanline.old_size; i += 2) {
|
|---|
| 830 | Edge *left = scanline.old[i];
|
|---|
| 831 | Edge *right = scanline.old[i+1];
|
|---|
| 832 | if (!emit_clever || left->mark || right->mark) {
|
|---|
| 833 | Q27Dot5 top = qMax(left->y_right, right->y_left);
|
|---|
| 834 | if (top != y) {
|
|---|
| 835 | QTessellator::Trapezoid trap;
|
|---|
| 836 | fillTrapezoid(top, y, left->edge, right->edge, vertices, &trap);
|
|---|
| 837 | tessellator->addTrap(trap);
|
|---|
| 838 | }
|
|---|
| 839 | // QDEBUG() << " top=" << Q27Dot5ToDouble(top) << "left=" << left->edge << "right=" << right->edge;
|
|---|
| 840 | left->y_left = y;
|
|---|
| 841 | left->y_right = y;
|
|---|
| 842 | right->y_left = y;
|
|---|
| 843 | right->y_right = y;
|
|---|
| 844 | left->mark = right->mark = false;
|
|---|
| 845 | }
|
|---|
| 846 | }
|
|---|
| 847 | }
|
|---|
| 848 | }
|
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 | void QTessellatorPrivate::processIntersections()
|
|---|
| 852 | {
|
|---|
| 853 | QDEBUG() << "PROCESS INTERSECTIONS";
|
|---|
| 854 | // process intersections
|
|---|
| 855 | while (!intersections.isEmpty()) {
|
|---|
| 856 | Intersections::iterator it = intersections.begin();
|
|---|
| 857 | if (it.key().y != y)
|
|---|
| 858 | break;
|
|---|
| 859 |
|
|---|
| 860 | // swap edges
|
|---|
| 861 | QDEBUG() << " swapping intersecting edges ";
|
|---|
| 862 | int min = scanline.size;
|
|---|
| 863 | int max = 0;
|
|---|
| 864 | Q27Dot5 xmin = INT_MAX;
|
|---|
| 865 | Q27Dot5 xmax = INT_MIN;
|
|---|
| 866 | int num = 0;
|
|---|
| 867 | while (1) {
|
|---|
| 868 | const Intersection &i = it.key();
|
|---|
| 869 | int next = it->next;
|
|---|
| 870 |
|
|---|
| 871 | int edgePos = scanline.findEdge(i.edge);
|
|---|
| 872 | if (edgePos >= 0) {
|
|---|
| 873 | ++num;
|
|---|
| 874 | min = qMin(edgePos, min);
|
|---|
| 875 | max = qMax(edgePos, max);
|
|---|
| 876 | Edge *edge = scanline.edges[edgePos];
|
|---|
| 877 | xmin = qMin(xmin, edge->positionAt(y));
|
|---|
| 878 | xmax = qMax(xmax, edge->positionAt(y));
|
|---|
| 879 | }
|
|---|
| 880 | Intersection key;
|
|---|
| 881 | key.y = y;
|
|---|
| 882 | key.edge = next;
|
|---|
| 883 | it = intersections.find(key);
|
|---|
| 884 | intersections.remove(i);
|
|---|
| 885 | if (it == intersections.end())
|
|---|
| 886 | break;
|
|---|
| 887 | }
|
|---|
| 888 | if (num < 2)
|
|---|
| 889 | continue;
|
|---|
| 890 |
|
|---|
| 891 | Q_ASSERT(min != max);
|
|---|
| 892 | QDEBUG() << "sorting between" << min << "and" << max << "xpos=" << xmin << xmax;
|
|---|
| 893 | while (min > 0 && scanline.edges[min - 1]->positionAt(y) >= xmin) {
|
|---|
| 894 | QDEBUG() << " adding edge on left";
|
|---|
| 895 | --min;
|
|---|
| 896 | }
|
|---|
| 897 | while (max < scanline.size - 1 && scanline.edges[max + 1]->positionAt(y) <= xmax) {
|
|---|
| 898 | QDEBUG() << " adding edge on right";
|
|---|
| 899 | ++max;
|
|---|
| 900 | }
|
|---|
| 901 |
|
|---|
| 902 | qSort(scanline.edges + min, scanline.edges + max + 1, EdgeSorter(y));
|
|---|
| 903 | #ifdef DEBUG
|
|---|
| 904 | for (int i = min; i <= max; ++i)
|
|---|
| 905 | QDEBUG() << " " << scanline.edges[i]->edge << "at pos" << i;
|
|---|
| 906 | #endif
|
|---|
| 907 | for (int i = min; i <= max; ++i) {
|
|---|
| 908 | Edge *edge = scanline.edges[i];
|
|---|
| 909 | edge->intersect_left = true;
|
|---|
| 910 | edge->intersect_right = true;
|
|---|
| 911 | edge->mark = true;
|
|---|
| 912 | }
|
|---|
| 913 | }
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | void QTessellatorPrivate::removeEdges()
|
|---|
| 917 | {
|
|---|
| 918 | int cv = currentVertex;
|
|---|
| 919 | while (cv < vertices.nPoints) {
|
|---|
| 920 | const Vertex *v = vertices.sorted[cv];
|
|---|
| 921 | if (v->y > y)
|
|---|
| 922 | break;
|
|---|
| 923 | if (v->flags & LineBeforeEnds) {
|
|---|
| 924 | QDEBUG() << " removing edge" << vertices.prevPos(v);
|
|---|
| 925 | int pos = scanline.findEdge(vertices.prevPos(v));
|
|---|
| 926 | if (pos == -1)
|
|---|
| 927 | continue;
|
|---|
| 928 | scanline.edges[pos]->mark = true;
|
|---|
| 929 | if (pos > 0)
|
|---|
| 930 | scanline.edges[pos - 1]->intersect_right = true;
|
|---|
| 931 | if (pos < scanline.size - 1)
|
|---|
| 932 | scanline.edges[pos + 1]->intersect_left = true;
|
|---|
| 933 | scanline.removeAt(pos);
|
|---|
| 934 | }
|
|---|
| 935 | if (v->flags & LineAfterEnds) {
|
|---|
| 936 | QDEBUG() << " removing edge" << vertices.position(v);
|
|---|
| 937 | int pos = scanline.findEdge(vertices.position(v));
|
|---|
| 938 | if (pos == -1)
|
|---|
| 939 | continue;
|
|---|
| 940 | scanline.edges[pos]->mark = true;
|
|---|
| 941 | if (pos > 0)
|
|---|
| 942 | scanline.edges[pos - 1]->intersect_right = true;
|
|---|
| 943 | if (pos < scanline.size - 1)
|
|---|
| 944 | scanline.edges[pos + 1]->intersect_left = true;
|
|---|
| 945 | scanline.removeAt(pos);
|
|---|
| 946 | }
|
|---|
| 947 | ++cv;
|
|---|
| 948 | }
|
|---|
| 949 | }
|
|---|
| 950 |
|
|---|
| 951 | void QTessellatorPrivate::addEdges()
|
|---|
| 952 | {
|
|---|
| 953 | while (currentVertex < vertices.nPoints) {
|
|---|
| 954 | const Vertex *v = vertices.sorted[currentVertex];
|
|---|
| 955 | if (v->y > y)
|
|---|
| 956 | break;
|
|---|
| 957 | if (v->flags & LineBeforeStarts) {
|
|---|
| 958 | // add new edge
|
|---|
| 959 | int start = vertices.prevPos(v);
|
|---|
| 960 | Edge e(vertices, start);
|
|---|
| 961 | int pos = scanline.findEdgePosition(e);
|
|---|
| 962 | QDEBUG() << " adding edge" << start << "at position" << pos;
|
|---|
| 963 | scanline.insert(pos, e);
|
|---|
| 964 | if (!mark_clever || !(v->flags & LineAfterEnds)) {
|
|---|
| 965 | if (pos > 0)
|
|---|
| 966 | scanline.edges[pos - 1]->mark = true;
|
|---|
| 967 | if (pos < scanline.size - 1)
|
|---|
| 968 | scanline.edges[pos + 1]->mark = true;
|
|---|
| 969 | }
|
|---|
| 970 | }
|
|---|
| 971 | if (v->flags & LineAfterStarts) {
|
|---|
| 972 | Edge e(vertices, vertices.position(v));
|
|---|
| 973 | int pos = scanline.findEdgePosition(e);
|
|---|
| 974 | QDEBUG() << " adding edge" << vertices.position(v) << "at position" << pos;
|
|---|
| 975 | scanline.insert(pos, e);
|
|---|
| 976 | if (!mark_clever || !(v->flags & LineBeforeEnds)) {
|
|---|
| 977 | if (pos > 0)
|
|---|
| 978 | scanline.edges[pos - 1]->mark = true;
|
|---|
| 979 | if (pos < scanline.size - 1)
|
|---|
| 980 | scanline.edges[pos + 1]->mark = true;
|
|---|
| 981 | }
|
|---|
| 982 | }
|
|---|
| 983 | if (v->flags & LineAfterHorizontal) {
|
|---|
| 984 | int pos1 = scanline.findEdgePosition(v->x, v->y);
|
|---|
| 985 | const Vertex *next = vertices.next(v);
|
|---|
| 986 | Q_ASSERT(v->y == next->y);
|
|---|
| 987 | int pos2 = scanline.findEdgePosition(next->x, next->y);
|
|---|
| 988 | if (pos2 < pos1)
|
|---|
| 989 | qSwap(pos1, pos2);
|
|---|
| 990 | if (pos1 > 0)
|
|---|
| 991 | --pos1;
|
|---|
| 992 | if (pos2 == scanline.size)
|
|---|
| 993 | --pos2;
|
|---|
| 994 | //QDEBUG() << "marking horizontal edge from " << pos1 << "to" << pos2;
|
|---|
| 995 | scanline.markEdges(pos1, pos2);
|
|---|
| 996 | }
|
|---|
| 997 | ++currentVertex;
|
|---|
| 998 | }
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | #ifdef DEBUG
|
|---|
| 1002 | static void checkLinkChain(const QTessellatorPrivate::Intersections &intersections,
|
|---|
| 1003 | QTessellatorPrivate::Intersection i)
|
|---|
| 1004 | {
|
|---|
| 1005 | // qDebug() << " Link chain: ";
|
|---|
| 1006 | int end = i.edge;
|
|---|
| 1007 | while (1) {
|
|---|
| 1008 | QTessellatorPrivate::IntersectionLink l = intersections.value(i);
|
|---|
| 1009 | // qDebug() << " " << i.edge << "next=" << l.next << "prev=" << l.prev;
|
|---|
| 1010 | if (l.next == end)
|
|---|
| 1011 | break;
|
|---|
| 1012 | Q_ASSERT(l.next != -1);
|
|---|
| 1013 | Q_ASSERT(l.prev != -1);
|
|---|
| 1014 |
|
|---|
| 1015 | QTessellatorPrivate::Intersection i2 = i;
|
|---|
| 1016 | i2.edge = l.next;
|
|---|
| 1017 | QTessellatorPrivate::IntersectionLink l2 = intersections.value(i2);
|
|---|
| 1018 |
|
|---|
| 1019 | Q_ASSERT(l2.next != -1);
|
|---|
| 1020 | Q_ASSERT(l2.prev != -1);
|
|---|
| 1021 | Q_ASSERT(l.next == i2.edge);
|
|---|
| 1022 | Q_ASSERT(l2.prev == i.edge);
|
|---|
| 1023 | i = i2;
|
|---|
| 1024 | }
|
|---|
| 1025 | }
|
|---|
| 1026 | #endif
|
|---|
| 1027 |
|
|---|
| 1028 | bool QTessellatorPrivate::edgeInChain(Intersection i, int edge)
|
|---|
| 1029 | {
|
|---|
| 1030 | int end = i.edge;
|
|---|
| 1031 | while (1) {
|
|---|
| 1032 | if (i.edge == edge)
|
|---|
| 1033 | return true;
|
|---|
| 1034 | IntersectionLink l = intersections.value(i);
|
|---|
| 1035 | if (l.next == end)
|
|---|
| 1036 | break;
|
|---|
| 1037 | Q_ASSERT(l.next != -1);
|
|---|
| 1038 | Q_ASSERT(l.prev != -1);
|
|---|
| 1039 |
|
|---|
| 1040 | Intersection i2 = i;
|
|---|
| 1041 | i2.edge = l.next;
|
|---|
| 1042 |
|
|---|
| 1043 | #ifndef QT_NO_DEBUG
|
|---|
| 1044 | IntersectionLink l2 = intersections.value(i2);
|
|---|
| 1045 | Q_ASSERT(l2.next != -1);
|
|---|
| 1046 | Q_ASSERT(l2.prev != -1);
|
|---|
| 1047 | Q_ASSERT(l.next == i2.edge);
|
|---|
| 1048 | Q_ASSERT(l2.prev == i.edge);
|
|---|
| 1049 | #endif
|
|---|
| 1050 | i = i2;
|
|---|
| 1051 | }
|
|---|
| 1052 | return false;
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 |
|
|---|
| 1056 | void QTessellatorPrivate::addIntersection(const Edge *e1, const Edge *e2)
|
|---|
| 1057 | {
|
|---|
| 1058 | const IntersectionLink emptyLink = {-1, -1};
|
|---|
| 1059 |
|
|---|
| 1060 | int next = vertices.nextPos(vertices[e1->edge]);
|
|---|
| 1061 | if (e2->edge == next)
|
|---|
| 1062 | return;
|
|---|
| 1063 | int prev = vertices.prevPos(vertices[e1->edge]);
|
|---|
| 1064 | if (e2->edge == prev)
|
|---|
| 1065 | return;
|
|---|
| 1066 |
|
|---|
| 1067 | Q27Dot5 yi;
|
|---|
| 1068 | bool det_positive;
|
|---|
| 1069 | bool isect = e1->intersect(*e2, &yi, &det_positive);
|
|---|
| 1070 | QDEBUG("checking edges %d and %d", e1->edge, e2->edge);
|
|---|
| 1071 | if (!isect) {
|
|---|
| 1072 | QDEBUG() << " no intersection";
|
|---|
| 1073 | return;
|
|---|
| 1074 | }
|
|---|
| 1075 |
|
|---|
| 1076 | // don't emit an intersection if it's at the start of a line segment or above us
|
|---|
| 1077 | if (yi <= y) {
|
|---|
| 1078 | if (!det_positive)
|
|---|
| 1079 | return;
|
|---|
| 1080 | QDEBUG() << " ----->>>>>> WRONG ORDER!";
|
|---|
| 1081 | yi = y;
|
|---|
| 1082 | }
|
|---|
| 1083 | QDEBUG() << " between edges " << e1->edge << "and" << e2->edge << "at point ("
|
|---|
| 1084 | << Q27Dot5ToDouble(yi) << ")";
|
|---|
| 1085 |
|
|---|
| 1086 | Intersection i1;
|
|---|
| 1087 | i1.y = yi;
|
|---|
| 1088 | i1.edge = e1->edge;
|
|---|
| 1089 | IntersectionLink link1 = intersections.value(i1, emptyLink);
|
|---|
| 1090 | Intersection i2;
|
|---|
| 1091 | i2.y = yi;
|
|---|
| 1092 | i2.edge = e2->edge;
|
|---|
| 1093 | IntersectionLink link2 = intersections.value(i2, emptyLink);
|
|---|
| 1094 |
|
|---|
| 1095 | // new pair of edges
|
|---|
| 1096 | if (link1.next == -1 && link2.next == -1) {
|
|---|
| 1097 | link1.next = link1.prev = i2.edge;
|
|---|
| 1098 | link2.next = link2.prev = i1.edge;
|
|---|
| 1099 | } else if (link1.next == i2.edge || link1.prev == i2.edge
|
|---|
| 1100 | || link2.next == i1.edge || link2.prev == i1.edge) {
|
|---|
| 1101 | #ifdef DEBUG
|
|---|
| 1102 | checkLinkChain(intersections, i1);
|
|---|
| 1103 | checkLinkChain(intersections, i2);
|
|---|
| 1104 | Q_ASSERT(edgeInChain(i1, i2.edge));
|
|---|
| 1105 | #endif
|
|---|
| 1106 | return;
|
|---|
| 1107 | } else if (link1.next == -1 || link2.next == -1) {
|
|---|
| 1108 | if (link2.next == -1) {
|
|---|
| 1109 | qSwap(i1, i2);
|
|---|
| 1110 | qSwap(link1, link2);
|
|---|
| 1111 | }
|
|---|
| 1112 | Q_ASSERT(link1.next == -1);
|
|---|
| 1113 | #ifdef DEBUG
|
|---|
| 1114 | checkLinkChain(intersections, i2);
|
|---|
| 1115 | #endif
|
|---|
| 1116 | // only i2 in list
|
|---|
| 1117 | link1.next = i2.edge;
|
|---|
| 1118 | link1.prev = link2.prev;
|
|---|
| 1119 | link2.prev = i1.edge;
|
|---|
| 1120 | Intersection other;
|
|---|
| 1121 | other.y = yi;
|
|---|
| 1122 | other.edge = link1.prev;
|
|---|
| 1123 | IntersectionLink link = intersections.value(other, emptyLink);
|
|---|
| 1124 | Q_ASSERT(link.next == i2.edge);
|
|---|
| 1125 | Q_ASSERT(link.prev != -1);
|
|---|
| 1126 | link.next = i1.edge;
|
|---|
| 1127 | intersections.insert(other, link);
|
|---|
| 1128 | } else {
|
|---|
| 1129 | bool connected = edgeInChain(i1, i2.edge);
|
|---|
| 1130 | if (connected)
|
|---|
| 1131 | return;
|
|---|
| 1132 | #ifdef DEBUG
|
|---|
| 1133 | checkLinkChain(intersections, i1);
|
|---|
| 1134 | checkLinkChain(intersections, i2);
|
|---|
| 1135 | #endif
|
|---|
| 1136 | // both already in some list. Have to make sure they are connected
|
|---|
| 1137 | // this can be done by cutting open the ring(s) after the two eges and
|
|---|
| 1138 | // connecting them again
|
|---|
| 1139 | Intersection other1;
|
|---|
| 1140 | other1.y = yi;
|
|---|
| 1141 | other1.edge = link1.next;
|
|---|
| 1142 | IntersectionLink linko1 = intersections.value(other1, emptyLink);
|
|---|
| 1143 | Intersection other2;
|
|---|
| 1144 | other2.y = yi;
|
|---|
| 1145 | other2.edge = link2.next;
|
|---|
| 1146 | IntersectionLink linko2 = intersections.value(other2, emptyLink);
|
|---|
| 1147 |
|
|---|
| 1148 | linko1.prev = i2.edge;
|
|---|
| 1149 | link2.next = other1.edge;
|
|---|
| 1150 |
|
|---|
| 1151 | linko2.prev = i1.edge;
|
|---|
| 1152 | link1.next = other2.edge;
|
|---|
| 1153 | intersections.insert(other1, linko1);
|
|---|
| 1154 | intersections.insert(other2, linko2);
|
|---|
| 1155 | }
|
|---|
| 1156 | intersections.insert(i1, link1);
|
|---|
| 1157 | intersections.insert(i2, link2);
|
|---|
| 1158 | #ifdef DEBUG
|
|---|
| 1159 | checkLinkChain(intersections, i1);
|
|---|
| 1160 | checkLinkChain(intersections, i2);
|
|---|
| 1161 | Q_ASSERT(edgeInChain(i1, i2.edge));
|
|---|
| 1162 | #endif
|
|---|
| 1163 | return;
|
|---|
| 1164 |
|
|---|
| 1165 | }
|
|---|
| 1166 |
|
|---|
| 1167 |
|
|---|
| 1168 | void QTessellatorPrivate::addIntersections()
|
|---|
| 1169 | {
|
|---|
| 1170 | if (scanline.size) {
|
|---|
| 1171 | QDEBUG() << "INTERSECTIONS";
|
|---|
| 1172 | // check marked edges for intersections
|
|---|
| 1173 | #ifdef DEBUG
|
|---|
| 1174 | for (int i = 0; i < scanline.size; ++i) {
|
|---|
| 1175 | Edge *e = scanline.edges[i];
|
|---|
| 1176 | QDEBUG() << " " << i << e->edge << "isect=(" << e->intersect_left << e->intersect_right
|
|---|
| 1177 | << ")";
|
|---|
| 1178 | }
|
|---|
| 1179 | #endif
|
|---|
| 1180 |
|
|---|
| 1181 | for (int i = 0; i < scanline.size - 1; ++i) {
|
|---|
| 1182 | Edge *e1 = scanline.edges[i];
|
|---|
| 1183 | Edge *e2 = scanline.edges[i + 1];
|
|---|
| 1184 | // check for intersection
|
|---|
| 1185 | if (e1->intersect_right || e2->intersect_left)
|
|---|
| 1186 | addIntersection(e1, e2);
|
|---|
| 1187 | }
|
|---|
| 1188 | }
|
|---|
| 1189 | #if 0
|
|---|
| 1190 | if (intersections.constBegin().key().y == y) {
|
|---|
| 1191 | QDEBUG() << "----------------> intersection on same line";
|
|---|
| 1192 | scanline.clearMarks();
|
|---|
| 1193 | scanline.processIntersections(y, &intersections);
|
|---|
| 1194 | goto redo;
|
|---|
| 1195 | }
|
|---|
| 1196 | #endif
|
|---|
| 1197 | }
|
|---|
| 1198 |
|
|---|
| 1199 |
|
|---|
| 1200 | QTessellator::QTessellator()
|
|---|
| 1201 | {
|
|---|
| 1202 | d = new QTessellatorPrivate;
|
|---|
| 1203 | }
|
|---|
| 1204 |
|
|---|
| 1205 | QTessellator::~QTessellator()
|
|---|
| 1206 | {
|
|---|
| 1207 | delete d;
|
|---|
| 1208 | }
|
|---|
| 1209 |
|
|---|
| 1210 | void QTessellator::setWinding(bool w)
|
|---|
| 1211 | {
|
|---|
| 1212 | d->winding = w;
|
|---|
| 1213 | }
|
|---|
| 1214 |
|
|---|
| 1215 |
|
|---|
| 1216 | QRectF QTessellator::tessellate(const QPointF *points, int nPoints)
|
|---|
| 1217 | {
|
|---|
| 1218 | Q_ASSERT(points[0] == points[nPoints-1]);
|
|---|
| 1219 | --nPoints;
|
|---|
| 1220 |
|
|---|
| 1221 | #ifdef DEBUG
|
|---|
| 1222 | QDEBUG()<< "POINTS:";
|
|---|
| 1223 | for (int i = 0; i < nPoints; ++i) {
|
|---|
| 1224 | QDEBUG() << points[i];
|
|---|
| 1225 | }
|
|---|
| 1226 | #endif
|
|---|
| 1227 |
|
|---|
| 1228 | // collect edges and calculate bounds
|
|---|
| 1229 | d->vertices.nPoints = nPoints;
|
|---|
| 1230 | d->vertices.init(nPoints);
|
|---|
| 1231 |
|
|---|
| 1232 | int maxActiveEdges = 0;
|
|---|
| 1233 | QRectF br = d->collectAndSortVertices(points, &maxActiveEdges);
|
|---|
| 1234 | d->cancelCoincidingEdges();
|
|---|
| 1235 |
|
|---|
| 1236 | #ifdef DEBUG
|
|---|
| 1237 | QDEBUG() << "nPoints = " << nPoints << "using " << d->vertices.nPoints;
|
|---|
| 1238 | QDEBUG()<< "VERTICES:";
|
|---|
| 1239 | for (int i = 0; i < d->vertices.nPoints; ++i) {
|
|---|
| 1240 | QDEBUG() << " " << i << ": "
|
|---|
| 1241 | << "point=" << d->vertices.position(d->vertices.sorted[i])
|
|---|
| 1242 | << "flags=" << d->vertices.sorted[i]->flags
|
|---|
| 1243 | << "pos=(" << Q27Dot5ToDouble(d->vertices.sorted[i]->x) << "/"
|
|---|
| 1244 | << Q27Dot5ToDouble(d->vertices.sorted[i]->y) << ")";
|
|---|
| 1245 | }
|
|---|
| 1246 | #endif
|
|---|
| 1247 |
|
|---|
| 1248 | d->scanline.init(maxActiveEdges);
|
|---|
| 1249 | d->y = INT_MIN/256;
|
|---|
| 1250 | d->currentVertex = 0;
|
|---|
| 1251 |
|
|---|
| 1252 | while (d->currentVertex < d->vertices.nPoints) {
|
|---|
| 1253 | d->scanline.clearMarks();
|
|---|
| 1254 |
|
|---|
| 1255 | d->y = d->vertices.sorted[d->currentVertex]->y;
|
|---|
| 1256 | if (!d->intersections.isEmpty())
|
|---|
| 1257 | d->y = qMin(d->y, d->intersections.constBegin().key().y);
|
|---|
| 1258 |
|
|---|
| 1259 | QDEBUG()<< "===== SCANLINE: y =" << Q27Dot5ToDouble(d->y) << " =====";
|
|---|
| 1260 |
|
|---|
| 1261 | d->scanline.prepareLine();
|
|---|
| 1262 | d->processIntersections();
|
|---|
| 1263 | d->removeEdges();
|
|---|
| 1264 | d->addEdges();
|
|---|
| 1265 | d->addIntersections();
|
|---|
| 1266 | d->emitEdges(this);
|
|---|
| 1267 | d->scanline.lineDone();
|
|---|
| 1268 |
|
|---|
| 1269 | #ifdef DEBUG
|
|---|
| 1270 | QDEBUG()<< "===== edges:";
|
|---|
| 1271 | for (int i = 0; i < d->scanline.size; ++i) {
|
|---|
| 1272 | QDEBUG() << " " << d->scanline.edges[i]->edge
|
|---|
| 1273 | << "p0= (" << Q27Dot5ToDouble(d->scanline.edges[i]->v0->x)
|
|---|
| 1274 | << "/" << Q27Dot5ToDouble(d->scanline.edges[i]->v0->y)
|
|---|
| 1275 | << ") p1= (" << Q27Dot5ToDouble(d->scanline.edges[i]->v1->x)
|
|---|
| 1276 | << "/" << Q27Dot5ToDouble(d->scanline.edges[i]->v1->y) << ")"
|
|---|
| 1277 | << "x=" << Q27Dot5ToDouble(d->scanline.edges[i]->positionAt(d->y))
|
|---|
| 1278 | << "isLeftOfNext="
|
|---|
| 1279 | << ((i < d->scanline.size - 1)
|
|---|
| 1280 | ? d->scanline.edges[i]->isLeftOf(*d->scanline.edges[i+1], d->y)
|
|---|
| 1281 | : true);
|
|---|
| 1282 | }
|
|---|
| 1283 | #endif
|
|---|
| 1284 | }
|
|---|
| 1285 |
|
|---|
| 1286 | d->scanline.done();
|
|---|
| 1287 | d->intersections.clear();
|
|---|
| 1288 | return br;
|
|---|
| 1289 | }
|
|---|
| 1290 |
|
|---|
| 1291 | // tessellates the given convex polygon
|
|---|
| 1292 | void QTessellator::tessellateConvex(const QPointF *points, int nPoints)
|
|---|
| 1293 | {
|
|---|
| 1294 | Q_ASSERT(points[0] == points[nPoints-1]);
|
|---|
| 1295 | --nPoints;
|
|---|
| 1296 |
|
|---|
| 1297 | d->vertices.nPoints = nPoints;
|
|---|
| 1298 | d->vertices.init(nPoints);
|
|---|
| 1299 |
|
|---|
| 1300 | for (int i = 0; i < nPoints; ++i) {
|
|---|
| 1301 | d->vertices[i]->x = FloatToQ27Dot5(points[i].x());
|
|---|
| 1302 | d->vertices[i]->y = FloatToQ27Dot5(points[i].y());
|
|---|
| 1303 | }
|
|---|
| 1304 |
|
|---|
| 1305 | int left = 0, right = 0;
|
|---|
| 1306 |
|
|---|
| 1307 | int top = 0;
|
|---|
| 1308 | for (int i = 1; i < nPoints; ++i) {
|
|---|
| 1309 | if (d->vertices[i]->y < d->vertices[top]->y)
|
|---|
| 1310 | top = i;
|
|---|
| 1311 | }
|
|---|
| 1312 |
|
|---|
| 1313 | left = (top + nPoints - 1) % nPoints;
|
|---|
| 1314 | right = (top + 1) % nPoints;
|
|---|
| 1315 |
|
|---|
| 1316 | while (d->vertices[left]->x == d->vertices[top]->x && d->vertices[left]->y == d->vertices[top]->y && left != right)
|
|---|
| 1317 | left = (left + nPoints - 1) % nPoints;
|
|---|
| 1318 |
|
|---|
| 1319 | while (d->vertices[right]->x == d->vertices[top]->x && d->vertices[right]->y == d->vertices[top]->y && left != right)
|
|---|
| 1320 | right = (right + 1) % nPoints;
|
|---|
| 1321 |
|
|---|
| 1322 | if (left == right)
|
|---|
| 1323 | return;
|
|---|
| 1324 |
|
|---|
| 1325 | int dir = 1;
|
|---|
| 1326 |
|
|---|
| 1327 | Vertex dLeft = { d->vertices[top]->x - d->vertices[left]->x,
|
|---|
| 1328 | d->vertices[top]->y - d->vertices[left]->y };
|
|---|
| 1329 |
|
|---|
| 1330 | Vertex dRight = { d->vertices[right]->x - d->vertices[top]->x,
|
|---|
| 1331 | d->vertices[right]->y - d->vertices[top]->y };
|
|---|
| 1332 |
|
|---|
| 1333 | Q27Dot5 cross = dLeft.x * dRight.y - dLeft.y * dRight.x;
|
|---|
| 1334 |
|
|---|
| 1335 | // flip direction if polygon is clockwise
|
|---|
| 1336 | if (cross < 0 || (cross == 0 && dLeft.x > 0)) {
|
|---|
| 1337 | qSwap(left, right);
|
|---|
| 1338 | dir = -1;
|
|---|
| 1339 | }
|
|---|
| 1340 |
|
|---|
| 1341 | Vertex *lastLeft = d->vertices[top];
|
|---|
| 1342 | Vertex *lastRight = d->vertices[top];
|
|---|
| 1343 |
|
|---|
| 1344 | QTessellator::Trapezoid trap;
|
|---|
| 1345 |
|
|---|
| 1346 | while (lastLeft->y == d->vertices[left]->y && left != right) {
|
|---|
| 1347 | lastLeft = d->vertices[left];
|
|---|
| 1348 | left = (left + nPoints - dir) % nPoints;
|
|---|
| 1349 | }
|
|---|
| 1350 |
|
|---|
| 1351 | while (lastRight->y == d->vertices[right]->y && left != right) {
|
|---|
| 1352 | lastRight = d->vertices[right];
|
|---|
| 1353 | right = (right + nPoints + dir) % nPoints;
|
|---|
| 1354 | }
|
|---|
| 1355 |
|
|---|
| 1356 | while (true) {
|
|---|
| 1357 | trap.top = qMax(lastRight->y, lastLeft->y);
|
|---|
| 1358 | trap.bottom = qMin(d->vertices[left]->y, d->vertices[right]->y);
|
|---|
| 1359 | trap.topLeft = lastLeft;
|
|---|
| 1360 | trap.topRight = lastRight;
|
|---|
| 1361 | trap.bottomLeft = d->vertices[left];
|
|---|
| 1362 | trap.bottomRight = d->vertices[right];
|
|---|
| 1363 |
|
|---|
| 1364 | if (trap.bottom > trap.top)
|
|---|
| 1365 | addTrap(trap);
|
|---|
| 1366 |
|
|---|
| 1367 | if (left == right)
|
|---|
| 1368 | break;
|
|---|
| 1369 |
|
|---|
| 1370 | if (d->vertices[right]->y < d->vertices[left]->y) {
|
|---|
| 1371 | do {
|
|---|
| 1372 | lastRight = d->vertices[right];
|
|---|
| 1373 | right = (right + nPoints + dir) % nPoints;
|
|---|
| 1374 | }
|
|---|
| 1375 | while (lastRight->y == d->vertices[right]->y && left != right);
|
|---|
| 1376 | } else {
|
|---|
| 1377 | do {
|
|---|
| 1378 | lastLeft = d->vertices[left];
|
|---|
| 1379 | left = (left + nPoints - dir) % nPoints;
|
|---|
| 1380 | }
|
|---|
| 1381 | while (lastLeft->y == d->vertices[left]->y && left != right);
|
|---|
| 1382 | }
|
|---|
| 1383 | }
|
|---|
| 1384 | }
|
|---|
| 1385 |
|
|---|
| 1386 | // tessellates the stroke of the line from a_ to b_ with the given width and a flat cap
|
|---|
| 1387 | void QTessellator::tessellateRect(const QPointF &a_, const QPointF &b_, qreal width)
|
|---|
| 1388 | {
|
|---|
| 1389 | Vertex a = { FloatToQ27Dot5(a_.x()), FloatToQ27Dot5(a_.y()) };
|
|---|
| 1390 | Vertex b = { FloatToQ27Dot5(b_.x()), FloatToQ27Dot5(b_.y()) };
|
|---|
| 1391 |
|
|---|
| 1392 | QPointF pa = a_, pb = b_;
|
|---|
| 1393 |
|
|---|
| 1394 | if (a.y > b.y) {
|
|---|
| 1395 | qSwap(a, b);
|
|---|
| 1396 | qSwap(pa, pb);
|
|---|
| 1397 | }
|
|---|
| 1398 |
|
|---|
| 1399 | Vertex delta = { b.x - a.x, b.y - a.y };
|
|---|
| 1400 |
|
|---|
| 1401 | if (delta.x == 0 && delta.y == 0)
|
|---|
| 1402 | return;
|
|---|
| 1403 |
|
|---|
| 1404 | qreal hw = 0.5 * width;
|
|---|
| 1405 |
|
|---|
| 1406 | if (delta.x == 0) {
|
|---|
| 1407 | Q27Dot5 halfWidth = FloatToQ27Dot5(hw);
|
|---|
| 1408 |
|
|---|
| 1409 | if (halfWidth == 0)
|
|---|
| 1410 | return;
|
|---|
| 1411 |
|
|---|
| 1412 | Vertex topLeft = { a.x - halfWidth, a.y };
|
|---|
| 1413 | Vertex topRight = { a.x + halfWidth, a.y };
|
|---|
| 1414 | Vertex bottomLeft = { a.x - halfWidth, b.y };
|
|---|
| 1415 | Vertex bottomRight = { a.x + halfWidth, b.y };
|
|---|
| 1416 |
|
|---|
| 1417 | QTessellator::Trapezoid trap = { topLeft.y, bottomLeft.y, &topLeft, &bottomLeft, &topRight, &bottomRight };
|
|---|
| 1418 | addTrap(trap);
|
|---|
| 1419 | } else if (delta.y == 0) {
|
|---|
| 1420 | Q27Dot5 halfWidth = FloatToQ27Dot5(hw);
|
|---|
| 1421 |
|
|---|
| 1422 | if (halfWidth == 0)
|
|---|
| 1423 | return;
|
|---|
| 1424 |
|
|---|
| 1425 | if (a.x > b.x)
|
|---|
| 1426 | qSwap(a.x, b.x);
|
|---|
| 1427 |
|
|---|
| 1428 | Vertex topLeft = { a.x, a.y - halfWidth };
|
|---|
| 1429 | Vertex topRight = { b.x, a.y - halfWidth };
|
|---|
| 1430 | Vertex bottomLeft = { a.x, a.y + halfWidth };
|
|---|
| 1431 | Vertex bottomRight = { b.x, a.y + halfWidth };
|
|---|
| 1432 |
|
|---|
| 1433 | QTessellator::Trapezoid trap = { topLeft.y, bottomLeft.y, &topLeft, &bottomLeft, &topRight, &bottomRight };
|
|---|
| 1434 | addTrap(trap);
|
|---|
| 1435 | } else {
|
|---|
| 1436 | QPointF perp(pb.y() - pa.y(), pa.x() - pb.x());
|
|---|
| 1437 | qreal length = qSqrt(perp.x() * perp.x() + perp.y() * perp.y());
|
|---|
| 1438 |
|
|---|
| 1439 | if (qFuzzyCompare(length + 1, static_cast<qreal>(1)))
|
|---|
| 1440 | return;
|
|---|
| 1441 |
|
|---|
| 1442 | // need the half of the width
|
|---|
| 1443 | perp *= hw / length;
|
|---|
| 1444 |
|
|---|
| 1445 | QPointF pta = pa + perp;
|
|---|
| 1446 | QPointF ptb = pa - perp;
|
|---|
| 1447 | QPointF ptc = pb - perp;
|
|---|
| 1448 | QPointF ptd = pb + perp;
|
|---|
| 1449 |
|
|---|
| 1450 | Vertex ta = { FloatToQ27Dot5(pta.x()), FloatToQ27Dot5(pta.y()) };
|
|---|
| 1451 | Vertex tb = { FloatToQ27Dot5(ptb.x()), FloatToQ27Dot5(ptb.y()) };
|
|---|
| 1452 | Vertex tc = { FloatToQ27Dot5(ptc.x()), FloatToQ27Dot5(ptc.y()) };
|
|---|
| 1453 | Vertex td = { FloatToQ27Dot5(ptd.x()), FloatToQ27Dot5(ptd.y()) };
|
|---|
| 1454 |
|
|---|
| 1455 | if (ta.y < tb.y) {
|
|---|
| 1456 | if (tb.y < td.y) {
|
|---|
| 1457 | QTessellator::Trapezoid top = { ta.y, tb.y, &ta, &tb, &ta, &td };
|
|---|
| 1458 | QTessellator::Trapezoid bottom = { td.y, tc.y, &tb, &tc, &td, &tc };
|
|---|
| 1459 | addTrap(top);
|
|---|
| 1460 | addTrap(bottom);
|
|---|
| 1461 |
|
|---|
| 1462 | QTessellator::Trapezoid middle = { tb.y, td.y, &tb, &tc, &ta, &td };
|
|---|
| 1463 | addTrap(middle);
|
|---|
| 1464 | } else {
|
|---|
| 1465 | QTessellator::Trapezoid top = { ta.y, td.y, &ta, &tb, &ta, &td };
|
|---|
| 1466 | QTessellator::Trapezoid bottom = { tb.y, tc.y, &tb, &tc, &td, &tc };
|
|---|
| 1467 | addTrap(top);
|
|---|
| 1468 | addTrap(bottom);
|
|---|
| 1469 |
|
|---|
| 1470 | if (tb.y != td.y) {
|
|---|
| 1471 | QTessellator::Trapezoid middle = { td.y, tb.y, &ta, &tb, &td, &tc };
|
|---|
| 1472 | addTrap(middle);
|
|---|
| 1473 | }
|
|---|
| 1474 | }
|
|---|
| 1475 | } else {
|
|---|
| 1476 | if (ta.y < tc.y) {
|
|---|
| 1477 | QTessellator::Trapezoid top = { tb.y, ta.y, &tb, &tc, &tb, &ta };
|
|---|
| 1478 | QTessellator::Trapezoid bottom = { tc.y, td.y, &tc, &td, &ta, &td };
|
|---|
| 1479 | addTrap(top);
|
|---|
| 1480 | addTrap(bottom);
|
|---|
| 1481 |
|
|---|
| 1482 | QTessellator::Trapezoid middle = { ta.y, tc.y, &tb, &tc, &ta, &td };
|
|---|
| 1483 | addTrap(middle);
|
|---|
| 1484 | } else {
|
|---|
| 1485 | QTessellator::Trapezoid top = { tb.y, tc.y, &tb, &tc, &tb, &ta };
|
|---|
| 1486 | QTessellator::Trapezoid bottom = { ta.y, td.y, &tc, &td, &ta, &td };
|
|---|
| 1487 | addTrap(top);
|
|---|
| 1488 | addTrap(bottom);
|
|---|
| 1489 |
|
|---|
| 1490 | if (ta.y != tc.y) {
|
|---|
| 1491 | QTessellator::Trapezoid middle = { tc.y, ta.y, &tc, &td, &tb, &ta };
|
|---|
| 1492 | addTrap(middle);
|
|---|
| 1493 | }
|
|---|
| 1494 | }
|
|---|
| 1495 | }
|
|---|
| 1496 | }
|
|---|
| 1497 | }
|
|---|
| 1498 |
|
|---|
| 1499 | QT_END_NAMESPACE
|
|---|