Commit 0954e067 authored by Philip Rogers's avatar Philip Rogers Committed by Commit Bot

[CI] Document FloatQuad and FloatRoundedRect inclusive intersections

The following intersection functions are edge-inclusive:
FloatQuad::IntersectsRect
FloatQuad::IntersectsCircle
FloatQuad::IntersectsEllipse
FloatRoundedRect::IntersectsQuad

This is different from FloatRect::Intersects and LayoutRect::Intersects
which are edge-exclusive. This patch adds a comment above each of these
functions specifying that they are edge-inclusive, and adds tests.

Bug: 818772
Change-Id: I7c1212a08e5e8874fb9679c5a676f91a97d3fce7
Reviewed-on: https://chromium-review.googlesource.com/960841Reviewed-by: default avatarStefan Zager <szager@chromium.org>
Commit-Queue: Philip Rogers <pdr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542961}
parent 06c498e3
...@@ -147,6 +147,9 @@ static inline FloatPoint RightMostCornerToVector(const FloatRect& rect, ...@@ -147,6 +147,9 @@ static inline FloatPoint RightMostCornerToVector(const FloatRect& rect,
} }
bool FloatQuad::IntersectsRect(const FloatRect& rect) const { bool FloatQuad::IntersectsRect(const FloatRect& rect) const {
// IntersectsRect is only valid on convex quads which an empty quad is not.
DCHECK(!IsEmpty());
// For each side of the quad clockwise we check if the rectangle is to the // For each side of the quad clockwise we check if the rectangle is to the
// left of it since only content on the right can onlap with the quad. This // left of it since only content on the right can onlap with the quad. This
// only works if the quad is convex. // only works if the quad is convex.
......
...@@ -96,10 +96,14 @@ class PLATFORM_EXPORT FloatQuad { ...@@ -96,10 +96,14 @@ class PLATFORM_EXPORT FloatQuad {
// Tests whether any part of the rectangle intersects with this quad. // Tests whether any part of the rectangle intersects with this quad.
// This only works for convex quads. // This only works for convex quads.
// This intersection is edge-inclusive and will return true even if the
// intersecting area is empty (i.e., the intersection is a line or a point).
bool IntersectsRect(const FloatRect&) const; bool IntersectsRect(const FloatRect&) const;
// Test whether any part of the circle/ellipse intersects with this quad. // Test whether any part of the circle/ellipse intersects with this quad.
// Note that these two functions only work for convex quads. // Note that these two functions only work for convex quads.
// These intersections are edge-inclusive and will return true even if the
// intersecting area is empty (i.e., the intersection is a line or a point).
bool IntersectsCircle(const FloatPoint& center, float radius) const; bool IntersectsCircle(const FloatPoint& center, float radius) const;
bool IntersectsEllipse(const FloatPoint& center, bool IntersectsEllipse(const FloatPoint& center,
const FloatSize& radii) const; const FloatSize& radii) const;
......
...@@ -36,4 +36,78 @@ TEST(FloatQuadTest, BoundingBoxSaturateInf) { ...@@ -36,4 +36,78 @@ TEST(FloatQuadTest, BoundingBoxSaturateInf) {
EXPECT_EQ(rect.Width(), 17.0f - std::numeric_limits<int>::min()); EXPECT_EQ(rect.Width(), 17.0f - std::numeric_limits<int>::min());
EXPECT_EQ(rect.Height(), std::numeric_limits<int>::max() - 3.0f); EXPECT_EQ(rect.Height(), std::numeric_limits<int>::max() - 3.0f);
} }
TEST(FloatQuadTest, RectIntersectionIsInclusive) {
// A rectilinear quad at (10, 10) with dimensions 10x10.
FloatQuad quad(FloatRect(10, 10, 10, 10));
// A rect fully contained in the quad should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(11, 11, 8, 8)));
// A point fully contained in the quad should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(11, 11, 0, 0)));
// A rect that touches the quad only at the point (10, 10) should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(9, 9, 1, 1)));
// A rect that touches the quad only on the left edge should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(9, 11, 1, 1)));
// A rect that touches the quad only on the top edge should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(11, 9, 1, 1)));
// A rect that touches the quad only on the right edge should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(20, 11, 1, 1)));
// A rect that touches the quad only on the bottom edge should intersect.
EXPECT_TRUE(quad.IntersectsRect(FloatRect(11, 20, 1, 1)));
// A rect that is fully outside the quad should not intersect.
EXPECT_FALSE(quad.IntersectsRect(FloatRect(8, 8, 1, 1)));
// A point that is fully outside the quad should not intersect.
EXPECT_FALSE(quad.IntersectsRect(FloatRect(9, 9, 0, 0)));
}
TEST(FloatQuadTest, CircleIntersectionIsInclusive) {
// A rectilinear quad at (10, 10) with dimensions 10x10.
FloatQuad quad(FloatRect(10, 10, 10, 10));
// A circle fully contained in the top-left of the quad should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(12, 12), 1));
// A point fully contained in the top-left of the quad should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(12, 12), 0));
// A circle that touches the left edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(9, 11), 1));
// A circle that touches the top edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(11, 9), 1));
// A circle that touches the right edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(21, 11), 1));
// A circle that touches the bottom edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(11, 21), 1));
// A point that touches the left edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(10, 11), 0));
// A point that touches the top edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(11, 10), 0));
// A point that touches the right edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(20, 11), 0));
// A point that touches the bottom edge should intersect.
EXPECT_TRUE(quad.IntersectsCircle(FloatPoint(11, 20), 0));
// A circle that is fully outside the quad should not intersect.
EXPECT_FALSE(quad.IntersectsCircle(FloatPoint(9, 9), 1));
// A point that is fully outside the quad should not intersect.
EXPECT_FALSE(quad.IntersectsCircle(FloatPoint(9, 9), 0));
}
} // namespace blink } // namespace blink
...@@ -167,6 +167,8 @@ class PLATFORM_EXPORT FloatRoundedRect { ...@@ -167,6 +167,8 @@ class PLATFORM_EXPORT FloatRoundedRect {
// Tests whether the quad intersects any part of this rounded rectangle. // Tests whether the quad intersects any part of this rounded rectangle.
// This only works for convex quads. // This only works for convex quads.
// This intersection is edge-inclusive and will return true even if the
// intersecting area is empty (i.e., the intersection is a line or a point).
bool IntersectsQuad(const FloatQuad&) const; bool IntersectsQuad(const FloatQuad&) const;
void AdjustRadii(); void AdjustRadii();
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "platform/geometry/FloatRoundedRect.h" #include "platform/geometry/FloatRoundedRect.h"
#include "platform/geometry/FloatQuad.h"
#include "platform/geometry/LayoutRect.h" #include "platform/geometry/LayoutRect.h"
#include "platform/wtf/text/WTFString.h" #include "platform/wtf/text/WTFString.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -127,7 +128,6 @@ TEST(FloatRoundedRectTest, circle) { ...@@ -127,7 +128,6 @@ TEST(FloatRoundedRectTest, circle) {
* (25, 15) x=25 x=80 (20, 30) * (25, 15) x=25 x=80 (20, 30)
*/ */
TEST(FloatRoundedRectTest, ellipticalCorners) { TEST(FloatRoundedRectTest, ellipticalCorners) {
FloatSize corner_size(10, 20);
FloatRoundedRect::Radii corner_radii; FloatRoundedRect::Radii corner_radii;
corner_radii.SetTopLeft(FloatSize(10, 15)); corner_radii.SetTopLeft(FloatSize(10, 15));
corner_radii.SetTopRight(FloatSize(10, 20)); corner_radii.SetTopRight(FloatSize(10, 20));
...@@ -180,6 +180,58 @@ TEST(FloatRoundedRectTest, radiusCenterRect) { ...@@ -180,6 +180,58 @@ TEST(FloatRoundedRectTest, radiusCenterRect) {
EXPECT_TRUE(r2.RadiusCenterRect().IsEmpty()); EXPECT_TRUE(r2.RadiusCenterRect().IsEmpty());
} }
TEST(FloatRoundedRectTest, IntersectsQuadIsInclusive) {
FloatRoundedRect::Radii corner_radii;
corner_radii.SetTopLeft(FloatSize(5, 5));
corner_radii.SetTopRight(FloatSize(5, 5));
corner_radii.SetBottomLeft(FloatSize(5, 5));
corner_radii.SetBottomRight(FloatSize(5, 5));
// A rect at (10, 10) with dimensions 20x20 and radii of size 5x5.
FloatRoundedRect r(FloatRect(10, 10, 20, 20), corner_radii);
// A quad fully inside the rounded rect should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(11, 11, 8, 8))));
// A quad fully outside the rounded rect should not intersect.
EXPECT_FALSE(r.IntersectsQuad(FloatQuad(FloatRect(0, 0, 1, 1))));
// A quad touching the top edge of the rounded rect should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(15, 9, 5, 1))));
// A quad touching the right edge of the rounded rect should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(30, 15, 1, 1))));
// A quad touching the bottom edge of the rounded rect should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(15, 30, 1, 1))));
// A quad touching the left edge of the rounded rect should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(9, 15, 1, 1))));
// A quad outside the top-left arc should not intersect.
EXPECT_FALSE(r.IntersectsQuad(FloatQuad(FloatRect(10, 10, 1, 1))));
// A quad inside the top-left arc should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(13, 13, 1, 1))));
// A quad outside the top-right arc should not intersect.
EXPECT_FALSE(r.IntersectsQuad(FloatQuad(FloatRect(29, 10, 1, 1))));
// A quad inside the top-right arc should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(26, 13, 1, 1))));
// A quad outside the bottom-right arc should not intersect.
EXPECT_FALSE(r.IntersectsQuad(FloatQuad(FloatRect(29, 29, 1, 1))));
// A quad inside the bottom-right arc should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(26, 26, 1, 1))));
// A quad outside the bottom-left arc should not intersect.
EXPECT_FALSE(r.IntersectsQuad(FloatQuad(FloatRect(10, 29, 1, 1))));
// A quad inside the bottom-left arc should intersect.
EXPECT_TRUE(r.IntersectsQuad(FloatQuad(FloatRect(13, 26, 1, 1))));
}
TEST(FloatRoundedRectTest, ToString) { TEST(FloatRoundedRectTest, ToString) {
FloatSize corner_rect(1, 2); FloatSize corner_rect(1, 2);
FloatRoundedRect rounded_rect( FloatRoundedRect rounded_rect(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment