aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pathops/SkOpAngle.cpp
blob: aa72394043a695c6b57203ef6c407fe87a40132d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkIntersections.h"
#include "SkOpAngle.h"
#include "SkPathOpsCurve.h"
#include "SkTSort.h"

#if DEBUG_SORT || DEBUG_SORT_SINGLE
#include "SkOpSegment.h"
#endif

// FIXME: this is bogus for quads and cubics
// if the quads and cubics' line from end pt to ctrl pt are coincident,
// there's no obvious way to determine the curve ordering from the
// derivatives alone. In particular, if one quadratic's coincident tangent
// is longer than the other curve, the final control point can place the
// longer curve on either side of the shorter one.
// Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf
// may provide some help, but nothing has been figured out yet.

/*(
for quads and cubics, set up a parameterized line (e.g. LineParameters )
for points [0] to [1]. See if point [2] is on that line, or on one side
or the other. If it both quads' end points are on the same side, choose
the shorter tangent. If the tangents are equal, choose the better second
tangent angle

maybe I could set up LineParameters lazily
*/
static int simple_compare(double x, double y, double rx, double ry) {
    if ((y < 0) ^ (ry < 0)) {  // OPTIMIZATION: better to use y * ry < 0 ?
        return y < 0;
    }
    if (y == 0 && ry == 0 && x * rx < 0) {
        return x < rx;
    }
    double x_ry = x * ry;
    double rx_y = rx * y;
    double cmp = x_ry - rx_y;
    if (!approximately_zero(cmp)) {
        return cmp < 0;
    }
    if (approximately_zero(x_ry) && approximately_zero(rx_y)
            && !approximately_zero_squared(cmp)) {
        return cmp < 0;
    }
    return -1;
}

