alaCarte Maps
Renderer for OpenStreetMap tiles
rect.hpp
Go to the documentation of this file.
1 
21 #pragma once
22 #ifndef RECT_HPP
23 #define RECT_HPP
24 
25 /*
26  * =====================================================================================
27  *
28  * Filename: rect.hpp
29  *
30  * Description: Utility class for rectangle operations.
31  *
32  * =====================================================================================
33  */
34 #include "point.hpp"
35 
36 #include <boost/archive/text_oarchive.hpp>
37 #include <boost/archive/text_iarchive.hpp>
38 
39 template<typename T>
40 class basic_rect {
41 private:
42  friend class boost::serialization::access;
43  template<typename Archive>
44  void serialize(Archive &ar, const unsigned int version){
45  ar & minX;
46  ar & minY;
47  ar & maxX;
48  ar & maxY;
49  }
50 public:
51  T minX;
52  T minY;
53  T maxX;
54  T maxY;
55 
56  basic_rect() : minX(0), minY(0), maxX(0), maxY(0) {}
57 
59  {
60  minX = std::min(p1.x, p2.x);
61  minY = std::min(p1.y, p2.y);
62  maxX = std::max(p1.x, p2.x);
63  maxY = std::max(p1.y, p2.y);
64  assert(maxY >= minY);
65  assert(maxX >= minX);
66  }
67 
68  basic_rect(T minX, T minY, T maxX, T maxY)
69  : minY(minY)
70  , maxY(maxY)
71  , maxX(maxX)
72  , minX(minX)
73  {
74  assert(maxY >= minY);
75  assert(maxX >= minX);
76  }
77 
78  basic_rect(const basic_vector2<T>& minCoord, T width, T height)
79  : minX(minCoord.x)
80  , minY(minCoord.y)
81  , maxX(minCoord.x + width)
82  , maxY(minCoord.y + height)
83  {
84  }
85 
86  inline basic_rect<T> translate(T dx, T dy) const
87  {
88  return basic_rect<T>(minX + dx, minY + dy, maxX + dx, maxY + dy);
89  }
90 
91  inline basic_rect<T> grow(T dx, T dy) const
92  {
93  return basic_rect<T>(minX - dx, minY - dy, maxX + dx, maxY + dy);
94  }
95 
96  inline void enclose(const basic_rect<T>& other)
97  {
98  minX = std::min(minX, other.minX);
99  maxX = std::max(maxX, other.maxX);
100  minY = std::min(minY, other.minY);
101  maxY = std::max(maxY, other.maxY);
102  }
103 
104  inline void enclose(const basic_vector2<T>& other)
105  {
106  minX = std::min(minX, other.x);
107  maxX = std::max(maxX, other.x);
108  minY = std::min(minY, other.y);
109  maxY = std::max(maxY, other.y);
110  }
111 
112  inline T getArea() const {
113  return (maxY - minY) * (maxX - minX);
114  }
115 
116  inline basic_vector2<T> getCenter() const {
117  return basic_vector2<T>((minX + maxX) / 2.0, (minY + maxY) / 2.0);
118  }
119 
120  inline basic_rect<T> getIntersection(const basic_rect<T>& other) const {
121  T x0 = std::max(other.minX, minX);
122  T y0 = std::max(other.minY, minY);
123  T x1 = std::min(other.maxX, maxX);
124  T y1 = std::min(other.maxY, maxY);
125  if (x0 >= x1 || y0 >= y1)
126  return basic_rect<T>();
127  return basic_rect<T>(x0, y0, x1, y1);
128  }
129 
130  inline bool contains(const basic_vector2<T>& p) const
131  {
132  return (between(minX, p.x, maxX) && between(minY, p.y, maxY));
133  }
134 
135  inline bool contains(const basic_rect<T>& other) const
136  {
137  return (between(minX, other.minX, maxX)
138  && between(minY, other.minY, maxY)
139  && between(minX, other.maxX, maxX)
140  && between(minY, other.maxY, maxY));
141  }
142 
143  inline bool intersects(const basic_rect<T>& bounding) const {
144  return ((between(minX, bounding.maxX, maxX)
145  || between(minX, bounding.minX, maxX)
146  || between(bounding.minX, maxX, bounding.maxX)
147  || between(bounding.minX, minX, bounding.maxX))
148  && (between(minY, bounding.maxY, maxY)
149  || between(minY, bounding.minY, maxY)
150  || between(bounding.minY, maxY, bounding.maxY)
151  || between(bounding.minY, minY, bounding.maxY)));
152  }
153 
154  inline T getWidth() const {
155  return (maxX - minX);
156  }
157 
158  inline T getHeight() const {
159  return (maxY - minY);
160  }
161 
162 private:
163  inline bool between(const T& min, const T& value, const T& max) const
164  {
165  assert(min <= max);
166  return (min <= value && value <= max);
167  }
168 };
169 
172 
173 template<class T>
174 bool operator==(const basic_rect<T>& a, const basic_rect<T>& b)
175 {
176  return (a.minX == b.minX && a.minY == b.minY && a.maxX == b.maxX && a.maxY == b.maxY);
177 }
178 
179 template<class T>
180 bool operator!=(const basic_rect<T>& a, const basic_rect<T>& b)
181 {
182  return (a.minX != b.minX || a.minY != b.minY || a.maxX != b.maxX || a.maxY != b.maxY);
183 }
184 
185 #endif
basic_rect< double > FloatRect
Definition: rect.hpp:170
basic_rect< coord_t > FixedRect
Definition: rect.hpp:171
void serialize(Archive &ar, const unsigned int version)
Definition: rect.hpp:44
T getHeight() const
Definition: rect.hpp:158
bool intersects(const basic_rect< T > &bounding) const
Definition: rect.hpp:143
void enclose(const basic_vector2< T > &other)
Definition: rect.hpp:104
T getArea() const
Definition: rect.hpp:112
T getWidth() const
Definition: rect.hpp:154
bool contains(const basic_vector2< T > &p) const
Definition: rect.hpp:130
bool contains(const basic_rect< T > &other) const
Definition: rect.hpp:135
T minY
Definition: rect.hpp:52
basic_rect()
Definition: rect.hpp:56
bool operator==(const basic_rect< T > &a, const basic_rect< T > &b)
Definition: rect.hpp:174
basic_rect(const basic_vector2< T > &minCoord, T width, T height)
Definition: rect.hpp:78
basic_rect< T > grow(T dx, T dy) const
Definition: rect.hpp:91
basic_rect(T minX, T minY, T maxX, T maxY)
Definition: rect.hpp:68
void enclose(const basic_rect< T > &other)
Definition: rect.hpp:96
basic_rect< T > translate(T dx, T dy) const
Definition: rect.hpp:86
bool between(const T &min, const T &value, const T &max) const
Definition: rect.hpp:163
basic_rect< T > getIntersection(const basic_rect< T > &other) const
Definition: rect.hpp:120
T maxX
Definition: rect.hpp:53
bool operator!=(const basic_rect< T > &a, const basic_rect< T > &b)
Definition: rect.hpp:180
basic_rect(const basic_vector2< T > &p1, const basic_vector2< T > &p2)
Definition: rect.hpp:58
T maxY
Definition: rect.hpp:54
T minX
Definition: rect.hpp:51
basic_vector2< T > getCenter() const
Definition: rect.hpp:116