| 1 |
/* |
| 2 |
* Copyright 2011 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 |
*/ |
| 7 |
|
| 8 |
#include "src/core/SkLineClipper.h" |
| 9 |
|
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
#include "include/private/base/SkTo.h" |
| 16 |
|
| 17 |
#include <cstring> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
template <typename T> T pin_unsorted(T value, T limit0, T limit1) { |
| 21 |
if (limit1 < limit0) { |
| 22 |
using std::swap; |
| 23 |
swap(limit0, limit1); |
| 24 |
} |
| 25 |
// now the limits are sorted |
| 26 |
SkASSERT(limit0 <= limit1); |
| 27 |
|
| 28 |
if (value < limit0) { |
| 29 |
value = limit0; |
| 30 |
} else if (value > limit1) { |
| 31 |
value = limit1; |
| 32 |
} |
| 33 |
return value; |
| 34 |
} |
| 35 |
|
| 36 |
// return X coordinate of intersection with horizontal line at Y |
| 37 |
static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { |
| 38 |
SkScalar dy = src[1].fY - src[0].fY; |
| 39 |
if (SkScalarNearlyZero(dy)) { |
| 40 |
return sk_float_midpoint(src[0].fX, src[1].fX); |
| 41 |
} else { |
| 42 |
// need the extra precision so we don't compute a value that exceeds |
| 43 |
// our original limits |
| 44 |
double X0 = src[0].fX; |
| 45 |
double Y0 = src[0].fY; |
| 46 |
double X1 = src[1].fX; |
| 47 |
double Y1 = src[1].fY; |
| 48 |
double result = X0 + ((double)Y - Y0) * (X1 - X0) / (Y1 - Y0); |
| 49 |
|
| 50 |
// The computed X value might still exceed [X0..X1] due to quantum flux |
| 51 |
// when the doubles were added and subtracted, so we have to pin the |
| 52 |
// answer :( |
| 53 |
return (float)pin_unsorted(result, X0, X1); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// return Y coordinate of intersection with vertical line at X |
| 58 |
static SkScalar sect_with_vertical(const SkPoint src[2], SkScalar X) { |
| 59 |
SkScalar dx = src[1].fX - src[0].fX; |
| 60 |
if (SkScalarNearlyZero(dx)) { |
| 61 |
return sk_float_midpoint(src[0].fY, src[1].fY); |
| 62 |
} else { |
| 63 |
// need the extra precision so we don't compute a value that exceeds |
| 64 |
// our original limits |
| 65 |
double X0 = src[0].fX; |
| 66 |
double Y0 = src[0].fY; |
| 67 |
double X1 = src[1].fX; |
| 68 |
double Y1 = src[1].fY; |
| 69 |
double result = Y0 + ((double)X - X0) * (Y1 - Y0) / (X1 - X0); |
| 70 |
return (float)result; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static SkScalar sect_clamp_with_vertical(const SkPoint src[2], SkScalar x) { |
| 75 |
SkScalar y = sect_with_vertical(src, x); |
| 76 |
// Our caller expects y to be between src[0].fY and src[1].fY (unsorted), but due to the |
| 77 |
// numerics of floats/doubles, we might have computed a value slightly outside of that, |
| 78 |
// so we have to manually clamp afterwards. |
| 79 |
// See skbug.com/40038736 |
| 80 |
return pin_unsorted(y, src[0].fY, src[1].fY); |
| 81 |
} |
| 82 |
|
| 83 |
/////////////////////////////////////////////////////////////////////////////// |
| 84 |
|
| 85 |
static inline bool nestedLT(SkScalar a, SkScalar b, SkScalar dim) { |
| 86 |
return a <= b && (a < b || dim > 0); |
| 87 |
} |
| 88 |
|
| 89 |
// returns true if outer contains inner, even if inner is empty. |
| 90 |
// note: outer.contains(inner) always returns false if inner is empty. |
| 91 |
static inline bool containsNoEmptyCheck(const SkRect& outer, |
| 92 |
const SkRect& inner) { |
| 93 |
return outer.fLeft <= inner.fLeft && outer.fTop <= inner.fTop && |
| 94 |
outer.fRight >= inner.fRight && outer.fBottom >= inner.fBottom; |
| 95 |
} |
| 96 |
|
| 97 |
bool SkLineClipper::IntersectLine(const SkPoint src[2], const SkRect& clip, |
| 98 |
SkPoint dst[2]) { |
| 99 |
SkRect bounds; |
| 100 |
|
| 101 |
bounds.set(src[0], src[1]); |
| 102 |
if (containsNoEmptyCheck(clip, bounds)) { |
| 103 |
if (src != dst) { |
| 104 |
memcpy(dst, src, 2 * sizeof(SkPoint)); |
| 105 |
} |
| 106 |
return true; |
| 107 |
} |
| 108 |
// check for no overlap, and only permit coincident edges if the line |
| 109 |
// and the edge are colinear |
| 110 |
if (nestedLT(bounds.fRight, clip.fLeft, bounds.width()) || |
| 111 |
nestedLT(clip.fRight, bounds.fLeft, bounds.width()) || |
| 112 |
nestedLT(bounds.fBottom, clip.fTop, bounds.height()) || |
| 113 |
nestedLT(clip.fBottom, bounds.fTop, bounds.height())) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
int index0, index1; |
| 118 |
|
| 119 |
if (src[0].fY < src[1].fY) { |
| 120 |
index0 = 0; |
| 121 |
index1 = 1; |
| 122 |
} else { |
| 123 |
index0 = 1; |
| 124 |
index1 = 0; |
| 125 |
} |
| 126 |
|
| 127 |
SkPoint tmp[2]; |
| 128 |
memcpy(tmp, src, sizeof(tmp)); |
| 129 |
|
| 130 |
// now compute Y intersections |
| 131 |
if (tmp[index0].fY < clip.fTop) { |
| 132 |
tmp[index0].set(sect_with_horizontal(src, clip.fTop), clip.fTop); |
| 133 |
} |
| 134 |
if (tmp[index1].fY > clip.fBottom) { |
| 135 |
tmp[index1].set(sect_with_horizontal(src, clip.fBottom), clip.fBottom); |
| 136 |
} |
| 137 |
|
| 138 |
if (tmp[0].fX < tmp[1].fX) { |
| 139 |
index0 = 0; |
| 140 |
index1 = 1; |
| 141 |
} else { |
| 142 |
index0 = 1; |
| 143 |
index1 = 0; |
| 144 |
} |
| 145 |
|
| 146 |
// check for quick-reject in X again, now that we may have been chopped |
| 147 |
if ((tmp[index1].fX <= clip.fLeft || tmp[index0].fX >= clip.fRight)) { |
| 148 |
// usually we will return false, but we don't if the line is vertical and coincident |
| 149 |
// with the clip. |
| 150 |
if (tmp[0].fX != tmp[1].fX || tmp[0].fX < clip.fLeft || tmp[0].fX > clip.fRight) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (tmp[index0].fX < clip.fLeft) { |
| 156 |
tmp[index0].set(clip.fLeft, sect_with_vertical(tmp, clip.fLeft)); |
| 157 |
} |
| 158 |
if (tmp[index1].fX > clip.fRight) { |
| 159 |
tmp[index1].set(clip.fRight, sect_with_vertical(tmp, clip.fRight)); |
| 160 |
} |
| 161 |
#ifdef SK_DEBUG |
| 162 |
bounds.set(tmp[0], tmp[1]); |
| 163 |
SkASSERT(containsNoEmptyCheck(clip, bounds)); |
| 164 |
#endif |
| 165 |
memcpy(dst, tmp, sizeof(tmp)); |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
#ifdef SK_DEBUG |
| 170 |
// return value between the two limits, where the limits are either ascending |
| 171 |
// or descending. |
| 172 |
static bool is_between_unsorted(SkScalar value, |
| 173 |
SkScalar limit0, SkScalar limit1) { |
| 174 |
if (limit0 < limit1) { |
| 175 |
return limit0 <= value && value <= limit1; |
| 176 |
} else { |
| 177 |
return limit1 <= value && value <= limit0; |
| 178 |
} |
| 179 |
} |
| 180 |
#endif |
| 181 |
|
| 182 |
int SkLineClipper::ClipLine(const SkPoint pts[2], const SkRect& clip, SkPoint lines[kMaxPoints], |
| 183 |
bool canCullToTheRight) { |
| 184 |
int index0, index1; |
| 185 |
|
| 186 |
if (pts[0].fY < pts[1].fY) { |
| 187 |
index0 = 0; |
| 188 |
index1 = 1; |
| 189 |
} else { |
| 190 |
index0 = 1; |
| 191 |
index1 = 0; |
| 192 |
} |
| 193 |
|
| 194 |
// Check if we're completely clipped out in Y (above or below |
| 195 |
|
| 196 |
if (pts[index1].fY <= clip.fTop) { // we're above the clip |
| 197 |
return 0; |
| 198 |
} |
| 199 |
if (pts[index0].fY >= clip.fBottom) { // we're below the clip |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
// Chop in Y to produce a single segment, stored in tmp[0..1] |
| 204 |
|
| 205 |
SkPoint tmp[2]; |
| 206 |
memcpy(tmp, pts, sizeof(tmp)); |
| 207 |
|
| 208 |
// now compute intersections |
| 209 |
if (pts[index0].fY < clip.fTop) { |
| 210 |
tmp[index0].set(sect_with_horizontal(pts, clip.fTop), clip.fTop); |
| 211 |
SkASSERT(is_between_unsorted(tmp[index0].fX, pts[0].fX, pts[1].fX)); |
| 212 |
} |
| 213 |
if (tmp[index1].fY > clip.fBottom) { |
| 214 |
tmp[index1].set(sect_with_horizontal(pts, clip.fBottom), clip.fBottom); |
| 215 |
SkASSERT(is_between_unsorted(tmp[index1].fX, pts[0].fX, pts[1].fX)); |
| 216 |
} |
| 217 |
|
| 218 |
// Chop it into 1..3 segments that are wholly within the clip in X. |
| 219 |
|
| 220 |
// temp storage for up to 3 segments |
| 221 |
SkPoint resultStorage[kMaxPoints]; |
| 222 |
SkPoint* result; // points to our results, either tmp or resultStorage |
| 223 |
int lineCount = 1; |
| 224 |
bool reverse; |
| 225 |
|
| 226 |
if (pts[0].fX < pts[1].fX) { |
| 227 |
index0 = 0; |
| 228 |
index1 = 1; |
| 229 |
reverse = false; |
| 230 |
} else { |
| 231 |
index0 = 1; |
| 232 |
index1 = 0; |
| 233 |
reverse = true; |
| 234 |
} |
| 235 |
|
| 236 |
if (tmp[index1].fX <= clip.fLeft) { // wholly to the left |
| 237 |
tmp[0].fX = tmp[1].fX = clip.fLeft; |
| 238 |
result = tmp; |
| 239 |
reverse = false; |
| 240 |
} else if (tmp[index0].fX >= clip.fRight) { // wholly to the right |
| 241 |
if (canCullToTheRight) { |
| 242 |
return 0; |
| 243 |
} |
| 244 |
tmp[0].fX = tmp[1].fX = clip.fRight; |
| 245 |
result = tmp; |
| 246 |
reverse = false; |
| 247 |
} else { |
| 248 |
result = resultStorage; |
| 249 |
SkPoint* r = result; |
| 250 |
|
| 251 |
if (tmp[index0].fX < clip.fLeft) { |
| 252 |
r->set(clip.fLeft, tmp[index0].fY); |
| 253 |
r += 1; |
| 254 |
r->set(clip.fLeft, sect_clamp_with_vertical(tmp, clip.fLeft)); |
| 255 |
SkASSERT(is_between_unsorted(r->fY, tmp[0].fY, tmp[1].fY)); |
| 256 |
} else { |
| 257 |
*r = tmp[index0]; |
| 258 |
} |
| 259 |
r += 1; |
| 260 |
|
| 261 |
if (tmp[index1].fX > clip.fRight) { |
| 262 |
r->set(clip.fRight, sect_clamp_with_vertical(tmp, clip.fRight)); |
| 263 |
SkASSERT(is_between_unsorted(r->fY, tmp[0].fY, tmp[1].fY)); |
| 264 |
r += 1; |
| 265 |
r->set(clip.fRight, tmp[index1].fY); |
| 266 |
} else { |
| 267 |
*r = tmp[index1]; |
| 268 |
} |
| 269 |
|
| 270 |
lineCount = SkToInt(r - result); |
| 271 |
} |
| 272 |
|
| 273 |
// Now copy the results into the caller's lines[] parameter |
| 274 |
if (reverse) { |
| 275 |
// copy the pts in reverse order to maintain winding order |
| 276 |
for (int i = 0; i <= lineCount; i++) { |
| 277 |
lines[lineCount - i] = result[i]; |
| 278 |
} |
| 279 |
} else { |
| 280 |
memcpy(lines, result, (lineCount + 1) * sizeof(SkPoint)); |
| 281 |
} |
| 282 |
return lineCount; |
| 283 |
} |