From dc46fcb3c813e01d8a71d6292c5d7be4251ac077 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 18 Nov 2014 03:28:56 +0000 Subject: [PATCH] Add some more documentation and methods to Geometry classes --- dgl/Geometry.hpp | 126 ++++++++++++++++++++++++++++++++++--------- dgl/src/Geometry.cpp | 94 +++++++++++++++++++++++++------- 2 files changed, 178 insertions(+), 42 deletions(-) diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp index 117c0a93..e6d260d8 100644 --- a/dgl/Geometry.hpp +++ b/dgl/Geometry.hpp @@ -30,8 +30,12 @@ template class Triangle; template class Rectangle; // ----------------------------------------------------------------------- -// Point +/** + DGL Point class. + + This class describes a single point in space, defined by an X and Y value. + */ template class Point { @@ -62,17 +66,17 @@ public: const T& getY() const noexcept; /** - Set X value as @a x. + Set X value to @a x. */ void setX(const T& x) noexcept; /** - Set Y value as @a y. + Set Y value to @a y. */ void setY(const T& y) noexcept; /** - Set X and Y values as @a x and @a y respectively. + Set X and Y values to @a x and @a y respectively. */ void setPos(const T& x, const T& y) noexcept; @@ -96,6 +100,11 @@ public: */ bool isZero() const noexcept; + /** + Return true if point is not (0, 0). + */ + bool isNotZero() const noexcept; + Point operator+(const Point& pos) noexcept; Point operator-(const Point& pos) noexcept; Point& operator=(const Point& pos) noexcept; @@ -113,8 +122,12 @@ private: }; // ----------------------------------------------------------------------- -// Size +/** + DGL Size class. + + This class describes a size, defined by a width and height value. + */ template class Size { @@ -155,7 +168,7 @@ public: void setHeight(const T& height) noexcept; /** - Set size using @a width and @a height. + Set size to @a width and @a height. */ void setSize(const T& width, const T& height) noexcept; @@ -176,14 +189,27 @@ public: /** Return true if size is null (0x0). + An null size is also invalid. */ bool isNull() const noexcept; /** Return true if size is not null (0x0). + A non-null size is still invalid if its width or height is negative. */ bool isNotNull() const noexcept; + /** + Return true if size is valid (width and height are higher than zero). + */ + bool isValid() const noexcept; + + /** + Return true if size is invalid (width or height are lower or equal to zero). + An invalid size might not be null under some circumstances. + */ + bool isInvalid() const noexcept; + Size operator+(const Size& size) noexcept; Size operator-(const Size& size) noexcept; Size& operator=(const Size& size) noexcept; @@ -200,14 +226,18 @@ private: }; // ----------------------------------------------------------------------- -// Line +/** + DGL Line class. + + This class describes a line, defined by two points. + */ template class Line { public: /** - Constructor for a null line ([0, 0] to [0, 0]). + Constructor for a null line ([0,0] to [0,0]). */ Line() noexcept; @@ -217,7 +247,7 @@ public: Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept; /** - Constructor using custom start X, start Y, end pos values. + Constructor using custom start X, start Y and end pos values. */ Line(const T& startX, const T& startY, const Point& endPos) noexcept; @@ -267,17 +297,17 @@ public: const Point& getEndPos() const noexcept; /** - Set start X value as @a x. + Set start X value to @a x. */ void setStartX(const T& x) noexcept; /** - Set start Y value as @a y. + Set start Y value to @a y. */ void setStartY(const T& y) noexcept; /** - Set start X and Y values as @a x and @a y respectively. + Set start X and Y values to @a x and @a y respectively. */ void setStartPos(const T& x, const T& y) noexcept; @@ -287,17 +317,17 @@ public: void setStartPos(const Point& pos) noexcept; /** - Set end X value as @a x. + Set end X value to @a x. */ void setEndX(const T& x) noexcept; /** - Set end Y value as @a y. + Set end Y value to @a y. */ void setEndY(const T& y) noexcept; /** - Set end X and Y values as @a x and @a y respectively. + Set end X and Y values to @a x and @a y respectively. */ void setEndPos(const T& x, const T& y) noexcept; @@ -321,6 +351,16 @@ public: */ void draw(); + /** + Return true if line is null (start and end pos are equal). + */ + bool isNull() const noexcept; + + /** + Return true if line is not null (start and end pos are different). + */ + bool isNotNull() const noexcept; + Line& operator=(const Line& line) noexcept; bool operator==(const Line& line) const noexcept; bool operator!=(const Line& line) const noexcept; @@ -330,8 +370,15 @@ private: }; // ----------------------------------------------------------------------- -// Circle +/** + DGL Circle class. + + This class describes a circle, defined by position, size and a minimum of 3 segments. + + TODO: report if circle starts at top-left, bottom-right or center. + and size grows from which point? + */ template class Circle { @@ -372,17 +419,17 @@ public: const Point& getPos() const noexcept; /** - Set X value as @a x. + Set X value to @a x. */ void setX(const T& x) noexcept; /** - Set Y value as @a y. + Set Y value to @a y. */ void setY(const T& y) noexcept; /** - Set X and Y values as @a x and @a y respectively. + Set X and Y values to @a x and @a y respectively. */ void setPos(const T& x, const T& y) noexcept; @@ -435,12 +482,16 @@ private: // cached values float fTheta, fCos, fSin; - void _draw(const bool isOutline); + void _draw(const bool outline); }; // ----------------------------------------------------------------------- -// Triangle +/** + DGL Triangle class. + + This class describes a triangle, defined by 3 points. + */ template class Triangle { @@ -475,6 +526,29 @@ public: */ void drawOutline(); + /** + Return true if triangle is null (all its points are equal). + An null triangle is also invalid. + */ + bool isNull() const noexcept; + + /** + Return true if triangle is not null (one its points is different from the others). + A non-null triangle is still invalid if two of its points are equal. + */ + bool isNotNull() const noexcept; + + /** + Return true if triangle is valid (all its points are different). + */ + bool isValid() const noexcept; + + /** + Return true if triangle is invalid (one or two of its points are equal). + An invalid triangle might not be null under some circumstances. + */ + bool isInvalid() const noexcept; + Triangle& operator=(const Triangle& tri) noexcept; bool operator==(const Triangle& tri) const noexcept; bool operator!=(const Triangle& tri) const noexcept; @@ -482,12 +556,16 @@ public: private: Point fPos1, fPos2, fPos3; - void _draw(const bool isOutline); + void _draw(const bool outline); }; // ----------------------------------------------------------------------- -// Rectangle +/** + DGL Rectangle class. + + This class describes a rectangle, defined by a starting point and a size. + */ template class Rectangle { @@ -662,7 +740,7 @@ private: Point fPos; Size fSize; - void _draw(const bool isOutline); + void _draw(const bool outline); }; // ----------------------------------------------------------------------- diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp index a7565fae..9131cdd2 100644 --- a/dgl/src/Geometry.cpp +++ b/dgl/src/Geometry.cpp @@ -98,6 +98,12 @@ bool Point::isZero() const noexcept return fX == 0 && fY == 0; } +template +bool Point::isNotZero() const noexcept +{ + return fX != 0 || fY != 0; +} + template Point Point::operator+(const Point& pos) noexcept { @@ -228,6 +234,17 @@ bool Size::isNotNull() const noexcept return fWidth != 0 || fHeight != 0; } +template +bool Size::isValid() const noexcept +{ + return fWidth > 1 && fHeight > 1; +} + +template +bool Size::isInvalid() const noexcept +{ + return fWidth <= 0 || fHeight <= 0; +} template Size Size::operator+(const Size& size) noexcept @@ -427,16 +444,30 @@ void Line::moveBy(const Point& pos) noexcept template void Line::draw() { + DISTRHO_SAFE_ASSERT_RETURN(fPosStart != fPosEnd,); + glBegin(GL_LINES); { - glVertex2i(fPosStart.fX, fPosStart.fY); - glVertex2i(fPosEnd.fX, fPosEnd.fY); + glVertex2d(fPosStart.fX, fPosStart.fY); + glVertex2d(fPosEnd.fX, fPosEnd.fY); } glEnd(); } +template +bool Line::isNull() const noexcept +{ + return fPosStart == fPosEnd; +} + +template +bool Line::isNotNull() const noexcept +{ + return fPosStart != fPosEnd; +} + template Line& Line::operator=(const Line& line) noexcept { @@ -620,14 +651,13 @@ bool Circle::operator!=(const Circle& cir) const noexcept } template -void Circle::_draw(const bool isOutline) +void Circle::_draw(const bool outline) { - if (fNumSegments < 3 || fSize <= 0.0f) - return; + DISTRHO_SAFE_ASSERT_RETURN(fNumSegments >= 3 && fSize > 0.0f,); - float t, x = fSize, y = 0; + float t, x = fSize, y = 0.0f; - glBegin(isOutline ? GL_LINE_LOOP : GL_POLYGON); + glBegin(outline ? GL_LINE_LOOP : GL_POLYGON); for (uint i=0; i::drawOutline() _draw(true); } +template +bool Triangle::isNull() const noexcept +{ + return fPos1 == fPos2 && fPos1 == fPos3; +} + +template +bool Triangle::isNotNull() const noexcept +{ + return fPos1 != fPos2 || fPos1 != fPos3; +} + +template +bool Triangle::isValid() const noexcept +{ + return fPos1 != fPos2 && fPos1 != fPos3; +} + +template +bool Triangle::isInvalid() const noexcept +{ + return fPos1 == fPos2 || fPos1 == fPos3; +} + template Triangle& Triangle::operator=(const Triangle& tri) noexcept { @@ -702,14 +756,16 @@ bool Triangle::operator!=(const Triangle& tri) const noexcept } template -void Triangle::_draw(const bool isOutline) +void Triangle::_draw(const bool outline) { - glBegin(isOutline ? GL_LINE_LOOP : GL_TRIANGLES); + DISTRHO_SAFE_ASSERT_RETURN(fPos1 != fPos2 && fPos1 != fPos3,); + + glBegin(outline ? GL_LINE_LOOP : GL_TRIANGLES); { - glVertex2i(fPos1.fX, fPos1.fY); - glVertex2i(fPos2.fX, fPos2.fY); - glVertex2i(fPos3.fX, fPos3.fY); + glVertex2d(fPos1.fX, fPos1.fY); + glVertex2d(fPos2.fX, fPos2.fY); + glVertex2d(fPos3.fX, fPos3.fY); } glEnd(); @@ -943,22 +999,24 @@ bool Rectangle::operator!=(const Rectangle& rect) const noexcept } template -void Rectangle::_draw(const bool isOutline) +void Rectangle::_draw(const bool outline) { - glBegin(isOutline ? GL_LINE_LOOP : GL_QUADS); + DISTRHO_SAFE_ASSERT_RETURN(fSize.isValid(),); + + glBegin(outline ? GL_LINE_LOOP : GL_QUADS); { glTexCoord2f(0.0f, 0.0f); - glVertex2i(fPos.fX, fPos.fY); + glVertex2d(fPos.fX, fPos.fY); glTexCoord2f(1.0f, 0.0f); - glVertex2i(fPos.fX+fSize.fWidth, fPos.fY); + glVertex2d(fPos.fX+fSize.fWidth, fPos.fY); glTexCoord2f(1.0f, 1.0f); - glVertex2i(fPos.fX+fSize.fWidth, fPos.fY+fSize.fHeight); + glVertex2d(fPos.fX+fSize.fWidth, fPos.fY+fSize.fHeight); glTexCoord2f(0.0f, 1.0f); - glVertex2i(fPos.fX, fPos.fY+fSize.fHeight); + glVertex2d(fPos.fX, fPos.fY+fSize.fHeight); } glEnd();