blob: edc90e1f052f61978d5df65943651512d5e0c77b [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
27///////////////////////////////////////////////////////////////////////////////
28// Structs
29///////////////////////////////////////////////////////////////////////////////
30
Mathias Agopian83b186a2011-09-19 16:00:46 -070031class Rect {
32 static inline float min(float a, float b) { return (a<b) ? a : b; }
33 static inline float max(float a, float b) { return (a>b) ? a : b; }
34 Rect intersectWith(float l, float t, float r, float b) const {
35 Rect tmp;
36 tmp.left = max(left, l);
37 tmp.top = max(top, t);
38 tmp.right = min(right, r);
39 tmp.bottom = min(bottom, b);
40 return tmp;
41 }
42
43public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070044 float left;
45 float top;
46 float right;
47 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070048
Romain Guy5b3b3522010-10-27 18:57:51 -070049 // Used by Region
50 typedef float value_type;
51
Mathias Agopian83b186a2011-09-19 16:00:46 -070052 // we don't provide copy-ctor and operator= on purpose
53 // because we want the compiler generated versions
54
Romain Guy5b3b3522010-10-27 18:57:51 -070055 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070056 left(0),
57 top(0),
58 right(0),
59 bottom(0) {
60 }
Romain Guy9d5316e2010-06-24 19:30:36 -070061
Romain Guy5b3b3522010-10-27 18:57:51 -070062 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070063 left(left),
64 top(top),
65 right(right),
66 bottom(bottom) {
67 }
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy5b3b3522010-10-27 18:57:51 -070069 inline Rect(float width, float height):
70 left(0.0f),
71 top(0.0f),
72 right(width),
73 bottom(height) {
74 }
75
Romain Guy7ae7ac42010-06-25 13:46:18 -070076 friend int operator==(const Rect& a, const Rect& b) {
77 return !memcmp(&a, &b, sizeof(a));
78 }
Romain Guybb9524b2010-06-22 18:56:38 -070079
Romain Guy7ae7ac42010-06-25 13:46:18 -070080 friend int operator!=(const Rect& a, const Rect& b) {
81 return memcmp(&a, &b, sizeof(a));
82 }
Romain Guybb9524b2010-06-22 18:56:38 -070083
Romain Guy5b3b3522010-10-27 18:57:51 -070084 inline void clear() {
85 left = top = right = bottom = 0.0f;
86 }
87
88 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070089 // this is written in such way this it'll handle NANs to return
90 // true (empty)
91 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070092 }
Romain Guybb9524b2010-06-22 18:56:38 -070093
Romain Guy5b3b3522010-10-27 18:57:51 -070094 inline void setEmpty() {
95 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070096 }
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guy5b3b3522010-10-27 18:57:51 -070098 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070099 this->left = left;
100 this->right = right;
101 this->top = top;
102 this->bottom = bottom;
103 }
Romain Guybb9524b2010-06-22 18:56:38 -0700104
Romain Guy5b3b3522010-10-27 18:57:51 -0700105 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700106 set(r.left, r.top, r.right, r.bottom);
107 }
Romain Guybb9524b2010-06-22 18:56:38 -0700108
Romain Guy8aef54f2010-09-01 15:13:49 -0700109 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 return right - left;
111 }
Romain Guybb9524b2010-06-22 18:56:38 -0700112
Romain Guy8aef54f2010-09-01 15:13:49 -0700113 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700114 return bottom - top;
115 }
Romain Guybb9524b2010-06-22 18:56:38 -0700116
Mathias Agopian83b186a2011-09-19 16:00:46 -0700117 bool intersects(float l, float t, float r, float b) const {
118 return !intersectWith(l,t,r,b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 }
Romain Guybb9524b2010-06-22 18:56:38 -0700120
Romain Guy7ae7ac42010-06-25 13:46:18 -0700121 bool intersects(const Rect& r) const {
122 return intersects(r.left, r.top, r.right, r.bottom);
123 }
Romain Guybb9524b2010-06-22 18:56:38 -0700124
Mathias Agopian83b186a2011-09-19 16:00:46 -0700125 bool intersect(float l, float t, float r, float b) {
126 Rect tmp(intersectWith(l,t,r,b));
127 if (!tmp.isEmpty()) {
128 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 return true;
130 }
131 return false;
132 }
Romain Guybb9524b2010-06-22 18:56:38 -0700133
Romain Guy7ae7ac42010-06-25 13:46:18 -0700134 bool intersect(const Rect& r) {
135 return intersect(r.left, r.top, r.right, r.bottom);
136 }
Romain Guybb9524b2010-06-22 18:56:38 -0700137
Romain Guy079ba2c2010-07-16 14:12:24 -0700138 bool unionWith(const Rect& r) {
139 if (r.left < r.right && r.top < r.bottom) {
140 if (left < right && top < bottom) {
141 if (left > r.left) left = r.left;
142 if (top > r.top) top = r.top;
143 if (right < r.right) right = r.right;
144 if (bottom < r.bottom) bottom = r.bottom;
145 return true;
146 } else {
147 left = r.left;
148 top = r.top;
149 right = r.right;
150 bottom = r.bottom;
151 return true;
152 }
153 }
154 return false;
155 }
156
Romain Guy5b3b3522010-10-27 18:57:51 -0700157 void translate(float dx, float dy) {
158 left += dx;
159 right += dx;
160 top += dy;
161 bottom += dy;
162 }
163
Romain Guybf434112010-09-16 14:40:17 -0700164 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700165 left = floorf(left + 0.5f);
166 top = floorf(top + 0.5f);
167 right = floorf(right + 0.5f);
168 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700169 }
170
Romain Guy7ae7ac42010-06-25 13:46:18 -0700171 void dump() const {
172 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
173 }
Romain Guybb9524b2010-06-22 18:56:38 -0700174
Mathias Agopian83b186a2011-09-19 16:00:46 -0700175}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700176
Romain Guy9d5316e2010-06-24 19:30:36 -0700177}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700178}; // namespace android
179
Romain Guy5b3b3522010-10-27 18:57:51 -0700180#endif // ANDROID_HWUI_RECT_H