blob: 7531769081b009bff4ee8d27d8fc43f56b624061 [file] [log] [blame]
Romain Guybb9524b2010-06-22 18:56:38 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_RECT_H
18#define ANDROID_HWUI_RECT_H
19
20#include <cmath>
Romain Guybb9524b2010-06-22 18:56:38 -070021
Romain Guy5cbbce52010-06-27 22:59:20 -070022#include <utils/Log.h>
23
Romain Guybb9524b2010-06-22 18:56:38 -070024namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070025namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070026
Chris Craik39a908c2013-06-13 14:39:01 -070027#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f"
Chris Craik28ce94a2013-05-31 11:38:03 -070028#define RECT_ARGS(r) \
29 (r).left, (r).top, (r).right, (r).bottom
30
Romain Guybb9524b2010-06-22 18:56:38 -070031///////////////////////////////////////////////////////////////////////////////
32// Structs
33///////////////////////////////////////////////////////////////////////////////
34
Mathias Agopian83b186a2011-09-19 16:00:46 -070035class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070036public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070037 float left;
38 float top;
39 float right;
40 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070041
Romain Guy5b3b3522010-10-27 18:57:51 -070042 // Used by Region
43 typedef float value_type;
44
Mathias Agopian83b186a2011-09-19 16:00:46 -070045 // we don't provide copy-ctor and operator= on purpose
46 // because we want the compiler generated versions
47
Romain Guy5b3b3522010-10-27 18:57:51 -070048 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070049 left(0),
50 top(0),
51 right(0),
52 bottom(0) {
53 }
Romain Guy9d5316e2010-06-24 19:30:36 -070054
Romain Guy5b3b3522010-10-27 18:57:51 -070055 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070056 left(left),
57 top(top),
58 right(right),
59 bottom(bottom) {
60 }
Romain Guybb9524b2010-06-22 18:56:38 -070061
Romain Guy5b3b3522010-10-27 18:57:51 -070062 inline Rect(float width, float height):
63 left(0.0f),
64 top(0.0f),
65 right(width),
66 bottom(height) {
67 }
68
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 friend int operator==(const Rect& a, const Rect& b) {
70 return !memcmp(&a, &b, sizeof(a));
71 }
Romain Guybb9524b2010-06-22 18:56:38 -070072
Romain Guy7ae7ac42010-06-25 13:46:18 -070073 friend int operator!=(const Rect& a, const Rect& b) {
74 return memcmp(&a, &b, sizeof(a));
75 }
Romain Guybb9524b2010-06-22 18:56:38 -070076
Romain Guy5b3b3522010-10-27 18:57:51 -070077 inline void clear() {
78 left = top = right = bottom = 0.0f;
79 }
80
81 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070082 // this is written in such way this it'll handle NANs to return
83 // true (empty)
84 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070085 }
Romain Guybb9524b2010-06-22 18:56:38 -070086
Romain Guy5b3b3522010-10-27 18:57:51 -070087 inline void setEmpty() {
88 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070089 }
Romain Guybb9524b2010-06-22 18:56:38 -070090
Romain Guy5b3b3522010-10-27 18:57:51 -070091 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070092 this->left = left;
93 this->right = right;
94 this->top = top;
95 this->bottom = bottom;
96 }
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guy5b3b3522010-10-27 18:57:51 -070098 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070099 set(r.left, r.top, r.right, r.bottom);
100 }
Romain Guybb9524b2010-06-22 18:56:38 -0700101
Romain Guy8aef54f2010-09-01 15:13:49 -0700102 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700103 return right - left;
104 }
Romain Guybb9524b2010-06-22 18:56:38 -0700105
Romain Guy8aef54f2010-09-01 15:13:49 -0700106 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700107 return bottom - top;
108 }
Romain Guybb9524b2010-06-22 18:56:38 -0700109
Mathias Agopian83b186a2011-09-19 16:00:46 -0700110 bool intersects(float l, float t, float r, float b) const {
Romain Guya1d3c912011-12-13 14:55:06 -0800111 return !intersectWith(l, t, r, b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700112 }
Romain Guybb9524b2010-06-22 18:56:38 -0700113
Romain Guy7ae7ac42010-06-25 13:46:18 -0700114 bool intersects(const Rect& r) const {
115 return intersects(r.left, r.top, r.right, r.bottom);
116 }
Romain Guybb9524b2010-06-22 18:56:38 -0700117
Mathias Agopian83b186a2011-09-19 16:00:46 -0700118 bool intersect(float l, float t, float r, float b) {
Romain Guy8f85e802011-12-14 19:23:32 -0800119 Rect tmp(l, t, r, b);
120 intersectWith(tmp);
Mathias Agopian83b186a2011-09-19 16:00:46 -0700121 if (!tmp.isEmpty()) {
122 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 return true;
124 }
125 return false;
126 }
Romain Guybb9524b2010-06-22 18:56:38 -0700127
Romain Guy7ae7ac42010-06-25 13:46:18 -0700128 bool intersect(const Rect& r) {
129 return intersect(r.left, r.top, r.right, r.bottom);
130 }
Romain Guybb9524b2010-06-22 18:56:38 -0700131
Romain Guy2db5e992013-05-21 15:29:59 -0700132 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800133 return l >= left && t >= top && r <= right && b <= bottom;
134 }
135
Romain Guy2db5e992013-05-21 15:29:59 -0700136 inline bool contains(const Rect& r) const {
Romain Guyec31f832011-12-13 18:39:19 -0800137 return contains(r.left, r.top, r.right, r.bottom);
138 }
139
Romain Guy079ba2c2010-07-16 14:12:24 -0700140 bool unionWith(const Rect& r) {
141 if (r.left < r.right && r.top < r.bottom) {
142 if (left < right && top < bottom) {
143 if (left > r.left) left = r.left;
144 if (top > r.top) top = r.top;
145 if (right < r.right) right = r.right;
146 if (bottom < r.bottom) bottom = r.bottom;
147 return true;
148 } else {
149 left = r.left;
150 top = r.top;
151 right = r.right;
152 bottom = r.bottom;
153 return true;
154 }
155 }
156 return false;
157 }
158
Romain Guy5b3b3522010-10-27 18:57:51 -0700159 void translate(float dx, float dy) {
160 left += dx;
161 right += dx;
162 top += dy;
163 bottom += dy;
164 }
165
Chris Craikc3566d02013-02-04 16:16:33 -0800166 void outset(float delta) {
167 left -= delta;
168 top -= delta;
169 right += delta;
170 bottom += delta;
171 }
172
Romain Guybf434112010-09-16 14:40:17 -0700173 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700174 left = floorf(left + 0.5f);
175 top = floorf(top + 0.5f);
176 right = floorf(right + 0.5f);
177 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700178 }
179
Romain Guy7ae7ac42010-06-25 13:46:18 -0700180 void dump() const {
Steve Block5baa3a62011-12-20 16:23:08 +0000181 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700182 }
Romain Guybb9524b2010-06-22 18:56:38 -0700183
Romain Guya1d3c912011-12-13 14:55:06 -0800184private:
Romain Guy8f85e802011-12-14 19:23:32 -0800185 void intersectWith(Rect& tmp) const {
Chris Craik2af46352012-11-26 18:30:17 -0800186 tmp.left = fmaxf(left, tmp.left);
187 tmp.top = fmaxf(top, tmp.top);
188 tmp.right = fminf(right, tmp.right);
189 tmp.bottom = fminf(bottom, tmp.bottom);
Romain Guy8f85e802011-12-14 19:23:32 -0800190 }
191
Romain Guya1d3c912011-12-13 14:55:06 -0800192 Rect intersectWith(float l, float t, float r, float b) const {
193 Rect tmp;
Chris Craik2af46352012-11-26 18:30:17 -0800194 tmp.left = fmaxf(left, l);
195 tmp.top = fmaxf(top, t);
196 tmp.right = fminf(right, r);
197 tmp.bottom = fminf(bottom, b);
Romain Guya1d3c912011-12-13 14:55:06 -0800198 return tmp;
199 }
200
Mathias Agopian83b186a2011-09-19 16:00:46 -0700201}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700202
Romain Guy9d5316e2010-06-24 19:30:36 -0700203}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700204}; // namespace android
205
Romain Guy5b3b3522010-10-27 18:57:51 -0700206#endif // ANDROID_HWUI_RECT_H