bool SkOpAngle::operator<(const SkOpAngle& rh) const {
    double x = dx();
    double y = dy();
    double rx = rh.dx();
    double ry = rh.dy();
    int simple = simple_compare(x, y, rx, ry);
    if (simple >= 0) {
        return simple;
    }
    // at this point, the initial tangent line is coincident
    // see if edges curl away from each other
    if (fSide * rh.fSide <= 0 && (!approximately_zero(fSide)
            || !approximately_zero(rh.fSide))) {
        // FIXME: running demo will trigger this assertion
        // (don't know if commenting out will trigger further assertion or not)
        // commenting it out allows demo to run in release, though
        return fSide < rh.fSide;
    }
    // see if either curve can be lengthened and try the tangent compare again
    if (/* cmp && */ (*fSpans)[fEnd].fOther != rh.fSegment  // tangents not absolutely identical
            && (*rh.fSpans)[rh.fEnd].fOther != fSegment) {  // and not intersecting
        SkOpAngle longer = *this;
        SkOpAngle rhLonger = rh;
        if (longer.lengthen() | rhLonger.lengthen()) {
            return longer < rhLonger;
        }
    }
    if ((fVerb == SkPath::kLine_Verb && approximately_zero(x) && approximately_zero(y))
            || (rh.fVerb == SkPath::kLine_Verb
            && approximately_zero(rx) && approximately_zero(ry))) {
        // See general unsortable comment below. This case can happen when
        // one line has a non-zero change in t but no change in x and y.
        fUnsortable = true;
        rh.fUnsortable = true;
        return this < &rh;  // even with no solution, return a stable sort
    }
    if ((*rh.fSpans)[SkMin32(rh.fStart, rh.fEnd)].fTiny
            || (*fSpans)[SkMin32(fStart, fEnd)].fTiny) {
        fUnsortable = true;
        rh.fUnsortable = true;
        return this < &rh;  // even with no solution, return a stable sort
    }
    SkASSERT(fVerb >= SkPath::kQuad_Verb);
    SkASSERT(rh.fVerb >= SkPath::kQuad_Verb);
    // FIXME: until I can think of something better, project a ray from the
    // end of the shorter tangent to midway between the end points
    // through both curves and use the resulting angle to sort
    // FIXME: some of this setup can be moved to set() if it works, or cached if it's expensive
    double len = fTangent1.normalSquared();
    double rlen = rh.fTangent1.normalSquared();
    SkDLine ray;
    SkIntersections i, ri;
    int roots, rroots;
    bool flip = false;
    bool useThis;
    bool leftLessThanRight = fSide > 0;
    do {
        useThis = (len < rlen) ^ flip;
        const SkDCubic& part = useThis ? fCurvePart : rh.fCurvePart;
        SkPath::Verb partVerb = useThis ? fVerb : rh.fVerb;
        ray[0] = partVerb == SkPath::kCubic_Verb && part[0].approximatelyEqual(part[1]) ?
            part[2] : part[1];
        ray[1].fX = (part[0].fX + part[partVerb].fX) / 2;
        ray[1].fY = (part[0].fY + part[partVerb].fY) / 2;
        SkASSERT(ray[0] != ray[1]);
        roots = (i.*CurveRay[fVerb])(fPts, ray);
        rroots = (ri.*CurveRay[rh.fVerb])(rh.fPts, ray);
    } while ((roots == 0 || rroots == 0) && (flip ^= true));
    if (roots == 0 || rroots == 0) {
        // FIXME: we don't have a solution in this case. The interim solution
        // is to mark the edges as unsortable, exclude them from this and
        // future computations, and allow the returned path to be fragmented
        fUnsortable = true;
        rh.fUnsortable = true;
        return this < &rh;  // even with no solution, return a stable sort
    }
    SkASSERT(fSide != 0 && rh.fSide != 0);
    SkASSERT(fSide * rh.fSide > 0); // both are the same sign
    SkDPoint lLoc;
    double best = SK_ScalarInfinity;
#if DEBUG_SORT
    SkDebugf("lh=%d rh=%d use-lh=%d ray={{%1.9g,%1.9g}, {%1.9g,%1.9g}} %c\n",
            fSegment->debugID(), rh.fSegment->debugID(), useThis, ray[0].fX, ray[0].fY,
            ray[1].fX, ray[1].fY, "-+"[fSide > 0]);
#endif
    for (int index = 0; index < roots; ++index) {
        SkDPoint loc = i.pt(index);
        SkDVector dxy = loc - ray[0];
        double dist = dxy.lengthSquared();
#if DEBUG_SORT
        SkDebugf("best=%1.9g dist=%1.9g loc={%1.9g,%1.9g} dxy={%1.9g,%1.9g}\n",
                best, dist, loc.fX, loc.fY, dxy.fX, dxy.fY);
#endif
        if (best > dist) {
            lLoc = loc;
            best = dist;
        }
    }
    flip = false;
    SkDPoint rLoc;
    for (int index = 0; index < rroots; ++index) {
        rLoc = ri.pt(index);
        SkDVector dxy = rLoc - ray[0];
        double dist = dxy.lengthSquared();
#if DEBUG_SORT
        SkDebugf("best=%1.9g dist=%1.9g %c=(fSide < 0) rLoc={%1.9g,%1.9g} dxy={%1.9g,%1.9g}\n",
                best, dist, "><"[fSide < 0], rLoc.fX, rLoc.fY, dxy.fX, dxy.fY);
#endif
        if (best > dist) {
            flip = true;
            break;
        }
    }
 #if 0
    SkDVector lRay = lLoc - fCurvePart[0];
    SkDVector rRay = rLoc - fCurvePart[0];
    int rayDir = simple_compare(lRay.fX, lRay.fY, rRay.fX, rRay.fY);
    SkASSERT(rayDir >= 0);
    if (rayDir < 0) {
        fUnsortable = true;
        rh.fUnsortable = true;
        return this < &rh;  // even with no solution, return a stable sort
    }
#endif
   if (flip) {
        leftLessThanRight = !leftLessThanRight;
  //      rayDir = !rayDir;
    }
#if 0 && (DEBUG_SORT || DEBUG_SORT_SINGLE)
    SkDebugf("%d %c %d (fSide %c 0) loc={{%1.9g,%1.9g}, {%1.9g,%1.9g}} flip=%d rayDir=%d\n",
                fSegment->debugID(), "><"[leftLessThanRight], rh.fSegment->debugID(),
                "<>"[fSide > 0], lLoc.fX, lLoc.fY, rLoc.fX, rLoc.fY, flip, rayDir);
#endif
//    SkASSERT(leftLessThanRight == (bool) rayDir);
    return leftLessThanRight;
}

bool SkOpAngle::lengthen() {
    int newEnd = fEnd;
    if (fStart < fEnd ? ++newEnd < fSpans->count() : --newEnd >= 0) {
        fEnd = newEnd;
        setSpans();
        return true;
    }
    return false;
}

bool SkOpAngle::reverseLengthen() {
    if (fReversed) {
        return false;
    }
    int newEnd = fStart;
    if (fStart > fEnd ? ++newEnd < fSpans->count() : --newEnd >= 0) {
        fEnd = newEnd;
        fReversed = true;
        setSpans();
        return true;
    }
    return false;
}

void SkOpAngle::set(const SkPoint* orig, SkPath::Verb verb, const SkOpSegment* segment,
        int start, int end, const SkTDArray<SkOpSpan>& spans) {
    fSegment = segment;
    fStart = start;
    fEnd = end;
    fPts = orig;
    fVerb = verb;
    fSpans = &spans;
    fReversed = false;
    fUnsortable = false;
    setSpans();
}


