Point: Add reflect and make scalar ops more flexible (flutter/engine#51)

This commit is contained in:
Brandon DeRosier
2022-03-02 12:39:34 -08:00
committed by Dan Field
parent a14812c9e8
commit 7c6aeace61
2 changed files with 84 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "impeller/geometry/geometry_unittests.h"
#include <limits>
#include "flutter/testing/testing.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/path_builder.h"
@@ -279,6 +280,38 @@ TEST(GeometryTest, CanPerformAlgebraicPointOps) {
}
}
TEST(GeometryTest, CanPerformAlgebraicPointOpsWithArithmeticTypes) {
// LHS
{
IPoint p1(1, 2);
IPoint p2 = p1 * 2.0f;
ASSERT_EQ(p2.x, 2u);
ASSERT_EQ(p2.y, 4u);
}
{
IPoint p1(2, 6);
IPoint p2 = p1 / 2.0f;
ASSERT_EQ(p2.x, 1u);
ASSERT_EQ(p2.y, 3u);
}
// RHS
{
IPoint p1(1, 2);
IPoint p2 = 2.0f * p1;
ASSERT_EQ(p2.x, 2u);
ASSERT_EQ(p2.y, 4u);
}
{
IPoint p1(2, 6);
IPoint p2 = 12.0f / p1;
ASSERT_EQ(p2.x, 6u);
ASSERT_EQ(p2.y, 2u);
}
}
TEST(GeometryTest, PointIntegerCoercesToFloat) {
// Integer on LHS, float on RHS
{
@@ -499,6 +532,31 @@ TEST(GeometryTest, PointCrossProduct) {
}
}
TEST(GeometryTest, PointReflect) {
{
Point axis = Point(0, 1);
Point a(2, 3);
auto reflected = a.Reflect(axis);
auto expected = Point(2, -3);
ASSERT_POINT_NEAR(reflected, expected);
}
{
Point axis = Point(1, 1).Normalize();
Point a(1, 0);
auto reflected = a.Reflect(axis);
auto expected = Point(0, -1);
ASSERT_POINT_NEAR(reflected, expected);
}
{
Point axis = Point(1, 1).Normalize();
Point a(-1, -1);
auto reflected = a.Reflect(axis);
ASSERT_POINT_NEAR(reflected, -a);
}
}
TEST(GeometryTest, CanConvertBetweenDegressAndRadians) {
{
auto deg = Degrees{90.0};

View File

@@ -8,6 +8,7 @@
#include <cmath>
#include <ostream>
#include <string>
#include <type_traits>
#include "impeller/geometry/scalar.h"
#include "impeller/geometry/size.h"
@@ -121,8 +122,9 @@ struct TPoint {
return {x - static_cast<Type>(s.width), y - static_cast<Type>(s.height)};
}
constexpr TPoint operator*(Scalar scale) const {
return {x * scale, y * scale};
template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint operator*(U scale) const {
return {static_cast<Type>(x * scale), static_cast<Type>(y * scale)};
}
constexpr TPoint operator*(const TPoint& p) const {
@@ -134,7 +136,10 @@ struct TPoint {
return {x * static_cast<Type>(s.width), y * static_cast<Type>(s.height)};
}
constexpr TPoint operator/(Scalar d) const { return {x / d, y / d}; }
template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint operator/(U d) const {
return {static_cast<Type>(x / d), static_cast<Type>(y / d)};
}
constexpr TPoint operator/(const TPoint& p) const {
return {x / p.x, y / p.y};
@@ -175,11 +180,13 @@ struct TPoint {
return {x / length, y / length};
}
constexpr Scalar Cross(const TPoint& p) const {
return (x * p.y) - (y * p.x);
}
constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); }
constexpr Scalar Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
constexpr TPoint Reflect(const TPoint& axis) const {
return *this - axis * this->Dot(axis) * 2;
}
constexpr bool IsZero() const { return x == 0 && y == 0; }
};
@@ -226,6 +233,18 @@ constexpr TPoint<F> operator/(const TPoint<I>& p1, const TPoint<F>& p2) {
return {static_cast<F>(p1.x) / p2.x, static_cast<F>(p1.y) / p2.y};
}
// RHS algebraic operations with arithmetic types.
template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint<T> operator*(U s, const TPoint<T>& p) {
return p * s;
}
template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint<T> operator/(U s, const TPoint<T>& p) {
return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
}
// RHS algebraic operations with TSize.
template <class T, class U>