Debian Codegraph @

src/core/SkRect.cpp

raw
1 /*
2 * Copyright 2006 The Android Open Source Project
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 "include/core/SkRect.h"
9
10 #include "include/core/SkM44.h"
11 #include "include/private/base/SkDebug.h"
12 #include "include/private/base/SkTPin.h"
13 #include "src/core/SkRectPriv.h"
14
15 class SkMatrix;
16
17 bool SkIRect::intersect(const SkIRect& a, const SkIRect& b) {
18 SkIRect tmp = {
19 std::max(a.fLeft, b.fLeft),
20 std::max(a.fTop, b.fTop),
21 std::min(a.fRight, b.fRight),
22 std::min(a.fBottom, b.fBottom)
23 };
24 if (tmp.isEmpty()) {
25 return false;
26 }
27 *this = tmp;
28 return true;
29 }
30
31 void SkIRect::join(const SkIRect& r) {
32 // do nothing if the params are empty
33 if (r.fLeft >= r.fRight || r.fTop >= r.fBottom) {
34 return;
35 }
36
37 // if we are empty, just assign
38 if (fLeft >= fRight || fTop >= fBottom) {
39 *this = r;
40 } else {
41 if (r.fLeft < fLeft) fLeft = r.fLeft;
42 if (r.fTop < fTop) fTop = r.fTop;
43 if (r.fRight > fRight) fRight = r.fRight;
44 if (r.fBottom > fBottom) fBottom = r.fBottom;
45 }
46 }
47
48 /////////////////////////////////////////////////////////////////////////////
49
50 std::optional<SkRect> SkRect::Bounds(SkSpan<const SkPoint> points) {
51 if (points.empty()) {
52 return SkRect::MakeEmpty();
53 }
54
55 /*
56 * Both of these variants compute the same numerics.
57 *
58 * But, the "simple" one (no explicit skvx) runs faster (most of the time) on 64bit
59 * machines, and the tricky skvx version runs faster (most of the time) on 32bit machines.
60 *
61 * Hence the if/else
62 */
63
64 if constexpr (sizeof(void*) == 8) {
65 float L = points[0].fX, T = points[0].fY, R = points[0].fX, B = points[0].fY;
66 float nx = 0, ny = 0;
67 for (auto p : points) {
68 L = std::fminf(p.fX, L);
69 T = std::fminf(p.fY, T);
70 R = std::fmaxf(p.fX, R);
71 B = std::fmaxf(p.fY, B);
72
73 // we do this to look for infinities or nans
74 nx *= p.fX;
75 ny *= p.fY;
76 }
77
78 // if this is true, all our values were finite
79 if (nx == 0 && ny == 0) {
80 return {{L, T, R, B}};
81 }
82 } else {
83 auto count = points.size();
84 auto pts = points.data();
85
86 skvx::float4 min, max;
87 if (count & 1) {
88 min = max = skvx::float2::Load(pts).xyxy();
89 pts += 1;
90 count -= 1;
91 } else {
92 min = max = skvx::float4::Load(pts);
93 pts += 2;
94 count -= 2;
95 }
96
97 skvx::float4 accum = min * 0;
98 while (count) {
99 skvx::float4 xy = skvx::float4::Load(pts);
100 accum = accum * xy;
101 min = skvx::min(min, xy);
102 max = skvx::max(max, xy);
103 pts += 2;
104 count -= 2;
105 }
106
107 const bool all_finite = all(accum * 0 == 0);
108 if (all_finite) {
109 return MakeLTRB(std::min(min[0], min[2]), std::min(min[1], min[3]),
110 std::max(max[0], max[2]), std::max(max[1], max[3]));
111 }
112 }
113
114 /*
115 * If we got here, we were not empty, and at least one of the span values was
116 * either an Infinity or NaN -- so we return failure (no finite bounds)
117 */
118 return {};
119 }
120
121 bool SkRect::setBoundsCheck(SkSpan<const SkPoint> pts) {
122 if (auto bounds = Bounds(pts)) {
123 *this = bounds.value();
124 return true;
125 } else {
126 *this = MakeEmpty();
127 return false;
128 }
129 }
130
131 void SkRect::setBoundsNoCheck(SkSpan<const SkPoint> pts) {
132 if (auto bounds = Bounds(pts)) {
133 *this = bounds.value();
134 } else {
135 this->setLTRB(SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN);
136 }
137 }
138
139 #define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \
140 float L = std::max(al, bl); \
141 float R = std::min(ar, br); \
142 float T = std::max(at, bt); \
143 float B = std::min(ab, bb); \
144 do { if (!(L < R && T < B)) return false; } while (0)
145 // do the !(opposite) check so we return false if either arg is NaN
146
147 bool SkRect::intersect(const SkRect& r) {
148 CHECK_INTERSECT(r.fLeft, r.fTop, r.fRight, r.fBottom, fLeft, fTop, fRight, fBottom);
149 this->setLTRB(L, T, R, B);
150 return true;
151 }
152
153 bool SkRect::intersect(const SkRect& a, const SkRect& b) {
154 CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
155 this->setLTRB(L, T, R, B);
156 return true;
157 }
158
159 void SkRect::join(const SkRect& r) {
160 if (r.isEmpty()) {
161 return;
162 }
163
164 if (this->isEmpty()) {
165 *this = r;
166 } else {
167 fLeft = std::min(fLeft, r.fLeft);
168 fTop = std::min(fTop, r.fTop);
169 fRight = std::max(fRight, r.fRight);
170 fBottom = std::max(fBottom, r.fBottom);
171 }
172 }
173
174 ////////////////////////////////////////////////////////////////////////////////////////////////
175
176 #include "include/core/SkString.h"
177 #include "src/core/SkStringUtils.h"
178
179 static const char* set_scalar(SkString* storage, float value, SkScalarAsStringType asType) {
180 storage->reset();
181 SkAppendScalar(storage, value, asType);
182 return storage->c_str();
183 }
184
185 SkString SkRect::dumpToString(bool asHex) const {
186 SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType;
187
188 SkString line;
189 if (asHex) {
190 SkString tmp;
191 line.printf( "SkRect::MakeLTRB(%s, /* %f */\n", set_scalar(&tmp, fLeft, asType), fLeft);
192 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fTop, asType), fTop);
193 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fRight, asType), fRight);
194 line.appendf(" %s /* %f */);", set_scalar(&tmp, fBottom, asType), fBottom);
195 } else {
196 SkString strL, strT, strR, strB;
197 SkAppendScalarDec(&strL, fLeft);
198 SkAppendScalarDec(&strT, fTop);
199 SkAppendScalarDec(&strR, fRight);
200 SkAppendScalarDec(&strB, fBottom);
201 line.printf("SkRect::MakeLTRB(%s, %s, %s, %s);",
202 strL.c_str(), strT.c_str(), strR.c_str(), strB.c_str());
203 }
204 return line;
205 }
206
207 void SkRect::dump(bool asHex) const {
208 SkDebugf("%s\n", this->dumpToString(asHex).c_str());
209 }
210
211 ////////////////////////////////////////////////////////////////////////////////////////////////
212
213 template<typename R>
214 static bool subtract(const R& a, const R& b, R* out) {
215 if (a.isEmpty() || b.isEmpty() || !R::Intersects(a, b)) {
216 // Either already empty, or subtracting the empty rect, or there's no intersection, so
217 // in all cases the answer is A.
218 *out = a;
219 return true;
220 }
221
222 // 4 rectangles to consider. If the edge in A is contained in B, the resulting difference can
223 // be represented exactly as a rectangle. Otherwise the difference is the largest subrectangle
224 // that is disjoint from B:
225 // 1. Left part of A: (A.left, A.top, B.left, A.bottom)
226 // 2. Right part of A: (B.right, A.top, A.right, A.bottom)
227 // 3. Top part of A: (A.left, A.top, A.right, B.top)
228 // 4. Bottom part of A: (A.left, B.bottom, A.right, A.bottom)
229 //
230 // Depending on how B intersects A, there will be 1 to 4 positive areas:
231 // - 4 occur when A contains B
232 // - 3 occur when B intersects a single edge
233 // - 2 occur when B intersects at a corner, or spans two opposing edges
234 // - 1 occurs when B spans two opposing edges and contains a 3rd, resulting in an exact rect
235 // - 0 occurs when B contains A, resulting in the empty rect
236 //
237 // Compute the relative areas of the 4 rects described above. Since each subrectangle shares
238 // either the width or height of A, we only have to divide by the other dimension, which avoids
239 // overflow on int32 types, and even if the float relative areas overflow to infinity, the
240 // comparisons work out correctly and (one of) the infinitely large subrects will be chosen.
241 float aHeight = (float) a.height();
242 float aWidth = (float) a.width();
243 float leftArea = 0.f, rightArea = 0.f, topArea = 0.f, bottomArea = 0.f;
244 int positiveCount = 0;
245 if (b.fLeft > a.fLeft) {
246 leftArea = (b.fLeft - a.fLeft) / aWidth;
247 positiveCount++;
248 }
249 if (a.fRight > b.fRight) {
250 rightArea = (a.fRight - b.fRight) / aWidth;
251 positiveCount++;
252 }
253 if (b.fTop > a.fTop) {
254 topArea = (b.fTop - a.fTop) / aHeight;
255 positiveCount++;
256 }
257 if (a.fBottom > b.fBottom) {
258 bottomArea = (a.fBottom - b.fBottom) / aHeight;
259 positiveCount++;
260 }
261
262 if (positiveCount == 0) {
263 SkASSERT(b.contains(a));
264 *out = R::MakeEmpty();
265 return true;
266 }
267
268 *out = a;
269 if (leftArea > rightArea && leftArea > topArea && leftArea > bottomArea) {
270 // Left chunk of A, so the new right edge is B's left edge
271 out->fRight = b.fLeft;
272 } else if (rightArea > topArea && rightArea > bottomArea) {
273 // Right chunk of A, so the new left edge is B's right edge
274 out->fLeft = b.fRight;
275 } else if (topArea > bottomArea) {
276 // Top chunk of A, so the new bottom edge is B's top edge
277 out->fBottom = b.fTop;
278 } else {
279 // Bottom chunk of A, so the new top edge is B's bottom edge
280 SkASSERT(bottomArea > 0.f);
281 out->fTop = b.fBottom;
282 }
283
284 // If we have 1 valid area, the disjoint shape is representable as a rectangle.
285 SkASSERT(!R::Intersects(*out, b));
286 return positiveCount == 1;
287 }
288
289 bool SkRectPriv::Subtract(const SkRect& a, const SkRect& b, SkRect* out) {
290 return subtract<SkRect>(a, b, out);
291 }
292
293 bool SkRectPriv::Subtract(const SkIRect& a, const SkIRect& b, SkIRect* out) {
294 return subtract<SkIRect>(a, b, out);
295 }
296
297
298 bool SkRectPriv::QuadContainsRect(const SkMatrix& m,
299 const SkIRect& a,
300 const SkIRect& b,
301 float tol) {
302 return QuadContainsRect(SkM44(m), SkRect::Make(a), SkRect::Make(b), tol);
303 }
304
305 bool SkRectPriv::QuadContainsRect(const SkM44& m, const SkRect& a, const SkRect& b, float tol) {
306 return all(QuadContainsRectMask(m, a, b, tol));
307 }
308
309 skvx::int4 SkRectPriv::QuadContainsRectMask(const SkM44& m,
310 const SkRect& a,
311 const SkRect& b,
312 float tol) {
313 SkDEBUGCODE(SkM44 inverse;)
314 SkASSERT(m.invert(&inverse));
315 // With empty rectangles, the calculated edges could give surprising results. If 'a' were not
316 // sorted, its normals would point outside the sorted rectangle, so lots of potential rects
317 // would be seen as "contained". If 'a' is all 0s, its edge equations are also (0,0,0) so every
318 // point has a distance of 0, and would be interpreted as inside.
319 if (a.isEmpty()) {
320 return skvx::int4(0); // all "false"
321 }
322 // However, 'b' is only used to define its 4 corners to check against the transformed edges.
323 // This is valid regardless of b's emptiness or sortedness.
324
325 // Calculate the 4 homogenous coordinates of 'a' transformed by 'm' where Z=0 and W=1.
326 auto ax = skvx::float4{a.fLeft, a.fRight, a.fRight, a.fLeft};
327 auto ay = skvx::float4{a.fTop, a.fTop, a.fBottom, a.fBottom};
328
329 auto max = m.rc(0,0)*ax + m.rc(0,1)*ay + m.rc(0,3);
330 auto may = m.rc(1,0)*ax + m.rc(1,1)*ay + m.rc(1,3);
331 auto maw = m.rc(3,0)*ax + m.rc(3,1)*ay + m.rc(3,3);
332
333 if (all(maw < 0.f)) {
334 // If all points of A are mapped to w < 0, then the edge equations end up representing the
335 // convex hull of projected points when A should in fact be considered empty.
336 return skvx::int4(0); // all "false"
337 }
338
339 // Cross product of adjacent vertices provides homogenous lines for the 4 sides of the quad
340 auto lA = may*skvx::shuffle<1,2,3,0>(maw) - maw*skvx::shuffle<1,2,3,0>(may);
341 auto lB = maw*skvx::shuffle<1,2,3,0>(max) - max*skvx::shuffle<1,2,3,0>(maw);
342 auto lC = max*skvx::shuffle<1,2,3,0>(may) - may*skvx::shuffle<1,2,3,0>(max);
343
344 // Before transforming, the corners of 'a' were in CW order, but afterwards they may become CCW,
345 // so the sign corrects the direction of the edge normals to point inwards.
346 float sign = (lA[0]*lB[1] - lB[0]*lA[1]) < 0 ? -1.f : 1.f;
347
348 // Calculate distance from 'b' to each edge. Since 'b' has presumably been transformed by 'm'
349 // *and* projected, this assumes W = 1.
350 SkRect bInset = b.makeInset(tol, tol);
351 auto d0 = sign * (lA*bInset.fLeft + lB*bInset.fTop + lC);
352 auto d1 = sign * (lA*bInset.fRight + lB*bInset.fTop + lC);
353 auto d2 = sign * (lA*bInset.fRight + lB*bInset.fBottom + lC);
354 auto d3 = sign * (lA*bInset.fLeft + lB*bInset.fBottom + lC);
355
356 // 'b' is contained in the mapped rectangle if all distances are >= 0
357 return (d0 >= 0.f) & (d1 >= 0.f) & (d2 >= 0.f) & (d3 >= 0.f);
358 }
359
360 SkIRect SkRectPriv::ClosestDisjointEdge(const SkIRect& src, const SkIRect& dst) {
361 if (src.isEmpty() || dst.isEmpty()) {
362 return SkIRect::MakeEmpty();
363 }
364
365 int l = src.fLeft;
366 int r = src.fRight;
367 if (r <= dst.fLeft) {
368 // Select right column of pixels in crop
369 l = r - 1;
370 } else if (l >= dst.fRight) {
371 // Left column of 'crop'
372 r = l + 1;
373 } else {
374 // Regular intersection along X axis.
375 l = SkTPin(l, dst.fLeft, dst.fRight);
376 r = SkTPin(r, dst.fLeft, dst.fRight);
377 }
378
379 int t = src.fTop;
380 int b = src.fBottom;
381 if (b <= dst.fTop) {
382 // Select bottom row of pixels in crop
383 t = b - 1;
384 } else if (t >= dst.fBottom) {
385 // Top row of 'crop'
386 b = t + 1;
387 } else {
388 t = SkTPin(t, dst.fTop, dst.fBottom);
389 b = SkTPin(b, dst.fTop, dst.fBottom);
390 }
391
392 return SkIRect::MakeLTRB(l,t,r,b);
393 }