void SkOpAngle::setSpans() {
    double startT = (*fSpans)[fStart].fT;
    double endT = (*fSpans)[fEnd].fT;
    switch (fVerb) {
    case SkPath::kLine_Verb: {
        SkDLine l = SkDLine::SubDivide(fPts, startT, endT);
        // OPTIMIZATION: for pure line compares, we never need fTangent1.c
        fTangent1.lineEndPoints(l);
        fSide = 0;
        } break;
    case SkPath::kQuad_Verb: {
        SkDQuad& quad = *SkTCast<SkDQuad*>(&fCurvePart);
        quad = SkDQuad::SubDivide(fPts, startT, endT);
        fTangent1.quadEndPoints(quad, 0, 1);
        if (dx() == 0 && dy() == 0) {
            fTangent1.quadEndPoints(quad);
        }
        fSide = -fTangent1.pointDistance(fCurvePart[2]);  // not normalized -- compare sign only
        } break;
    case SkPath::kCubic_Verb: {
 //     int nextC = 2;
        fCurvePart = SkDCubic::SubDivide(fPts, startT, endT);
        fTangent1.cubicEndPoints(fCurvePart, 0, 1);
        if (dx() == 0 && dy() == 0) {
            fTangent1.cubicEndPoints(fCurvePart, 0, 2);
 //         nextC = 3;
            if (dx() == 0 && dy() == 0) {
                fTangent1.cubicEndPoints(fCurvePart, 0, 3);
            }
        }
 //     fSide = -fTangent1.pointDistance(fCurvePart[nextC]);  // compare sign only
 //     if (nextC == 2 && approximately_zero(fSide)) {
 //         fSide = -fTangent1.pointDistance(fCurvePart[3]);
 //     }
        double testTs[4];
        // OPTIMIZATION: keep inflections precomputed with cubic segment?
        int testCount = SkDCubic::FindInflections(fPts, testTs);
        double limitT = endT;
        int index;
        for (index = 0; index < testCount; ++index) {
            if (!between(startT, testTs[index], limitT)) {
                testTs[index] = -1;
            }
        }
        testTs[testCount++] = startT;
        testTs[testCount++] = endT;
        SkTQSort<double>(testTs, &testTs[testCount - 1]);
        double bestSide = 0;
        int testCases = (testCount << 1) - 1;
        index = 0;
        while (testTs[index] < 0) {
            ++index;
        }
        index <<= 1;
        for (; index < testCases; ++index) {
            int testIndex = index >> 1;
            double testT = testTs[testIndex];
            if (index & 1) {
                testT = (testT + testTs[testIndex + 1]) / 2;
            }
            // OPTIMIZE: could avoid call for t == startT, endT
            SkDPoint pt = dcubic_xy_at_t(fPts, testT);
            double testSide = fTangent1.pointDistance(pt);
            if (fabs(bestSide) < fabs(testSide)) {
                bestSide = testSide;
            }
        }
        fSide = -bestSide;  // compare sign only
        } break;
    default:
        SkASSERT(0);
    }
    fUnsortable = dx() == 0 && dy() == 0;
    if (fUnsortable) {
        return;
    }
    SkASSERT(fStart != fEnd);
    int step = fStart < fEnd ? 1 : -1;  // OPTIMIZE: worth fStart - fEnd >> 31 type macro?
    for (int index = fStart; index != fEnd; index += step) {
#if 1
        const SkOpSpan& thisSpan = (*fSpans)[index];
        const SkOpSpan& nextSpan = (*fSpans)[index + step];
        if (thisSpan.fTiny || precisely_equal(thisSpan.fT, nextSpan.fT)) {
            continue;
        }
        fUnsortable = step > 0 ? thisSpan.fUnsortableStart : nextSpan.fUnsortableEnd;
#if DEBUG_UNSORTABLE
        if (fUnsortable) {
            SkPoint iPt = (*CurvePointAtT[fVerb])(fPts, thisSpan.fT);
            SkPoint ePt = (*CurvePointAtT[fVerb])(fPts, nextSpan.fT);
            SkDebugf("%s unsortable [%d] (%1.9g,%1.9g) [%d] (%1.9g,%1.9g)\n", __FUNCTION__,
                    index, iPt.fX, iPt.fY, fEnd, ePt.fX, ePt.fY);
        }
#endif
        return;
#else
        if ((*fSpans)[index].fUnsortableStart) {
            fUnsortable = true;
            return;
        }
#endif
    }
#if 1
#if DEBUG_UNSORTABLE
    SkPoint iPt = (*CurvePointAtT[fVerb])(fPts, startT);
    SkPoint ePt = (*CurvePointAtT[fVerb])(fPts, endT);
    SkDebugf("%s all tiny unsortable [%d] (%1.9g,%1.9g) [%d] (%1.9g,%1.9g)\n", __FUNCTION__,
        fStart, iPt.fX, iPt.fY, fEnd, ePt.fX, ePt.fY);
#endif
    fUnsortable = true;
#endif
}