From 29e7f695d5d162ecfacda2f4225d662986ce1e7a Mon Sep 17 00:00:00 2001 From: Tomas Pavlicek Date: Mon, 23 Sep 2019 09:27:17 +0200 Subject: [PATCH] Polish classes in Tools2D --- src/Base/Tools2D.cpp | 7 - src/Base/Tools2D.h | 349 ++++++++++++++++++++++++++++++------------- 2 files changed, 241 insertions(+), 115 deletions(-) diff --git a/src/Base/Tools2D.cpp b/src/Base/Tools2D.cpp index 1c8659f839..973469d36f 100644 --- a/src/Base/Tools2D.cpp +++ b/src/Base/Tools2D.cpp @@ -165,13 +165,6 @@ bool BoundBox2d::Intersect(const Polygon2d &rclPoly) const return false; } -bool BoundBox2d::Contains (const Vector2d &rclV) const -{ - return - (rclV.x >= MinX) && (rclV.x <= MaxX) && - (rclV.y >= MinY) && (rclV.y <= MaxY); -} - /********************************************************/ /** LINE2D **********************************************/ diff --git a/src/Base/Tools2D.h b/src/Base/Tools2D.h index 0ce86aa937..2fd39a9c7b 100644 --- a/src/Base/Tools2D.h +++ b/src/Base/Tools2D.h @@ -49,30 +49,46 @@ class BaseExport Vector2d public: double x, y; - inline Vector2d (void); - inline Vector2d (float x, float y); - inline Vector2d (double x, double y); - inline Vector2d (const Vector2d &rclVct); - - // methods - inline double Length (void) const; - inline double Distance(const Vector2d&) const; - inline bool IsEqual(const Vector2d&, double tolerance) const; + inline Vector2d(void); + inline Vector2d(float x, float y); + inline Vector2d(double x, double y); + inline Vector2d(const Vector2d &v); // operators - inline Vector2d& operator= (const Vector2d &rclVct); - inline double operator* (const Vector2d &rclVct) const; - inline bool operator== (const Vector2d &rclVct) const; - inline Vector2d operator+ (const Vector2d &rclVct) const; - inline Vector2d operator- (const Vector2d &rclVct) const; - inline Vector2d operator* (double c) const; - inline Vector2d operator/ (double c) const; + inline Vector2d& operator= (const Vector2d &v); + inline bool operator== (const Vector2d &v) const; + inline Vector2d operator+ (void) const; + inline Vector2d operator+ (const Vector2d &v) const; + inline Vector2d& operator+= (const Vector2d &v); + inline Vector2d operator- (void) const; + inline Vector2d operator- (const Vector2d &v) const; + inline Vector2d& operator-= (const Vector2d &v); + inline Vector2d operator* (double c) const; + inline Vector2d& operator*= (double c); + inline double operator* (const Vector2d &v) const; + inline Vector2d operator/ (double c) const; + inline Vector2d& operator/= (double c); - inline void Set (double fPX, double fPY); - inline void Scale (double fS); - inline void Normalize (void); - double GetAngle (const Vector2d &rclVect) const; - void ProjectToLine (const Vector2d &rclPt, const Vector2d &rclLine); + // methods + inline bool IsNull(double tolerance = 0.0) const; + inline double Length(void) const; + inline double Angle(void) const; + inline double Sqr(void) const; + + inline Vector2d& Set(double x, double y); + inline Vector2d& Negate(void); + inline Vector2d& Scale(double factor); + inline Vector2d& Rotate(double angle); + inline Vector2d& Normalize(void); + + inline Vector2d Perpendicular(bool clockwise = false) const; + static inline Vector2d FromPolar(double r, double fi); + + inline double Distance(const Vector2d &v) const; + inline bool IsEqual(const Vector2d &v, double tolerance = 0.0) const; + + double GetAngle(const Vector2d &v) const; + void ProjectToLine(const Vector2d &point, const Vector2d &line); }; /** BoundBox2d ********************************************/ @@ -94,20 +110,20 @@ public: // operators inline BoundBox2d& operator= (const BoundBox2d& rclBB); inline bool operator== (const BoundBox2d& rclBB) const; + + // methods + inline double Width(void) const; + inline double Height(void) const; + inline bool Contains(const Vector2d &v) const; + inline bool Contains(const Vector2d &v, double tolerance) const; + inline Vector2d GetCenter(void) const; + + inline void SetVoid(void); + inline void Add(const Vector2d &v); + bool Intersect(const Line2d &rclLine) const; bool Intersect(const BoundBox2d &rclBB) const; bool Intersect(const Polygon2d &rclPoly) const; - inline void Add(const Vector2d &rclVct); - - void SetVoid (void) { MinX = MinY = DOUBLE_MAX; MaxX = MaxY = -DOUBLE_MAX; } - - // misc - bool Contains (const Vector2d &rclV) const; - - inline Vector2d GetCenter() const { - return Vector2d((MinX+MaxX)/2,(MinY+MaxY)/2); - } - }; /** Line2d ********************************************/ @@ -175,106 +191,190 @@ private: /** INLINES ********************************************/ -inline Vector2d::Vector2d (void) +inline Vector2d::Vector2d(void) : x(0.0), y(0.0) { } -inline Vector2d::Vector2d (float x, float y) +inline Vector2d::Vector2d(float x, float y) : x(x), y(y) { } -inline Vector2d::Vector2d (double x, double y) +inline Vector2d::Vector2d(double x, double y) : x(x), y(y) { } -inline Vector2d::Vector2d (const Vector2d &rclVct) -: x(rclVct.x), y(rclVct.y) +inline Vector2d::Vector2d(const Vector2d &v) +: x(v.x), y(v.y) { } -inline double Vector2d::Length (void) const +inline Vector2d& Vector2d::operator= (const Vector2d &v) { - return sqrt ((x * x) + (y * y)); + x = v.x; + y = v.y; + return *this; +} + +inline bool Vector2d::operator== (const Vector2d &v) const +{ + return (x == v.x) && (y == v.y); +} + +inline Vector2d Vector2d::operator+ (void) const +{ + return Vector2d(x, y); +} + +inline Vector2d Vector2d::operator+ (const Vector2d &v) const +{ + return Vector2d(x + v.x, y + v.y); +} + +inline Vector2d& Vector2d::operator+= (const Vector2d &v) +{ + x += v.x; + y += v.y; + return *this; +} + +inline Vector2d Vector2d::operator- (void) const +{ + return Vector2d(-x, -y); +} + +inline Vector2d Vector2d::operator- (const Vector2d &v) const +{ + return Vector2d(x - v.x, y - v.y); +} + +inline Vector2d& Vector2d::operator-= (const Vector2d &v) +{ + x -= v.x; + y -= v.y; + return *this; +} + +inline Vector2d Vector2d::operator* (double c) const +{ + return Vector2d(c*x, c*y); +} + +inline Vector2d& Vector2d::operator*= (double c) +{ + x *= c; + y *= c; + return *this; +} + +inline double Vector2d::operator* (const Vector2d &v) const +{ + return x*v.x + y*v.y; +} + +inline Vector2d operator* (double c, const Vector2d &v) +{ + return Vector2d(c*v.x, c*v.y); +} + +inline Vector2d Vector2d::operator/ (double c) const +{ + return Vector2d(x/c, y/c); +} + +inline Vector2d& Vector2d::operator/= (double c) +{ + x /= c; + y /= c; + return *this; +} + +inline bool Vector2d::IsNull(double tolerance) const +{ + return x*x + y*y <= tolerance*tolerance; +} + +inline double Vector2d::Length(void) const +{ + return sqrt(x*x + y*y); +} + +inline double Vector2d::Angle(void) const +{ + return atan2(y, x); +} + +inline double Vector2d::Sqr(void) const +{ + return x*x + y*y; +} + +inline Vector2d& Vector2d::Set(double x, double y) +{ + this->x = x; + this->y = y; + return *this; +} + +inline Vector2d& Vector2d::Negate(void) +{ + x = -x; + y = -y; + return *this; +} + +inline Vector2d& Vector2d::Scale(double factor) +{ + x *= factor; + y *= factor; + return *this; +} + +inline Vector2d& Vector2d::Rotate(double angle) +{ + x = x*cos(angle) - y*sin(angle); + y = x*sin(angle) + y*cos(angle); + return *this; +} + +inline Vector2d& Vector2d::Normalize(void) +{ + double length = Length(); + if (length > 0.0) + { + x /= length; + y /= length; + } + + return *this; +} + +inline Vector2d Vector2d::Perpendicular(bool clockwise) const +{ + return clockwise ? Vector2d(y, -x) : Vector2d(-y, x); +} + +inline Vector2d Vector2d::FromPolar(double r, double fi) +{ + return Vector2d(r*cos(fi), r*sin(fi)); } inline double Vector2d::Distance(const Vector2d& v) const { double dx = (x - v.x); double dy = (y - v.y); + return sqrt(dx*dx + dy*dy); } inline bool Vector2d::IsEqual(const Vector2d& v, double tolerance) const { - return Distance (v) <= tolerance; + return Distance(v) <= tolerance; } -inline Vector2d& Vector2d::operator= (const Vector2d &rclVct) -{ - x = rclVct.x; - y = rclVct.y; - return *this; -} - -inline bool Vector2d::operator== (const Vector2d &rclVct) const -{ - return (x == rclVct.x) && (y == rclVct.y); -} - -inline Vector2d Vector2d::operator+ (const Vector2d &rclVct) const -{ - return Vector2d(x + rclVct.x, y + rclVct.y); -} - -inline Vector2d Vector2d::operator- (const Vector2d &rclVct) const -{ - return Vector2d(x - rclVct.x, y - rclVct.y); -} - -inline double Vector2d::operator* (const Vector2d &rclVct) const -{ - return (x * rclVct.x) + (y * rclVct.y); -} - -inline Vector2d operator* (double c, const Vector2d &rclVct) -{ - return Vector2d(c * rclVct.x, c * rclVct.y); -} - -inline Vector2d Vector2d::operator* (double c) const -{ - return Vector2d(c * x, c * y); -} - -inline Vector2d Vector2d::operator/ (double c) const -{ - return Vector2d(x / c, y / c); -} - -inline void Vector2d::Scale (double fS) -{ - x *= fS; - y *= fS; -} - -inline void Vector2d::Normalize (void) -{ - double fLen = Length(); - if (fLen != 0.0f) - { - x /= fLen; - y /= fLen; - } -} - -inline void Vector2d::Set (double fPX, double fPY) -{ - x = fPX; - y = fPY; -} +// ======================================== inline Polygon2d::Polygon2d (const Polygon2d &rclPoly) { @@ -408,12 +508,45 @@ inline bool BoundBox2d::operator== (const BoundBox2d& rclBB) const (MaxY == rclBB.MaxY); } -inline void BoundBox2d::Add(const Vector2d &rclVct) +inline double BoundBox2d::Width(void) const { - MinX = std::min(MinX, rclVct.x); - MinY = std::min(MinY, rclVct.y); - MaxX = std::max(MaxX, rclVct.x); - MaxY = std::max(MaxY, rclVct.y); + return MaxX - MinX; +} + +inline double BoundBox2d::Height(void) const +{ + return MaxY - MinY; +} + +inline bool BoundBox2d::Contains(const Vector2d &v) const +{ + return v.x >= MinX && v.x <= MaxX + && v.y >= MinY && v.y <= MaxY; +} + +inline bool BoundBox2d::Contains(const Vector2d &v, double tolerance) const +{ + return v.x >= MinX - tolerance && v.x <= MaxX + tolerance + && v.y >= MinY - tolerance && v.y <= MaxY + tolerance; +} + +inline Vector2d BoundBox2d::GetCenter(void) const +{ + return Vector2d((MinX + MaxX)*0.5, (MinY + MaxY)*0.5); +} + +inline void BoundBox2d::SetVoid(void) +{ + MinX = MinY = DOUBLE_MAX; + MaxX = MaxY = -DOUBLE_MAX; +} + +inline void BoundBox2d::Add(const Vector2d &v) +{ + MinX = std::min(MinX, v.x); + MinY = std::min(MinY, v.y); + MaxX = std::max(MaxX, v.x); + MaxY = std::max(MaxY, v.y); } } // namespace Base