|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750 |
- /*
- * DISTRHO Plugin Framework (DPF)
- * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
- *
- * Permission to use, copy, modify, and/or distribute this software for any purpose with
- * or without fee is hereby granted, provided that the above copyright notice and this
- * permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
- * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
- * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
- * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
- #ifndef DGL_GEOMETRY_HPP_INCLUDED
- #define DGL_GEOMETRY_HPP_INCLUDED
-
- #include "Base.hpp"
-
- START_NAMESPACE_DGL
-
- // -----------------------------------------------------------------------
- // Forward class names
-
- template<typename> class Line;
- template<typename> class Circle;
- template<typename> class Triangle;
- template<typename> class Rectangle;
-
- // -----------------------------------------------------------------------
-
- /**
- DGL Point class.
-
- This class describes a single point in space, defined by an X and Y value.
- */
- template<typename T>
- class Point
- {
- public:
- /**
- Constructor for (0, 0) point.
- */
- Point() noexcept;
-
- /**
- Constructor using custom X and Y values.
- */
- Point(const T& x, const T& y) noexcept;
-
- /**
- Constructor using another Point class values.
- */
- Point(const Point<T>& pos) noexcept;
-
- /**
- Get X value.
- */
- const T& getX() const noexcept;
-
- /**
- Get Y value.
- */
- const T& getY() const noexcept;
-
- /**
- Set X value to @a x.
- */
- void setX(const T& x) noexcept;
-
- /**
- Set Y value to @a y.
- */
- void setY(const T& y) noexcept;
-
- /**
- Set X and Y values to @a x and @a y respectively.
- */
- void setPos(const T& x, const T& y) noexcept;
-
- /**
- Set X and Y values according to @a pos.
- */
- void setPos(const Point<T>& pos) noexcept;
-
- /**
- Move this point by @a x and @a y values.
- */
- void moveBy(const T& x, const T& y) noexcept;
-
- /**
- Move this point by @a pos.
- */
- void moveBy(const Point<T>& pos) noexcept;
-
- /**
- Return true if point is (0, 0).
- */
- bool isZero() const noexcept;
-
- /**
- Return true if point is not (0, 0).
- */
- bool isNotZero() const noexcept;
-
- Point<T> operator+(const Point<T>& pos) noexcept;
- Point<T> operator-(const Point<T>& pos) noexcept;
- Point<T>& operator=(const Point<T>& pos) noexcept;
- Point<T>& operator+=(const Point<T>& pos) noexcept;
- Point<T>& operator-=(const Point<T>& pos) noexcept;
- bool operator==(const Point<T>& pos) const noexcept;
- bool operator!=(const Point<T>& pos) const noexcept;
-
- private:
- T fX, fY;
- template<typename> friend class Line;
- template<typename> friend class Circle;
- template<typename> friend class Triangle;
- template<typename> friend class Rectangle;
- };
-
- // -----------------------------------------------------------------------
-
- /**
- DGL Size class.
-
- This class describes a size, defined by a width and height value.
- */
- template<typename T>
- class Size
- {
- public:
- /**
- Constructor for null size (0x0).
- */
- Size() noexcept;
-
- /**
- Constructor using custom width and height values.
- */
- Size(const T& width, const T& height) noexcept;
-
- /**
- Constructor using another Size class values.
- */
- Size(const Size<T>& size) noexcept;
-
- /**
- Get width.
- */
- const T& getWidth() const noexcept;
-
- /**
- Get height.
- */
- const T& getHeight() const noexcept;
-
- /**
- Set width.
- */
- void setWidth(const T& width) noexcept;
-
- /**
- Set height.
- */
- void setHeight(const T& height) noexcept;
-
- /**
- Set size to @a width and @a height.
- */
- void setSize(const T& width, const T& height) noexcept;
-
- /**
- Set size.
- */
- void setSize(const Size<T>& size) noexcept;
-
- /**
- Grow size by @a multiplier.
- */
- void growBy(double multiplier) noexcept;
-
- /**
- Shrink size by @a divider.
- */
- void shrinkBy(double divider) noexcept;
-
- /**
- 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<T> operator+(const Size<T>& size) noexcept;
- Size<T> operator-(const Size<T>& size) noexcept;
- Size<T>& operator=(const Size<T>& size) noexcept;
- Size<T>& operator+=(const Size<T>& size) noexcept;
- Size<T>& operator-=(const Size<T>& size) noexcept;
- Size<T>& operator*=(double m) noexcept;
- Size<T>& operator/=(double d) noexcept;
- bool operator==(const Size<T>& size) const noexcept;
- bool operator!=(const Size<T>& size) const noexcept;
-
- private:
- T fWidth, fHeight;
- template<typename> friend class Rectangle;
- };
-
- // -----------------------------------------------------------------------
-
- /**
- DGL Line class.
-
- This class describes a line, defined by two points.
- */
- template<typename T>
- class Line
- {
- public:
- /**
- Constructor for a null line ([0,0] to [0,0]).
- */
- Line() noexcept;
-
- /**
- Constructor using custom start X, start Y, end X and end Y values.
- */
- Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept;
-
- /**
- Constructor using custom start X, start Y and end pos values.
- */
- Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept;
-
- /**
- Constructor using custom start pos, end X and end Y values.
- */
- Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept;
-
- /**
- Constructor using custom start and end pos values.
- */
- Line(const Point<T>& startPos, const Point<T>& endPos) noexcept;
-
- /**
- Constructor using another Line class values.
- */
- Line(const Line<T>& line) noexcept;
-
- /**
- Get start X value.
- */
- const T& getStartX() const noexcept;
-
- /**
- Get start Y value.
- */
- const T& getStartY() const noexcept;
-
- /**
- Get end X value.
- */
- const T& getEndX() const noexcept;
-
- /**
- Get end Y value.
- */
- const T& getEndY() const noexcept;
-
- /**
- Get start position.
- */
- const Point<T>& getStartPos() const noexcept;
-
- /**
- Get end position.
- */
- const Point<T>& getEndPos() const noexcept;
-
- /**
- Set start X value to @a x.
- */
- void setStartX(const T& x) noexcept;
-
- /**
- Set start Y value to @a y.
- */
- void setStartY(const T& y) noexcept;
-
- /**
- Set start X and Y values to @a x and @a y respectively.
- */
- void setStartPos(const T& x, const T& y) noexcept;
-
- /**
- Set start X and Y values according to @a pos.
- */
- void setStartPos(const Point<T>& pos) noexcept;
-
- /**
- Set end X value to @a x.
- */
- void setEndX(const T& x) noexcept;
-
- /**
- Set end Y value to @a y.
- */
- void setEndY(const T& y) noexcept;
-
- /**
- Set end X and Y values to @a x and @a y respectively.
- */
- void setEndPos(const T& x, const T& y) noexcept;
-
- /**
- Set end X and Y values according to @a pos.
- */
- void setEndPos(const Point<T>& pos) noexcept;
-
- /**
- Move this line by @a x and @a y values.
- */
- void moveBy(const T& x, const T& y) noexcept;
-
- /**
- Move this line by @a pos.
- */
- void moveBy(const Point<T>& pos) noexcept;
-
- /**
- Draw this line using the current OpenGL state.
- */
- 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<T>& operator=(const Line<T>& line) noexcept;
- bool operator==(const Line<T>& line) const noexcept;
- bool operator!=(const Line<T>& line) const noexcept;
-
- private:
- Point<T> fPosStart, fPosEnd;
- };
-
- // -----------------------------------------------------------------------
-
- /**
- 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<typename T>
- class Circle
- {
- public:
- /**
- Constructor for a null circle.
- */
- Circle() noexcept;
-
- /**
- Constructor using custom X, Y and size values.
- */
- Circle(const T& x, const T& y, const float size, const uint numSegments = 300);
-
- /**
- Constructor using custom position and size values.
- */
- Circle(const Point<T>& pos, const float size, const uint numSegments = 300);
-
- /**
- Constructor using another Circle class values.
- */
- Circle(const Circle<T>& cir) noexcept;
-
- /**
- Get X value.
- */
- const T& getX() const noexcept;
-
- /**
- Get Y value.
- */
- const T& getY() const noexcept;
-
- /**
- Get position.
- */
- const Point<T>& getPos() const noexcept;
-
- /**
- Set X value to @a x.
- */
- void setX(const T& x) noexcept;
-
- /**
- Set Y value to @a y.
- */
- void setY(const T& y) noexcept;
-
- /**
- Set X and Y values to @a x and @a y respectively.
- */
- void setPos(const T& x, const T& y) noexcept;
-
- /**
- Set X and Y values according to @a pos.
- */
- void setPos(const Point<T>& pos) noexcept;
-
- /**
- Get size.
- */
- float getSize() const noexcept;
-
- /**
- Set size.
- @note Must always be > 0
- */
- void setSize(const float size) noexcept;
-
- /**
- Get the current number of line segments that make this circle.
- */
- uint getNumSegments() const noexcept;
-
- /**
- Set the number of line segments that will make this circle.
- @note Must always be >= 3
- */
- void setNumSegments(const uint num);
-
- /**
- Draw this circle using the current OpenGL state.
- */
- void draw();
-
- /**
- Draw lines (outline of this circle) using the current OpenGL state.
- */
- void drawOutline();
-
- Circle<T>& operator=(const Circle<T>& cir) noexcept;
- bool operator==(const Circle<T>& cir) const noexcept;
- bool operator!=(const Circle<T>& cir) const noexcept;
-
- private:
- Point<T> fPos;
- float fSize;
- uint fNumSegments;
-
- // cached values
- float fTheta, fCos, fSin;
-
- void _draw(const bool outline);
- };
-
- // -----------------------------------------------------------------------
-
- /**
- DGL Triangle class.
-
- This class describes a triangle, defined by 3 points.
- */
- template<typename T>
- class Triangle
- {
- public:
- /**
- Constructor for a null triangle.
- */
- Triangle() noexcept;
-
- /**
- Constructor using custom X and Y values.
- */
- Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
-
- /**
- Constructor using custom position values.
- */
- Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
-
- /**
- Constructor using another Triangle class values.
- */
- Triangle(const Triangle<T>& tri) noexcept;
-
- /**
- Draw this triangle using the current OpenGL state.
- */
- void draw();
-
- /**
- Draw lines (outline of this triangle) using the current OpenGL state.
- */
- 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<T>& operator=(const Triangle<T>& tri) noexcept;
- bool operator==(const Triangle<T>& tri) const noexcept;
- bool operator!=(const Triangle<T>& tri) const noexcept;
-
- private:
- Point<T> fPos1, fPos2, fPos3;
-
- void _draw(const bool outline);
- };
-
- // -----------------------------------------------------------------------
-
- /**
- DGL Rectangle class.
-
- This class describes a rectangle, defined by a starting point and a size.
- */
- template<typename T>
- class Rectangle
- {
- public:
- /**
- Constructor for a null rectangle.
- */
- Rectangle() noexcept;
-
- /**
- Constructor using custom X, Y, width and height values.
- */
- Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
-
- /**
- Constructor using custom X, Y and size values.
- */
- Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
-
- /**
- Constructor using custom pos, width and height values.
- */
- Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
-
- /**
- Constructor using custom position and size.
- */
- Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
-
- /**
- Constructor using another Rectangle class values.
- */
- Rectangle(const Rectangle<T>& rect) noexcept;
-
- /**
- Get X value.
- */
- const T& getX() const noexcept;
-
- /**
- Get Y value.
- */
- const T& getY() const noexcept;
-
- /**
- Get width.
- */
- const T& getWidth() const noexcept;
-
- /**
- Get height.
- */
- const T& getHeight() const noexcept;
-
- /**
- Get position.
- */
- const Point<T>& getPos() const noexcept;
-
- /**
- Get size.
- */
- const Size<T>& getSize() const noexcept;
-
- /**
- Set X value as @a x.
- */
- void setX(const T& x) noexcept;
-
- /**
- Set Y value as @a y.
- */
- void setY(const T& y) noexcept;
-
- /**
- Set X and Y values as @a x and @a y respectively.
- */
- void setPos(const T& x, const T& y) noexcept;
-
- /**
- Set X and Y values according to @a pos.
- */
- void setPos(const Point<T>& pos) noexcept;
-
- /**
- Move this rectangle by @a x and @a y values.
- */
- void moveBy(const T& x, const T& y) noexcept;
-
- /**
- Move this rectangle by @a pos.
- */
- void moveBy(const Point<T>& pos) noexcept;
-
- /**
- Set width.
- */
- void setWidth(const T& width) noexcept;
-
- /**
- Set height.
- */
- void setHeight(const T& height) noexcept;
-
- /**
- Set size using @a width and @a height.
- */
- void setSize(const T& width, const T& height) noexcept;
-
- /**
- Set size.
- */
- void setSize(const Size<T>& size) noexcept;
-
- /**
- Grow size by @a multiplier.
- */
- void growBy(double multiplier) noexcept;
-
- /**
- Shrink size by @a divider.
- */
- void shrinkBy(double divider) noexcept;
-
- /**
- Set rectangle using @a pos and @a size.
- */
- void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
-
- /**
- Set rectangle.
- */
- void setRectangle(const Rectangle<T>& rect) noexcept;
-
- /**
- Check if this rectangle contains the point defined by @a X and @a Y.
- */
- bool contains(const T& x, const T& y) const noexcept;
-
- /**
- Check if this rectangle contains the point @a pos.
- */
- bool contains(const Point<T>& pos) const noexcept;
-
- /**
- Check if this rectangle contains X.
- */
- bool containsX(const T& x) const noexcept;
-
- /**
- Check if this rectangle contains Y.
- */
- bool containsY(const T& y) const noexcept;
-
- /**
- Draw this rectangle using the current OpenGL state.
- */
- void draw();
-
- /**
- Draw lines (outline of this rectangle) using the current OpenGL state.
- */
- void drawOutline();
-
- Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
- Rectangle<T>& operator*=(double m) noexcept;
- Rectangle<T>& operator/=(double d) noexcept;
- bool operator==(const Rectangle<T>& size) const noexcept;
- bool operator!=(const Rectangle<T>& size) const noexcept;
-
- private:
- Point<T> fPos;
- Size<T> fSize;
-
- void _draw(const bool outline);
- };
-
- // -----------------------------------------------------------------------
-
- END_NAMESPACE_DGL
-
- #endif // DGL_GEOMETRY_HPP_INCLUDED
|