2013-12-28 18:01:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Geometric primitives
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-08-05
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2013-12-28 18:01:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2013-12-28 18:01:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__GEOMETRY_H_
|
|
|
|
#define _INCLUDE__UTIL__GEOMETRY_H_
|
|
|
|
|
|
|
|
#include <util/misc_math.h>
|
|
|
|
#include <base/stdint.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
#include <base/output.h>
|
2013-12-28 18:01:25 +00:00
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
template <typename CT = int> class Point;
|
|
|
|
template <typename DT = unsigned> class Area;
|
|
|
|
template <typename CT = int, typename DT = unsigned> class Rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \param CT coordinate type
|
|
|
|
*/
|
|
|
|
template <typename CT>
|
|
|
|
class Genode::Point
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
CT _x, _y;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Point(CT x, CT y): _x(x), _y(y) { }
|
|
|
|
Point(): _x(0), _y(0) { }
|
|
|
|
|
|
|
|
int x() const { return _x; }
|
|
|
|
int y() const { return _y; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for adding points
|
|
|
|
*/
|
|
|
|
Point operator + (Point const &p) const { return Point(_x + p.x(), _y + p.y()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for subtracting points
|
|
|
|
*/
|
|
|
|
Point operator - (Point const &p) const { return Point(_x - p.x(), _y - p.y()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for testing non-equality of two points
|
|
|
|
*/
|
|
|
|
bool operator != (Point const &p) const { return p.x() != _x || p.y() != _y; }
|
2014-04-28 19:03:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for testing equality of two points
|
|
|
|
*/
|
|
|
|
bool operator == (Point const &p) const { return p.x() == _x && p.y() == _y; }
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
|
|
|
|
void print(Output &out) const
|
|
|
|
{
|
|
|
|
Genode::print(out, _x >= 0 ? "+" : "-", abs(_x),
|
|
|
|
_y >= 0 ? "+" : "-", abs(_y));
|
|
|
|
}
|
2013-12-28 18:01:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \param DT distance type
|
|
|
|
*/
|
|
|
|
template <typename DT>
|
|
|
|
class Genode::Area
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
DT _w, _h;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Area(DT w, DT h): _w(w), _h(h) { }
|
|
|
|
Area(): _w(0), _h(0) { }
|
|
|
|
|
|
|
|
DT w() const { return _w; }
|
|
|
|
DT h() const { return _h; }
|
|
|
|
|
|
|
|
bool valid() const { return _w > 0 && _h > 0; }
|
|
|
|
|
|
|
|
size_t count() const { return _w*_h; }
|
2014-07-24 13:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for testing non-equality of two areas
|
|
|
|
*/
|
|
|
|
bool operator != (Area const &a) const { return a.w() != _w || a.h() != _h; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator for testing equality of two areas
|
|
|
|
*/
|
|
|
|
bool operator == (Area const &a) const { return a.w() == _w && a.h() == _h; }
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
|
|
|
|
void print(Output &out) const { Genode::print(out, _w, "x", _h); }
|
2013-12-28 18:01:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rectangle
|
|
|
|
*
|
|
|
|
* A valid rectangle consists of two points wheras point 2 has higher or equal
|
|
|
|
* coordinates than point 1. All other cases are threated as invalid
|
|
|
|
* rectangles.
|
|
|
|
*
|
|
|
|
* \param CT coordinate type
|
|
|
|
* \param DT distance type
|
|
|
|
*/
|
|
|
|
template <typename CT, typename DT>
|
|
|
|
class Genode::Rect
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Point<CT> _p1, _p2;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors
|
|
|
|
*/
|
|
|
|
Rect(Point<CT> p1, Point<CT> p2): _p1(p1), _p2(p2) { }
|
|
|
|
|
|
|
|
Rect(Point<CT> p, Area<DT> a)
|
|
|
|
: _p1(p), _p2(p.x() + a.w() - 1, p.y() + a.h() - 1) { }
|
|
|
|
|
2014-04-30 19:30:32 +00:00
|
|
|
Rect() : /* invalid */ _p1(1, 1), _p2(0, 0) { }
|
2013-12-28 18:01:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessors
|
|
|
|
*/
|
|
|
|
CT x1() const { return _p1.x(); }
|
|
|
|
CT y1() const { return _p1.y(); }
|
|
|
|
CT x2() const { return _p2.x(); }
|
|
|
|
CT y2() const { return _p2.y(); }
|
|
|
|
DT w() const { return _p2.x() - _p1.x() + 1; }
|
|
|
|
DT h() const { return _p2.y() - _p1.y() + 1; }
|
|
|
|
Point<CT> p1() const { return _p1; }
|
|
|
|
Point<CT> p2() const { return _p2; }
|
|
|
|
Area<DT> area() const { return Area<DT>(w(), h()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if rectangle area is greater than zero
|
|
|
|
*/
|
|
|
|
bool valid() const { return _p1.x() <= _p2.x() && _p1.y() <= _p2.y(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if area fits in rectangle
|
|
|
|
*/
|
|
|
|
bool fits(Area<DT> area) const { return w() >= area.w() && h() >= area.h(); }
|
|
|
|
|
2014-04-25 22:06:11 +00:00
|
|
|
/**
|
|
|
|
* Return true if the specified point lies within the rectangle
|
|
|
|
*/
|
|
|
|
bool contains(Point<CT> p) const {
|
|
|
|
return p.x() >= x1() && p.x() <= x2() && p.y() >= y1() && p.y() <= y2(); }
|
|
|
|
|
2013-12-28 18:01:25 +00:00
|
|
|
/**
|
|
|
|
* Create new rectangle by intersecting two rectangles
|
|
|
|
*/
|
|
|
|
static Rect intersect(Rect r1, Rect r2) {
|
|
|
|
return Rect(Point<CT>(max(r1.x1(), r2.x1()), max(r1.y1(), r2.y1())),
|
|
|
|
Point<CT>(min(r1.x2(), r2.x2()), min(r1.y2(), r2.y2()))); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute compounding rectangle of two rectangles
|
|
|
|
*/
|
|
|
|
static Rect compound(Rect r1, Rect r2) {
|
|
|
|
return Rect(Point<CT>(min(r1.x1(), r2.x1()), min(r1.y1(), r2.y1())),
|
|
|
|
Point<CT>(max(r1.x2(), r2.x2()), max(r1.y2(), r2.y2()))); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cut out rectangle from rectangle
|
|
|
|
*
|
|
|
|
* \param r rectangle to cut out
|
|
|
|
*
|
|
|
|
* In the worst case (if we cut a hole into the rectangle) we get
|
|
|
|
* four valid resulting rectangles.
|
|
|
|
*/
|
|
|
|
void cut(Rect r, Rect *top, Rect *left, Rect *right, Rect *bottom) const
|
|
|
|
{
|
|
|
|
/* limit the cut-out area to the actual rectangle */
|
|
|
|
r = intersect(r, *this);
|
|
|
|
|
|
|
|
*top = Rect(Point<CT>(x1(), y1()), Point<CT>(x2(), r.y1() - 1));
|
|
|
|
*left = Rect(Point<CT>(x1(), r.y1()), Point<CT>(r.x1() - 1, r.y2()));
|
|
|
|
*right = Rect(Point<CT>(r.x2() + 1, r.y1()), Point<CT>(x2(), r.y2()));
|
|
|
|
*bottom = Rect(Point<CT>(x1(), r.y2() + 1), Point<CT>(x2(), y2()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return position of an area when centered within the rectangle
|
|
|
|
*/
|
|
|
|
Point<CT> center(Area<DT> area) const {
|
|
|
|
return Point<CT>(((CT)w() - (CT)area.w())/2,
|
|
|
|
((CT)h() - (CT)area.h())/2) + p1(); }
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Print rectangle coordinates
|
|
|
|
*
|
|
|
|
* The output has the form 'width' x 'height' +/- 'p1.x' +/- 'p1.y'.
|
|
|
|
* For example, a rectange of size 15x16 as position (-13, 14) is
|
|
|
|
* printed as "15x16-13+14"
|
|
|
|
*/
|
|
|
|
void print(Output &out) const { Genode::print(out, area(), p1()); }
|
2013-12-28 18:01:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__GEOMETRY_H_ */
|