caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 7 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 8 | #include "DataTypes.h" |
| 9 | #include "Intersections.h" |
| 10 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 11 | void Intersections::addCoincident(double s1, double e1, double s2, double e2) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 12 | SkASSERT((fCoincidentUsed & 1) != 1); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 13 | for (int index = 0; index < fCoincidentUsed; index += 2) { |
| 14 | double cs1 = fCoincidentT[fSwap][index]; |
| 15 | double ce1 = fCoincidentT[fSwap][index + 1]; |
| 16 | bool s1in = approximately_between(cs1, s1, ce1); |
| 17 | bool e1in = approximately_between(cs1, e1, ce1); |
| 18 | double cs2 = fCoincidentT[fSwap ^ 1][index]; |
| 19 | double ce2 = fCoincidentT[fSwap ^ 1][index + 1]; |
| 20 | bool s2in = approximately_between(cs2, s2, ce2); |
| 21 | bool e2in = approximately_between(cs2, e2, ce2); |
| 22 | if ((s1in | e1in) & (s2in | e2in)) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 23 | double lesser1 = SkTMin(cs1, ce1); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 24 | index += cs1 > ce1; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 25 | if (s1 < lesser1) { |
| 26 | fCoincidentT[fSwap][index] = s1; |
| 27 | } else if (e1 < lesser1) { |
| 28 | fCoincidentT[fSwap][index] = e1; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 29 | } |
| 30 | index ^= 1; |
| 31 | double greater1 = fCoincidentT[fSwap][index]; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 32 | if (s1 > greater1) { |
| 33 | fCoincidentT[fSwap][index] = s1; |
| 34 | } else if (e1 > greater1) { |
| 35 | fCoincidentT[fSwap][index] = e1; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 36 | } |
| 37 | index &= ~1; |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 38 | double lesser2 = SkTMin(cs2, ce2); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 39 | index += cs2 > ce2; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 40 | if (s2 < lesser2) { |
| 41 | fCoincidentT[fSwap ^ 1][index] = s2; |
| 42 | } else if (e2 < lesser2) { |
| 43 | fCoincidentT[fSwap ^ 1][index] = e2; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 44 | } |
| 45 | index ^= 1; |
| 46 | double greater2 = fCoincidentT[fSwap ^ 1][index]; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 47 | if (s2 > greater2) { |
| 48 | fCoincidentT[fSwap ^ 1][index] = s2; |
| 49 | } else if (e2 > greater2) { |
| 50 | fCoincidentT[fSwap ^ 1][index] = e2; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 51 | } |
| 52 | return; |
| 53 | } |
| 54 | } |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 55 | SkASSERT(fCoincidentUsed < 9); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 56 | fCoincidentT[fSwap][fCoincidentUsed] = s1; |
| 57 | fCoincidentT[fSwap ^ 1][fCoincidentUsed] = s2; |
| 58 | ++fCoincidentUsed; |
| 59 | fCoincidentT[fSwap][fCoincidentUsed] = e1; |
| 60 | fCoincidentT[fSwap ^ 1][fCoincidentUsed] = e2; |
| 61 | ++fCoincidentUsed; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 62 | // FIXME: assert that no entries in normal range lie between s and e |
| 63 | remove(s1, e1); |
| 64 | remove(s2, e2); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 65 | } |
| 66 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 67 | void Intersections::cleanUp() { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 68 | SkASSERT(fCoincidentUsed); |
| 69 | SkASSERT(fUsed); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 70 | // find any entries in fT that could be part of the coincident range |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 71 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 72 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 73 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 74 | // FIXME: this doesn't respect swap, but add coincident does -- seems inconsistent |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 75 | void Intersections::insert(double one, double two) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 76 | SkASSERT(fUsed <= 1 || fT[0][0] < fT[0][1]); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 77 | int index; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 78 | for (index = 0; index < fCoincidentUsed; ++index ) { |
| 79 | if (approximately_equal(fCoincidentT[0][index], one) |
| 80 | && approximately_equal(fCoincidentT[1][index], two)) { |
| 81 | return; |
| 82 | } |
| 83 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 84 | for (index = 0; index < fUsed; ++index) { |
| 85 | if (approximately_equal(fT[0][index], one) |
| 86 | && approximately_equal(fT[1][index], two)) { |
| 87 | return; |
| 88 | } |
| 89 | if (fT[0][index] > one) { |
| 90 | break; |
| 91 | } |
| 92 | } |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 93 | SkASSERT(fUsed < 9); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 94 | int remaining = fUsed - index; |
| 95 | if (remaining > 0) { |
| 96 | memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining); |
| 97 | memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining); |
| 98 | } |
| 99 | fT[0][index] = one; |
| 100 | fT[1][index] = two; |
| 101 | ++fUsed; |
| 102 | } |
| 103 | |
| 104 | // FIXME: all callers should be moved to regular insert. Failures are likely |
| 105 | // if two separate callers differ on whether ts are equal or not |
| 106 | void Intersections::insertOne(double t, int side) { |
| 107 | int used = side ? fUsed2 : fUsed; |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 108 | SkASSERT(used <= 1 || fT[side][0] < fT[side][1]); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 109 | int index; |
| 110 | for (index = 0; index < used; ++index) { |
| 111 | if (approximately_equal(fT[side][index], t)) { |
| 112 | return; |
| 113 | } |
| 114 | if (fT[side][index] > t) { |
| 115 | break; |
| 116 | } |
| 117 | } |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 118 | SkASSERT(used < 9); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 119 | int remaining = used - index; |
| 120 | if (remaining > 0) { |
| 121 | memmove(&fT[side][index + 1], &fT[side][index], sizeof(fT[side][0]) * remaining); |
| 122 | } |
| 123 | fT[side][index] = t; |
| 124 | side ? ++fUsed2 : ++fUsed; |
| 125 | } |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame^] | 126 | |
| 127 | void Intersections::remove(double one, double two) { |
| 128 | int index; |
| 129 | for (index = 0; index < fUsed; ++index) { |
| 130 | if (approximately_equal(fT[0][index], one) |
| 131 | && approximately_equal(fT[1][index], two)) { |
| 132 | goto foundIt; |
| 133 | } |
| 134 | if (fT[0][index] > one) { |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | return; |
| 139 | foundIt: |
| 140 | SkASSERT(fUsed > 0); |
| 141 | int remaining = fUsed - index; |
| 142 | if (remaining > 0) { |
| 143 | memmove(&fT[0][index], &fT[0][index - 1], sizeof(fT[0][0]) * remaining); |
| 144 | memmove(&fT[1][index], &fT[1][index - 1], sizeof(fT[1][0]) * remaining); |
| 145 | } |
| 146 | --fUsed; |
| 147 | } |