diff --git a/engine/src/flutter/impeller/geometry/BUILD.gn b/engine/src/flutter/impeller/geometry/BUILD.gn index 414b5026f0..becb1677f9 100644 --- a/engine/src/flutter/impeller/geometry/BUILD.gn +++ b/engine/src/flutter/impeller/geometry/BUILD.gn @@ -29,6 +29,8 @@ impeller_component("geometry") { "shear.h", "size.cc", "size.h", + "type_traits.cc", + "type_traits.h", "vector.cc", "vector.h", ] diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index af5cb6c9bb..3c4330c4b5 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -225,6 +225,13 @@ TEST(GeometryTest, CanConvertTTypesExplicitly) { ASSERT_EQ(s2.height, 2u); } + { + Size s1(1.0, 2.0); + Point p1 = static_cast(s1); + ASSERT_EQ(p1.x, 1u); + ASSERT_EQ(p1.y, 2u); + } + { Rect r1(1.0, 2.0, 3.0, 4.0); IRect r2 = static_cast(r1); @@ -235,6 +242,156 @@ TEST(GeometryTest, CanConvertTTypesExplicitly) { } } +TEST(GeometryTest, CanPerformAlgebraicPointOps) { + { + IPoint p1(1, 2); + IPoint p2 = p1 + IPoint(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(3, 6); + IPoint p2 = p1 - IPoint(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(1, 2); + IPoint p2 = p1 * IPoint(2, 3); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 6u); + } + + { + IPoint p1(2, 6); + IPoint p2 = p1 / IPoint(2, 3); + ASSERT_EQ(p2.x, 1u); + ASSERT_EQ(p2.y, 2u); + } +} + +TEST(GeometryTest, PointIntegerCoercesToFloat) { + // Integer on LHS, float on RHS + { + IPoint p1(1, 2); + Point p2 = p1 + Point(1, 2); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 4u); + } + + { + IPoint p1(3, 6); + Point p2 = p1 - Point(1, 2); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 4u); + } + + { + IPoint p1(1, 2); + Point p2 = p1 * Point(2, 3); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 6u); + } + + { + IPoint p1(2, 6); + Point p2 = p1 / Point(2, 3); + ASSERT_FLOAT_EQ(p2.x, 1u); + ASSERT_FLOAT_EQ(p2.y, 2u); + } + + // Float on LHS, integer on RHS + { + Point p1(1, 2); + Point p2 = p1 + IPoint(1, 2); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 4u); + } + + { + Point p1(3, 6); + Point p2 = p1 - IPoint(1, 2); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 4u); + } + + { + Point p1(1, 2); + Point p2 = p1 * IPoint(2, 3); + ASSERT_FLOAT_EQ(p2.x, 2u); + ASSERT_FLOAT_EQ(p2.y, 6u); + } + + { + Point p1(2, 6); + Point p2 = p1 / IPoint(2, 3); + ASSERT_FLOAT_EQ(p2.x, 1u); + ASSERT_FLOAT_EQ(p2.y, 2u); + } +} + +TEST(GeometryTest, SizeCoercesToPoint) { + // Point on LHS, Size on RHS + { + IPoint p1(1, 2); + IPoint p2 = p1 + ISize(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(3, 6); + IPoint p2 = p1 - ISize(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + IPoint p1(1, 2); + IPoint p2 = p1 * ISize(2, 3); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 6u); + } + + { + IPoint p1(2, 6); + IPoint p2 = p1 / ISize(2, 3); + ASSERT_EQ(p2.x, 1u); + ASSERT_EQ(p2.y, 2u); + } + + // Size on LHS, Point on RHS + { + ISize p1(1, 2); + IPoint p2 = p1 + IPoint(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + ISize p1(3, 6); + IPoint p2 = p1 - IPoint(1, 2); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 4u); + } + + { + ISize p1(1, 2); + IPoint p2 = p1 * IPoint(2, 3); + ASSERT_EQ(p2.x, 2u); + ASSERT_EQ(p2.y, 6u); + } + + { + ISize p1(2, 6); + IPoint p2 = p1 / IPoint(2, 3); + ASSERT_EQ(p2.x, 1u); + ASSERT_EQ(p2.y, 2u); + } +} + TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { { auto deg = Degrees{90.0}; diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index 0d497a70f9..8d183b7771 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -11,6 +11,7 @@ #include "impeller/geometry/scalar.h" #include "impeller/geometry/size.h" +#include "impeller/geometry/type_traits.h" namespace impeller { @@ -27,6 +28,11 @@ struct TPoint { explicit constexpr TPoint(const TPoint& other) : TPoint(static_cast(other.x), static_cast(other.y)) {} + template + explicit constexpr TPoint(const TSize& other) + : TPoint(static_cast(other.width), + static_cast(other.height)) {} + constexpr TPoint(Type x, Type y) : x(x), y(y) {} static constexpr TPoint MakeXY(Type x, Type y) { return {x, y}; } @@ -45,16 +51,18 @@ struct TPoint { return {x + p.x, y + p.y}; } - constexpr TPoint operator+(const TSize& s) const { - return {x + s.width, y + s.height}; + template + constexpr TPoint operator+(const TSize& s) const { + return {x + static_cast(s.width), y + static_cast(s.height)}; } constexpr TPoint operator-(const TPoint& p) const { return {x - p.x, y - p.y}; } - constexpr TPoint operator-(const TSize& s) const { - return {x - s.width, y - s.height}; + template + constexpr TPoint operator-(const TSize& s) const { + return {x - static_cast(s.width), y - static_cast(s.height)}; } constexpr TPoint operator*(Scalar scale) const { @@ -65,8 +73,9 @@ struct TPoint { return {x * p.x, y * p.y}; } - constexpr TPoint operator*(const TSize& s) const { - return {x * s.width, y * s.height}; + template + constexpr TPoint operator*(const TSize& s) const { + return {x * static_cast(s.width), y * static_cast(s.height)}; } constexpr TPoint operator/(Scalar d) const { return {x / d, y / d}; } @@ -75,8 +84,9 @@ struct TPoint { return {x / p.x, y / p.y}; } - constexpr TPoint operator/(const TSize& s) const { - return {x / s.width, y / s.height}; + template + constexpr TPoint operator/(const TSize& s) const { + return {x / static_cast(s.width), y / static_cast(s.height)}; } constexpr Type GetDistanceSquared(const TPoint& p) const { @@ -112,6 +122,70 @@ struct TPoint { constexpr bool IsZero() const { return x == 0 && y == 0; } }; +// Specializations for mixed (float & integer) algebraic operations. + +template > +constexpr TPoint operator+(const TPoint& p1, const TPoint& p2) { + return {p1.x + static_cast(p2.x), p1.y + static_cast(p2.y)}; +} + +template > +constexpr TPoint operator+(const TPoint& p1, const TPoint& p2) { + return p2 + p1; +} + +template > +constexpr TPoint operator-(const TPoint& p1, const TPoint& p2) { + return {p1.x - static_cast(p2.x), p1.y - static_cast(p2.y)}; +} + +template > +constexpr TPoint operator-(const TPoint& p1, const TPoint& p2) { + return {static_cast(p1.x) - p2.x, static_cast(p1.y) - p2.y}; +} + +template > +constexpr TPoint operator*(const TPoint& p1, const TPoint& p2) { + return {p1.x * static_cast(p2.x), p1.y * static_cast(p2.y)}; +} + +template > +constexpr TPoint operator*(const TPoint& p1, const TPoint& p2) { + return p2 * p1; +} + +template > +constexpr TPoint operator/(const TPoint& p1, const TPoint& p2) { + return {p1.x / static_cast(p2.x), p1.y / static_cast(p2.y)}; +} + +template > +constexpr TPoint operator/(const TPoint& p1, const TPoint& p2) { + return {static_cast(p1.x) / p2.x, static_cast(p1.y) / p2.y}; +} + +// RHS algebraic operations with TSize. + +template +constexpr TPoint operator+(const TSize& s, const TPoint& p) { + return p + s; +} + +template +constexpr TPoint operator-(const TSize& s, const TPoint& p) { + return {static_cast(s.width) - p.x, static_cast(s.height) - p.y}; +} + +template +constexpr TPoint operator*(const TSize& s, const TPoint& p) { + return p * s; +} + +template +constexpr TPoint operator/(const TSize& s, const TPoint& p) { + return {static_cast(s.width) / p.x, static_cast(s.height) / p.y}; +} + using Point = TPoint; using IPoint = TPoint; diff --git a/engine/src/flutter/impeller/geometry/type_traits.cc b/engine/src/flutter/impeller/geometry/type_traits.cc new file mode 100644 index 0000000000..84aea9a933 --- /dev/null +++ b/engine/src/flutter/impeller/geometry/type_traits.cc @@ -0,0 +1,11 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "type_traits.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/type_traits.h b/engine/src/flutter/impeller/geometry/type_traits.h new file mode 100644 index 0000000000..4e884d16c1 --- /dev/null +++ b/engine/src/flutter/impeller/geometry/type_traits.h @@ -0,0 +1,20 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include + +namespace impeller { + +template && + std::is_integral_v>> +struct MixedOp_ : public std::true_type {}; + +template +using MixedOp = typename MixedOp_::type; + +} // namespace impeller