• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
dump(bool asHex) const152 void SkRect::dump(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     SkDebugf("%s\n", line.c_str());
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////////////////////
175 
176 template<typename R>
subtract(const R & a,const R & b,R * out)177 static bool subtract(const R& a, const R& b, R* out) {
178     if (a.isEmpty() || b.isEmpty() || !R::Intersects(a, b)) {
179         // Either already empty, or subtracting the empty rect, or there's no intersection, so
180         // in all cases the answer is A.
181         *out = a;
182         return true;
183     }
184 
185     // 4 rectangles to consider. If the edge in A is contained in B, the resulting difference can
186     // be represented exactly as a rectangle. Otherwise the difference is the largest subrectangle
187     // that is disjoint from B:
188     // 1. Left part of A:   (A.left,  A.top,    B.left,  A.bottom)
189     // 2. Right part of A:  (B.right, A.top,    A.right, A.bottom)
190     // 3. Top part of A:    (A.left,  A.top,    A.right, B.top)
191     // 4. Bottom part of A: (A.left,  B.bottom, A.right, A.bottom)
192     //
193     // Depending on how B intersects A, there will be 1 to 4 positive areas:
194     //  - 4 occur when A contains B
195     //  - 3 occur when B intersects a single edge
196     //  - 2 occur when B intersects at a corner, or spans two opposing edges
197     //  - 1 occurs when B spans two opposing edges and contains a 3rd, resulting in an exact rect
198     //  - 0 occurs when B contains A, resulting in the empty rect
199     //
200     // Compute the relative areas of the 4 rects described above. Since each subrectangle shares
201     // either the width or height of A, we only have to divide by the other dimension, which avoids
202     // overflow on int32 types, and even if the float relative areas overflow to infinity, the
203     // comparisons work out correctly and (one of) the infinitely large subrects will be chosen.
204     float aHeight = (float) a.height();
205     float aWidth = (float) a.width();
206     float leftArea = 0.f, rightArea = 0.f, topArea = 0.f, bottomArea = 0.f;
207     int positiveCount = 0;
208     if (b.fLeft > a.fLeft) {
209         leftArea = (b.fLeft - a.fLeft) / aWidth;
210         positiveCount++;
211     }
212     if (a.fRight > b.fRight) {
213         rightArea = (a.fRight - b.fRight) / aWidth;
214         positiveCount++;
215     }
216     if (b.fTop > a.fTop) {
217         topArea = (b.fTop - a.fTop) / aHeight;
218         positiveCount++;
219     }
220     if (a.fBottom > b.fBottom) {
221         bottomArea = (a.fBottom - b.fBottom) / aHeight;
222         positiveCount++;
223     }
224 
225     if (positiveCount == 0) {
226         SkASSERT(b.contains(a));
227         *out = R::MakeEmpty();
228         return true;
229     }
230 
231     *out = a;
232     if (leftArea > rightArea && leftArea > topArea && leftArea > bottomArea) {
233         // Left chunk of A, so the new right edge is B's left edge
234         out->fRight = b.fLeft;
235     } else if (rightArea > topArea && rightArea > bottomArea) {
236         // Right chunk of A, so the new left edge is B's right edge
237         out->fLeft = b.fRight;
238     } else if (topArea > bottomArea) {
239         // Top chunk of A, so the new bottom edge is B's top edge
240         out->fBottom = b.fTop;
241     } else {
242         // Bottom chunk of A, so the new top edge is B's bottom edge
243         SkASSERT(bottomArea > 0.f);
244         out->fTop = b.fBottom;
245     }
246 
247     // If we have 1 valid area, the disjoint shape is representable as a rectangle.
248     SkASSERT(!R::Intersects(*out, b));
249     return positiveCount == 1;
250 }
251 
Subtract(const SkRect & a,const SkRect & b,SkRect * out)252 bool SkRectPriv::Subtract(const SkRect& a, const SkRect& b, SkRect* out) {
253     return subtract<SkRect>(a, b, out);
254 }
255 
Subtract(const SkIRect & a,const SkIRect & b,SkIRect * out)256 bool SkRectPriv::Subtract(const SkIRect& a, const SkIRect& b, SkIRect* out) {
257     return subtract<SkIRect>(a, b, out);
258 }
259 
260 
QuadContainsRect(const SkMatrix & m,const SkIRect & a,const SkIRect & b,float tol)261 bool SkRectPriv::QuadContainsRect(const SkMatrix& m,
262                                   const SkIRect& a,
263                                   const SkIRect& b,
264                                   float tol) {
265     return QuadContainsRect(SkM44(m), SkRect::Make(a), SkRect::Make(b), tol);
266 }
267 
QuadContainsRect(const SkM44 & m,const SkRect & a,const SkRect & b,float tol)268 bool SkRectPriv::QuadContainsRect(const SkM44& m, const SkRect& a, const SkRect& b, float tol) {
269     return all(QuadContainsRectMask(m, a, b, tol));
270 }
271 
QuadContainsRectMask(const SkM44 & m,const SkRect & a,const SkRect & b,float tol)272 skvx::int4 SkRectPriv::QuadContainsRectMask(const SkM44& m,
273                                             const SkRect& a,
274                                             const SkRect& b,
275                                             float tol) {
276     SkDEBUGCODE(SkM44 inverse;)
277     SkASSERT(m.invert(&inverse));
278     // With empty rectangles, the calculated edges could give surprising results. If 'a' were not
279     // sorted, its normals would point outside the sorted rectangle, so lots of potential rects
280     // would be seen as "contained". If 'a' is all 0s, its edge equations are also (0,0,0) so every
281     // point has a distance of 0, and would be interpreted as inside.
282     if (a.isEmpty()) {
283         return skvx::int4(0); // all "false"
284     }
285     // However, 'b' is only used to define its 4 corners to check against the transformed edges.
286     // This is valid regardless of b's emptiness or sortedness.
287 
288     // Calculate the 4 homogenous coordinates of 'a' transformed by 'm' where Z=0 and W=1.
289     auto ax = skvx::float4{a.fLeft, a.fRight, a.fRight, a.fLeft};
290     auto ay = skvx::float4{a.fTop, a.fTop, a.fBottom, a.fBottom};
291 
292     auto max = m.rc(0,0)*ax + m.rc(0,1)*ay + m.rc(0,3);
293     auto may = m.rc(1,0)*ax + m.rc(1,1)*ay + m.rc(1,3);
294     auto maw = m.rc(3,0)*ax + m.rc(3,1)*ay + m.rc(3,3);
295 
296     if (all(maw < 0.f)) {
297         // If all points of A are mapped to w < 0, then the edge equations end up representing the
298         // convex hull of projected points when A should in fact be considered empty.
299         return skvx::int4(0); // all "false"
300     }
301 
302     // Cross product of adjacent vertices provides homogenous lines for the 4 sides of the quad
303     auto lA = may*skvx::shuffle<1,2,3,0>(maw) - maw*skvx::shuffle<1,2,3,0>(may);
304     auto lB = maw*skvx::shuffle<1,2,3,0>(max) - max*skvx::shuffle<1,2,3,0>(maw);
305     auto lC = max*skvx::shuffle<1,2,3,0>(may) - may*skvx::shuffle<1,2,3,0>(max);
306 
307     // Before transforming, the corners of 'a' were in CW order, but afterwards they may become CCW,
308     // so the sign corrects the direction of the edge normals to point inwards.
309     float sign = (lA[0]*lB[1] - lB[0]*lA[1]) < 0 ? -1.f : 1.f;
310 
311     // Calculate distance from 'b' to each edge. Since 'b' has presumably been transformed by 'm'
312     // *and* projected, this assumes W = 1.
313     SkRect bInset = b.makeInset(tol, tol);
314     auto d0 = sign * (lA*bInset.fLeft  + lB*bInset.fTop    + lC);
315     auto d1 = sign * (lA*bInset.fRight + lB*bInset.fTop    + lC);
316     auto d2 = sign * (lA*bInset.fRight + lB*bInset.fBottom + lC);
317     auto d3 = sign * (lA*bInset.fLeft  + lB*bInset.fBottom + lC);
318 
319     // 'b' is contained in the mapped rectangle if all distances are >= 0
320     return (d0 >= 0.f) & (d1 >= 0.f) & (d2 >= 0.f) & (d3 >= 0.f);
321 }
322 
ClosestDisjointEdge(const SkIRect & src,const SkIRect & dst)323 SkIRect SkRectPriv::ClosestDisjointEdge(const SkIRect& src, const SkIRect& dst) {
324     if (src.isEmpty() || dst.isEmpty()) {
325         return SkIRect::MakeEmpty();
326     }
327 
328     int l = src.fLeft;
329     int r = src.fRight;
330     if (r <= dst.fLeft) {
331         // Select right column of pixels in crop
332         l = r - 1;
333     } else if (l >= dst.fRight) {
334         // Left column of 'crop'
335         r = l + 1;
336     } else {
337         // Regular intersection along X axis.
338         l = SkTPin(l, dst.fLeft, dst.fRight);
339         r = SkTPin(r, dst.fLeft, dst.fRight);
340     }
341 
342     int t = src.fTop;
343     int b = src.fBottom;
344     if (b <= dst.fTop) {
345         // Select bottom row of pixels in crop
346         t = b - 1;
347     } else if (t >= dst.fBottom) {
348         // Top row of 'crop'
349         b = t + 1;
350     } else {
351         t = SkTPin(t, dst.fTop, dst.fBottom);
352         b = SkTPin(b, dst.fTop, dst.fBottom);
353     }
354 
355     return SkIRect::MakeLTRB(l,t,r,b);
356 }
357