blob: afd8bd8f719cfbea8814984eaf9b9fe4f5fd4f2c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkRect.h"
11
reed@google.com91a2ae92011-02-08 21:59:59 +000012void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000013 // do nothing if the params are empty
reed@google.com91a2ae92011-02-08 21:59:59 +000014 if (left >= right || top >= bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 return;
reed@google.com91a2ae92011-02-08 21:59:59 +000016 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18 // if we are empty, just assign
reed@google.com91a2ae92011-02-08 21:59:59 +000019 if (fLeft >= fRight || fTop >= fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 this->set(left, top, right, bottom);
reed@google.com91a2ae92011-02-08 21:59:59 +000021 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 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.com91a2ae92011-02-08 21:59:59 +000029void SkIRect::sort() {
30 if (fLeft > fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkTSwap<int32_t>(fLeft, fRight);
reed@google.com91a2ae92011-02-08 21:59:59 +000032 }
33 if (fTop > fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkTSwap<int32_t>(fTop, fBottom);
reed@google.com91a2ae92011-02-08 21:59:59 +000035 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
38/////////////////////////////////////////////////////////////////////////////
39
reed@google.com91a2ae92011-02-08 21:59:59 +000040void SkRect::toQuad(SkPoint quad[4]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 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.com0bb18bb2012-07-26 15:20:36 +000049bool SkRect::setBoundsCheck(const SkPoint pts[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SkASSERT((pts && count > 0) || count == 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.com0bb18bb2012-07-26 15:20:36 +000052 bool isFinite = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
54 if (count <= 0) {
reed@android.com4516f472009-06-29 16:25:36 +000055 sk_bzero(this, sizeof(SkRect));
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SkScalar l, t, r, b;
58
59 l = r = pts[0].fX;
60 t = b = pts[0].fY;
reed@google.com30d90eb2012-05-15 14:17:36 +000061
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.com8f4d2302013-12-17 16:44:46 +000064 float accum = 0;
65 accum *= l; accum *= t;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
67 for (int i = 1; i < count; i++) {
68 SkScalar x = pts[i].fX;
69 SkScalar y = pts[i].fY;
reed@google.com30d90eb2012-05-15 14:17:36 +000070
reed@google.com8f4d2302013-12-17 16:44:46 +000071 accum *= x; accum *= y;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
reed@google.com8f4d2302013-12-17 16:44:46 +000073 // 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.com8a1c16f2008-12-17 15:59:43 +000080 }
reed@google.comfe701122011-11-08 19:41:23 +000081
reed@google.com30d90eb2012-05-15 14:17:36 +000082 SkASSERT(!accum || !SkScalarIsFinite(accum));
83 if (accum) {
reed@google.comfe701122011-11-08 19:41:23 +000084 l = t = r = b = 0;
reed@google.com0bb18bb2012-07-26 15:20:36 +000085 isFinite = false;
reed@google.comfe701122011-11-08 19:41:23 +000086 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 this->set(l, t, r, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
reed@google.com0bb18bb2012-07-26 15:20:36 +000090 return isFinite;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091}
92
reed1119c872014-10-09 14:29:01 -070093#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
100bool 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.com8a1c16f2008-12-17 15:59:43 +0000104}
105
reed@google.com91a2ae92011-02-08 21:59:59 +0000106bool SkRect::intersect(const SkRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
108}
109
reed@google.comf0f617a2011-10-21 14:05:06 +0000110bool SkRect::intersect(const SkRect& a, const SkRect& b) {
reed1119c872014-10-09 14:29:01 -0700111 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.comf0f617a2011-10-21 14:05:06 +0000114}
115
reedd9c42f72014-10-01 07:21:23 -0700116void SkRect::join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 // do nothing if the params are empty
reed@google.com91a2ae92011-02-08 21:59:59 +0000118 if (left >= right || top >= bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 return;
reed@google.com91a2ae92011-02-08 21:59:59 +0000120 }
reed@google.com077910e2011-02-08 21:56:39 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 // if we are empty, just assign
reed@google.com91a2ae92011-02-08 21:59:59 +0000123 if (fLeft >= fRight || fTop >= fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 this->set(left, top, right, bottom);
reed@google.com91a2ae92011-02-08 21:59:59 +0000125 } else {
reedd9c42f72014-10-01 07:21:23 -0700126 fLeft = SkMinScalar(fLeft, left);
127 fTop = SkMinScalar(fTop, top);
128 fRight = SkMaxScalar(fRight, right);
129 fBottom = SkMaxScalar(fBottom, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
131}
reed1119c872014-10-09 14:29:01 -0700132
reede05fed02014-12-15 07:59:53 -0800133////////////////////////////////////////////////////////////////////////////////////////////////
134
135#include "SkString.h"
136#include "SkStringUtils.h"
137
138static 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
144void 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}