start refactoring pigeometry.h
This commit is contained in:
@@ -1,10 +1,25 @@
|
|||||||
/*! @file pigeometry.h
|
/*! \file pigeometry.h
|
||||||
* @brief Geometry base class
|
* \ingroup Math
|
||||||
|
* \brief
|
||||||
|
* \~english Geometry base classes
|
||||||
|
* \~russian Базовые геометрические классы
|
||||||
|
* \~\details
|
||||||
|
* \~english
|
||||||
|
* Add \a PIPoint, \a PILine and \a PIRect classes.
|
||||||
|
* \~russian
|
||||||
|
* Содержит классы: \a PIPoint, \a PILine и \a PIRect.
|
||||||
|
* * \~\authors
|
||||||
|
* \~english
|
||||||
|
* Ivan Pelipenko peri4ko@yandex.ru;
|
||||||
|
* Andrey Bychkov work.a.b@yandex.ru;
|
||||||
|
* \~russian
|
||||||
|
* Иван Пелипенко peri4ko@yandex.ru;
|
||||||
|
* Андрей Бычков work.a.b@yandex.ru;
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
PIP - Platform Independent Primitives
|
PIP - Platform Independent Primitives
|
||||||
Geometry
|
Geometry base classes
|
||||||
Ivan Pelipenko peri4ko@yandex.ru
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -19,130 +34,11 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef PIGEOMETRY_H
|
#ifndef PIGEOMETRY_H
|
||||||
#define PIGEOMETRY_H
|
#define PIGEOMETRY_H
|
||||||
|
|
||||||
#include "pimathbase.h"
|
#include "pipoint.h"
|
||||||
|
#include "piline.h"
|
||||||
template<typename Type>
|
#include "pirect.h"
|
||||||
class PIP_EXPORT PIPoint {
|
|
||||||
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
|
||||||
public:
|
|
||||||
Type x;
|
|
||||||
Type y;
|
|
||||||
|
|
||||||
PIPoint() {x = y = Type();}
|
|
||||||
PIPoint(Type x_, Type y_) {set(x_, y_);}
|
|
||||||
|
|
||||||
PIPoint<Type> & set(Type x_, Type y_) {x = x_; y = y_; return *this;}
|
|
||||||
PIPoint<Type> & move(Type x_, Type y_) {x += x_; y += y_; return *this;}
|
|
||||||
PIPoint<Type> & move(const PIPoint<Type> & p) {x += p.x; y += p.y; return *this;}
|
|
||||||
double angleRad() const {return atan2(y, x);}
|
|
||||||
int angleDeg() const {return round(atan2(y, x) * rad2deg);}
|
|
||||||
PIPoint<Type> toPolar(bool isDeg = false) const {return PIPoint<Type>(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());}
|
|
||||||
static PIPoint<Type> fromPolar(const PIPoint<Type> & p) {return PIPoint<Type>(p.y * cos(p.x), p.y * sin(p.x));}
|
|
||||||
|
|
||||||
PIPoint<Type> operator +(const PIPoint<Type> & p) {return PIPoint<Type>(x + p.x, y + p.y);}
|
|
||||||
PIPoint<Type> operator +(const Type & p) {return PIPoint<Type>(x + p, y + p);}
|
|
||||||
PIPoint<Type> operator -(const PIPoint<Type> & p) {return PIPoint<Type>(x - p.x, y - p.y);}
|
|
||||||
PIPoint<Type> operator -(const Type & p) {return PIPoint<Type>(x - p, y - p);}
|
|
||||||
PIPoint<Type> operator -() {return PIPoint<Type>(-x, -y);}
|
|
||||||
PIPoint<Type> operator *(const Type & d) {return PIPoint<Type>(x * d, y * d);}
|
|
||||||
PIPoint<Type> operator /(const Type & d) {return PIPoint<Type>(x / d, y / d);}
|
|
||||||
bool operator ==(const PIPoint<Type> & p) const {return (x == p.x && y == p.y);}
|
|
||||||
bool operator !=(const PIPoint<Type> & p) const {return (x != p.x || y != p.y);}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
PICout operator <<(PICout & s, const PIPoint<Type> & v) {s.setControl(0, true); s << "Point{" << v.x << ", " << v.y << "}"; s.restoreControl(); return s;}
|
|
||||||
|
|
||||||
|
|
||||||
typedef PIPoint<int> PIPointi;
|
|
||||||
typedef PIPoint<uint> PIPointu;
|
|
||||||
typedef PIPoint<float> PIPointf;
|
|
||||||
typedef PIPoint<double> PIPointd;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
class PIP_EXPORT PIRect {
|
|
||||||
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
|
||||||
public:
|
|
||||||
Type x0;
|
|
||||||
Type y0;
|
|
||||||
Type x1;
|
|
||||||
Type y1;
|
|
||||||
|
|
||||||
PIRect() {x0 = y0 = x1 = y1 = Type();}
|
|
||||||
PIRect(Type x, Type y, Type w, Type h) {set(x, y, w, h);}
|
|
||||||
PIRect(const PIPoint<Type> & tl, const PIPoint<Type> & br) {set(tl.x, tl.y, br.x, br.y);}
|
|
||||||
PIRect(const PIPoint<Type> & p0, const PIPoint<Type> & p1, const PIPoint<Type> & p2) {set(piMin<Type>(p0.x, p1.x, p2.x), piMin<Type>(p0.y, p1.y, p2.y),
|
|
||||||
piMax<Type>(p0.x, p1.x, p2.x), piMax<Type>(p0.y, p1.y, p2.y));}
|
|
||||||
|
|
||||||
PIRect<Type> & set(Type x, Type y, Type w, Type h) {x0 = x; y0 = y; x1 = x + w; y1 = y + h; return *this;}
|
|
||||||
bool pointIn(Type x, Type y) const {return (x <= x1 && x >= x0 && y <= y1 && y >= y0);}
|
|
||||||
bool pointIn(const PIPoint<Type> & p) const {return pointIn(p.x, p.y);}
|
|
||||||
bool isEmpty() const {return (x1 - x0 == 0 && y1 - y0 == 0);}
|
|
||||||
PIRect<Type> & translate(Type x, Type y) {x0 += x; x1 += x; y0 += y; y1 += y; return *this;}
|
|
||||||
PIRect<Type> & translate(const PIPoint<Type> & p) {x0 += p.x; x1 += p.x; y0 += p.y; y1 += p.y; return *this;}
|
|
||||||
PIRect<Type> translated(Type x, Type y) {PIRect<Type> r(*this); r.translate(x, y); return r;}
|
|
||||||
PIRect<Type> translated(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.translate(p); return r;}
|
|
||||||
PIRect<Type> & scale(Type x, Type y) {setWidth(width() * x); setHeight(height() * y); return *this;}
|
|
||||||
PIRect<Type> & scale(const PIPoint<Type> & p) {setWidth(width() * p.x); setHeight(height() * p.y); return *this;}
|
|
||||||
PIRect<Type> scaled(Type x, Type y) {PIRect<Type> r(*this); r.scale(x, y); return r;}
|
|
||||||
PIRect<Type> scaled(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.scale(p); return r;}
|
|
||||||
PIRect<Type> & normalize() {if (x0 > x1) piSwap<Type>(x0, x1); if (y0 > y1) piSwap<Type>(y0, y1); return *this;}
|
|
||||||
PIRect<Type> normalized() {PIRect<Type> r(*this); r.normalize(); return r;}
|
|
||||||
PIRect<Type> & unite(const PIRect<Type> & r) {x0 = piMin<Type>(x0, r.x0); y0 = piMin<Type>(y0, r.y0); x1 = piMax<Type>(x1, r.x1); y1 = piMax<Type>(y1, r.y1); return *this;}
|
|
||||||
PIRect<Type> united(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.unite(rect); return r;}
|
|
||||||
PIRect<Type> & intersect(const PIRect<Type> & r) {x0 = piMax<Type>(x0, r.x0); y0 = piMax<Type>(y0, r.y0); x1 = piMin<Type>(x1, r.x1); y1 = piMin<Type>(y1, r.y1); if (x0 > x1 || y0 > y1) x0 = x1 = y0 = y1 = Type(0); return *this;}
|
|
||||||
PIRect<Type> intersected(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.intersect(rect); return r;}
|
|
||||||
Type top() const {return y0;}
|
|
||||||
Type left() const {return x0;}
|
|
||||||
Type right() const {return x1;}
|
|
||||||
Type bottom() const {return y1;}
|
|
||||||
Type width() const {return x1 - x0;}
|
|
||||||
Type height() const {return y1 - y0;}
|
|
||||||
PIPoint<Type> topLeft() {return PIPoint<Type>(x0, y0);}
|
|
||||||
PIPoint<Type> topRigth() {return PIPoint<Type>(x1, y0);}
|
|
||||||
PIPoint<Type> bottomLeft() {return PIPoint<Type>(x0, y1);}
|
|
||||||
PIPoint<Type> bottomRight() {return PIPoint<Type>(x1, y1);}
|
|
||||||
void setTop(Type v) {y0 = v;}
|
|
||||||
void setLeft(Type v) {x0 = v;}
|
|
||||||
void setRigth(Type v) {x1 = v;}
|
|
||||||
void setBottom(Type v) {y1 = v;}
|
|
||||||
void setWidth(Type v) {x1 = x0 + v;}
|
|
||||||
void setHeight(Type v) {y1 = y0 + v;}
|
|
||||||
|
|
||||||
PIRect<Type> operator -() {return PIRect<Type>(-x0, -y0, -width(), -height());}
|
|
||||||
void operator +=(Type x) {translate(x, x);}
|
|
||||||
void operator +=(const PIPoint<Type> & p) {translate(p);}
|
|
||||||
void operator -=(Type x) {translate(-x, -x);}
|
|
||||||
void operator -=(const PIPoint<Type> & p) {translate(-p);}
|
|
||||||
void operator *=(Type p) {x0 *= p; x1 *= p; y0 *= p; y1 *= p;}
|
|
||||||
void operator /=(Type p) {x0 /= p; x1 /= p; y0 /= p; y1 /= p;}
|
|
||||||
void operator |=(const PIRect<Type> & r) {unite(r);}
|
|
||||||
void operator &=(const PIRect<Type> & r) {intersect(r);}
|
|
||||||
PIRect<Type> operator +(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(p);}
|
|
||||||
PIRect<Type> operator -(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(-p);}
|
|
||||||
PIRect<Type> operator |(const PIRect<Type> & r) {return PIRect<Type>(*this).united(r);}
|
|
||||||
PIRect<Type> operator &(const PIRect<Type> & r) {return PIRect<Type>(*this).intersected(r);}
|
|
||||||
bool operator ==(const PIRect<Type> & r) const {return (x0 == r.x0 && y0 == r.y0 && x1 == r.x1 && y1 == r.y10);}
|
|
||||||
bool operator !=(const PIRect<Type> & r) const {return (x0 != r.x0 || y0 != r.y0 || x1 != r.x1 || y1 != r.y10);}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
PICout operator <<(PICout & s, const PIRect<Type> & v) {s.setControl(0, true); s << "Rect{" << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << "}"; s.restoreControl(); return s;}
|
|
||||||
|
|
||||||
|
|
||||||
typedef PIRect<int> PIRecti;
|
|
||||||
typedef PIRect<uint> PIRectu;
|
|
||||||
typedef PIRect<float> PIRectf;
|
|
||||||
typedef PIRect<double> PIRectd;
|
|
||||||
|
|
||||||
#endif // PIGEOMETRY_H
|
#endif // PIGEOMETRY_H
|
||||||
|
|||||||
48
libs/main/math/piline.h
Normal file
48
libs/main/math/piline.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*! \file pirect.h
|
||||||
|
* \ingroup Math
|
||||||
|
* \brief
|
||||||
|
* \~english Two-dimensional line class
|
||||||
|
* \~russian Класс отрезка двумерной линии
|
||||||
|
* * \~\authors
|
||||||
|
* \~english
|
||||||
|
* Ivan Pelipenko peri4ko@yandex.ru;
|
||||||
|
* Andrey Bychkov work.a.b@yandex.ru;
|
||||||
|
* \~russian
|
||||||
|
* Иван Пелипенко peri4ko@yandex.ru;
|
||||||
|
* Андрей Бычков work.a.b@yandex.ru;
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
Two-dimensional line class
|
||||||
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifndef PILINE_H
|
||||||
|
#define PILINE_H
|
||||||
|
|
||||||
|
#include "pipoint.h"
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
class PIP_EXPORT PILine {
|
||||||
|
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||||
|
public:
|
||||||
|
PIPoint p0;
|
||||||
|
PIPoint p1;
|
||||||
|
|
||||||
|
PILine() {}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PILINE_H
|
||||||
@@ -471,5 +471,5 @@ double randomn(double dv, double sv) {
|
|||||||
|
|
||||||
|
|
||||||
double randomd() {
|
double randomd() {
|
||||||
return (double) randomi() / RAND_MAX * 2. - 1.;
|
return (double) rand() / RAND_MAX * 2. - 1.;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
/*! @file pimathbase.h
|
/*! \file pimathbase.h
|
||||||
* @brief Basic mathematical functions and defines
|
* \defgroup Math
|
||||||
|
* \brief
|
||||||
|
* \~english Basic mathematical functions and defines
|
||||||
|
* \~russian Базовые математические функции и дефайны
|
||||||
|
* \~\authors
|
||||||
|
* \~english
|
||||||
|
* Ivan Pelipenko peri4ko@yandex.ru;
|
||||||
|
* Andrey Bychkov work.a.b@yandex.ru;
|
||||||
|
* \~russian
|
||||||
|
* Иван Пелипенко peri4ko@yandex.ru;
|
||||||
|
* Андрей Бычков work.a.b@yandex.ru;
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
PIP - Platform Independent Primitives
|
PIP - Platform Independent Primitives
|
||||||
Basic mathematical functions and defines
|
Basic mathematical functions and defines
|
||||||
Ivan Pelipenko peri4ko@yandex.ru
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
|||||||
101
libs/main/math/pipoint.h
Normal file
101
libs/main/math/pipoint.h
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
/*! \file pipoint.h
|
||||||
|
* \ingroup Math
|
||||||
|
* \brief
|
||||||
|
* \~english Two-dimensional point class
|
||||||
|
* \~russian Класс двумерной точки
|
||||||
|
* \~\authors
|
||||||
|
* \~english
|
||||||
|
* Ivan Pelipenko peri4ko@yandex.ru;
|
||||||
|
* Andrey Bychkov work.a.b@yandex.ru;
|
||||||
|
* \~russian
|
||||||
|
* Иван Пелипенко peri4ko@yandex.ru;
|
||||||
|
* Андрей Бычков work.a.b@yandex.ru;
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
Two-dimensional point class
|
||||||
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifndef PIPOINT_H
|
||||||
|
#define PIPOINT_H
|
||||||
|
|
||||||
|
#include "pimathbase.h"
|
||||||
|
|
||||||
|
/*! \brief
|
||||||
|
* \~english Two-dimensional point class
|
||||||
|
* \~russian Класс двумерной точки
|
||||||
|
*/
|
||||||
|
template<typename Type>
|
||||||
|
class PIP_EXPORT PIPoint {
|
||||||
|
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||||
|
public:
|
||||||
|
Type x;
|
||||||
|
Type y;
|
||||||
|
|
||||||
|
PIPoint() {x = y = Type();}
|
||||||
|
PIPoint(Type x_, Type y_) {set(x_, y_);}
|
||||||
|
|
||||||
|
PIPoint<Type> & set(Type x_, Type y_) {
|
||||||
|
x = x_;
|
||||||
|
y = y_;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIPoint<Type> & set(const PIPoint<Type> & p) {
|
||||||
|
x = p.x;
|
||||||
|
y = p.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIPoint<Type> & translate(Type x_, Type y_) {
|
||||||
|
x += x_;
|
||||||
|
y += y_;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIPoint<Type> & translate(const PIPoint<Type> & p) {
|
||||||
|
x += p.x;
|
||||||
|
y += p.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIPoint<Type> & move(Type x_, Type y_) {return translate(x_, y_);}
|
||||||
|
PIPoint<Type> & move(const PIPoint<Type> & p) {return translate(p);}
|
||||||
|
double angleRad() const {return atan2(y, x);}
|
||||||
|
double angleDeg() const {return toDeg(atan2(y, x));}
|
||||||
|
PIPoint<Type> toPolar(bool isDeg = false) const {return PIPoint<Type>(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());}
|
||||||
|
static PIPoint<Type> fromPolar(const PIPoint<Type> & p) {return PIPoint<Type>(p.y * cos(p.x), p.y * sin(p.x));}
|
||||||
|
|
||||||
|
void operator +=(const PIPoint<Type> & p) {translate(p);}
|
||||||
|
PIPoint<Type> operator +(const PIPoint<Type> & p) {return PIPoint<Type>(x + p.x, y + p.y);}
|
||||||
|
PIPoint<Type> operator +(const Type & p) {return PIPoint<Type>(x + p, y + p);}
|
||||||
|
PIPoint<Type> operator -(const PIPoint<Type> & p) {return PIPoint<Type>(x - p.x, y - p.y);}
|
||||||
|
PIPoint<Type> operator -(const Type & p) {return PIPoint<Type>(x - p, y - p);}
|
||||||
|
PIPoint<Type> operator -() {return PIPoint<Type>(-x, -y);}
|
||||||
|
bool operator ==(const PIPoint<Type> & p) const {return (x == p.x && y == p.y);}
|
||||||
|
bool operator !=(const PIPoint<Type> & p) const {return (x != p.x || y != p.y);}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
PICout operator <<(PICout & s, const PIPoint<Type> & v) {
|
||||||
|
s.setControl(0, true);
|
||||||
|
s << "Point{" << v.x << ", " << v.y << "}";
|
||||||
|
s.restoreControl();
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
typedef PIPoint<int> PIPointi;
|
||||||
|
typedef PIPoint<uint> PIPointu;
|
||||||
|
typedef PIPoint<float> PIPointf;
|
||||||
|
typedef PIPoint<double> PIPointd;
|
||||||
|
|
||||||
|
#endif // PIPOINT_H
|
||||||
196
libs/main/math/pirect.h
Normal file
196
libs/main/math/pirect.h
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
/*! \file pirect.h
|
||||||
|
* \ingroup Math
|
||||||
|
* \brief
|
||||||
|
* \~english Rect class
|
||||||
|
* \~russian Класс прямоугольника
|
||||||
|
* * \~\authors
|
||||||
|
* \~english
|
||||||
|
* Ivan Pelipenko peri4ko@yandex.ru;
|
||||||
|
* Andrey Bychkov work.a.b@yandex.ru;
|
||||||
|
* \~russian
|
||||||
|
* Иван Пелипенко peri4ko@yandex.ru;
|
||||||
|
* Андрей Бычков work.a.b@yandex.ru;
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
Rect class
|
||||||
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifndef PIRECT_H
|
||||||
|
#define PIRECT_H
|
||||||
|
|
||||||
|
#include "pipoint.h"
|
||||||
|
|
||||||
|
/*! \brief
|
||||||
|
* \~english Rect class
|
||||||
|
* \~russian Класс прямоугольника
|
||||||
|
*/
|
||||||
|
template<typename Type>
|
||||||
|
class PIP_EXPORT PIRect {
|
||||||
|
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||||
|
public:
|
||||||
|
PIRect() {}
|
||||||
|
PIRect(Type x, Type y, Type w, Type h) {
|
||||||
|
set(x, y, w, h);
|
||||||
|
normalize();
|
||||||
|
}
|
||||||
|
PIRect(const PIPoint<Type> & top_left, const PIPoint<Type> & bottom_right) {
|
||||||
|
tl = top_left;
|
||||||
|
br = bottom_right;
|
||||||
|
normalize();
|
||||||
|
}
|
||||||
|
// PIRect(const PIPoint<Type> & p0, const PIPoint<Type> & p1, const PIPoint<Type> & p2) {
|
||||||
|
// set(piMin<Type>(p0.x, p1.x, p2.x), piMin<Type>(p0.y, p1.y, p2.y),
|
||||||
|
// piMax<Type>(p0.x, p1.x, p2.x), piMax<Type>(p0.y, p1.y, p2.y));
|
||||||
|
// }
|
||||||
|
PIRect<Type> & set(Type x, Type y, Type w, Type h) {
|
||||||
|
tl = PIPoint<Type>(x, y);
|
||||||
|
br = PIPoint<Type>(x + w, y + h);
|
||||||
|
return normalize();
|
||||||
|
}
|
||||||
|
PIRect<Type> & set(const PIPoint<Type> & top_left, const PIPoint<Type> & bottom_right) {
|
||||||
|
tl = top_left;
|
||||||
|
br = bottom_right;
|
||||||
|
return normalize();
|
||||||
|
}
|
||||||
|
bool pointIn(Type x, Type y) const {
|
||||||
|
return (x <= br.x && x >= tl.x && y <= br.y && y >= tl.y);
|
||||||
|
}
|
||||||
|
bool pointIn(const PIPoint<Type> & p) const {
|
||||||
|
return pointIn(p.x, p.y);
|
||||||
|
}
|
||||||
|
bool isEmpty() const {
|
||||||
|
return (width() == 0 && height() == 0);
|
||||||
|
}
|
||||||
|
PIRect<Type> & translate(Type x, Type y) {
|
||||||
|
tl.translate(x, y);
|
||||||
|
br.translate(x, y);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIRect<Type> & translate(const PIPoint<Type> & p) {
|
||||||
|
tl.translate(p);
|
||||||
|
br.translate(p);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIRect<Type> translated(Type x, Type y) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.translate(x, y);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> translated(const PIPoint<Type> & p) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.translate(p);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> & scale(Type x, Type y) {
|
||||||
|
setWidth(width() * x);
|
||||||
|
setHeight(height() * y);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIRect<Type> & scale(Type s) {return scale(s, s);}
|
||||||
|
PIRect<Type> & scale(const PIPoint<Type> & p) {return scale(p.x, p.y);}
|
||||||
|
PIRect<Type> scaled(Type x, Type y) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.scale(x, y);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> scaled(Type s) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.scale(s);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> scaled(const PIPoint<Type> & p) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.scale(p);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> & normalize() {
|
||||||
|
if (tl.x > br.x) piSwap<Type>(tl.x, br.x);
|
||||||
|
if (tl.y > br.y) piSwap<Type>(tl.y, br.y);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
PIRect<Type> normalized() const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.normalize();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> & unite(const PIRect<Type> & r) {
|
||||||
|
tl.x = piMax<Type>(tl.x, r.left());
|
||||||
|
tl.y = piMax<Type>(tl.y, r.top());
|
||||||
|
br.x = piMin<Type>(br.x, r.right());
|
||||||
|
br.y = piMin<Type>(br.y, r.bottom());
|
||||||
|
return normalize();
|
||||||
|
}
|
||||||
|
PIRect<Type> united(const PIRect<Type> & rect) const {
|
||||||
|
PIRect<Type> r(*this);
|
||||||
|
r.unite(rect);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
PIRect<Type> & intersect(const PIRect<Type> & r) {x0 = piMax<Type>(x0, r.x0); y0 = piMax<Type>(y0, r.y0); x1 = piMin<Type>(x1, r.x1); y1 = piMin<Type>(y1, r.y1); if (x0 > x1 || y0 > y1) x0 = x1 = y0 = y1 = Type(0); return *this;}
|
||||||
|
PIRect<Type> intersected(const PIRect<Type> & rect) const {PIRect<Type> r(*this); r.intersect(rect); return r;}
|
||||||
|
Type top() const {return tl.y;}
|
||||||
|
Type left() const {return x0;}
|
||||||
|
Type right() const {return x1;}
|
||||||
|
Type bottom() const {return y1;}
|
||||||
|
Type width() const {return x1 - x0;}
|
||||||
|
Type height() const {return y1 - y0;}
|
||||||
|
PIPoint<Type> topLeft() const {return tl;}
|
||||||
|
PIPoint<Type> topRigth() const {return PIPoint<Type>(p1.x, p0.y);}
|
||||||
|
PIPoint<Type> bottomLeft() const {return PIPoint<Type>(x0, y1);}
|
||||||
|
PIPoint<Type> bottomRight() const {return br;}
|
||||||
|
void setTop(Type v) {y0 = v;}
|
||||||
|
void setLeft(Type v) {x0 = v;}
|
||||||
|
void setRigth(Type v) {x1 = v;}
|
||||||
|
void setBottom(Type v) {y1 = v;}
|
||||||
|
void setWidth(Type v) {x1 = x0 + v;}
|
||||||
|
void setHeight(Type v) {y1 = y0 + v;}
|
||||||
|
void setTopLeft(const PIPoint<Type> & p) {p0 = p;}
|
||||||
|
void bottomRight(const PIPoint<Type> & p) {p0 = p;}
|
||||||
|
|
||||||
|
PIRect<Type> operator -() {return PIRect<Type>(-x0, -y0, -width(), -height());}
|
||||||
|
void operator +=(Type x) {translate(x, x);}
|
||||||
|
void operator +=(const PIPoint<Type> & p) {translate(p);}
|
||||||
|
void operator -=(Type x) {translate(-x, -x);}
|
||||||
|
void operator -=(const PIPoint<Type> & p) {translate(-p);}
|
||||||
|
void operator |=(const PIRect<Type> & r) {unite(r);}
|
||||||
|
void operator &=(const PIRect<Type> & r) {intersect(r);}
|
||||||
|
PIRect<Type> operator +(const PIPoint<Type> & p) {return translated(p);}
|
||||||
|
PIRect<Type> operator -(const PIPoint<Type> & p) {return translated(-p);}
|
||||||
|
PIRect<Type> operator |(const PIRect<Type> & r) {return united(r);}
|
||||||
|
PIRect<Type> operator &(const PIRect<Type> & r) {return intersected(r);}
|
||||||
|
bool operator ==(const PIRect<Type> & r) const {return (x0 == r.x0 && y0 == r.y0 && x1 == r.x1 && y1 == r.y10);}
|
||||||
|
bool operator !=(const PIRect<Type> & r) const {return (x0 != r.x0 || y0 != r.y0 || x1 != r.x1 || y1 != r.y10);}
|
||||||
|
|
||||||
|
private:
|
||||||
|
PIPoint<Type> tl;
|
||||||
|
PIPoint<Type> br;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
PICout operator <<(PICout & s, const PIRect<Type> & v) {
|
||||||
|
s.setControl(0, true);
|
||||||
|
s << "Rect{" << v.topLeft() << "," << v.width() << "x" << v.height() << "}";
|
||||||
|
s.restoreControl();
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
typedef PIRect<int> PIRecti;
|
||||||
|
typedef PIRect<uint> PIRectu;
|
||||||
|
typedef PIRect<float> PIRectf;
|
||||||
|
typedef PIRect<double> PIRectd;
|
||||||
|
|
||||||
|
#endif // PIRECT_H
|
||||||
Reference in New Issue
Block a user