| 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/private/SkMalloc.h" | 
|---|
| 11 |  | 
|---|
| 12 | bool SkIRect::intersect(const SkIRect& a, const SkIRect& b) { | 
|---|
| 13 | SkIRect tmp = { | 
|---|
| 14 | std::max(a.fLeft,   b.fLeft), | 
|---|
| 15 | std::max(a.fTop,    b.fTop), | 
|---|
| 16 | std::min(a.fRight,  b.fRight), | 
|---|
| 17 | std::min(a.fBottom, b.fBottom) | 
|---|
| 18 | }; | 
|---|
| 19 | if (tmp.isEmpty()) { | 
|---|
| 20 | return false; | 
|---|
| 21 | } | 
|---|
| 22 | *this = tmp; | 
|---|
| 23 | return true; | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | void SkIRect::join(const SkIRect& r) { | 
|---|
| 27 | // do nothing if the params are empty | 
|---|
| 28 | if (r.fLeft >= r.fRight || r.fTop >= r.fBottom) { | 
|---|
| 29 | return; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | // if we are empty, just assign | 
|---|
| 33 | if (fLeft >= fRight || fTop >= fBottom) { | 
|---|
| 34 | *this = r; | 
|---|
| 35 | } else { | 
|---|
| 36 | if (r.fLeft < fLeft)     fLeft = r.fLeft; | 
|---|
| 37 | if (r.fTop < fTop)       fTop = r.fTop; | 
|---|
| 38 | if (r.fRight > fRight)   fRight = r.fRight; | 
|---|
| 39 | if (r.fBottom > fBottom) fBottom = r.fBottom; | 
|---|
| 40 | } | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 44 |  | 
|---|
| 45 | void SkRect::toQuad(SkPoint quad[4]) const { | 
|---|
| 46 | SkASSERT(quad); | 
|---|
| 47 |  | 
|---|
| 48 | quad[0].set(fLeft, fTop); | 
|---|
| 49 | quad[1].set(fRight, fTop); | 
|---|
| 50 | quad[2].set(fRight, fBottom); | 
|---|
| 51 | quad[3].set(fLeft, fBottom); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | #include "include/private/SkNx.h" | 
|---|
| 55 |  | 
|---|
| 56 | bool SkRect::setBoundsCheck(const SkPoint pts[], int count) { | 
|---|
| 57 | SkASSERT((pts && count > 0) || count == 0); | 
|---|
| 58 |  | 
|---|
| 59 | if (count <= 0) { | 
|---|
| 60 | this->setEmpty(); | 
|---|
| 61 | return true; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | Sk4s min, max; | 
|---|
| 65 | if (count & 1) { | 
|---|
| 66 | min = max = Sk4s(pts->fX, pts->fY, | 
|---|
| 67 | pts->fX, pts->fY); | 
|---|
| 68 | pts   += 1; | 
|---|
| 69 | count -= 1; | 
|---|
| 70 | } else { | 
|---|
| 71 | min = max = Sk4s::Load(pts); | 
|---|
| 72 | pts   += 2; | 
|---|
| 73 | count -= 2; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | Sk4s accum = min * 0; | 
|---|
| 77 | while (count) { | 
|---|
| 78 | Sk4s xy = Sk4s::Load(pts); | 
|---|
| 79 | accum = accum * xy; | 
|---|
| 80 | min = Sk4s::Min(min, xy); | 
|---|
| 81 | max = Sk4s::Max(max, xy); | 
|---|
| 82 | pts   += 2; | 
|---|
| 83 | count -= 2; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | bool all_finite = (accum * 0 == 0).allTrue(); | 
|---|
| 87 | if (all_finite) { | 
|---|
| 88 | this->setLTRB(std::min(min[0], min[2]), std::min(min[1], min[3]), | 
|---|
| 89 | std::max(max[0], max[2]), std::max(max[1], max[3])); | 
|---|
| 90 | } else { | 
|---|
| 91 | this->setEmpty(); | 
|---|
| 92 | } | 
|---|
| 93 | return all_finite; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void SkRect::setBoundsNoCheck(const SkPoint pts[], int count) { | 
|---|
| 97 | if (!this->setBoundsCheck(pts, count)) { | 
|---|
| 98 | this->setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | #define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \ | 
|---|
| 103 | SkScalar L = std::max(al, bl);                   \ | 
|---|
| 104 | SkScalar R = std::min(ar, br);                   \ | 
|---|
| 105 | SkScalar T = std::max(at, bt);                   \ | 
|---|
| 106 | SkScalar B = std::min(ab, bb);                   \ | 
|---|
| 107 | do { if (!(L < R && T < B)) return false; } while (0) | 
|---|
| 108 | // do the !(opposite) check so we return false if either arg is NaN | 
|---|
| 109 |  | 
|---|
| 110 | bool SkRect::intersect(const SkRect& r) { | 
|---|
| 111 | CHECK_INTERSECT(r.fLeft, r.fTop, r.fRight, r.fBottom, fLeft, fTop, fRight, fBottom); | 
|---|
| 112 | this->setLTRB(L, T, R, B); | 
|---|
| 113 | return true; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | bool SkRect::intersect(const SkRect& a, const SkRect& b) { | 
|---|
| 117 | CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom); | 
|---|
| 118 | this->setLTRB(L, T, R, B); | 
|---|
| 119 | return true; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void SkRect::join(const SkRect& r) { | 
|---|
| 123 | if (r.isEmpty()) { | 
|---|
| 124 | return; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | if (this->isEmpty()) { | 
|---|
| 128 | *this = r; | 
|---|
| 129 | } else { | 
|---|
| 130 | fLeft   = std::min(fLeft, r.fLeft); | 
|---|
| 131 | fTop    = std::min(fTop, r.fTop); | 
|---|
| 132 | fRight  = std::max(fRight, r.fRight); | 
|---|
| 133 | fBottom = std::max(fBottom, r.fBottom); | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | //////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 138 |  | 
|---|
| 139 | #include "include/core/SkString.h" | 
|---|
| 140 | #include "src/core/SkStringUtils.h" | 
|---|
| 141 |  | 
|---|
| 142 | static const char* set_scalar(SkString* storage, SkScalar value, SkScalarAsStringType asType) { | 
|---|
| 143 | storage->reset(); | 
|---|
| 144 | SkAppendScalar(storage, value, asType); | 
|---|
| 145 | return storage->c_str(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | void SkRect::dump(bool asHex) const { | 
|---|
| 149 | SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType; | 
|---|
| 150 |  | 
|---|
| 151 | SkString line; | 
|---|
| 152 | if (asHex) { | 
|---|
| 153 | SkString tmp; | 
|---|
| 154 | line.printf( "SkRect::MakeLTRB(%s, /* %f */\n", set_scalar(&tmp, fLeft, asType), fLeft); | 
|---|
| 155 | line.appendf( "                 %s, /* %f */\n", set_scalar(&tmp, fTop, asType), fTop); | 
|---|
| 156 | line.appendf( "                 %s, /* %f */\n", set_scalar(&tmp, fRight, asType), fRight); | 
|---|
| 157 | line.appendf( "                 %s  /* %f */);", set_scalar(&tmp, fBottom, asType), fBottom); | 
|---|
| 158 | } else { | 
|---|
| 159 | SkString strL, strT, strR, strB; | 
|---|
| 160 | SkAppendScalarDec(&strL, fLeft); | 
|---|
| 161 | SkAppendScalarDec(&strT, fTop); | 
|---|
| 162 | SkAppendScalarDec(&strR, fRight); | 
|---|
| 163 | SkAppendScalarDec(&strB, fBottom); | 
|---|
| 164 | line.printf( "SkRect::MakeLTRB(%s, %s, %s, %s);", | 
|---|
| 165 | strL.c_str(), strT.c_str(), strR.c_str(), strB.c_str()); | 
|---|
| 166 | } | 
|---|
| 167 | SkDebugf( "%s\n", line.c_str()); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|