Flutter Engine
The Flutter Engine
Public Types | Static Public Member Functions | List of all members
SkLineClipper Class Reference

#include <SkLineClipper.h>

Public Types

enum  { kMaxPoints = 4 , kMaxClippedLineSegments = kMaxPoints - 1 }
 

Static Public Member Functions

static int ClipLine (const SkPoint pts[2], const SkRect &clip, SkPoint lines[kMaxPoints], bool canCullToTheRight)
 
static bool IntersectLine (const SkPoint src[2], const SkRect &clip, SkPoint dst[2])
 

Detailed Description

Definition at line 13 of file SkLineClipper.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
kMaxPoints 
kMaxClippedLineSegments 

Definition at line 15 of file SkLineClipper.h.

15 {
16 kMaxPoints = 4,
18 };

Member Function Documentation

◆ ClipLine()

int SkLineClipper::ClipLine ( const SkPoint  pts[2],
const SkRect clip,
SkPoint  lines[kMaxPoints],
bool  canCullToTheRight 
)
static

Definition at line 181 of file SkLineClipper.cpp.

182 {
183 int index0, index1;
184
185 if (pts[0].fY < pts[1].fY) {
186 index0 = 0;
187 index1 = 1;
188 } else {
189 index0 = 1;
190 index1 = 0;
191 }
192
193 // Check if we're completely clipped out in Y (above or below
194
195 if (pts[index1].fY <= clip.fTop) { // we're above the clip
196 return 0;
197 }
198 if (pts[index0].fY >= clip.fBottom) { // we're below the clip
199 return 0;
200 }
201
202 // Chop in Y to produce a single segment, stored in tmp[0..1]
203
204 SkPoint tmp[2];
205 memcpy(tmp, pts, sizeof(tmp));
206
207 // now compute intersections
208 if (pts[index0].fY < clip.fTop) {
209 tmp[index0].set(sect_with_horizontal(pts, clip.fTop), clip.fTop);
210 SkASSERT(is_between_unsorted(tmp[index0].fX, pts[0].fX, pts[1].fX));
211 }
212 if (tmp[index1].fY > clip.fBottom) {
213 tmp[index1].set(sect_with_horizontal(pts, clip.fBottom), clip.fBottom);
214 SkASSERT(is_between_unsorted(tmp[index1].fX, pts[0].fX, pts[1].fX));
215 }
216
217 // Chop it into 1..3 segments that are wholly within the clip in X.
218
219 // temp storage for up to 3 segments
220 SkPoint resultStorage[kMaxPoints];
221 SkPoint* result; // points to our results, either tmp or resultStorage
222 int lineCount = 1;
223 bool reverse;
224
225 if (pts[0].fX < pts[1].fX) {
226 index0 = 0;
227 index1 = 1;
228 reverse = false;
229 } else {
230 index0 = 1;
231 index1 = 0;
232 reverse = true;
233 }
234
235 if (tmp[index1].fX <= clip.fLeft) { // wholly to the left
236 tmp[0].fX = tmp[1].fX = clip.fLeft;
237 result = tmp;
238 reverse = false;
239 } else if (tmp[index0].fX >= clip.fRight) { // wholly to the right
240 if (canCullToTheRight) {
241 return 0;
242 }
243 tmp[0].fX = tmp[1].fX = clip.fRight;
244 result = tmp;
245 reverse = false;
246 } else {
247 result = resultStorage;
248 SkPoint* r = result;
249
250 if (tmp[index0].fX < clip.fLeft) {
251 r->set(clip.fLeft, tmp[index0].fY);
252 r += 1;
253 r->set(clip.fLeft, sect_clamp_with_vertical(tmp, clip.fLeft));
254 SkASSERT(is_between_unsorted(r->fY, tmp[0].fY, tmp[1].fY));
255 } else {
256 *r = tmp[index0];
257 }
258 r += 1;
259
260 if (tmp[index1].fX > clip.fRight) {
261 r->set(clip.fRight, sect_clamp_with_vertical(tmp, clip.fRight));
262 SkASSERT(is_between_unsorted(r->fY, tmp[0].fY, tmp[1].fY));
263 r += 1;
264 r->set(clip.fRight, tmp[index1].fY);
265 } else {
266 *r = tmp[index1];
267 }
268
269 lineCount = SkToInt(r - result);
270 }
271
272 // Now copy the results into the caller's lines[] parameter
273 if (reverse) {
274 // copy the pts in reverse order to maintain winding order
275 for (int i = 0; i <= lineCount; i++) {
276 lines[lineCount - i] = result[i];
277 }
278 } else {
279 memcpy(lines, result, (lineCount + 1) * sizeof(SkPoint));
280 }
281 return lineCount;
282}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y)
static SkScalar sect_clamp_with_vertical(const SkPoint src[2], SkScalar x)
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
constexpr int SkToInt(S x)
Definition: SkTo.h:29
GAsyncResult * result
float fX
x-axis value
Definition: SkPoint_impl.h:164
void set(float x, float y)
Definition: SkPoint_impl.h:200
float fY
y-axis value
Definition: SkPoint_impl.h:165

