Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_GEOMETRY_QUAD_F_H_ |
| 6 | #define UI_GFX_GEOMETRY_QUAD_F_H_ |
| 7 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <cmath> |
[email protected] | a109fd0 | 2014-07-10 07:41:43 | [diff] [blame] | 12 | #include <iosfwd> |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Hans Wennborg | 4b46b3b | 2020-06-23 08:29:08 | [diff] [blame] | 15 | #include "base/check_op.h" |
Kalvin Lee | ba222e7 | 2024-11-21 04:18:44 | [diff] [blame] | 16 | #include "base/component_export.h" |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 17 | #include "ui/gfx/geometry/point_f.h" |
| 18 | #include "ui/gfx/geometry/rect_f.h" |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 19 | |
| 20 | namespace gfx { |
| 21 | |
| 22 | // A Quad is defined by four corners, allowing it to have edges that are not |
| 23 | // axis-aligned, unlike a Rect. |
Kalvin Lee | ba222e7 | 2024-11-21 04:18:44 | [diff] [blame] | 24 | class COMPONENT_EXPORT(GEOMETRY) QuadF { |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 25 | public: |
Peter Kasting | 275539a6 | 2016-06-15 01:41:42 | [diff] [blame] | 26 | constexpr QuadF() = default; |
| 27 | constexpr QuadF(const PointF& p1, |
| 28 | const PointF& p2, |
| 29 | const PointF& p3, |
| 30 | const PointF& p4) |
| 31 | : p1_(p1), p2_(p2), p3_(p3), p4_(p4) {} |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 32 | |
Peter Kasting | 275539a6 | 2016-06-15 01:41:42 | [diff] [blame] | 33 | constexpr explicit QuadF(const RectF& rect) |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 34 | : p1_(rect.x(), rect.y()), |
| 35 | p2_(rect.right(), rect.y()), |
| 36 | p3_(rect.right(), rect.bottom()), |
| 37 | p4_(rect.x(), rect.bottom()) {} |
| 38 | |
| 39 | void operator=(const RectF& rect); |
| 40 | |
| 41 | void set_p1(const PointF& p) { p1_ = p; } |
| 42 | void set_p2(const PointF& p) { p2_ = p; } |
| 43 | void set_p3(const PointF& p) { p3_ = p; } |
| 44 | void set_p4(const PointF& p) { p4_ = p; } |
| 45 | |
Peter Kasting | 275539a6 | 2016-06-15 01:41:42 | [diff] [blame] | 46 | constexpr const PointF& p1() const { return p1_; } |
| 47 | constexpr const PointF& p2() const { return p2_; } |
| 48 | constexpr const PointF& p3() const { return p3_; } |
| 49 | constexpr const PointF& p4() const { return p4_; } |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 50 | |
| 51 | // Returns true if the quad is an axis-aligned rectangle. |
| 52 | bool IsRectilinear() const; |
| 53 | |
| 54 | // Returns true if the points of the quad are in counter-clockwise order. This |
| 55 | // assumes that the quad is convex, and that no three points are collinear. |
| 56 | bool IsCounterClockwise() const; |
| 57 | |
| 58 | // Returns true if the |point| is contained within the quad, or lies on on |
[email protected] | b63a661 | 2014-05-16 20:13:54 | [diff] [blame] | 59 | // edge of the quad. This assumes that the quad is convex. |
Xianzhu Wang | c69eabba | 2022-01-06 13:52:40 | [diff] [blame] | 60 | bool Contains(const PointF& point) const; |
| 61 | |
| 62 | // Returns true if the |quad| parameter is contained within |this| quad. |
| 63 | // This method assumes |this| quad is convex. The |quad| parameter has no |
| 64 | // restrictions. |
| 65 | bool ContainsQuad(const QuadF& quad) const; |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 66 | |
Fredrik Söderquist | 9bdb9a8 | 2024-02-16 11:16:56 | [diff] [blame] | 67 | // Returns two points (forming an axis-aligned bounding box) that bounds the |
| 68 | // four points of the quad. |
| 69 | std::pair<PointF, PointF> Extents() const { |
Peter Kasting | 14eab5c | 2019-09-12 18:18:27 | [diff] [blame] | 70 | float rl = std::min({p1_.x(), p2_.x(), p3_.x(), p4_.x()}); |
| 71 | float rr = std::max({p1_.x(), p2_.x(), p3_.x(), p4_.x()}); |
| 72 | float rt = std::min({p1_.y(), p2_.y(), p3_.y(), p4_.y()}); |
| 73 | float rb = std::max({p1_.y(), p2_.y(), p3_.y(), p4_.y()}); |
Fredrik Söderquist | 9bdb9a8 | 2024-02-16 11:16:56 | [diff] [blame] | 74 | return std::make_pair(PointF(rl, rt), PointF(rr, rb)); |
| 75 | } |
| 76 | |
| 77 | // Returns a rectangle that bounds the four points of the quad. The points of |
| 78 | // the quad may lie on the right/bottom edge of the resulting rectangle, |
| 79 | // rather than being strictly inside it. |
| 80 | RectF BoundingBox() const { |
| 81 | const auto [min, max] = Extents(); |
| 82 | return RectF(min.x(), min.y(), max.x() - min.x(), max.y() - min.y()); |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 83 | } |
| 84 | |
awoloszyn | 3d8eb1d | 2015-03-12 14:38:32 | [diff] [blame] | 85 | // Realigns the corners in the quad by rotating them n corners to the right. |
| 86 | void Realign(size_t times) { |
| 87 | DCHECK_LE(times, 4u); |
| 88 | for (size_t i = 0; i < times; ++i) { |
| 89 | PointF temp = p1_; |
| 90 | p1_ = p2_; |
| 91 | p2_ = p3_; |
| 92 | p3_ = p4_; |
| 93 | p4_ = temp; |
| 94 | } |
| 95 | } |
| 96 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 97 | // Add a vector to the quad, offseting each point in the quad by the vector. |
| 98 | void operator+=(const Vector2dF& rhs); |
| 99 | // Subtract a vector from the quad, offseting each point in the quad by the |
| 100 | // inverse of the vector. |
| 101 | void operator-=(const Vector2dF& rhs); |
| 102 | |
| 103 | // Scale each point in the quad by the |scale| factor. |
| 104 | void Scale(float scale) { Scale(scale, scale); } |
| 105 | |
| 106 | // Scale each point in the quad by the scale factors along each axis. |
| 107 | void Scale(float x_scale, float y_scale); |
| 108 | |
Xianzhu Wang | c69eabba | 2022-01-06 13:52:40 | [diff] [blame] | 109 | // Tests whether any part of the rectangle intersects with this quad. |
| 110 | // This only works for convex quads. |
| 111 | // This intersection is edge-inclusive and will return true even if the |
| 112 | // intersecting area is empty (i.e., the intersection is a line or a point). |
| 113 | bool IntersectsRect(const RectF&) const; |
| 114 | |
Fredrik Söderquist | 9bdb9a8 | 2024-02-16 11:16:56 | [diff] [blame] | 115 | // Like the above, but only checks `rect` against the sides of quad ("does |
| 116 | // half of the job"). Can be used if it is known beforehand that the bounding |
| 117 | // box of the quad intersects `rect`. |
| 118 | bool IntersectsRectPartial(const RectF& rect) const; |
| 119 | |
Fredrik Söderquist | c82682c | 2024-02-21 11:21:27 | [diff] [blame] | 120 | // Tests whether any part of the quad intersects with this quad. |
| 121 | // This intersection is edge-inclusive. |
| 122 | bool IntersectsQuad(const QuadF& quad) const; |
| 123 | |
Xianzhu Wang | c69eabba | 2022-01-06 13:52:40 | [diff] [blame] | 124 | // Test whether any part of the circle/ellipse intersects with this quad. |
| 125 | // Note that these two functions only work for convex quads. |
| 126 | // These intersections are edge-inclusive and will return true even if the |
| 127 | // intersecting area is empty (i.e., the intersection is a line or a point). |
| 128 | bool IntersectsCircle(const PointF& center, float radius) const; |
| 129 | bool IntersectsEllipse(const PointF& center, const SizeF& radii) const; |
| 130 | |
| 131 | // The center of the quad. If the quad is the result of a affine-transformed |
| 132 | // rectangle this is the same as the original center transformed. |
| 133 | PointF CenterPoint() const { |
| 134 | return PointF((p1_.x() + p2_.x() + p3_.x() + p4_.x()) / 4.0, |
| 135 | (p1_.y() + p2_.y() + p3_.y() + p4_.y()) / 4.0); |
| 136 | } |
| 137 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 138 | // Returns a string representation of quad. |
| 139 | std::string ToString() const; |
| 140 | |
Jan Keitel | 69d6518 | 2025-05-12 14:24:51 | [diff] [blame] | 141 | friend bool operator==(const QuadF&, const QuadF&) = default; |
| 142 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 143 | private: |
Fredrik Söderquist | c82682c | 2024-02-21 11:21:27 | [diff] [blame] | 144 | bool IsToTheLeftOfOrTouchingLine(const PointF& base, |
| 145 | const Vector2dF& vector) const; |
| 146 | bool FullyOutsideOneEdge(const QuadF& quad) const; |
| 147 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 148 | PointF p1_; |
| 149 | PointF p2_; |
| 150 | PointF p3_; |
| 151 | PointF p4_; |
| 152 | }; |
| 153 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 154 | // Add a vector to a quad, offseting each point in the quad by the vector. |
Kalvin Lee | ba222e7 | 2024-11-21 04:18:44 | [diff] [blame] | 155 | COMPONENT_EXPORT(GEOMETRY) |
| 156 | QuadF operator+(const QuadF& lhs, const Vector2dF& rhs); |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 157 | // Subtract a vector from a quad, offseting each point in the quad by the |
| 158 | // inverse of the vector. |
Kalvin Lee | ba222e7 | 2024-11-21 04:18:44 | [diff] [blame] | 159 | COMPONENT_EXPORT(GEOMETRY) |
| 160 | QuadF operator-(const QuadF& lhs, const Vector2dF& rhs); |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 161 | |
[email protected] | a109fd0 | 2014-07-10 07:41:43 | [diff] [blame] | 162 | // This is declared here for use in gtest-based unit tests but is defined in |
mlliu | a8e9f71 | 2016-08-20 02:17:52 | [diff] [blame] | 163 | // the //ui/gfx:test_support target. Depend on that to use this in your unit |
| 164 | // test. This should not be used in production code - call ToString() instead. |
[email protected] | a109fd0 | 2014-07-10 07:41:43 | [diff] [blame] | 165 | void PrintTo(const QuadF& quad, ::std::ostream* os); |
| 166 | |
[email protected] | b5e2d78 | 2013-12-18 21:01:15 | [diff] [blame] | 167 | } // namespace gfx |
| 168 | |
| 169 | #endif // UI_GFX_GEOMETRY_QUAD_F_H_ |