]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag-gfx/include/geometry
5f57ef1c4baaa1a0b6ff882fe91bb3815de723ad
[l4.git] / l4 / pkg / mag-gfx / include / geometry
1 // vi:ft=cpp
2 /*
3  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #pragma once
11
12 #include <algorithm>
13
14 namespace Mag_gfx {
15
16 class Vector_2d_base
17 {
18 protected:
19   int _x, _y;
20   Vector_2d_base(int x, int y) : _x(x), _y(y) {}
21 };
22
23 template< typename R >
24 class Vector_2d : public Vector_2d_base
25 {
26 public:
27   Vector_2d(int x, int y) : Vector_2d_base(x, y) {}
28   explicit Vector_2d(Vector_2d_base const &o) : Vector_2d_base(o) {}
29   //  Point() : _x(-1), _y(-1) {}
30
31   //int x() const { return _x; }
32   //int y() const { return _y; }
33
34   R operator + (R const &o) const
35   { return R(_x + o._x, _y + o._y); }
36
37   R operator - (R const &o) const
38   { return R(_x - o._x, _y - o._y); }
39
40   bool operator == (R const &o) const
41   { return _x == o._x && _y == o._y; }
42
43   R &operator += (R const &o)
44   { _x += o._x; _y += o._y; return *static_cast<R*>(this); }
45
46   R &operator -= (R const &o)
47   { _x -= o._x; _y -= o._y; return *static_cast<R*>(this); }
48
49   bool operator != (R const &o) const
50   { return !operator == (o); }
51
52   bool operator >= (R const &o) const
53   { return _x >= o._x && _y >= o._y; }
54
55   bool operator <= (R const &o) const
56   { return _x <= o._x && _y <= o._y; }
57
58   bool operator < (R const &o) const
59   { return _x < o._x && _y < o._y; }
60
61   R max(R const &o) const
62   { return R(std::max<int>(_x, o._x), std::max<int>(_y, o._y)); }
63
64   R min(R const &o) const
65   { return R(std::min<int>(_x, o._x), std::min<int>(_y, o._y)); }
66
67   R operator / (int d) const
68   { return R(_x / d, _y / d); }
69 };
70
71 class Point : public Vector_2d<Point>
72 {
73 private:
74   typedef Vector_2d<Point> B;
75 public:
76   Point(int x, int y) : B(x, y) {}
77   Point() : B(-1, -1) {}
78
79   explicit Point(Vector_2d_base const &o) : B(o) {}
80
81   int x() const { return _x; }
82   int y() const { return _y; }
83
84   void x(int x) { _x = x; }
85   void y(int y) { _y = y; }
86
87 };
88
89 class Area : public Vector_2d<Area>
90 {
91 private:
92   typedef Vector_2d<Area> B;
93
94 public:
95   enum { Max_w = 0x100000, Max_h = Max_w };
96
97   Area(int w, int h) : B(w, h) {}
98   Area() : B(0, 0) {}
99
100   int w() const { return _x; }
101   int h() const { return _y; }
102
103   void w(int w) { _x = w; }
104   void h(int h) { _y = h; }
105
106   bool valid() const { return _x > 0 && _y > 0; }
107   int pixels() const { return _x * _y; }
108
109   Area grow(Point const &diff) const
110   { return Area(_x + diff.x(), _y + diff.y()); }
111 };
112
113 class Rect
114 {
115 private:
116   Point _p1, _p2;
117
118 public:
119   Rect(Point const &p1, Point const &p2) : _p1(p1), _p2(p2) {}
120   Rect(Point const &p1, Area const &a)
121   : _p1(p1), _p2(p1.x() + a.w() - 1, p1.y() + a.h() - 1)
122   {}
123
124   explicit Rect(Area const &a) : _p1(0, 0), _p2(a.w() - 1, a.h() - 1) {}
125
126   Rect() {}
127
128   Rect operator = (Rect const &o) { _p1 = o._p1; _p2 = o._p2; return *this; }
129   bool operator == (Rect const &o) const
130   { return _p1 == o._p1 && _p2 == o._p2; }
131
132   bool operator != (Rect const &o) const
133   { return !operator == (o); }
134
135   Point const &p1() const { return _p1; }
136   Point const &p2() const { return _p2; }
137
138   int x1() const { return _p1.x(); }
139   int y1() const { return _p1.y(); }
140   int x2() const { return _p2.x(); }
141   int y2() const { return _p2.y(); }
142   int w() const  { return x2() - x1() + 1; }
143   int h() const  { return y2() - y1() + 1; }
144
145   Area area() const { return Area(w(), h()); }
146
147   bool valid() const { return x1() <= x2() && y1() <= y2(); }
148   bool fits(Area const &a) const  { return w() >= a.w() && h() >= a.h(); }
149
150   /** intersection */
151   Rect operator & (Rect const &o) const
152   {
153     return Rect(Point(std::max(x1(), o.x1()), std::max(y1(), o.y1())),
154                 Point(std::min(x2(), o.x2()), std::min(y2(), o.y2())));
155   }
156
157   Rect operator | (Rect const &o) const
158   {
159     return Rect(Point(std::min(x1(), o.x1()), std::min(y1(), o.y1())),
160                 Point(std::max(x2(), o.x2()), std::max(y2(), o.y2())));
161   }
162
163   Rect &operator |= (Rect const &o)
164   {
165     *this = *this | o;
166     return *this;
167   }
168
169   Rect grow(int x) const
170   { return Rect(Point(x1() - x, y1() - x), Point(x2() + x, y2() + x)); }
171
172   Rect top(int h) const
173   {
174     Rect n = *this;
175     n._p2 = Point(_p2.x(), _p1.y() + h - 1);
176     return n;
177   }
178
179   Rect left(int w) const
180   {
181     Rect n = *this;
182     n._p2 = Point(_p1.x() + w - 1, _p2.y());
183     return n;
184   }
185
186   Rect bottom(int h) const
187   {
188     Rect n = *this;
189     n._p1 = Point(_p1.x(), _p2.y() - h + 1);
190     return n;
191   }
192
193   Rect right(int w) const
194   {
195     Rect n = *this;
196     n._p1 = Point(_p2.x() - w + 1, _p1.y());
197     return n;
198   }
199
200   Rect offset(int _x1, int _y1, int _x2, int _y2) const
201   { return Rect(Point(x1() + _x1, y1() + _y1), Point(x2() + _x2, y2() + _y2)); }
202
203   Point center(Area const &a) const
204   { return Point((w() - a.w()) / 2, (h() - a.h()) / 2); }
205
206   bool contains(Point const &p) const
207   { return p >= p1() && p <= p2(); }
208
209   Rect operator - (Point const &p) const
210   { return Rect(p1() - p, p2() - p); }
211
212   Rect operator + (Point const &p) const
213   { return Rect(p1() + p, p2() + p); }
214
215   Rect move_to(Point const &p1) const
216   { return Rect(p1, area()); }
217
218 };
219
220 struct Rect_tuple
221 {
222   Rect t, l, r, b;
223   Rect_tuple(Rect const &t, Rect const &l, Rect const &r, Rect const &b)
224   : t(t), l(l), r(r), b(b)
225   {}
226 };
227
228 inline
229 Rect_tuple operator - (Rect const &lh, Rect const &rh)
230 {
231   Rect re = rh & lh;
232   return Rect_tuple(
233       Rect(Point(lh.x1(), lh.y1()), Point(lh.x2(), re.y1() - 1)),
234       Rect(Point(lh.x1(), re.y1()), Point(re.x1() - 1, re.y2())),
235       Rect(Point(re.x2() + 1, re.y1()), Point(lh.x2(), re.y2())),
236       Rect(Point(lh.x1(), re.y2() + 1), Point(lh.x2(), lh.y2())));
237 }
238
239
240 template< typename E >
241 class Flags
242 {
243 private:
244   unsigned _v;
245
246   Flags(unsigned v, bool) : _v(v) {}
247
248   struct Private_bool;
249
250 public:
251   typedef E Enum;
252
253   Flags(int z = 0) : _v(z) {} //Private_bool const * = 0) : _v(0) {}
254   Flags(E e) : _v(e) {}
255   Flags(Flags const &o) : _v(o._v) {}
256
257   operator Private_bool * () const { return (Private_bool *)_v; }
258   bool operator ! () const { return !_v; }
259   Flags operator | (Flags const &o) const { return Flags(_v | o._v, true); }
260   Flags operator | (Enum e) const { return Flags(_v | (unsigned)e, true); }
261   Flags &operator |= (Flags const &o) { _v |= o._v; return *this; }
262   Flags &operator |= (Enum e) { _v |= (unsigned)e; return *this; }
263   Flags operator & (Flags const &o) const { return Flags(_v & o._v, true); }
264   Flags operator & (Enum e) const { return Flags(_v & (unsigned)e, true); }
265   Flags &operator &= (Flags const &o) { _v &= o._v; return *this; }
266   Flags &operator &= (Enum e) { _v &= (unsigned)e; return *this; }
267   Flags operator ~ () const { return Flags(~_v, true); }
268
269   unsigned value() const { return _v; }
270 };
271
272 enum Orientation
273 {
274   Horizontal = 0x1,
275   Horz       = Horizontal,
276   Vertical   = 0x2,
277   Vert       = Vertical,
278 };
279
280 typedef Flags<Orientation> Orientations;
281
282
283 enum Alignment_flag
284 {
285   Align_left     = 0x0001,
286   Align_right    = 0x0002,
287   Align_h_center = 0x0004,
288   Align_justify  = 0x0008,
289
290   Align_horizontal_m = Align_left | Align_right | Align_h_center,
291
292   Align_top      = 0x0020,
293   Align_bottom   = 0x0040,
294   Align_v_center = 0x0080,
295
296   Align_vertical_m = Align_top | Align_bottom | Align_v_center,
297
298   Align_center   = Align_h_center | Align_v_center,
299 };
300
301 typedef Flags<Alignment_flag> Alignment;
302
303
304
305 }