◆ IntersectLine()

bool SkLineClipper::IntersectLine ( const SkPoint  src[2],
const SkRect clip,
SkPoint  dst[2] 
)
static

Definition at line 96 of file SkLineClipper.cpp.

97 {
99
100 bounds.set(src[0], src[1]);
102 if (src != dst) {
103 memcpy(dst, src, 2 * sizeof(SkPoint));
104 }
105 return true;
106 }
107 // check for no overlap, and only permit coincident edges if the line
108 // and the edge are colinear
109 if (nestedLT(bounds.fRight, clip.fLeft, bounds.width()) ||
110 nestedLT(clip.fRight, bounds.fLeft, bounds.width()) ||
111 nestedLT(bounds.fBottom, clip.fTop, bounds.height()) ||
112 nestedLT(clip.fBottom, bounds.fTop, bounds.height())) {
113 return false;
114 }
115
116 int index0, index1;
117
118 if (src[0].fY < src[1].fY) {
119 index0 = 0;
120 index1 = 1;
121 } else {
122 index0 = 1;
123 index1 = 0;
124 }
125
126 SkPoint tmp[2];
127 memcpy(tmp, src, sizeof(tmp));
128
129 // now compute Y intersections
130 if (tmp[index0].fY < clip.fTop) {
131 tmp[index0].set(sect_with_horizontal(src, clip.fTop), clip.fTop);
132 }
133 if (tmp[index1].fY > clip.fBottom) {
134 tmp[index1].set(sect_with_horizontal(src, clip.fBottom), clip.fBottom);
135 }
136
137 if (tmp[0].fX < tmp[1].fX) {
138 index0 = 0;
139 index1 = 1;
140 } else {
141 index0 = 1;
142 index1 = 0;
143 }
144
145 // check for quick-reject in X again, now that we may have been chopped
146 if ((tmp[index1].fX <= clip.fLeft || tmp[index0].fX >= clip.fRight)) {
147 // usually we will return false, but we don't if the line is vertical and coincident
148 // with the clip.
149 if (tmp[0].fX != tmp[1].fX || tmp[0].fX < clip.fLeft || tmp[0].fX > clip.fRight) {
150 return false;
151 }
152 }
153
154 if (tmp[index0].fX < clip.fLeft) {
155 tmp[index0].set(clip.fLeft, sect_with_vertical(tmp, clip.fLeft));
156 }
157 if (tmp[index1].fX > clip.fRight) {
158 tmp[index1].set(clip.fRight, sect_with_vertical(tmp, clip.fRight));
159 }
160#ifdef SK_DEBUG
161 bounds.set(tmp[0], tmp[1]);
163#endif
164 memcpy(dst, tmp, sizeof(tmp));
165 return true;
166}
static bool nestedLT(SkScalar a, SkScalar b, SkScalar dim)
static SkScalar sect_with_vertical(const SkPoint src[2], SkScalar X)
static bool containsNoEmptyCheck(const SkRect &outer, const SkRect &inner)
Optional< SkRect > bounds
Definition: SkRecords.h:189
dst
Definition: cp.py:12

The documentation for this class was generated from the following files: