Rect.h revision 39a908c1df89e1073627b0dcbce922d826b67055
1/* 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 17#ifndef ANDROID_HWUI_RECT_H 18#define ANDROID_HWUI_RECT_H 19 20#include <cmath> 21 22#include <utils/Log.h> 23 24namespace android { 25namespace uirenderer { 26 27#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f" 28#define RECT_ARGS(r) \ 29 (r).left, (r).top, (r).right, (r).bottom 30 31/////////////////////////////////////////////////////////////////////////////// 32// Structs 33/////////////////////////////////////////////////////////////////////////////// 34 35class Rect { 36public: 37 float left; 38 float top; 39 float right; 40 float bottom; 41 42 // Used by Region 43 typedef float value_type; 44 45 // we don't provide copy-ctor and operator= on purpose 46 // because we want the compiler generated versions 47 48 inline Rect(): 49 left(0), 50 top(0), 51 right(0), 52 bottom(0) { 53 } 54 55 inline Rect(float left, float top, float right, float bottom): 56 left(left), 57 top(top), 58 right(right), 59 bottom(bottom) { 60 } 61 62 inline Rect(float width, float height): 63 left(0.0f), 64 top(0.0f), 65 right(width), 66 bottom(height) { 67 } 68 69 friend int operator==(const Rect& a, const Rect& b) { 70 return !memcmp(&a, &b, sizeof(a)); 71 } 72 73 friend int operator!=(const Rect& a, const Rect& b) { 74 return memcmp(&a, &b, sizeof(a)); 75 } 76 77 inline void clear() { 78 left = top = right = bottom = 0.0f; 79 } 80 81 inline bool isEmpty() const { 82 // this is written in such way this it'll handle NANs to return 83 // true (empty) 84 return !((left < right) && (top < bottom)); 85 } 86 87 inline void setEmpty() { 88 left = top = right = bottom = 0.0f; 89 } 90 91 inline void set(float left, float top, float right, float bottom) { 92 this->left = left; 93 this->right = right; 94 this->top = top; 95 this->bottom = bottom; 96 } 97 98 inline void set(const Rect& r) { 99 set(r.left, r.top, r.right, r.bottom); 100 } 101 102 inline float getWidth() const { 103 return right - left; 104 } 105 106 inline float getHeight() const { 107 return bottom - top; 108 } 109 110 bool intersects(float l, float t, float r, float b) const { 111 return !intersectWith(l, t, r, b).isEmpty(); 112 } 113 114 bool intersects(const Rect& r) const { 115 return intersects(r.left, r.top, r.right, r.bottom); 116 } 117 118 bool intersect(float l, float t, float r, float b) { 119 Rect tmp(l, t, r, b); 120 intersectWith(tmp); 121 if (!tmp.isEmpty()) { 122 set(tmp); 123 return true; 124 } 125 return false; 126 } 127 128 bool intersect(const Rect& r) { 129 return intersect(r.left, r.top, r.right, r.bottom); 130 } 131 132 inline bool contains(float l, float t, float r, float b) const { 133 return l >= left && t >= top && r <= right && b <= bottom; 134 } 135 136 inline bool contains(const Rect& r) const { 137 return contains(r.left, r.top, r.right, r.bottom); 138 } 139 140 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 159 void translate(float dx, float dy) { 160 left += dx; 161 right += dx; 162 top += dy; 163 bottom += dy; 164 } 165 166 void outset(float delta) { 167 left -= delta; 168 top -= delta; 169 right += delta; 170 bottom += delta; 171 } 172 173 void snapToPixelBoundaries() { 174 left = floorf(left + 0.5f); 175 top = floorf(top + 0.5f); 176 right = floorf(right + 0.5f); 177 bottom = floorf(bottom + 0.5f); 178 } 179 180 void dump() const { 181 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); 182 } 183 184private: 185 void intersectWith(Rect& tmp) const { 186 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); 190 } 191 192 Rect intersectWith(float l, float t, float r, float b) const { 193 Rect tmp; 194 tmp.left = fmaxf(left, l); 195 tmp.top = fmaxf(top, t); 196 tmp.right = fminf(right, r); 197 tmp.bottom = fminf(bottom, b); 198 return tmp; 199 } 200 201}; // class Rect 202 203}; // namespace uirenderer 204}; // namespace android 205 206#endif // ANDROID_HWUI_RECT_H 207