aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/thingsToDo.txt
blob: f74d3ee1268f9fede56b2b8225809fcb5a47ff32 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
add unit test for quadratic horizontal intersection
add unit test for cubic horizontal intersection with left/right
add unit test for ActiveEdge::calcLeft (can currently loop forever)
does ActiveEdge::isCoincidentWith need to support quad, cubic?
figure out why variation in ActiveEdge::tooCloseToCall isn't better
why does 'lastPtr - 2' in addIntersectingTs break testSimplifyTriangle22?
add code to promote quad to cubic, or add quad/cubic intersection
figure out why testSimplifySkinnyTriangle13 fails

for quadratics and cubics, once various T values are added, see if consecutive
Ts have ys that go up instead of down. If so, the edge needs to be broken.

when splitting curves at inflection pts, should I retain the original curve
data and note that the first/last T are no longer 0/1 ?
I need to figure this out before I can proceed

would it make sense to leave the InEdge alone, and add multiple copies of
ActiveEdge, pointing to the same InEdge, where the copy has only the subset
of Ts that need to be walked in reverse order?


-- A Digression Which Shows Why Resolving Coincidence Does Not Make Sense --

Consider the following fine ASCII art:

  +------>-------+       +------>-------+
  |              |       |              |
  ^              V       ^              V
  |              |       |              |
  +------<-------+       +------<-------+
  +------>-------+       +------<-------+
  |              |       |              |
  ^              V       V              ^
  |              |       |              |
  +------<-------+       +------>-------+

(assume the bottom and top of the stacked rectangles are coincident)

Simplifying said rectangles, regardless of rectangle direction, and regardless
of winding or even/odd, eliminates the coincident edge, i.e., the result is
always:

  +------>-------+
  |              |
  |              |
  |              |
  ^              V
  |              |
  |              |
  |              |
  +------<-------+

But when the rectangles are enclosed in a larger rectangle:

+-------->---------+    +-------->---------+
| +------>-------+ |    | +------>-------+ |
| |              | |    | |              | |
| ^              V |    | ^              V |
| |              | |    | |              | |
| +------<-------+ |    | +------<-------+ |
| +------>-------+ |    | +------<-------+ |
| |              | |    | |              | |
| ^              V |    | V              ^ |
| |              | |    | |              | |
| +------<-------+ |    | +------>-------+ |
+--------<---------+    +--------<---------+

Simplifying them gives different results depending on the winding setting:

winding:
+-------->---------+    +-------->---------+
|                  |    |                  |
|                  |    |                  |
|                  |    |                  |
|                  |    |                  |
|                  |    | +------<-------+ |
|                  |    | |              | |
|                  |    | V              ^ |
|                  |    | |              | |
|                  |    | +------>-------+ |
+--------<---------+    +--------<---------+

even odd:
+-------->---------+    +-------->---------+
| +------<-------+ |    | +------<-------+ |
| |              | |    | |              | |
| |              | |    | |              | |
| |              | |    | |              | |
| |              | |    | |              | |
| V              ^ |    | V              ^ |
| |              | |    | |              | |
| |              | |    | |              | |
| |              | |    | |              | |
| +------>-------+ |    | +------>-------+ |
+--------<---------+    +--------<---------+

So, given the inner rectangles alone (e.g., given coincident pairs in some local
context), we can't know whether to keep the coincident edges or not.


-- Thoughts About Sortless Ops --

I can't come up with anything truly sortless. It seems that the crossings need
to be sorted to know which segment is next on the outside, although sometimes
we can use that it is not coincident just to follow the direction.

If it is coincident or if there's more than two crossing segments, sorting
seems inevitable.

Likewise, to resolve whether one contour is inside another, it seems that
sorting is required. Given a pair of segments on different contours, to know
if one is inside of the other, I need to know for each which side of the edge
is the inside/filled side. When the outer contour is walked, it seems like I
could record the inside info. I guess when the inner contour is found, its
inside sense is reversed (inside is above the top). But how do I know if the
next contour is inside another? Maybe shoot out a line and brute-force
intersect it with all the segments in all the other contours? If every contour
has an extra segment when the intersections are computed, this may not be as
crazy as it seems.

Suppose each contour has one extra segment shooting straight up from the top
(or straight up from any point on the segment). This ray is not intersected
with the home contour, but is intersected with all other contours as part of
the normal intersection engine. If it is possible to get from the T values to
the other segments to the other contours, it would be straightforward to
count the contour crossings and determine if the home contour is in another
contour or not (if the count is even, not, if odd, is inside). By itself that
doesn't tell us about winding, but it's a start.


