1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qtessellator_p.h"
57 #define QDEBUG if (1){} else qDebug
60 static const bool emit_clever = true;
61 static const bool mark_clever = false;
64 LineBeforeStarts = 0x1,
66 LineBeforeHorizontal = 0x4,
67 LineAfterStarts = 0x8,
69 LineAfterHorizontal = 0x20
74 class QTessellatorPrivate {
78 QTessellatorPrivate() {}
80 QRectF collectAndSortVertices(const QPointF *points, int *maxActiveEdges);
81 void cancelCoincidingEdges();
83 void emitEdges(QTessellator *tessellator);
84 void processIntersections();
87 void addIntersections();
89 struct Vertex : public QTessellator::Vertex
98 bool operator <(const Intersection &other) const {
101 return edge < other.edge;
104 struct IntersectionLink
109 typedef QMap<Intersection, IntersectionLink> Intersections;
112 Edge(const Vertices &v, int _edge);
118 signed int winding : 8;
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;
132 EdgeSorter(int _y) : y(_y) {}
133 bool operator() (const Edge *e1, const Edge *e2);
142 void init(int maxActiveEdges);
145 int findEdgePosition(Q27Dot5 x, Q27Dot5 y) const;
146 int findEdgePosition(const Edge &e) const;
147 int findEdge(int edge) const;
150 void swap(int p1, int p2) {
151 Edge *tmp = edges[p1];
152 edges[p1] = edges[p2];
155 void insert(int pos, const Edge &e);
156 void removeAt(int pos);
157 void markEdges(int pos1, int pos2);
172 enum { default_alloc = 32 };
176 enum { default_alloc = 128 };
179 void init(int maxVertices);
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 {
189 Vertex *next(Vertex *v) {
191 if (v == storage + nPoints)
195 const Vertex *next(const Vertex *v) const {
197 if (v == storage + nPoints)
201 int nextPos(const Vertex *v) const {
203 if (v == storage + nPoints)
207 Vertex *prev(Vertex *v) {
209 v = storage + nPoints;
213 const Vertex *prev(const Vertex *v) const {
215 v = storage + nPoints;
219 int prevPos(const Vertex *v) const {
221 v = storage + nPoints;
229 Intersections intersections;
236 void addIntersection(const Edge *e1, const Edge *e2);
237 bool edgeInChain(Intersection i, int edge);
241 QTessellatorPrivate::Edge::Edge(const QTessellatorPrivate::Vertices &vertices, int edge)
244 intersect_left = intersect_right = true;
249 v1 = vertices.next(v0);
251 Q_ASSERT(v0->y != v1->y);
259 y_left = y_right = v0->y;
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.
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):
275 // a.intersect(b) == b.intersect(a)
276 // a.isLeftOf(b) != b.isLeftOf(a)
278 // This is tricky to get right, so be very careful when changing anything in here!
280 static inline bool sameSign(qint64 a, qint64 b) {
281 return (((qint64) ((quint64) a ^ (quint64) b)) >= 0 );
284 bool QTessellatorPrivate::Edge::intersect(const Edge &other, Q27Dot5 *y, bool *det_positive) const
286 qint64 a1 = v1->y - v0->y;
287 qint64 b1 = v0->x - v1->x;
289 qint64 a2 = other.v1->y - other.v0->y;
290 qint64 b2 = other.v0->x - other.v1->x;
292 qint64 det = a1 * b2 - a2 * b1;
296 qint64 c1 = qint64(v1->x) * v0->y - qint64(v0->x) * v1->y;
298 qint64 r3 = a1 * other.v0->x + b1 * other.v0->y + c1;
299 qint64 r4 = a1 * other.v1->x + b1 * other.v1->y + c1;
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 ))
307 qint64 c2 = qint64(other.v1->x) * other.v0->y - qint64(other.v0->x) * other.v1->y;
309 qint64 r1 = a2 * v0->x + b2 * v0->y + c2;
310 qint64 r2 = a2 * v1->x + b2 * v1->y + c2;
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 ))
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;
324 qint64 num = a2 * c1 - a1 * c2;
325 *y = ( num < 0 ? num - offset : num + offset ) / det;
327 *det_positive = (det > 0);
334 bool QTessellatorPrivate::Edge::isLeftOf(const Edge &other, Q27Dot5 y) const
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;
342 qint64 c2 = qint64(other.v1->x) * other.v0->y - qint64(other.v0->x) * other.v1->y;
344 qint64 det = a1 * b2 - a2 * b1;
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;
351 return edge < other.edge;
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;
358 qint64 offset = det < 0 ? -det : det;
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;
365 return ((yi > y) ^ (det < 0));
368 static inline bool compareVertex(const QTessellatorPrivate::Vertex *p1,
369 const QTessellatorPrivate::Vertex *p2)
371 if (p1->y == p2->y) {
374 return p1->x < p2->x;
376 return p1->y < p2->y;
379 Q27Dot5 QTessellatorPrivate::Edge::positionAt(Q27Dot5 y) const
386 qint64 d = v1->x - v0->x;
387 return (v0->x + d*(y - v0->y)/(v1->y-v0->y));
390 bool QTessellatorPrivate::EdgeSorter::operator() (const Edge *e1, const Edge *e2)
392 return e1->isLeftOf(*e2, y);
396 QTessellatorPrivate::Scanline::Scanline()
403 void QTessellatorPrivate::Scanline::init(int maxActiveEdges)
406 if (!edges || maxActiveEdges > default_alloc) {
407 max_edges = maxActiveEdges;
408 int s = qMax(maxActiveEdges + 1, default_alloc + 1);
409 edges = q_check_ptr((Edge **)realloc(edges, s*sizeof(Edge *)));
410 edge_table = q_check_ptr((Edge *)realloc(edge_table, s*sizeof(Edge)));
411 old = q_check_ptr((Edge **)realloc(old, s*sizeof(Edge *)));
416 for (int i = 0; i < maxActiveEdges; ++i)
417 edge_table[i].edge = i+1;
418 edge_table[maxActiveEdges].edge = -1;
421 void QTessellatorPrivate::Scanline::done()
423 if (max_edges > default_alloc) {
433 QTessellatorPrivate::Scanline::~Scanline()
440 int QTessellatorPrivate::Scanline::findEdgePosition(Q27Dot5 x, Q27Dot5 y) const
445 int pos = min + ((max - min + 1) >> 1);
446 Q27Dot5 ax = edges[pos]->positionAt(y);
456 int QTessellatorPrivate::Scanline::findEdgePosition(const Edge &e) const
458 // qDebug() << ">> findEdgePosition";
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)) {
470 // qDebug() << "<< findEdgePosition got" << min;
474 int QTessellatorPrivate::Scanline::findEdge(int edge) const
476 for (int i = 0; i < size; ++i) {
477 int item_edge = edges[i]->edge;
478 if (item_edge == edge)
485 void QTessellatorPrivate::Scanline::clearMarks()
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;
494 void QTessellatorPrivate::Scanline::prepareLine()
496 Edge **end = edges + size;
507 void QTessellatorPrivate::Scanline::lineDone()
509 Edge **end = old + old_size;
513 (*e)->edge = first_unused;
514 first_unused = (*e - edge_table);
520 void QTessellatorPrivate::Scanline::insert(int pos, const Edge &e)
522 Edge *edge = edge_table + first_unused;
523 first_unused = edge->edge;
524 Q_ASSERT(first_unused != -1);
526 memmove(edges + pos + 1, edges + pos, (size - pos)*sizeof(Edge *));
531 void QTessellatorPrivate::Scanline::removeAt(int pos)
533 Edge *e = edges[pos];
536 memmove(edges + pos, edges + pos + 1, (size - pos)*sizeof(Edge *));
539 void QTessellatorPrivate::Scanline::markEdges(int pos1, int pos2)
544 for (int i = pos1; i <= pos2; ++i)
545 edges[i]->mark = true;
549 QTessellatorPrivate::Vertices::Vertices()
557 QTessellatorPrivate::Vertices::~Vertices()
565 void QTessellatorPrivate::Vertices::init(int maxVertices)
567 if (!storage || maxVertices > allocated) {
568 int size = qMax((int)default_alloc, maxVertices);
569 storage = q_check_ptr((Vertex *)realloc(storage, size*sizeof(Vertex)));
570 sorted = q_check_ptr((Vertex **)realloc(sorted, size*sizeof(Vertex *)));
571 allocated = maxVertices;
575 void QTessellatorPrivate::Vertices::done()
577 if (allocated > default_alloc) {
588 static inline void fillTrapezoid(Q27Dot5 y1, Q27Dot5 y2, int left, int right,
589 const QTessellatorPrivate::Vertices &vertices,
590 QTessellator::Trapezoid *trap)
594 const QTessellatorPrivate::Vertex *v = vertices[left];
596 trap->bottomLeft = vertices.next(v);
597 if (trap->topLeft->y > trap->bottomLeft->y)
598 qSwap(trap->topLeft,trap->bottomLeft);
601 trap->bottomRight = vertices.next(v);
602 if (trap->topRight->y > trap->bottomRight->y)
603 qSwap(trap->topRight, trap->bottomRight);
606 QRectF QTessellatorPrivate::collectAndSortVertices(const QPointF *points, int *maxActiveEdges)
609 Vertex *v = vertices.storage;
610 Vertex **vv = vertices.sorted;
612 qreal xmin(points[0].x());
613 qreal xmax(points[0].x());
614 qreal ymin(points[0].y());
615 qreal ymax(points[0].y());
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());
623 while (i < vertices.nPoints) {
624 Q27Dot5 y_curr = y_next;
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());
639 y_next = FloatToQ27Dot5(points[i+1].y());
640 x_next = FloatToQ27Dot5(points[i+1].x());
642 // skip vertices on top of each other
643 if (v->x == x_next && v->y == y_next) {
645 if (i < vertices.nPoints)
647 Vertex *v0 = vertices.storage;
648 v0->flags &= ~(LineBeforeStarts|LineBeforeEnds|LineBeforeHorizontal);
650 v0->flags |= LineBeforeEnds;
651 else if (y_prev > y_curr)
652 v0->flags |= LineBeforeStarts;
654 v0->flags |= LineBeforeHorizontal;
655 if ((v0->flags & (LineBeforeStarts|LineAfterStarts))
656 && !(v0->flags & (LineAfterEnds|LineBeforeEnds)))
657 *maxActiveEdges += 2;
662 v->flags |= LineBeforeEnds;
663 else if (y_prev > y_curr)
664 v->flags |= LineBeforeStarts;
666 v->flags |= LineBeforeHorizontal;
670 v->flags |= LineAfterStarts;
671 else if (y_curr > y_next)
672 v->flags |= LineAfterEnds;
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;
685 vertices.nPoints = j;
687 QDEBUG() << "maxActiveEdges=" << *maxActiveEdges;
688 vv = vertices.sorted;
689 qSort(vv, vv + vertices.nPoints, compareVertex);
691 return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
694 struct QCoincidingEdge {
695 QTessellatorPrivate::Vertex *start;
696 QTessellatorPrivate::Vertex *end;
700 inline bool operator<(const QCoincidingEdge &e2) const
702 return end->y == e2.end->y ? end->x < e2.end->x : end->y < e2.end->y;
706 static void cancelEdges(QCoincidingEdge &e1, QCoincidingEdge &e2)
709 e1.start->flags &= ~(LineBeforeStarts|LineBeforeHorizontal);
710 e1.end->flags &= ~(LineAfterEnds|LineAfterHorizontal);
712 e1.start->flags &= ~(LineAfterStarts|LineAfterHorizontal);
713 e1.end->flags &= ~(LineBeforeEnds|LineBeforeHorizontal);
716 e2.start->flags &= ~(LineBeforeStarts|LineBeforeHorizontal);
717 e2.end->flags &= ~(LineAfterEnds|LineAfterHorizontal);
719 e2.start->flags &= ~(LineAfterStarts|LineAfterHorizontal);
720 e2.end->flags &= ~(LineBeforeEnds|LineBeforeHorizontal);
722 e1.used = e2.used = true;
725 void QTessellatorPrivate::cancelCoincidingEdges()
727 Vertex **vv = vertices.sorted;
729 QCoincidingEdge *tl = 0;
732 for (int i = 0; i < vertices.nPoints - 1; ++i) {
734 int testListSize = 0;
735 while (i < vertices.nPoints - 1) {
737 if (v->x != n->x || v->y != n->y)
740 if (testListSize > tlSize - 2) {
741 tlSize = qMax(tlSize*2, 16);
742 tl = q_check_ptr((QCoincidingEdge *)realloc(tl, tlSize*sizeof(QCoincidingEdge)));
744 if (n->flags & (LineBeforeStarts|LineBeforeHorizontal)) {
745 tl[testListSize].start = n;
746 tl[testListSize].end = vertices.prev(n);
747 tl[testListSize].used = false;
748 tl[testListSize].before = true;
751 if (n->flags & (LineAfterStarts|LineAfterHorizontal)) {
752 tl[testListSize].start = n;
753 tl[testListSize].end = vertices.next(n);
754 tl[testListSize].used = false;
755 tl[testListSize].before = false;
763 qSort(tl, tl + testListSize);
765 for (int j = 0; j < testListSize; ++j) {
769 for (int k = j + 1; k < testListSize; ++k) {
770 if (tl[j].end->x != tl[k].end->x
771 || tl[j].end->y != tl[k].end->y
775 if (!winding || tl[j].before != tl[k].before) {
776 cancelEdges(tl[j], tl[k]);
788 void QTessellatorPrivate::emitEdges(QTessellator *tessellator)
790 //QDEBUG() << "TRAPS:";
791 if (!scanline.old_size)
799 scanline.old[0]->y_left = y;
801 for (int i = 0; i < scanline.old_size - 1; ++i) {
802 Edge *left = scanline.old[i];
803 Edge *right = scanline.old[i+1];
805 // qDebug() << "i=" << i << "edge->winding=" << left->winding << "winding=" << winding;
809 } else if (!emit_clever || left->mark || right->mark) {
810 Q27Dot5 top = qMax(left->y_right, right->y_left);
812 QTessellator::Trapezoid trap;
813 fillTrapezoid(top, y, left->edge, right->edge, vertices, &trap);
814 tessellator->addTrap(trap);
815 // QDEBUG() << " top=" << Q27Dot5ToDouble(top) << "left=" << left->edge << "right=" << right->edge;
822 if (scanline.old[scanline.old_size - 1]->mark) {
823 scanline.old[scanline.old_size - 1]->y_right = y;
824 scanline.old[scanline.old_size - 1]->mark = false;
827 // odd-even fill rule
828 for (int i = 0; i < scanline.old_size; i += 2) {
829 Edge *left = scanline.old[i];
830 Edge *right = scanline.old[i+1];
831 if (!emit_clever || left->mark || right->mark) {
832 Q27Dot5 top = qMax(left->y_right, right->y_left);
834 QTessellator::Trapezoid trap;
835 fillTrapezoid(top, y, left->edge, right->edge, vertices, &trap);
836 tessellator->addTrap(trap);
838 // QDEBUG() << " top=" << Q27Dot5ToDouble(top) << "left=" << left->edge << "right=" << right->edge;
843 left->mark = right->mark = false;
850 void QTessellatorPrivate::processIntersections()
852 QDEBUG() << "PROCESS INTERSECTIONS";
853 // process intersections
854 while (!intersections.isEmpty()) {
855 Intersections::iterator it = intersections.begin();
860 QDEBUG() << " swapping intersecting edges ";
861 int min = scanline.size;
863 Q27Dot5 xmin = INT_MAX;
864 Q27Dot5 xmax = INT_MIN;
867 const Intersection &i = it.key();
870 int edgePos = scanline.findEdge(i.edge);
873 min = qMin(edgePos, min);
874 max = qMax(edgePos, max);
875 Edge *edge = scanline.edges[edgePos];
876 xmin = qMin(xmin, edge->positionAt(y));
877 xmax = qMax(xmax, edge->positionAt(y));
882 it = intersections.find(key);
883 intersections.remove(i);
884 if (it == intersections.end())
890 Q_ASSERT(min != max);
891 QDEBUG() << "sorting between" << min << "and" << max << "xpos=" << xmin << xmax;
892 while (min > 0 && scanline.edges[min - 1]->positionAt(y) >= xmin) {
893 QDEBUG() << " adding edge on left";
896 while (max + 1 < scanline.size && scanline.edges[max + 1]->positionAt(y) <= xmax) {
897 QDEBUG() << " adding edge on right";
901 qSort(scanline.edges + min, scanline.edges + max + 1, EdgeSorter(y));
903 for (int i = min; i <= max; ++i)
904 QDEBUG() << " " << scanline.edges[i]->edge << "at pos" << i;
906 for (int i = min; i <= max; ++i) {
907 Edge *edge = scanline.edges[i];
908 edge->intersect_left = true;
909 edge->intersect_right = true;
915 void QTessellatorPrivate::removeEdges()
917 int cv = currentVertex;
918 while (cv < vertices.nPoints) {
919 const Vertex *v = vertices.sorted[cv];
922 if (v->flags & LineBeforeEnds) {
923 QDEBUG() << " removing edge" << vertices.prevPos(v);
924 int pos = scanline.findEdge(vertices.prevPos(v));
927 scanline.edges[pos]->mark = true;
929 scanline.edges[pos - 1]->intersect_right = true;
930 if (pos < scanline.size - 1)
931 scanline.edges[pos + 1]->intersect_left = true;
932 scanline.removeAt(pos);
934 if (v->flags & LineAfterEnds) {
935 QDEBUG() << " removing edge" << vertices.position(v);
936 int pos = scanline.findEdge(vertices.position(v));
939 scanline.edges[pos]->mark = true;
941 scanline.edges[pos - 1]->intersect_right = true;
942 if (pos < scanline.size - 1)
943 scanline.edges[pos + 1]->intersect_left = true;
944 scanline.removeAt(pos);
950 void QTessellatorPrivate::addEdges()
952 while (currentVertex < vertices.nPoints) {
953 const Vertex *v = vertices.sorted[currentVertex];
956 if (v->flags & LineBeforeStarts) {
958 int start = vertices.prevPos(v);
959 Edge e(vertices, start);
960 int pos = scanline.findEdgePosition(e);
961 QDEBUG() << " adding edge" << start << "at position" << pos;
962 scanline.insert(pos, e);
963 if (!mark_clever || !(v->flags & LineAfterEnds)) {
965 scanline.edges[pos - 1]->mark = true;
966 if (pos < scanline.size - 1)
967 scanline.edges[pos + 1]->mark = true;
970 if (v->flags & LineAfterStarts) {
971 Edge e(vertices, vertices.position(v));
972 int pos = scanline.findEdgePosition(e);
973 QDEBUG() << " adding edge" << vertices.position(v) << "at position" << pos;
974 scanline.insert(pos, e);
975 if (!mark_clever || !(v->flags & LineBeforeEnds)) {
977 scanline.edges[pos - 1]->mark = true;
978 if (pos < scanline.size - 1)
979 scanline.edges[pos + 1]->mark = true;
982 if (v->flags & LineAfterHorizontal) {
983 int pos1 = scanline.findEdgePosition(v->x, v->y);
984 const Vertex *next = vertices.next(v);
985 Q_ASSERT(v->y == next->y);
986 int pos2 = scanline.findEdgePosition(next->x, next->y);
991 if (pos2 == scanline.size)
993 //QDEBUG() << "marking horizontal edge from " << pos1 << "to" << pos2;
994 scanline.markEdges(pos1, pos2);
1001 static void checkLinkChain(const QTessellatorPrivate::Intersections &intersections,
1002 QTessellatorPrivate::Intersection i)
1004 // qDebug() << " Link chain: ";
1007 QTessellatorPrivate::IntersectionLink l = intersections.value(i);
1008 // qDebug() << " " << i.edge << "next=" << l.next << "prev=" << l.prev;
1011 Q_ASSERT(l.next != -1);
1012 Q_ASSERT(l.prev != -1);
1014 QTessellatorPrivate::Intersection i2 = i;
1016 QTessellatorPrivate::IntersectionLink l2 = intersections.value(i2);
1018 Q_ASSERT(l2.next != -1);
1019 Q_ASSERT(l2.prev != -1);
1020 Q_ASSERT(l.next == i2.edge);
1021 Q_ASSERT(l2.prev == i.edge);
1027 bool QTessellatorPrivate::edgeInChain(Intersection i, int edge)
1033 IntersectionLink l = intersections.value(i);
1036 Q_ASSERT(l.next != -1);
1037 Q_ASSERT(l.prev != -1);
1039 Intersection i2 = i;
1043 IntersectionLink l2 = intersections.value(i2);
1044 Q_ASSERT(l2.next != -1);
1045 Q_ASSERT(l2.prev != -1);
1046 Q_ASSERT(l.next == i2.edge);
1047 Q_ASSERT(l2.prev == i.edge);
1055 void QTessellatorPrivate::addIntersection(const Edge *e1, const Edge *e2)
1057 const IntersectionLink emptyLink = {-1, -1};
1059 int next = vertices.nextPos(vertices[e1->edge]);
1060 if (e2->edge == next)
1062 int prev = vertices.prevPos(vertices[e1->edge]);
1063 if (e2->edge == prev)
1068 bool isect = e1->intersect(*e2, &yi, &det_positive);
1069 QDEBUG("checking edges %d and %d", e1->edge, e2->edge);
1071 QDEBUG() << " no intersection";
1075 // don't emit an intersection if it's at the start of a line segment or above us
1079 QDEBUG() << " ----->>>>>> WRONG ORDER!";
1082 QDEBUG() << " between edges " << e1->edge << "and" << e2->edge << "at point ("
1083 << Q27Dot5ToDouble(yi) << ')';
1088 IntersectionLink link1 = intersections.value(i1, emptyLink);
1092 IntersectionLink link2 = intersections.value(i2, emptyLink);
1094 // new pair of edges
1095 if (link1.next == -1 && link2.next == -1) {
1096 link1.next = link1.prev = i2.edge;
1097 link2.next = link2.prev = i1.edge;
1098 } else if (link1.next == i2.edge || link1.prev == i2.edge
1099 || link2.next == i1.edge || link2.prev == i1.edge) {
1101 checkLinkChain(intersections, i1);
1102 checkLinkChain(intersections, i2);
1103 Q_ASSERT(edgeInChain(i1, i2.edge));
1106 } else if (link1.next == -1 || link2.next == -1) {
1107 if (link2.next == -1) {
1109 qSwap(link1, link2);
1111 Q_ASSERT(link1.next == -1);
1113 checkLinkChain(intersections, i2);
1116 link1.next = i2.edge;
1117 link1.prev = link2.prev;
1118 link2.prev = i1.edge;
1121 other.edge = link1.prev;
1122 IntersectionLink link = intersections.value(other, emptyLink);
1123 Q_ASSERT(link.next == i2.edge);
1124 Q_ASSERT(link.prev != -1);
1125 link.next = i1.edge;
1126 intersections.insert(other, link);
1128 bool connected = edgeInChain(i1, i2.edge);
1132 checkLinkChain(intersections, i1);
1133 checkLinkChain(intersections, i2);
1135 // both already in some list. Have to make sure they are connected
1136 // this can be done by cutting open the ring(s) after the two eges and
1137 // connecting them again
1138 Intersection other1;
1140 other1.edge = link1.next;
1141 IntersectionLink linko1 = intersections.value(other1, emptyLink);
1142 Intersection other2;
1144 other2.edge = link2.next;
1145 IntersectionLink linko2 = intersections.value(other2, emptyLink);
1147 linko1.prev = i2.edge;
1148 link2.next = other1.edge;
1150 linko2.prev = i1.edge;
1151 link1.next = other2.edge;
1152 intersections.insert(other1, linko1);
1153 intersections.insert(other2, linko2);
1155 intersections.insert(i1, link1);
1156 intersections.insert(i2, link2);
1158 checkLinkChain(intersections, i1);
1159 checkLinkChain(intersections, i2);
1160 Q_ASSERT(edgeInChain(i1, i2.edge));
1167 void QTessellatorPrivate::addIntersections()
1169 if (scanline.size) {
1170 QDEBUG() << "INTERSECTIONS";
1171 // check marked edges for intersections
1173 for (int i = 0; i < scanline.size; ++i) {
1174 Edge *e = scanline.edges[i];
1175 QDEBUG() << " " << i << e->edge << "isect=(" << e->intersect_left << e->intersect_right
1180 for (int i = 0; i < scanline.size - 1; ++i) {
1181 Edge *e1 = scanline.edges[i];
1182 Edge *e2 = scanline.edges[i + 1];
1183 // check for intersection
1184 if (e1->intersect_right || e2->intersect_left)
1185 addIntersection(e1, e2);
1189 if (intersections.constBegin().key().y == y) {
1190 QDEBUG() << "----------------> intersection on same line";
1191 scanline.clearMarks();
1192 scanline.processIntersections(y, &intersections);
1199 QTessellator::QTessellator()
1201 d = new QTessellatorPrivate;
1204 QTessellator::~QTessellator()
1209 void QTessellator::setWinding(bool w)
1215 QRectF QTessellator::tessellate(const QPointF *points, int nPoints)
1217 Q_ASSERT(points[0] == points[nPoints-1]);
1221 QDEBUG()<< "POINTS:";
1222 for (int i = 0; i < nPoints; ++i) {
1223 QDEBUG() << points[i];
1227 // collect edges and calculate bounds
1228 d->vertices.nPoints = nPoints;
1229 d->vertices.init(nPoints);
1231 int maxActiveEdges = 0;
1232 QRectF br = d->collectAndSortVertices(points, &maxActiveEdges);
1233 d->cancelCoincidingEdges();
1236 QDEBUG() << "nPoints = " << nPoints << "using " << d->vertices.nPoints;
1237 QDEBUG()<< "VERTICES:";
1238 for (int i = 0; i < d->vertices.nPoints; ++i) {
1239 QDEBUG() << " " << i << ": "
1240 << "point=" << d->vertices.position(d->vertices.sorted[i])
1241 << "flags=" << d->vertices.sorted[i]->flags
1242 << "pos=(" << Q27Dot5ToDouble(d->vertices.sorted[i]->x) << '/'
1243 << Q27Dot5ToDouble(d->vertices.sorted[i]->y) << ')';
1247 d->scanline.init(maxActiveEdges);
1249 d->currentVertex = 0;
1251 while (d->currentVertex < d->vertices.nPoints) {
1252 d->scanline.clearMarks();
1254 d->y = d->vertices.sorted[d->currentVertex]->y;
1255 if (!d->intersections.isEmpty())
1256 d->y = qMin(d->y, d->intersections.constBegin().key().y);
1258 QDEBUG()<< "===== SCANLINE: y =" << Q27Dot5ToDouble(d->y) << " =====";
1260 d->scanline.prepareLine();
1261 d->processIntersections();
1264 d->addIntersections();
1266 d->scanline.lineDone();
1269 QDEBUG()<< "===== edges:";
1270 for (int i = 0; i < d->scanline.size; ++i) {
1271 QDEBUG() << " " << d->scanline.edges[i]->edge
1272 << "p0= (" << Q27Dot5ToDouble(d->scanline.edges[i]->v0->x)
1273 << '/' << Q27Dot5ToDouble(d->scanline.edges[i]->v0->y)
1274 << ") p1= (" << Q27Dot5ToDouble(d->scanline.edges[i]->v1->x)
1275 << '/' << Q27Dot5ToDouble(d->scanline.edges[i]->v1->y) << ')'
1276 << "x=" << Q27Dot5ToDouble(d->scanline.edges[i]->positionAt(d->y))
1278 << ((i < d->scanline.size - 1)
1279 ? d->scanline.edges[i]->isLeftOf(*d->scanline.edges[i+1], d->y)
1286 d->intersections.clear();
1290 // tessellates the given convex polygon
1291 void QTessellator::tessellateConvex(const QPointF *points, int nPoints)
1293 Q_ASSERT(points[0] == points[nPoints-1]);
1296 d->vertices.nPoints = nPoints;
1297 d->vertices.init(nPoints);
1299 for (int i = 0; i < nPoints; ++i) {
1300 d->vertices[i]->x = FloatToQ27Dot5(points[i].x());
1301 d->vertices[i]->y = FloatToQ27Dot5(points[i].y());
1304 int left = 0, right = 0;
1307 for (int i = 1; i < nPoints; ++i) {
1308 if (d->vertices[i]->y < d->vertices[top]->y)
1312 left = (top + nPoints - 1) % nPoints;
1313 right = (top + 1) % nPoints;
1315 while (d->vertices[left]->x == d->vertices[top]->x && d->vertices[left]->y == d->vertices[top]->y && left != right)
1316 left = (left + nPoints - 1) % nPoints;
1318 while (d->vertices[right]->x == d->vertices[top]->x && d->vertices[right]->y == d->vertices[top]->y && left != right)
1319 right = (right + 1) % nPoints;
1326 Vertex dLeft = { d->vertices[top]->x - d->vertices[left]->x,
1327 d->vertices[top]->y - d->vertices[left]->y };
1329 Vertex dRight = { d->vertices[right]->x - d->vertices[top]->x,
1330 d->vertices[right]->y - d->vertices[top]->y };
1332 Q27Dot5 cross = dLeft.x * dRight.y - dLeft.y * dRight.x;
1334 // flip direction if polygon is clockwise
1335 if (cross < 0 || (cross == 0 && dLeft.x > 0)) {
1340 Vertex *lastLeft = d->vertices[top];
1341 Vertex *lastRight = d->vertices[top];
1343 QTessellator::Trapezoid trap;
1345 while (lastLeft->y == d->vertices[left]->y && left != right) {
1346 lastLeft = d->vertices[left];
1347 left = (left + nPoints - dir) % nPoints;
1350 while (lastRight->y == d->vertices[right]->y && left != right) {
1351 lastRight = d->vertices[right];
1352 right = (right + nPoints + dir) % nPoints;
1356 trap.top = qMax(lastRight->y, lastLeft->y);
1357 trap.bottom = qMin(d->vertices[left]->y, d->vertices[right]->y);
1358 trap.topLeft = lastLeft;
1359 trap.topRight = lastRight;
1360 trap.bottomLeft = d->vertices[left];
1361 trap.bottomRight = d->vertices[right];
1363 if (trap.bottom > trap.top)
1369 if (d->vertices[right]->y < d->vertices[left]->y) {
1371 lastRight = d->vertices[right];
1372 right = (right + nPoints + dir) % nPoints;
1374 while (lastRight->y == d->vertices[right]->y && left != right);
1377 lastLeft = d->vertices[left];
1378 left = (left + nPoints - dir) % nPoints;
1380 while (lastLeft->y == d->vertices[left]->y && left != right);
1385 // tessellates the stroke of the line from a_ to b_ with the given width and a flat cap
1386 void QTessellator::tessellateRect(const QPointF &a_, const QPointF &b_, qreal width)
1388 Vertex a = { FloatToQ27Dot5(a_.x()), FloatToQ27Dot5(a_.y()) };
1389 Vertex b = { FloatToQ27Dot5(b_.x()), FloatToQ27Dot5(b_.y()) };
1391 QPointF pa = a_, pb = b_;
1398 Vertex delta = { b.x - a.x, b.y - a.y };
1400 if (delta.x == 0 && delta.y == 0)
1403 qreal hw = 0.5 * width;
1406 Q27Dot5 halfWidth = FloatToQ27Dot5(hw);
1411 Vertex topLeft = { a.x - halfWidth, a.y };
1412 Vertex topRight = { a.x + halfWidth, a.y };
1413 Vertex bottomLeft = { a.x - halfWidth, b.y };
1414 Vertex bottomRight = { a.x + halfWidth, b.y };
1416 QTessellator::Trapezoid trap = { topLeft.y, bottomLeft.y, &topLeft, &bottomLeft, &topRight, &bottomRight };
1418 } else if (delta.y == 0) {
1419 Q27Dot5 halfWidth = FloatToQ27Dot5(hw);
1427 Vertex topLeft = { a.x, a.y - halfWidth };
1428 Vertex topRight = { b.x, a.y - halfWidth };
1429 Vertex bottomLeft = { a.x, a.y + halfWidth };
1430 Vertex bottomRight = { b.x, a.y + halfWidth };
1432 QTessellator::Trapezoid trap = { topLeft.y, bottomLeft.y, &topLeft, &bottomLeft, &topRight, &bottomRight };
1435 QPointF perp(pb.y() - pa.y(), pa.x() - pb.x());
1436 qreal length = qSqrt(perp.x() * perp.x() + perp.y() * perp.y());
1438 if (qFuzzyIsNull(length))
1441 // need the half of the width
1442 perp *= hw / length;
1444 QPointF pta = pa + perp;
1445 QPointF ptb = pa - perp;
1446 QPointF ptc = pb - perp;
1447 QPointF ptd = pb + perp;
1449 Vertex ta = { FloatToQ27Dot5(pta.x()), FloatToQ27Dot5(pta.y()) };
1450 Vertex tb = { FloatToQ27Dot5(ptb.x()), FloatToQ27Dot5(ptb.y()) };
1451 Vertex tc = { FloatToQ27Dot5(ptc.x()), FloatToQ27Dot5(ptc.y()) };
1452 Vertex td = { FloatToQ27Dot5(ptd.x()), FloatToQ27Dot5(ptd.y()) };
1456 QTessellator::Trapezoid top = { ta.y, tb.y, &ta, &tb, &ta, &td };
1457 QTessellator::Trapezoid bottom = { td.y, tc.y, &tb, &tc, &td, &tc };
1461 QTessellator::Trapezoid middle = { tb.y, td.y, &tb, &tc, &ta, &td };
1464 QTessellator::Trapezoid top = { ta.y, td.y, &ta, &tb, &ta, &td };
1465 QTessellator::Trapezoid bottom = { tb.y, tc.y, &tb, &tc, &td, &tc };
1470 QTessellator::Trapezoid middle = { td.y, tb.y, &ta, &tb, &td, &tc };
1476 QTessellator::Trapezoid top = { tb.y, ta.y, &tb, &tc, &tb, &ta };
1477 QTessellator::Trapezoid bottom = { tc.y, td.y, &tc, &td, &ta, &td };
1481 QTessellator::Trapezoid middle = { ta.y, tc.y, &tb, &tc, &ta, &td };
1484 QTessellator::Trapezoid top = { tb.y, tc.y, &tb, &tc, &tb, &ta };
1485 QTessellator::Trapezoid bottom = { ta.y, td.y, &tc, &td, &ta, &td };
1490 QTessellator::Trapezoid middle = { tc.y, ta.y, &tc, &td, &tb, &ta };