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