Since intersecting these rays is unrelated to computing other intersections,
it can be lazily done once the contour is found.

So
repeat the following
find the top segment of all contours
trace the outside, marking touching first and last segments as inside
continue tracing the touched segments with reversed outside/inside sense
once the edges are exhausted, remaining must be disjoint contours
send a ray from a disjoint point through all other contours
count the crossings, determine if disjoint is inside or outside, then continue

===

On Quadratic (and Cubic) Intersections

Currently, if only the end points touch, QuadracticIntersections does a lot of
work to figure that out. Can I test for that up front, then short circuit the
recursive search for the end points?

Or, is there something defective in the current approach that makes the end
point recursion go so deep? I'm seeing 56 stack frames (about 28 divides, but
thankfully, no splits) to find one matching endpoint.


Bezier curve focus may allow more quickly determining that end points with
identical tangents are practically coicident for some range of T, but I don't
understand the math yet to know.

Another approach is to determine how flat the curve is to make good guesses
about how far to move away in T before doing the intersection for the remainder
and/or to determine whether one curve is to the inside or outside of another.
According to Mike/Rob, the flatness for quadratics increases by 4 for each
subdivision, and a crude guess of the curvature can be had by comparing P1 to
(P0+P2)/2. By looking at the ULPS of the numbers, I can guess what value of
T may be far enough that the curves diverge but don't cross.

====

