tb_geometry.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_GEOMETRY_H
  6. #define TB_GEOMETRY_H
  7. #include "tb_core.h"
  8. namespace tb {
  9. /** Simple point class. */
  10. class TBPoint
  11. {
  12. public:
  13. int x, y;
  14. TBPoint() : x(0), y(0) {}
  15. TBPoint(int x, int y) : x(x), y(y) {}
  16. };
  17. /** Simple rectangle class. */
  18. class TBRect
  19. {
  20. public:
  21. int x, y, w, h;
  22. TBRect() : x(0), y(0), w(0), h(0) {}
  23. TBRect(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
  24. inline bool IsEmpty() const { return w <= 0 || h <= 0; }
  25. inline bool IsInsideOut() const { return w < 0 || h < 0; }
  26. inline bool Equals(const TBRect &rect) const { return rect.x == x && rect.y == y && rect.w == w && rect.h == h; }
  27. bool Intersects(const TBRect &rect) const;
  28. bool Contains(const TBPoint &p) const { return p.x >= x && p.y >= y && p.x < x + w && p.y < y + h; }
  29. inline void Reset() { x = y = w = h = 0; }
  30. inline void Set(int x, int y, int w, int h) { this->x = x; this->y = y; this->w = w; this->h = h; }
  31. inline TBRect Shrink(int left, int top, int right, int bottom) const { return TBRect(x + left, y + top, w - left - right, h - top - bottom); }
  32. inline TBRect Expand(int left, int top, int right, int bottom) const { return Shrink(-left, -top, -right, -bottom); }
  33. inline TBRect Shrink(int tx, int ty) const { return TBRect(x + tx, y + ty, w - tx * 2, h - ty * 2); }
  34. inline TBRect Expand(int tx, int ty) const { return Shrink(-tx, -ty); }
  35. inline TBRect Offset(int dx, int dy) const { return TBRect(x + dx, y + dy, w, h); }
  36. /** Return a rect moved inside bounding_rect. If the rect doesn't fit inside
  37. bounding_rect, it will be placed so the x and/or y matches bounding_rect. */
  38. TBRect MoveIn(const TBRect &bounding_rect) const;
  39. /** Return a rect centered in bounding_rect. */
  40. TBRect CenterIn(const TBRect &bounding_rect) const;
  41. TBRect Union(const TBRect &rect) const;
  42. TBRect Clip(const TBRect &clip_rect) const;
  43. };
  44. /** TBRegion does calculations on regions represented by a list of rectangles. */
  45. class TBRegion
  46. {
  47. public:
  48. TBRegion();
  49. ~TBRegion();
  50. /** Remove the rect at the given index. */
  51. void RemoveRect(int index);
  52. /** Remove the rect at the given index.
  53. This method will change the order of rectangles after index. */
  54. void RemoveRectFast(int index);
  55. /** Remove all rectangles so the region becomes empty.
  56. If free_memory is false, the internal buffers will be reused
  57. if more rectangles are added again under its life time. */
  58. void RemoveAll(bool free_memory = true);
  59. /** Set the region to the given rect. */
  60. bool Set(const TBRect &rect);
  61. /** Add the rect without doing any overlap check.
  62. If coalesce is true, it will coalesce the rectangle
  63. with existing rectangles if possible (until there's
  64. nothing more to coalesce it with). */
  65. bool AddRect(const TBRect &rect, bool coalesce);
  66. /** Include the rect in the region.
  67. This will add only the parts that's not already in the region so the result doesn't
  68. contain overlap parts. This assumes there's no overlap in the region already! */
  69. bool IncludeRect(const TBRect &include_rect);
  70. /** Exclude the rect from the region. */
  71. bool ExcludeRect(const TBRect &exclude_rect);
  72. /** Add the rectangles that's left of rect after excluding exclude_rect. */
  73. bool AddExcludingRects(const TBRect &rect, const TBRect &exclude_rect, bool coalesce);
  74. bool IsEmpty() const { return m_num_rects == 0; }
  75. int GetNumRects() const { return m_num_rects; }
  76. const TBRect &GetRect(int index) const;
  77. private:
  78. TBRect *m_rects;
  79. int m_num_rects;
  80. int m_capacity;
  81. bool GrowIfNeeded();
  82. };
  83. }; // namespace tb
  84. #endif // TB_GEOMETRY_H