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