Boost.UI
User Interface Boost library
coord.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Kolya Kosenko
2 
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
5 
7 
8 #ifndef BOOST_UI_COORD_HPP
9 #define BOOST_UI_COORD_HPP
10 
11 #ifdef DOXYGEN
12 #define BOOST_UI_USE_GEOMETRY
15 
18 #define BOOST_UI_USE_POLYGON
19 #endif
20 
21 #if defined(BOOST_UI_USE_GEOMETRY) && defined(BOOST_UI_USE_POLYGON)
22 #error Unable to use Boost.Geometry with Boost.Polygon
23 #endif
24 
25 #include <boost/ui/config.hpp>
26 
27 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #pragma once
29 #endif
30 
31 #ifdef BOOST_UI_USE_GEOMETRY
32 #include <boost/geometry.hpp> // TODO: Remove hotfix
33 #include <boost/geometry/geometries/point_xy.hpp>
34 #include <boost/geometry/geometries/box.hpp>
35 #endif
36 
37 #ifdef BOOST_UI_USE_POLYGON
38 #include <boost/polygon/point_data.hpp>
39 #include <boost/polygon/rectangle_data.hpp>
40 #endif
41 
42 namespace boost {
43 namespace ui {
44 
47 template <class T>
49 {
50 public:
52  basic_size() : m_width(T()), m_height(T()) {}
53 
55  basic_size(const T& width, const T& height)
56  : m_width(width), m_height(height) {}
57 
59  template <class D>
60  explicit basic_size(const basic_size<D>& s)
61  : m_width(s.width()), m_height(s.height()) {}
62 
64  bool operator==(const basic_size& other) const
65  { return m_width == other.m_width && m_height == other.m_height; }
66  bool operator!=(const basic_size& other) const
67  { return !operator==(other); }
69 
71  const T& width() const { return m_width; }
72 
74  const T& height() const { return m_height; }
75 
77  basic_size& operator*=(const T& value)
78  {
79  m_width *= value;
80  m_height *= value;
81  return *this;
82  }
83  basic_size& operator/=(const T& value)
84  {
85  m_width /= value;
86  m_height /= value;
87  return *this;
88  }
90 
91 private:
92  T m_width;
93  T m_height;
94 };
95 
97 template <class T>
98 basic_size<T> operator*(const T& value, const basic_size<T>& rhs)
99 {
100  return basic_size<T>(value * rhs.width(), value * rhs.height());
101 }
102 template <class T>
103 basic_size<T> operator*(const basic_size<T>& lhs, const T& value)
104 {
105  return basic_size<T>(lhs.width() * value, lhs.height() * value);
106 }
107 template <class T>
108 basic_size<T> operator/(const basic_size<T>& lhs, const T& value)
109 {
110  return basic_size<T>(lhs.width() / value, lhs.height() / value);
111 }
113 
117 
118 template <class T>
121  : public ::boost::geometry::model::d2::point_xy<T>
122 #endif
123 #ifdef BOOST_UI_USE_POLYGON
124  : public ::boost::polygon::point_data<T>
125 #endif
126 {
127 #if defined(BOOST_UI_USE_GEOMETRY)
128  typedef ::boost::geometry::model::d2::point_xy<T> base_type;
129 #elif defined(BOOST_UI_USE_POLYGON)
130  typedef ::boost::polygon::point_data<T> base_type;
131 #endif
132 
133 public:
134 
136  typedef T value_type;
137 
140 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
141  base_type(T(), T())
142 #else
143  m_x(T()), m_y(T())
144 #endif
145  {}
146 
148  basic_point(const T& x, const T& y) :
149 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
150  base_type(x, y)
151 #else
152  m_x(x), m_y(y)
153 #endif
154  {}
155 
156 #if (defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)) && !defined(DOXYGEN)
157  basic_point(const base_type& p) : base_type(p) {}
158 #endif
159 
161  template <class D>
162  explicit basic_point(const basic_point<D>& p) :
163 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
164  base_type(p.x(), p.y())
165 #else
166  m_x(p.x()), m_y(p.y())
167 #endif
168  {}
169 
171  T x() const
172 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
173  { return base_type::x(); }
174 #else
175  { return m_x; }
176 #endif
177 
179  T y() const
180 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
181  { return base_type::y(); }
182 #else
183  { return m_y; }
184 #endif
185 
187  void x(const T& x)
188 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
189  { base_type::x(x); }
190 #else
191  { m_x = x; }
192 #endif
193 
195  void y(const T& y)
196 #if defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)
197  { base_type::y(y); }
198 #else
199  { m_y = y; }
200 #endif
201 
203  bool operator==(const basic_point& other) const
204  { return x() == other.x() && y() == other.y(); }
205  bool operator!=(const basic_point& other) const
206  { return !operator==(other); }
208 
211  {
212  x(x() + other.width());
213  y(y() + other.height());
214  return *this;
215  }
217  {
218  x(x() - other.width());
219  y(y() - other.height());
220  return *this;
221  }
223 
224 private:
225 #if !defined(BOOST_UI_USE_GEOMETRY) && !defined(BOOST_UI_USE_POLYGON)
226  T m_x;
227  T m_y;
228 #endif
229 };
230 
232 template <class T>
234  const basic_size<T>& rhs)
235 {
236  return basic_point<T>(lhs.x() + rhs.width(), lhs.y() + rhs.height());
237 }
238 
239 template <class T>
241  const basic_size<T>& rhs)
242 {
243  return basic_point<T>(lhs.x() - rhs.width(), lhs.y() - rhs.height());
244 }
245 
246 template <class T>
248  const basic_point<T>& rhs)
249 {
250  return basic_size<T>(lhs.x() - rhs.x(), lhs.y() - rhs.y());
251 }
253 
257 
258 template <class T>
261  : public ::boost::geometry::model::box<
262  ::boost::geometry::model::d2::point_xy<T> >
263 #endif
264 #ifdef BOOST_UI_USE_POLYGON
265  : public ::boost::polygon::rectangle_data<T>
266 #endif
267 {
268 #ifdef BOOST_UI_USE_GEOMETRY
269  typedef ::boost::geometry::model::d2::point_xy<T> point_type;
270  typedef ::boost::geometry::model::box<point_type> base_type;
271 #endif
272 #ifdef BOOST_UI_USE_POLYGON
273  typedef ::boost::polygon::rectangle_data<T> base_type;
274 #endif
275 
276 public:
278  typedef T value_type;
279 
282 #if defined(BOOST_UI_USE_GEOMETRY)
283  : base_type(point_type(T(), T()), point_type(T(), T()))
284 #elif defined(BOOST_UI_USE_POLYGON)
285  : base_type(T(), T(), T(), T())
286 #endif
287  {}
288 
290  basic_rect(const T& x, const T& y, const T& width, const T& height) :
291 #if defined(BOOST_UI_USE_GEOMETRY)
292  base_type(point_type(x, y),
293  point_type(x + width, y + height))
294 #elif defined(BOOST_UI_USE_POLYGON)
295  base_type(x, y,
296  x + width, y + height)
297 #else
298  m_point(x, y), m_size(width, height)
299 #endif
300  {}
301 
303 #if defined(BOOST_UI_USE_GEOMETRY)
304  base_type(point_type(point.x(), point.y()),
305  point_type(point.x() + size.width(), point.y() + size.height()))
306 #elif defined(BOOST_UI_USE_POLYGON)
307  base_type(point.x(), point.y(),
308  point.x() + size.width(), point.y() + size.height())
309 #else
310  m_point(point.x(), point.y()), m_size(size.width(), size.height())
311 #endif
312  {}
313 
314  basic_rect(const basic_point<T>& point1, const basic_point<T>& point2) :
315 #if defined(BOOST_UI_USE_GEOMETRY)
316  base_type(point_type(point1.x(), point1.y()),
317  point_type(point2.x(), point2.y()))
318 #elif defined(BOOST_UI_USE_POLYGON)
319  base_type(point1.x(), point1.y(),
320  point2.x(), point2.y())
321 #else
322  m_point(point1.x(), point1.y()), m_size(point2 - point1)
323 #endif
324  {}
325 
326 #if (defined(BOOST_UI_USE_GEOMETRY) || defined(BOOST_UI_USE_POLYGON)) && !defined(DOXYGEN)
327  basic_rect(const base_type& other) : base_type(other) {}
328 #endif
329 
330  template <class D>
331  explicit basic_rect(const basic_rect<D>& other) :
332 #if defined(BOOST_UI_USE_GEOMETRY)
333  base_type(point_type(other.x(), other.y()),
334  point_type(static_cast<T>(other.x()) +
335  static_cast<T>(other.width()),
336  static_cast<T>(other.y()) +
337  static_cast<T>(other.height())))
338 #elif defined(BOOST_UI_USE_POLYGON)
339  base_type(other.x(), other.y(),
340  static_cast<T>(other.x()) +
341  static_cast<T>(other.width()),
342  static_cast<T>(other.y()) +
343  static_cast<T>(other.height()))
344 #else
345  m_point(other.x(), other.y()), m_size(other.width(), other.height())
346 #endif
347  {}
348 
350 
352  T x() const
353 #if defined(BOOST_UI_USE_GEOMETRY)
354  { return this->min_corner().x(); }
355 #elif defined(BOOST_UI_USE_POLYGON)
356  { return this->get(::boost::polygon::HORIZONTAL).low(); }
357 #else
358  { return m_point.x(); }
359 #endif
360 
362  T y() const
363 #if defined(BOOST_UI_USE_GEOMETRY)
364  { return this->min_corner().y(); }
365 #elif defined(BOOST_UI_USE_POLYGON)
366  { return this->get(::boost::polygon::VERTICAL).low(); }
367 #else
368  { return m_point.y(); }
369 #endif
370 
372  T width() const
373 #if defined(BOOST_UI_USE_GEOMETRY)
374  { return this->max_corner().x() - this->min_corner().x(); }
375 #elif defined(BOOST_UI_USE_POLYGON)
376  { return this->get(::boost::polygon::HORIZONTAL).high() -
377  this->get(::boost::polygon::HORIZONTAL).low(); }
378 #else
379  { return m_size.width(); }
380 #endif
381 
383  T height() const
384 #if defined(BOOST_UI_USE_GEOMETRY)
385  { return this->max_corner().y() - this->min_corner().y(); }
386 #elif defined(BOOST_UI_USE_POLYGON)
387  { return this->get(::boost::polygon::VERTICAL).high() -
388  this->get(::boost::polygon::VERTICAL).low(); }
389 #else
390  { return m_size.height(); }
391 #endif
392 
394  bool operator==(const basic_rect& other) const
395  {
396  return x() == other.x() && y() == other.y() &&
397  width() == other.width() && height() == other.height();
398  }
399  bool operator!=(const basic_rect& other) const
400  { return !operator==(other); }
402 
403 private:
404 #if !defined(BOOST_UI_USE_GEOMETRY) && !defined(BOOST_UI_USE_POLYGON)
405  basic_point<T> m_point;
406  basic_size<T> m_size;
407 #endif
408 };
409 
412 typedef int coord_type;
413 
417 
421 
425 
426 } // namespace ui
427 } // namespace boost
428 
429 #endif // BOOST_UI_COORD_HPP
boost::ui::operator/
basic_size< T > operator/(const basic_size< T > &lhs, const T &value)
Arithmetic operation.
Definition: coord.hpp:108
boost::ui::basic_point::y
T y() const
Returns y coordinate.
Definition: coord.hpp:179
boost::ui::rect
basic_rect< coord_type > rect
2D rectangle coordinates
Definition: coord.hpp:424
boost::ui::basic_point::operator+=
basic_point & operator+=(const basic_size< T > &other)
Arithmetic operation.
Definition: coord.hpp:210
boost::ui::basic_point
2D point data structure with custom coordinates type
Definition: coord.hpp:119
boost::ui::basic_point::y
void y(const T &y)
Sets y coordinate.
Definition: coord.hpp:195
boost::ui::basic_size::operator/=
basic_size & operator/=(const T &value)
Arithmetic operation.
Definition: coord.hpp:83
config.hpp
Configuration options.
boost::ui::basic_rect::basic_rect
basic_rect()
Constructs rectangle with empty coordinates.
Definition: coord.hpp:281
boost::ui::basic_point::basic_point
basic_point()
Constructs point with empty coordinates.
Definition: coord.hpp:139
boost::ui::basic_point::operator!=
bool operator!=(const basic_point &other) const
Compares two points.
Definition: coord.hpp:205
boost::ui::basic_rect::basic_rect
basic_rect(const basic_rect< D > &other)
Constructs rectangle with coordinates.
Definition: coord.hpp:331
boost::ui::basic_rect::width
T width() const
Returns width.
Definition: coord.hpp:372
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::basic_size::basic_size
basic_size()
Constructs empty size object.
Definition: coord.hpp:52
boost::ui::basic_size::basic_size
basic_size(const T &width, const T &height)
Constructs size with width and height.
Definition: coord.hpp:55
boost::ui::basic_rect::height
T height() const
Returns height.
Definition: coord.hpp:383
boost::ui::size
basic_size< coord_type > size
2D size coordinates
Definition: coord.hpp:416
boost::ui::basic_point::value_type
T value_type
Type of coordinates.
Definition: coord.hpp:136
boost::ui::basic_point::x
T x() const
Returns x coordinate.
Definition: coord.hpp:171
boost::ui::basic_rect::basic_rect
basic_rect(const basic_point< T > &point, const basic_size< T > &size)
Constructs rectangle with coordinates.
Definition: coord.hpp:302
boost::ui::basic_rect::x
T x() const
Returns x coordinate.
Definition: coord.hpp:352
boost::ui::basic_point::operator-=
basic_point & operator-=(const basic_size< T > &other)
Arithmetic operation.
Definition: coord.hpp:216
BOOST_UI_USE_POLYGON
#define BOOST_UI_USE_POLYGON
Enables Boost.Polygon.
Definition: coord.hpp:18
boost::ui::point
basic_point< coord_type > point
2D point coordinates
Definition: coord.hpp:420
boost::ui::operator-
basic_point< T > operator-(const basic_point< T > &lhs, const basic_size< T > &rhs)
Arithmetic operation.
Definition: coord.hpp:240
boost::ui::basic_size::operator==
bool operator==(const basic_size &other) const
Compares two sizes.
Definition: coord.hpp:64
boost::ui::operator+
uistring operator+(const uistring &lhs, const uistring &rhs)
Concatenates two strings or the string and the character.
Definition: string.hpp:408
boost::ui::basic_size::operator!=
bool operator!=(const basic_size &other) const
Compares two sizes.
Definition: coord.hpp:66
boost::ui::coord_type
int coord_type
Widget coordinates signed number type.
Definition: coord.hpp:412
boost::ui::basic_point::x
void x(const T &x)
Sets x coordinate.
Definition: coord.hpp:187
boost::ui::basic_point::basic_point
basic_point(const basic_point< D > &p)
Constructs size with an other size object.
Definition: coord.hpp:162
boost::ui::basic_size::operator*=
basic_size & operator*=(const T &value)
Arithmetic operation.
Definition: coord.hpp:77
boost::ui::basic_rect::basic_rect
basic_rect(const basic_point< T > &point1, const basic_point< T > &point2)
Constructs rectangle with coordinates.
Definition: coord.hpp:314
boost::ui::basic_point::operator==
bool operator==(const basic_point &other) const
Compares two points.
Definition: coord.hpp:203
boost::ui::basic_point::basic_point
basic_point(const T &x, const T &y)
Constructs point with x and y coordinates.
Definition: coord.hpp:148
boost::ui::basic_size::basic_size
basic_size(const basic_size< D > &s)
Constructs size with an other size object.
Definition: coord.hpp:60
boost::ui::basic_size::height
const T & height() const
Retruns height.
Definition: coord.hpp:74
boost::ui::basic_size::width
const T & width() const
Retruns width.
Definition: coord.hpp:71
BOOST_UI_USE_GEOMETRY
#define BOOST_UI_USE_GEOMETRY
Enables Boost.Geometry.
Definition: coord.hpp:14
boost::ui::basic_size
2D size of object data structure with custom coordinates type
Definition: coord.hpp:48
boost::ui::basic_rect::operator!=
bool operator!=(const basic_rect &other) const
Compares two rectangles.
Definition: coord.hpp:399
boost::ui::basic_rect::y
T y() const
Returns y coordinate.
Definition: coord.hpp:362
boost::ui::basic_rect::operator==
bool operator==(const basic_rect &other) const
Compares two rectangles.
Definition: coord.hpp:394
boost::ui::basic_rect
2D rectangle plane figure data structure with custom coordinates type
Definition: coord.hpp:259
boost::ui::basic_rect::basic_rect
basic_rect(const T &x, const T &y, const T &width, const T &height)
Constructs rectangle with coordinates.
Definition: coord.hpp:290
boost::ui::basic_rect::value_type
T value_type
Type of coordinates.
Definition: coord.hpp:278
boost::ui::operator*
basic_size< T > operator*(const T &value, const basic_size< T > &rhs)
Arithmetic operation.
Definition: coord.hpp:98