Add initial bits for Qt6 support
[carla.git] / source / modules / dgl / Geometry.hpp
blob9ac9483be5e80a1ae32f71676cecf88ddf26e884
1 /*
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
20 #include "Base.hpp"
22 START_NAMESPACE_DGL
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 // --------------------------------------------------------------------------------------------------------------------
34 /**
35 DGL Point class.
37 This class describes a single point in space, defined by an X and Y value.
39 template<typename T>
40 class Point
42 public:
43 /**
44 Constructor for (0, 0) point.
46 Point() noexcept;
48 /**
49 Constructor using custom X and Y values.
51 Point(const T& x, const T& y) noexcept;
53 /**
54 Constructor using another Point class values.
56 Point(const Point<T>& pos) noexcept;
58 /**
59 Get X value.
61 const T& getX() const noexcept;
63 /**
64 Get Y value.
66 const T& getY() const noexcept;
68 /**
69 Set X value to @a x.
71 void setX(const T& x) noexcept;
73 /**
74 Set Y value to @a y.
76 void setY(const T& y) noexcept;
78 /**
79 Set X and Y values to @a x and @a y respectively.
81 void setPos(const T& x, const T& y) noexcept;
83 /**
84 Set X and Y values according to @a pos.
86 void setPos(const Point<T>& pos) noexcept;
88 /**
89 Move this point by @a x and @a y values.
91 void moveBy(const T& x, const T& y) noexcept;
93 /**
94 Move this point by @a pos.
96 void moveBy(const Point<T>& pos) noexcept;
98 /**
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;
116 private:
117 T x, y;
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 // --------------------------------------------------------------------------------------------------------------------
127 DGL Size class.
129 This class describes a size, defined by a width and height value.
131 template<typename T>
132 class Size
134 public:
136 Constructor for null size (0x0).
138 Size() noexcept;
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;
151 Get width.
153 const T& getWidth() const noexcept;
156 Get height.
158 const T& getHeight() const noexcept;
161 Set width.
163 void setWidth(const T& width) noexcept;
166 Set height.
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;
176 Set size.
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;
227 private:
228 T fWidth, fHeight;
229 template<typename> friend class Rectangle;
232 // -----------------------------------------------------------------------
235 DGL Line class.
237 This class describes a line, defined by two points.
239 template<typename T>
240 class Line
242 public:
244 Constructor for a null line ([0,0] to [0,0]).
246 Line() noexcept;
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;
274 Get start X value.
276 const T& getStartX() const noexcept;
279 Get start Y value.
281 const T& getStartY() const noexcept;
284 Get end X value.
286 const T& getEndX() const noexcept;
289 Get end Y value.
291 const T& getEndY() const noexcept;
294 Get start position.
296 const Point<T>& getStartPos() const noexcept;
299 Get end position.
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);
368 #endif
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&)")
380 void draw();
381 #endif
383 private:
384 Point<T> posStart, posEnd;
387 // -----------------------------------------------------------------------
390 DGL Circle class.
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?
397 template<typename T>
398 class Circle
400 public:
402 Constructor for a null circle.
404 Circle() noexcept;
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;
422 Get X value.
424 const T& getX() const noexcept;
427 Get Y value.
429 const T& getY() const noexcept;
432 Get position.
434 const Point<T>& getPos() const noexcept;
437 Set X value to @a x.
439 void setX(const T& x) noexcept;
442 Set Y value to @a y.
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;
457 Get size.
459 float getSize() const noexcept;
462 Set size.
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&)")
498 void draw();
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&)")
505 void drawOutline();
506 #endif
508 private:
509 Point<T> fPos;
510 float fSize;
511 uint fNumSegments;
513 // cached values
514 float fTheta, fCos, fSin;
517 // -----------------------------------------------------------------------
520 DGL Triangle class.
522 This class describes a triangle, defined by 3 points.
524 template<typename T>
525 class Triangle
527 public:
529 Constructor for a null triangle.
531 Triangle() noexcept;
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&)")
591 void draw();
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&)")
598 void drawOutline();
599 #endif
601 private:
602 Point<T> pos1, pos2, pos3;
605 // -----------------------------------------------------------------------
608 DGL Rectangle class.
610 This class describes a rectangle, defined by a starting point and a size.
612 template<typename T>
613 class Rectangle
615 public:
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;
647 Get X value.
649 const T& getX() const noexcept;
652 Get Y value.
654 const T& getY() const noexcept;
657 Get width.
659 const T& getWidth() const noexcept;
662 Get height.
664 const T& getHeight() const noexcept;
667 Get position.
669 const Point<T>& getPos() const noexcept;
672 Get size.
674 const Size<T>& getSize() const noexcept;
677 Set X value as @a x.
679 void setX(const T& x) noexcept;
682 Set Y value as @a y.
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;
707 Set width.
709 void setWidth(const T& width) noexcept;
712 Set height.
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;
722 Set size.
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;
742 Set rectangle.
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&)")
821 void draw();
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&)")
828 void drawOutline();
830 private:
831 Point<T> pos;
832 Size<T> size;
835 // -----------------------------------------------------------------------
837 END_NAMESPACE_DGL
839 #endif // DGL_GEOMETRY_HPP_INCLUDED