OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLineClipper.h" | 8 #include "SkLineClipper.h" |
9 | 9 |
10 template <typename T> T pin_unsorted(T value, T limit0, T limit1) { | 10 template <typename T> T pin_unsorted(T value, T limit0, T limit1) { |
11 if (limit1 < limit0) { | 11 if (limit1 < limit0) { |
12 SkTSwap(limit0, limit1); | 12 SkTSwap(limit0, limit1); |
13 } | 13 } |
14 // now the limits are sorted | 14 // now the limits are sorted |
15 SkASSERT(limit0 <= limit1); | 15 SkASSERT(limit0 <= limit1); |
16 | 16 |
17 if (value < limit0) { | 17 if (value < limit0) { |
18 value = limit0; | 18 value = limit0; |
19 } else if (value > limit1) { | 19 } else if (value > limit1) { |
20 value = limit1; | 20 value = limit1; |
21 } | 21 } |
22 return value; | 22 return value; |
23 } | 23 } |
24 | 24 |
25 // This is an example of why we need to pin the result computed in | |
26 // sect_with_horizontal. If we didn't explicitly pin, is_between_unsorted would | |
27 // fail. | |
28 // | |
29 #if 0 | |
30 static void sect_with_horizontal_test_for_pin_results() { | |
31 const SkPoint pts[] = { | |
32 { -540000, -720000 }, | |
33 { -9.10000017e-05f, 9.99999996e-13f } | |
34 }; | |
35 float x = sect_with_horizontal(pts, 0); | |
36 SkASSERT(is_between_unsorted(x, pts[0].fX, pts[1].fX)); | |
37 } | |
38 #endif | |
39 | |
25 // return X coordinate of intersection with horizontal line at Y | 40 // return X coordinate of intersection with horizontal line at Y |
26 static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { | 41 static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { |
27 SkScalar dy = src[1].fY - src[0].fY; | 42 SkScalar dy = src[1].fY - src[0].fY; |
28 if (SkScalarNearlyZero(dy)) { | 43 if (SkScalarNearlyZero(dy)) { |
29 return SkScalarAve(src[0].fX, src[1].fX); | 44 return SkScalarAve(src[0].fX, src[1].fX); |
30 } else { | 45 } else { |
31 // need the extra precision so we don't compute a value that exceeds | 46 // need the extra precision so we don't compute a value that exceeds |
32 // our original limits | 47 // our original limits |
33 double X0 = src[0].fX; | 48 double X0 = src[0].fX; |
34 double Y0 = src[0].fY; | 49 double Y0 = src[0].fY; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 static bool is_between_unsorted(SkScalar value, | 165 static bool is_between_unsorted(SkScalar value, |
151 SkScalar limit0, SkScalar limit1) { | 166 SkScalar limit0, SkScalar limit1) { |
152 if (limit0 < limit1) { | 167 if (limit0 < limit1) { |
153 return limit0 <= value && value <= limit1; | 168 return limit0 <= value && value <= limit1; |
154 } else { | 169 } else { |
155 return limit1 <= value && value <= limit0; | 170 return limit1 <= value && value <= limit0; |
156 } | 171 } |
157 } | 172 } |
158 #endif | 173 #endif |
159 | 174 |
160 #ifdef SK_DEBUG | |
161 // This is an example of why we need to pin the result computed in | |
162 // sect_with_horizontal. If we didn't explicitly pin, is_between_unsorted would | |
163 // fail. | |
164 // | |
165 static void sect_with_horizontal_test_for_pin_results() { | |
166 const SkPoint pts[] = { | |
167 { -540000, -720000 }, | |
168 { -9.10000017e-05f, 9.99999996e-13f } | |
169 }; | |
170 float x = sect_with_horizontal(pts, 0); | |
171 SkASSERT(is_between_unsorted(x, pts[0].fX, pts[1].fX)); | |
172 } | |
173 #endif | |
174 | |
175 int SkLineClipper::ClipLine(const SkPoint pts[], const SkRect& clip, SkPoint lin es[], | 175 int SkLineClipper::ClipLine(const SkPoint pts[], const SkRect& clip, SkPoint lin es[], |
176 bool canCullToTheRight) { | 176 bool canCullToTheRight) { |
177 | |
178 #ifdef SK_DEBUG | |
179 { | |
180 static bool gOnce; | |
mtklein
2016/08/17 13:12:53
If you want to keep this and make it thread safe,
| |
181 if (!gOnce) { | |
182 sect_with_horizontal_test_for_pin_results(); | |
183 gOnce = true; | |
184 } | |
185 } | |
186 #endif | |
187 | |
188 int index0, index1; | 177 int index0, index1; |
189 | 178 |
190 if (pts[0].fY < pts[1].fY) { | 179 if (pts[0].fY < pts[1].fY) { |
191 index0 = 0; | 180 index0 = 0; |
192 index1 = 1; | 181 index1 = 1; |
193 } else { | 182 } else { |
194 index0 = 1; | 183 index0 = 1; |
195 index1 = 0; | 184 index1 = 0; |
196 } | 185 } |
197 | 186 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 if (reverse) { | 267 if (reverse) { |
279 // copy the pts in reverse order to maintain winding order | 268 // copy the pts in reverse order to maintain winding order |
280 for (int i = 0; i <= lineCount; i++) { | 269 for (int i = 0; i <= lineCount; i++) { |
281 lines[lineCount - i] = result[i]; | 270 lines[lineCount - i] = result[i]; |
282 } | 271 } |
283 } else { | 272 } else { |
284 memcpy(lines, result, (lineCount + 1) * sizeof(SkPoint)); | 273 memcpy(lines, result, (lineCount + 1) * sizeof(SkPoint)); |
285 } | 274 } |
286 return lineCount; | 275 return lineCount; |
287 } | 276 } |
OLD | NEW |