Code I May Not Need Any More

    static bool CoincidentCandidate(const Angle* current) {
        const Segment* segment = current->segment();
        int min = SkMin32(current->start(), current->end());
        do {
            const Span& span = segment->fTs[min];
            if (span.fCoincident == Span::kStart_Coincidence) {
                return true;
            }
        } while (--min >= 0);
        return false;
    }

    static bool CoincidentHalf(const Angle* current, const Angle* next) {
        const Segment* other = next->segment();
        const Segment* segment = current->segment();
        int min = SkMin32(current->start(), current->end());
        const Span& minSpan = segment->fTs[min];
        if (minSpan.fOther == other) {
            return minSpan.fCoincident == Span::kStart_Coincidence;
        }
        int index = min;
        int spanCount = segment->fTs.count();
        while (++index < spanCount) {
            const Span& span = segment->fTs[index];
            if (minSpan.fT != span.fT) {
                break;
            }
            if (span.fOther != other) {
                continue;
            }
            return span.fCoincident == Span::kStart_Coincidence;
        }
        index = min;
        while (--index >= 0) {
            const Span& span = segment->fTs[index];
            if (span.fOther != other) {
                continue;
            }
            return span.fCoincident == Span::kStart_Coincidence;
        }
        return false;
    }
    
    static bool Coincident(const Angle* current, const Angle* next) {
        return CoincidentHalf(current, next) &&
                CoincidentHalf(next, current);
    }

    // If three lines cancel in a - b - c order, a - b may or may not
    // eliminate the edge that describes the b - c cancellation. Check done to
    // determine this.
    static bool CoincidentCancels(const Angle* current, const Angle* next) {
        int curMin = SkMin32(current->start(), current->end());
        if (current->segment()->fTs[curMin].fDone) {
            return false;
        }
        int nextMin = SkMin32(next->start(), next->end());
        if (next->segment()->fTs[nextMin].fDone) {
            return false;
        }
        return SkSign32(current->start() - current->end())
                != SkSign32(next->start() - next->end());
    }

    // FIXME: at this point, just have two functions for the different steps
    int coincidentEnd(int from, int step) const {
        double fromT = fTs[from].fT;
        int count = fTs.count();
        int to = from;
        while (step > 0 ? ++to < count : --to >= 0) {
            const Span& span = fTs[to];
            if ((step > 0 ? span.fT - fromT : fromT - span.fT) >= FLT_EPSILON ) {
                // FIXME: we assume that if the T changes, we don't care about 
                // coincident -- but in nextSpan, we require that both the T
                // and actual loc change to represent a span. This asymettry may
                // be OK or may be trouble -- if trouble, probably will need to
                // detect coincidence earlier or sort differently 
                break;
            }
#if 01
            if (span.fCoincident == (step < 0 ? Span::kStart_Coincidence :
                    Span::kEnd_Coincidence)) {
                from = to;
            }
#else
            from = to;
#endif
        }
        return from;
    }

    // once past current span, if step>0, look for coicident==1
    // if step<0, look for coincident==-1
    int nextSpanEnd(int from, int step) const {
        int result = nextSpan(from, step);
        if (result < 0) {
            return result;
        }
        return coincidentEnd(result, step);
    }

    
    void adjustFirst(const SkTDArray<Angle*>& sorted, int& first, int& winding,
            bool outside) {
        int firstIndex = first;
        int angleCount = sorted.count();
        if (true || outside) {
            const Angle* angle = sorted[firstIndex];
            int prior = firstIndex;
            do {
                if (--prior < 0) {
                    prior = angleCount - 1;
                }
                if (prior == firstIndex) { // all are coincident with each other
                    return;
                }
                if (!Coincident(sorted[prior], sorted[first])) {
                    return;
                }
                winding += angle->sign();
                first = prior;
                angle = sorted[prior];
                winding -= angle->sign();
            } while (true);
        }
        do {
            int next = first + 1;
            if (next == angleCount) {
                next = 0;
            }
            if (next == firstIndex) { // all are coincident with each other
                return;
            }
            if (!Coincident(sorted[first], sorted[next])) {
                return;
            }
            first = next;
        } while (true);
    }

            bool nextIsCoincident = CoincidentCandidate(nextAngle);
            bool finalOrNoCoincident = true;
            bool pairCoincides = false;
            bool pairCancels = false;
            if (nextIsCoincident) {
                int followIndex = nextIndex + 1;
                if (followIndex == angleCount) {
                    followIndex = 0;
                }
                const Angle* followAngle = sorted[followIndex];
                finalOrNoCoincident = !Coincident(nextAngle, followAngle);
                if ((pairCoincides = Coincident(angle, nextAngle))) {
                    pairCancels = CoincidentCancels(angle, nextAngle);
                }
            }
            if (pairCancels && !foundAngle && !nextSegment->done()) {
                Segment* aSeg = angle->segment();
      //          alreadyMarked |= aSeg == sorted[firstIndex]->segment();
                aSeg->markAndChaseCoincident(angle->start(), angle->end(),
                        nextSegment);
                if (firstEdge) {
                    return NULL;
                }
            }
            if (pairCoincides) {
                if (pairCancels) {
                    goto doNext;
                }
                int minT = SkMin32(nextAngle->start(), nextAngle->end());
                bool markNext = abs(maxWinding) < abs(winding);
                if (markNext) {
                    nextSegment->markDone(minT, winding);
                } 
                int oldMinT = SkMin32(angle->start(), angle->end());
                if (true || !foundAngle) {
                 //   SkASSERT(0); // do we ever get here?
                    Segment* aSeg = angle->segment();
        //            alreadyMarked |= aSeg == sorted[firstIndex]->segment();
                    aSeg->markDone(oldMinT, maxWinding);
                }
            }

    // OPTIMIZATION: uses tail recursion. Unwise?
    void innerCoincidentChase(int step, Segment* other) {
        // find other at index
   //     SkASSERT(!done());
        const Span* start = NULL;
        const Span* end = NULL;
        int index, startIndex, endIndex;
        int count = fTs.count();
        for (index = 0; index < count; ++index) {
            const Span& span = fTs[index];
            if (!span.fCoincident || span.fOther != other) {
                continue;
            }
            if (!start) {
                startIndex = index;
                start = &span;
            } else {
                SkASSERT(!end);
                endIndex = index;
                end = &span;
            }
        }
        if (!end) {
            return;
        }
        bool thisDone = fTs[SkMin32(startIndex, endIndex)].fDone;
        bool otherDone = other->fTs[SkMin32(start->fOtherIndex,
                end->fOtherIndex)].fDone;
        if (thisDone && otherDone) {
            return;
        }
        Segment* next;
        Segment* nextOther;
        if (step < 0) {
            next = start->fT == 0 ? NULL : this;
            nextOther = other->fTs[start->fOtherIndex].fT > 1 - FLT_EPSILON ? NULL : other;
        } else {
            next = end->fT == 1 ? NULL : this;
            nextOther = other->fTs[end->fOtherIndex].fT < FLT_EPSILON ? NULL : other;
        }
        SkASSERT(!next || !nextOther);
        for (index = 0; index < count; ++index) {
            const Span& span = fTs[index];
            if (span.fCoincident || span.fOther == other) {
                continue;
            }
            bool checkNext = !next && (step < 0 ? span.fT < FLT_EPSILON
                && span.fOtherT > 1 - FLT_EPSILON : span.fT > 1 - FLT_EPSILON
                && span.fOtherT < FLT_EPSILON);
            bool checkOther = !nextOther && (step < 0 ? fabs(span.fT - start->fT) < FLT_EPSILON
                && span.fOtherT < FLT_EPSILON : fabs(span.fT - end->fT) < FLT_EPSILON
                && span.fOtherT > 1 - FLT_EPSILON);
            if (!checkNext && !checkOther) {
                continue;
            }
            Segment* oSegment = span.fOther;
            if (oSegment->done()) {
                continue;
            }
            int oCount = oSegment->fTs.count();
            for (int oIndex = 0; oIndex < oCount; ++oIndex) {
                const Span& oSpan = oSegment->fTs[oIndex];
                if (oSpan.fT >= FLT_EPSILON && oSpan.fT <= 1 - FLT_EPSILON) {
                    continue;
                }
                if (!oSpan.fCoincident) {
                    continue;
                }
                if (checkNext && (oSpan.fT < FLT_EPSILON ^ step < 0)) { 
                    next = oSegment;
                    checkNext = false;
                }
                if (checkOther && (oSpan.fT > 1 - FLT_EPSILON ^ step < 0)) {
                    nextOther = oSegment;
                    checkOther = false;
                }
            }
        }
        // this needs to walk both spans in lock step, skipping edges that
        // are already marked done on one or the other
        markCanceled(startIndex, endIndex);
        if (next && nextOther) {
            next->innerCoincidentChase(step, nextOther);
        }
    }

    // cancel coincident edges in lock step
    void markCanceled(int start, int end) {
        if (done()) {
            return;
        }
        Segment* other = fTs[start].fOther;
        if (other->done()) {
            return;
        }
        if (start > end) {
            SkTSwap<int>(start, end);
        }
        double maxT = fTs[end].fT - FLT_EPSILON;
        int spanCount = fTs.count();
        // since these cancel, this walks up and other walks down
        int oStart = fTs[start].fOtherIndex;
        double oStartT = other->fTs[oStart].fT;
        while (oStartT - other->fTs[--oStart].fT < FLT_EPSILON)
            ;
        double startT = fTs[start].fT;
        while (start > 0 && startT - fTs[start - 1].fT < FLT_EPSILON) {
            --start;
        }
        do {
            Span* span = &fTs[start];
            Span* oSpan = &other->fTs[oStart];
            // find start of each, and see if both are not done
            bool markDone = !span->fDone && !oSpan->fDone;
            double spanT = span->fT;
            do {
                if (markDone) {
                    span->fCanceled = true;
                #if DEBUG_MARK_DONE
                    const SkPoint& pt = xyAtT(span);
                    SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g)\n",
                            __FUNCTION__, fID, start, span->fT, pt.fX, pt.fY);
                #endif
                    SkASSERT(!span->fDone);
                    span->fDone = true;
                    span->fWinding = 0;
                    fDoneSpans++;
                }
                if (++start == spanCount) {
                    break;
                }
                span = &fTs[start];
            } while (span->fT - spanT < FLT_EPSILON);
            double oSpanT = oSpan->fT;
            do {
                if (markDone) {
                    oSpan->fCanceled = true;
                #if DEBUG_MARK_DONE
                    const SkPoint& oPt = xyAtT(oSpan);
                    SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g)\n",
                            __FUNCTION__, other->fID, oStart, oSpan->fT,
                            oPt.fX, oPt.fY);
                #endif
                    SkASSERT(!oSpan->fDone);
                    oSpan->fDone = true;
                    oSpan->fWinding = 0;
                    other->fDoneSpans++;
                }
                if (--oStart < 0) {
                    break;
                }
                oSpan = &other->fTs[oStart];
            } while (oSpanT - oSpan->fT < FLT_EPSILON);
        } while (fTs[start].fT <= maxT);
    }

    bool canceled(int start, int end) const {
        int min = SkMin32(start, end);
        return fTs[min].fCanceled;
    }

    void markAndChaseCoincident(int index, int endIndex, Segment* other) {
        int step = SkSign32(endIndex - index);
        innerCoincidentChase(step, other);
    }