epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkRect.h" |
| 11 | |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 12 | void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | // do nothing if the params are empty |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 14 | if (left >= right || top >= bottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | return; |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 16 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | // if we are empty, just assign |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 19 | if (fLeft >= fRight || fTop >= fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | this->set(left, top, right, bottom); |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 21 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | if (left < fLeft) fLeft = left; |
| 23 | if (top < fTop) fTop = top; |
| 24 | if (right > fRight) fRight = right; |
| 25 | if (bottom > fBottom) fBottom = bottom; |
| 26 | } |
| 27 | } |
| 28 | |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 29 | void SkIRect::sort() { |
| 30 | if (fLeft > fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkTSwap<int32_t>(fLeft, fRight); |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 32 | } |
| 33 | if (fTop > fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | SkTSwap<int32_t>(fTop, fBottom); |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 35 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ///////////////////////////////////////////////////////////////////////////// |
| 39 | |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 40 | void SkRect::toQuad(SkPoint quad[4]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | SkASSERT(quad); |
| 42 | |
| 43 | quad[0].set(fLeft, fTop); |
| 44 | quad[1].set(fRight, fTop); |
| 45 | quad[2].set(fRight, fBottom); |
| 46 | quad[3].set(fLeft, fBottom); |
| 47 | } |
| 48 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 49 | bool SkRect::setBoundsCheck(const SkPoint pts[], int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | SkASSERT((pts && count > 0) || count == 0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 52 | bool isFinite = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
| 54 | if (count <= 0) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 55 | sk_bzero(this, sizeof(SkRect)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | SkScalar l, t, r, b; |
| 58 | |
| 59 | l = r = pts[0].fX; |
| 60 | t = b = pts[0].fY; |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 61 | |
| 62 | // If all of the points are finite, accum should stay 0. If we encounter |
| 63 | // a NaN or infinity, then accum should become NaN. |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 64 | float accum = 0; |
| 65 | accum *= l; accum *= t; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
| 67 | for (int i = 1; i < count; i++) { |
| 68 | SkScalar x = pts[i].fX; |
| 69 | SkScalar y = pts[i].fY; |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 70 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 71 | accum *= x; accum *= y; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 73 | // we use if instead of if/else, so we can generate min/max |
| 74 | // float instructions (at least on SSE) |
| 75 | if (x < l) l = x; |
| 76 | if (x > r) r = x; |
| 77 | |
| 78 | if (y < t) t = y; |
| 79 | if (y > b) b = y; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 81 | |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 82 | SkASSERT(!accum || !SkScalarIsFinite(accum)); |
| 83 | if (accum) { |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 84 | l = t = r = b = 0; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 85 | isFinite = false; |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 86 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | this->set(l, t, r, b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 89 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 90 | return isFinite; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 93 | #define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \ |
| 94 | SkScalar L = SkMaxScalar(al, bl); \ |
| 95 | SkScalar R = SkMinScalar(ar, br); \ |
| 96 | SkScalar T = SkMaxScalar(at, bt); \ |
| 97 | SkScalar B = SkMinScalar(ab, bb); \ |
| 98 | do { if (L >= R || T >= B) return false; } while (0) |
| 99 | |
| 100 | bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { |
| 101 | CHECK_INTERSECT(left, top, right, bottom, fLeft, fTop, fRight, fBottom); |
| 102 | this->setLTRB(L, T, R, B); |
| 103 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 106 | bool SkRect::intersect(const SkRect& r) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 108 | } |
| 109 | |
reed@google.com | f0f617a | 2011-10-21 14:05:06 +0000 | [diff] [blame] | 110 | bool SkRect::intersect(const SkRect& a, const SkRect& b) { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 111 | CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom); |
| 112 | this->setLTRB(L, T, R, B); |
| 113 | return true; |
reed@google.com | f0f617a | 2011-10-21 14:05:06 +0000 | [diff] [blame] | 114 | } |
| 115 | |
reed | d9c42f7 | 2014-10-01 07:21:23 -0700 | [diff] [blame] | 116 | void SkRect::join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | // do nothing if the params are empty |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 118 | if (left >= right || top >= bottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | return; |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 120 | } |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | // if we are empty, just assign |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 123 | if (fLeft >= fRight || fTop >= fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | this->set(left, top, right, bottom); |
reed@google.com | 91a2ae9 | 2011-02-08 21:59:59 +0000 | [diff] [blame] | 125 | } else { |
reed | d9c42f7 | 2014-10-01 07:21:23 -0700 | [diff] [blame] | 126 | fLeft = SkMinScalar(fLeft, left); |
| 127 | fTop = SkMinScalar(fTop, top); |
| 128 | fRight = SkMaxScalar(fRight, right); |
| 129 | fBottom = SkMaxScalar(fBottom, bottom); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 132 | |
reed | e05fed0 | 2014-12-15 07:59:53 -0800 | [diff] [blame] | 133 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | #include "SkString.h" |
| 136 | #include "SkStringUtils.h" |
| 137 | |
| 138 | static const char* set_scalar(SkString* storage, SkScalar value, SkScalarAsStringType asType) { |
| 139 | storage->reset(); |
| 140 | SkAppendScalar(storage, value, asType); |
| 141 | return storage->c_str(); |
| 142 | } |
| 143 | |
| 144 | void SkRect::dump(bool asHex) const { |
| 145 | SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType; |
| 146 | |
| 147 | SkString line; |
| 148 | if (asHex) { |
| 149 | SkString tmp; |
| 150 | line.printf( "SkRect::MakeLTRB(%s, /* %f */\n", set_scalar(&tmp, fLeft, asType), fLeft); |
| 151 | line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fTop, asType), fTop); |
| 152 | line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fRight, asType), fRight); |
| 153 | line.appendf(" %s /* %f */);", set_scalar(&tmp, fBottom, asType), fBottom); |
| 154 | } else { |
| 155 | SkString strL, strT, strR, strB; |
| 156 | SkAppendScalarDec(&strL, fLeft); |
| 157 | SkAppendScalarDec(&strT, fTop); |
| 158 | SkAppendScalarDec(&strR, fRight); |
| 159 | SkAppendScalarDec(&strB, fBottom); |
| 160 | line.printf("SkRect::MakeLTRB(%s, %s, %s, %s);", |
| 161 | strL.c_str(), strT.c_str(), strR.c_str(), strB.c_str()); |
| 162 | } |
| 163 | SkDebugf("%s\n", line.c_str()); |
| 164 | } |