]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag-gfx/include/geometry
update
[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   R operator * (int d) const
71   { return R(_x * d, _y * d); }
72
73   R operator / (R const &o) const
74   { return R(_x / o._x, _y / o._y); }
75
76   R operator * (R const &o) const
77   { return R(_x * o._x, _y * o._y); }
78 };
79
80 class Point : public Vector_2d<Point>
81 {
82 private:
83   typedef Vector_2d<Point> B;
84 public:
85   Point(int x, int y) : B(x, y) {}
86   Point() : B(0, 0) {}
87
88   explicit Point(Vector_2d_base const &o) : B(o) {}
89
90   int x() const { return _x; }
91   int y() const { return _y; }
92
93   void x(int x) { _x = x; }
94   void y(int y) { _y = y; }
95
96 };
97
98 class Area : public Vector_2d<Area>
99 {
100 private:
101   typedef Vector_2d<Area> B;
102
103 public:
104   enum { Max_w = 0x100000, Max_h = Max_w };
105
106   Area(int w, int h) : B(w, h) {}
107   Area() : B(0, 0) {}
108
109   int w() const { return _x; }
110   int h() const { return _y; }
111
112   void w(int w) { _x = w; }
113   void h(int h) { _y = h; }
114
115   bool valid() const { return _x > 0 && _y > 0; }
116   int pixels() const { return _x * _y; }
117
118   Area grow(Point const &diff) const
119   { return Area(_x + diff.x(), _y + diff.y()); }
120 };
121
122 class Rect
123 {
124 private:
125   Point _p1, _p2;
126
127 public:
128   Rect(Point const &p1, Point const &p2) : _p1(p1), _p2(p2) {}
129   Rect(Point const &p1, Area const &a)
130   : _p1(p1), _p2(p1.x() + a.w() - 1, p1.y() + a.h() - 1)
131   {}
132
133   explicit Rect(Area const &a) : _p1(0, 0), _p2(a.w() - 1, a.h() - 1) {}
134
135   Rect() {}
136
137   Rect operator = (Rect const &o) { _p1 = o._p1; _p2 = o._p2; return *this; }
138   bool operator == (Rect const &o) const
139   { return _p1 == o._p1 && _p2 == o._p2; }
140
141   bool operator != (Rect const &o) const
142   { return !operator == (o); }
143
144   Point const &p1() const { return _p1; }
145   Point const &p2() const { return _p2; }
146
147   int x1() const { return _p1.x(); }
148   int y1() const { return _p1.y(); }
149   int x2() const { return _p2.x(); }
150   int y2() const { return _p2.y(); }
151   int w() const  { return x2() - x1() + 1; }
152   int h() const  { return y2() - y1() + 1; }
153
154   Area area() const { return Area(w(), h()); }
155
156   bool valid() const { return x1() <= x2() && y1() <= y2(); }
157   bool fits(Area const &a) const  { return w() >= a.w() && h() >= a.h(); }
158
159   /** intersection */
160   Rect operator & (Rect const &o) const
161   {
162     return Rect(Point(std::max(x1(), o.x1()), std::max(y1(), o.y1())),
163                 Point(std::min(x2(), o.x2()), std::min(y2(), o.y2())));
164   }
165
166   Rect operator | (Rect const &o) const
167   {
168     return Rect(Point(std::min(x1(), o.x1()), std::min(y1(), o.y1())),
169                 Point(std::max(x2(), o.x2()), std::max(y2(), o.y2())));
170   }
171
172   Rect &operator |= (Rect const &o)
173   {
174     *this = *this | o;
175     return *this;
176   }
177
178   Rect grow(int x) const
179   { return Rect(Point(x1() - x, y1() - x), Point(x2() + x, y2() + x)); }
180
181   Rect top(int h) const
182   {
183     Rect n = *this;
184     n._p2 = Point(_p2.x(), _p1.y() + h - 1);
185     return n;
186   }
187
188   Rect left(int w) const
189   {
190     Rect n = *this;
191     n._p2 = Point(_p1.x() + w - 1, _p2.y());
192     return n;
193   }
194
195   Rect bottom(int h) const
196   {
197     Rect n = *this;
198     n._p1 = Point(_p1.x(), _p2.y() - h + 1);
199     return n;
200   }
201
202   Rect right(int w) const
203   {
204     Rect n = *this;
205     n._p1 = Point(_p2.x() - w + 1, _p1.y());
206     return n;
207   }
208
209   Rect offset(int _x1, int _y1, int _x2, int _y2) const
210   { return Rect(Point(x1() + _x1, y1() + _y1), Point(x2() + _x2, y2() + _y2)); }
211
212   Point center(Area const &a) const
213   { return Point((w() - a.w()) / 2, (h() - a.h()) / 2); }
214
215   bool contains(Point const &p) const
216   { return p >= p1() && p <= p2(); }
217
218   Rect operator - (Point const &p) const
219   { return Rect(p1() - p, p2() - p); }
220
221   Rect operator + (Point const &p) const
222   { return Rect(p1() + p, p2() + p); }
223
224   Rect move_to(Point const &p1) const
225   { return Rect(p1, area()); }
226
227 };
228
229 struct Rect_tuple
230 {
231   Rect _r[4];
232   Rect_tuple() {}
233   Rect_tuple(Rect const &t, Rect const &l, Rect const &r, Rect const &b)
234   { _r[0] = t; _r[1] = l; _r[2] = r; _r[3] = b; }
235
236   Rect const &operator [] (unsigned i) const { return _r[i]; }
237   Rect const &t() const { return _r[0]; }
238   Rect const &l() const { return _r[1]; }
239   Rect const &r() const { return _r[2]; }
240   Rect const &b() const { return _r[3]; }
241 };
242
243 inline
244 Rect_tuple operator - (Rect const &lh, Rect const &rh)
245 {
246   Rect re = rh & lh;
247   return Rect_tuple(
248       Rect(Point(lh.x1(), lh.y1()), Point(lh.x2(), re.y1() - 1)),
249       Rect(Point(lh.x1(), re.y1()), Point(re.x1() - 1, re.y2())),
250       Rect(Point(re.x2() + 1, re.y1()), Point(lh.x2(), re.y2())),
251       Rect(Point(lh.x1(), re.y2() + 1), Point(lh.x2(), lh.y2())));
252 }
253
254
255 template< typename E >
256 class Flags
257 {
258 private:
259   unsigned _v;
260
261   Flags(unsigned v, bool) : _v(v) {}
262
263   struct Private_bool;
264
265 public:
266   typedef E Enum;
267
268   Flags(int z = 0) : _v(z) {} //Private_bool const * = 0) : _v(0) {}
269   Flags(E e) : _v(e) {}
270   Flags(Flags const &o) : _v(o._v) {}
271
272   operator Private_bool * () const { return (Private_bool *)_v; }
273   bool operator ! () const { return !_v; }
274   Flags operator | (Flags const &o) const { return Flags(_v | o._v, true); }
275   Flags operator | (Enum e) const { return Flags(_v | (unsigned)e, true); }
276   Flags &operator |= (Flags const &o) { _v |= o._v; return *this; }
277   Flags &operator |= (Enum e) { _v |= (unsigned)e; return *this; }
278   Flags operator & (Flags const &o) const { return Flags(_v & o._v, true); }
279   Flags operator & (Enum e) const { return Flags(_v & (unsigned)e, true); }
280   Flags &operator &= (Flags const &o) { _v &= o._v; return *this; }
281   Flags &operator &= (Enum e) { _v &= (unsigned)e; return *this; }
282   Flags operator ~ () const { return Flags(~_v, true); }
283
284   unsigned value() const { return _v; }
285 };
286
287 enum Orientation
288 {
289   Horizontal = 0x1,
290   Horz       = Horizontal,
291   Vertical   = 0x2,
292   Vert       = Vertical,
293 };
294
295 typedef Flags<Orientation> Orientations;
296
297
298 enum Alignment_flag
299 {
300   Align_left     = 0x0001,
301   Align_right    = 0x0002,
302   Align_h_center = 0x0004,
303   Align_justify  = 0x0008,
304
305   Align_horizontal_m = Align_left | Align_right | Align_h_center,
306
307   Align_top      = 0x0020,
308   Align_bottom   = 0x0040,
309   Align_v_center = 0x0080,
310
311   Align_vertical_m = Align_top | Align_bottom | Align_v_center,
312
313   Align_center   = Align_h_center | Align_v_center,
314 };
315
316 typedef Flags<Alignment_flag> Alignment;
317
318
319
320 }