2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef DGL_GEOMETRY_HPP_INCLUDED
18 #define DGL_GEOMETRY_HPP_INCLUDED
24 // --------------------------------------------------------------------------------------------------------------------
25 // Forward class names
27 template<typename
> class Line
;
28 template<typename
> class Circle
;
29 template<typename
> class Triangle
;
30 template<typename
> class Rectangle
;
32 // --------------------------------------------------------------------------------------------------------------------
37 This class describes a single point in space, defined by an X and Y value.
44 Constructor for (0, 0) point.
49 Constructor using custom X and Y values.
51 Point(const T
& x
, const T
& y
) noexcept
;
54 Constructor using another Point class values.
56 Point(const Point
<T
>& pos
) noexcept
;
61 const T
& getX() const noexcept
;
66 const T
& getY() const noexcept
;
71 void setX(const T
& x
) noexcept
;
76 void setY(const T
& y
) noexcept
;
79 Set X and Y values to @a x and @a y respectively.
81 void setPos(const T
& x
, const T
& y
) noexcept
;
84 Set X and Y values according to @a pos.
86 void setPos(const Point
<T
>& pos
) noexcept
;
89 Move this point by @a x and @a y values.
91 void moveBy(const T
& x
, const T
& y
) noexcept
;
94 Move this point by @a pos.
96 void moveBy(const Point
<T
>& pos
) noexcept
;
99 Return true if point is (0, 0).
101 bool isZero() const noexcept
;
104 Return true if point is not (0, 0).
106 bool isNotZero() const noexcept
;
108 Point
<T
> operator+(const Point
<T
>& pos
) noexcept
;
109 Point
<T
> operator-(const Point
<T
>& pos
) noexcept
;
110 Point
<T
>& operator=(const Point
<T
>& pos
) noexcept
;
111 Point
<T
>& operator+=(const Point
<T
>& pos
) noexcept
;
112 Point
<T
>& operator-=(const Point
<T
>& pos
) noexcept
;
113 bool operator==(const Point
<T
>& pos
) const noexcept
;
114 bool operator!=(const Point
<T
>& pos
) const noexcept
;
118 template<typename
> friend class Line
;
119 template<typename
> friend class Circle
;
120 template<typename
> friend class Triangle
;
121 template<typename
> friend class Rectangle
;
124 // --------------------------------------------------------------------------------------------------------------------
129 This class describes a size, defined by a width and height value.
136 Constructor for null size (0x0).
141 Constructor using custom width and height values.
143 Size(const T
& width
, const T
& height
) noexcept
;
146 Constructor using another Size class values.
148 Size(const Size
<T
>& size
) noexcept
;
153 const T
& getWidth() const noexcept
;
158 const T
& getHeight() const noexcept
;
163 void setWidth(const T
& width
) noexcept
;
168 void setHeight(const T
& height
) noexcept
;
171 Set size to @a width and @a height.
173 void setSize(const T
& width
, const T
& height
) noexcept
;
178 void setSize(const Size
<T
>& size
) noexcept
;
181 Grow size by @a multiplier.
183 void growBy(double multiplier
) noexcept
;
186 Shrink size by @a divider.
188 void shrinkBy(double divider
) noexcept
;
191 Return true if size is null (0x0).
192 An null size is also invalid.
194 bool isNull() const noexcept
;
197 Return true if size is not null (0x0).
198 A non-null size is still invalid if its width or height are negative.
200 bool isNotNull() const noexcept
;
203 Return true if size is valid (width and height are higher than zero).
205 bool isValid() const noexcept
;
208 Return true if size is invalid (width or height are lower or equal to zero).
209 An invalid size might not be null under some circumstances.
211 bool isInvalid() const noexcept
;
213 Size
<int> toInt() const noexcept
;
215 Size
<T
> operator+(const Size
<T
>& size
) noexcept
;
216 Size
<T
> operator-(const Size
<T
>& size
) noexcept
;
217 Size
<T
>& operator=(const Size
<T
>& size
) noexcept
;
218 Size
<T
>& operator+=(const Size
<T
>& size
) noexcept
;
219 Size
<T
>& operator-=(const Size
<T
>& size
) noexcept
;
220 Size
<T
>& operator*=(double m
) noexcept
;
221 Size
<T
>& operator/=(double d
) noexcept
;
222 Size
<T
> operator*(double m
) const noexcept
;
223 Size
<T
> operator/(double m
) const noexcept
;
224 bool operator==(const Size
<T
>& size
) const noexcept
;
225 bool operator!=(const Size
<T
>& size
) const noexcept
;
229 template<typename
> friend class Rectangle
;
232 // -----------------------------------------------------------------------
237 This class describes a line, defined by two points.
244 Constructor for a null line ([0,0] to [0,0]).
249 Constructor using custom start X, start Y, end X and end Y values.
251 Line(const T
& startX
, const T
& startY
, const T
& endX
, const T
& endY
) noexcept
;
254 Constructor using custom start X, start Y and end pos values.
256 Line(const T
& startX
, const T
& startY
, const Point
<T
>& endPos
) noexcept
;
259 Constructor using custom start pos, end X and end Y values.
261 Line(const Point
<T
>& startPos
, const T
& endX
, const T
& endY
) noexcept
;
264 Constructor using custom start and end pos values.
266 Line(const Point
<T
>& startPos
, const Point
<T
>& endPos
) noexcept
;
269 Constructor using another Line class values.
271 Line(const Line
<T
>& line
) noexcept
;
276 const T
& getStartX() const noexcept
;
281 const T
& getStartY() const noexcept
;
286 const T
& getEndX() const noexcept
;
291 const T
& getEndY() const noexcept
;
296 const Point
<T
>& getStartPos() const noexcept
;
301 const Point
<T
>& getEndPos() const noexcept
;
304 Set start X value to @a x.
306 void setStartX(const T
& x
) noexcept
;
309 Set start Y value to @a y.
311 void setStartY(const T
& y
) noexcept
;
314 Set start X and Y values to @a x and @a y respectively.
316 void setStartPos(const T
& x
, const T
& y
) noexcept
;
319 Set start X and Y values according to @a pos.
321 void setStartPos(const Point
<T
>& pos
) noexcept
;
324 Set end X value to @a x.
326 void setEndX(const T
& x
) noexcept
;
329 Set end Y value to @a y.
331 void setEndY(const T
& y
) noexcept
;
334 Set end X and Y values to @a x and @a y respectively.
336 void setEndPos(const T
& x
, const T
& y
) noexcept
;
339 Set end X and Y values according to @a pos.
341 void setEndPos(const Point
<T
>& pos
) noexcept
;
344 Move this line by @a x and @a y values.
346 void moveBy(const T
& x
, const T
& y
) noexcept
;
349 Move this line by @a pos.
351 void moveBy(const Point
<T
>& pos
) noexcept
;
354 Return true if line is null (start and end pos are equal).
356 bool isNull() const noexcept
;
359 Return true if line is not null (start and end pos are different).
361 bool isNotNull() const noexcept
;
363 #ifndef DPF_TEST_POINT_CPP
365 Draw this line using the provided graphics context, optionally specifying line width.
367 void draw(const GraphicsContext
& context
, T width
= 1);
370 Line
<T
>& operator=(const Line
<T
>& line
) noexcept
;
371 bool operator==(const Line
<T
>& line
) const noexcept
;
372 bool operator!=(const Line
<T
>& line
) const noexcept
;
374 #ifndef DPF_TEST_POINT_CPP
376 Draw this line using the current OpenGL state.@n
377 DEPRECATED Please use draw(const GraphicsContext&) instead.
379 DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
384 Point
<T
> posStart
, posEnd
;
387 // -----------------------------------------------------------------------
392 This class describes a circle, defined by position, size and a minimum of 3 segments.
394 TODO: report if circle starts at top-left, bottom-right or center.
395 and size grows from which point?
402 Constructor for a null circle.
407 Constructor using custom X, Y and size values.
409 Circle(const T
& x
, const T
& y
, const float size
, const uint numSegments
= 300);
412 Constructor using custom position and size values.
414 Circle(const Point
<T
>& pos
, const float size
, const uint numSegments
= 300);
417 Constructor using another Circle class values.
419 Circle(const Circle
<T
>& cir
) noexcept
;
424 const T
& getX() const noexcept
;
429 const T
& getY() const noexcept
;
434 const Point
<T
>& getPos() const noexcept
;
439 void setX(const T
& x
) noexcept
;
444 void setY(const T
& y
) noexcept
;
447 Set X and Y values to @a x and @a y respectively.
449 void setPos(const T
& x
, const T
& y
) noexcept
;
452 Set X and Y values according to @a pos.
454 void setPos(const Point
<T
>& pos
) noexcept
;
459 float getSize() const noexcept
;
463 @note Must always be > 0
465 void setSize(const float size
) noexcept
;
468 Get the current number of line segments that make this circle.
470 uint
getNumSegments() const noexcept
;
473 Set the number of line segments that will make this circle.
474 @note Must always be >= 3
476 void setNumSegments(const uint num
);
479 Draw this circle using the provided graphics context.
481 void draw(const GraphicsContext
& context
);
484 Draw lines (outline of this circle) using the provided graphics context, optionally specifying line width.
486 void drawOutline(const GraphicsContext
& context
, T lineWidth
= 1);
488 Circle
<T
>& operator=(const Circle
<T
>& cir
) noexcept
;
489 bool operator==(const Circle
<T
>& cir
) const noexcept
;
490 bool operator!=(const Circle
<T
>& cir
) const noexcept
;
492 #ifndef DPF_TEST_POINT_CPP
494 Draw this circle using the current OpenGL state.@n
495 DEPRECATED Please use draw(const GraphicsContext&) instead.
497 DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
501 Draw lines (outline of this circle) using the current OpenGL state.@n
502 DEPRECATED Please use drawOutline(const GraphicsContext&,T) instead.
504 DISTRHO_DEPRECATED_BY("drawOutline(const GraphicsContext&)")
514 float fTheta
, fCos
, fSin
;
517 // -----------------------------------------------------------------------
522 This class describes a triangle, defined by 3 points.
529 Constructor for a null triangle.
534 Constructor using custom X and Y values.
536 Triangle(const T
& x1
, const T
& y1
, const T
& x2
, const T
& y2
, const T
& x3
, const T
& y3
) noexcept
;
539 Constructor using custom position values.
541 Triangle(const Point
<T
>& pos1
, const Point
<T
>& pos2
, const Point
<T
>& pos3
) noexcept
;
544 Constructor using another Triangle class values.
546 Triangle(const Triangle
<T
>& tri
) noexcept
;
549 Return true if triangle is null (all its points are equal).
550 An null triangle is also invalid.
552 bool isNull() const noexcept
;
555 Return true if triangle is not null (one its points is different from the others).
556 A non-null triangle is still invalid if two of its points are equal.
558 bool isNotNull() const noexcept
;
561 Return true if triangle is valid (all its points are different).
563 bool isValid() const noexcept
;
566 Return true if triangle is invalid (one or two of its points are equal).
567 An invalid triangle might not be null under some circumstances.
569 bool isInvalid() const noexcept
;
572 Draw this triangle using the provided graphics context.
574 void draw(const GraphicsContext
& context
);
577 Draw lines (outline of this triangle) using the provided graphics context, optionally specifying line width.
579 void drawOutline(const GraphicsContext
& context
, T lineWidth
= 1);
581 Triangle
<T
>& operator=(const Triangle
<T
>& tri
) noexcept
;
582 bool operator==(const Triangle
<T
>& tri
) const noexcept
;
583 bool operator!=(const Triangle
<T
>& tri
) const noexcept
;
585 #ifndef DPF_TEST_POINT_CPP
587 Draw this triangle using the current OpenGL state.@n
588 DEPRECATED Please use draw(const GraphicsContext&) instead.
590 DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
594 Draw lines (outline of this triangle) using the current OpenGL state.@n
595 DEPRECATED Please use drawOutline(const GraphicsContext&,T) instead.
597 DISTRHO_DEPRECATED_BY("drawOutline(const GraphicsContext&)")
602 Point
<T
> pos1
, pos2
, pos3
;
605 // -----------------------------------------------------------------------
610 This class describes a rectangle, defined by a starting point and a size.
617 Constructor for a null rectangle.
619 Rectangle() noexcept
;
622 Constructor using custom X, Y, width and height values.
624 Rectangle(const T
& x
, const T
& y
, const T
& width
, const T
& height
) noexcept
;
627 Constructor using custom X, Y and size values.
629 Rectangle(const T
& x
, const T
& y
, const Size
<T
>& size
) noexcept
;
632 Constructor using custom pos, width and height values.
634 Rectangle(const Point
<T
>& pos
, const T
& width
, const T
& height
) noexcept
;
637 Constructor using custom position and size.
639 Rectangle(const Point
<T
>& pos
, const Size
<T
>& size
) noexcept
;
642 Constructor using another Rectangle class values.
644 Rectangle(const Rectangle
<T
>& rect
) noexcept
;
649 const T
& getX() const noexcept
;
654 const T
& getY() const noexcept
;
659 const T
& getWidth() const noexcept
;
664 const T
& getHeight() const noexcept
;
669 const Point
<T
>& getPos() const noexcept
;
674 const Size
<T
>& getSize() const noexcept
;
679 void setX(const T
& x
) noexcept
;
684 void setY(const T
& y
) noexcept
;
687 Set X and Y values as @a x and @a y respectively.
689 void setPos(const T
& x
, const T
& y
) noexcept
;
692 Set X and Y values according to @a pos.
694 void setPos(const Point
<T
>& pos
) noexcept
;
697 Move this rectangle by @a x and @a y values.
699 void moveBy(const T
& x
, const T
& y
) noexcept
;
702 Move this rectangle by @a pos.
704 void moveBy(const Point
<T
>& pos
) noexcept
;
709 void setWidth(const T
& width
) noexcept
;
714 void setHeight(const T
& height
) noexcept
;
717 Set size using @a width and @a height.
719 void setSize(const T
& width
, const T
& height
) noexcept
;
724 void setSize(const Size
<T
>& size
) noexcept
;
727 Grow size by @a multiplier.
729 void growBy(double multiplier
) noexcept
;
732 Shrink size by @a divider.
734 void shrinkBy(double divider
) noexcept
;
737 Set rectangle using @a pos and @a size.
739 void setRectangle(const Point
<T
>& pos
, const Size
<T
>& size
) noexcept
;
744 void setRectangle(const Rectangle
<T
>& rect
) noexcept
;
747 Check if this rectangle contains the point defined by @a X and @a Y.
749 bool contains(const T
& x
, const T
& y
) const noexcept
;
752 Check if this rectangle contains the point @a pos.
754 bool contains(const Point
<T
>& pos
) const noexcept
;
757 Check if this rectangle contains the point @a pos affected by a custom scale.
759 bool containsAfterScaling(const Point
<T
>& pos
, double scaling
) const noexcept
;
762 Check if this rectangle contains the point @a pos of another type.
764 template<typename T2
>
765 bool contains(const Point
<T2
>& pos
) const noexcept
;
768 Check if this rectangle contains X.
770 bool containsX(const T
& x
) const noexcept
;
773 Check if this rectangle contains Y.
775 bool containsY(const T
& y
) const noexcept
;
778 Return true if size is null (0x0).
779 An null size is also invalid.
781 bool isNull() const noexcept
;
784 Return true if size is not null (0x0).
785 A non-null size is still invalid if its width or height are negative.
787 bool isNotNull() const noexcept
;
790 Return true if size is valid (width and height are higher than zero).
792 bool isValid() const noexcept
;
795 Return true if size is invalid (width or height are lower or equal to zero).
796 An invalid size might not be null under some circumstances.
798 bool isInvalid() const noexcept
;
801 Draw this rectangle using the provided graphics context.
803 void draw(const GraphicsContext
& context
);
806 Draw lines (outline of this rectangle) using the provided graphics context, optionally specifying line width.
808 void drawOutline(const GraphicsContext
& context
, T lineWidth
= 1);
810 Rectangle
<T
>& operator=(const Rectangle
<T
>& rect
) noexcept
;
811 Rectangle
<T
>& operator*=(double m
) noexcept
;
812 Rectangle
<T
>& operator/=(double d
) noexcept
;
813 bool operator==(const Rectangle
<T
>& size
) const noexcept
;
814 bool operator!=(const Rectangle
<T
>& size
) const noexcept
;
817 Draw this rectangle using the current OpenGL state.@n
818 DEPRECATED Please use draw(const GraphicsContext&) instead.
820 DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
824 Draw lines (outline of this rectangle) using the current OpenGL state.@n
825 DEPRECATED Please use drawOutline(const GraphicsContext&,T) instead.
827 DISTRHO_DEPRECATED_BY("drawOutline(const GraphicsContext&)")
835 // -----------------------------------------------------------------------
839 #endif // DGL_GEOMETRY_HPP_INCLUDED