Geometry utils for unsigned types.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "impeller/geometry/point.h"
|
||||
#include "impeller/geometry/rect.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -19,5 +21,30 @@ TEST(GeometryTest, CanGenerateMipCounts) {
|
||||
ASSERT_EQ((Size{-128, 25}.MipCount()), 1u);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, CanConvertTTypesExplicitly) {
|
||||
{
|
||||
Point p1(1.0, 2.0);
|
||||
IPoint p2 = static_cast<IPoint>(p1);
|
||||
ASSERT_EQ(p2.x, 1u);
|
||||
ASSERT_EQ(p2.y, 2u);
|
||||
}
|
||||
|
||||
{
|
||||
Size s1(1.0, 2.0);
|
||||
ISize s2 = static_cast<ISize>(s1);
|
||||
ASSERT_EQ(s2.width, 1u);
|
||||
ASSERT_EQ(s2.height, 2u);
|
||||
}
|
||||
|
||||
{
|
||||
Rect r1(1.0, 2.0, 3.0, 4.0);
|
||||
IRect r2 = static_cast<IRect>(r1);
|
||||
ASSERT_EQ(r2.origin.x, 1u);
|
||||
ASSERT_EQ(r2.origin.y, 2u);
|
||||
ASSERT_EQ(r2.size.width, 3u);
|
||||
ASSERT_EQ(r2.size.height, 4u);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
@@ -12,65 +12,81 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
struct Point {
|
||||
Scalar x = 0.0;
|
||||
Scalar y = 0.0;
|
||||
template <class T>
|
||||
struct TPoint {
|
||||
using Type = T;
|
||||
|
||||
constexpr Point() = default;
|
||||
Type x = {};
|
||||
Type y = {};
|
||||
|
||||
constexpr Point(Scalar x, Scalar y) : x(x), y(y) {}
|
||||
constexpr TPoint() = default;
|
||||
|
||||
constexpr bool operator==(const Point& p) const {
|
||||
template <class U>
|
||||
explicit constexpr TPoint(const TPoint<U>& other)
|
||||
: TPoint(static_cast<Type>(other.x), static_cast<Type>(other.y)) {}
|
||||
|
||||
constexpr TPoint(Type x, Type y) : x(x), y(y) {}
|
||||
|
||||
constexpr bool operator==(const TPoint& p) const {
|
||||
return p.x == x && p.y == y;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(const Point& p) const {
|
||||
constexpr bool operator!=(const TPoint& p) const {
|
||||
return p.x != x || p.y != y;
|
||||
}
|
||||
|
||||
constexpr Point operator-() const { return {-x, -y}; }
|
||||
constexpr TPoint operator-() const { return {-x, -y}; }
|
||||
|
||||
constexpr Point operator+(const Point& p) const { return {x + p.x, y + p.y}; }
|
||||
constexpr TPoint operator+(const TPoint& p) const {
|
||||
return {x + p.x, y + p.y};
|
||||
}
|
||||
|
||||
constexpr Point operator+(const Size& s) const {
|
||||
constexpr TPoint operator+(const TSize<Type>& s) const {
|
||||
return {x + s.width, y + s.height};
|
||||
}
|
||||
|
||||
constexpr Point operator-(const Point& p) const { return {x - p.x, y - p.y}; }
|
||||
constexpr TPoint operator-(const TPoint& p) const {
|
||||
return {x - p.x, y - p.y};
|
||||
}
|
||||
|
||||
constexpr Point operator-(const Size& s) const {
|
||||
constexpr TPoint operator-(const TSize<Type>& s) const {
|
||||
return {x - s.width, y - s.height};
|
||||
}
|
||||
|
||||
constexpr Point operator*(Scalar scale) const {
|
||||
constexpr TPoint operator*(Type scale) const {
|
||||
return {x * scale, y * scale};
|
||||
}
|
||||
|
||||
constexpr Point operator*(const Point& p) const { return {x * p.x, y * p.y}; }
|
||||
constexpr TPoint operator*(const TPoint& p) const {
|
||||
return {x * p.x, y * p.y};
|
||||
}
|
||||
|
||||
constexpr Point operator*(const Size& s) const {
|
||||
constexpr TPoint operator*(const TSize<Type>& s) const {
|
||||
return {x * s.width, y * s.height};
|
||||
}
|
||||
|
||||
constexpr Point operator/(Scalar d) const { return {x / d, y / d}; }
|
||||
constexpr TPoint operator/(Type d) const { return {x / d, y / d}; }
|
||||
|
||||
constexpr Point operator/(const Point& p) const { return {x / p.x, y / p.y}; }
|
||||
constexpr TPoint operator/(const TPoint& p) const {
|
||||
return {x / p.x, y / p.y};
|
||||
}
|
||||
|
||||
constexpr Point operator/(const Size& s) const {
|
||||
constexpr TPoint operator/(const TSize<Type>& s) const {
|
||||
return {x / s.width, y / s.height};
|
||||
}
|
||||
|
||||
constexpr Scalar GetDistanceSquared(const Point& p) const {
|
||||
constexpr Type GetDistanceSquared(const TPoint& p) const {
|
||||
double dx = p.x - x;
|
||||
double dy = p.y - y;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
|
||||
constexpr Scalar GetDistance(const Point& p) const {
|
||||
constexpr Type GetDistance(const TPoint& p) const {
|
||||
return sqrt(GetDistanceSquared(p));
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(Point) == 2 * sizeof(Scalar));
|
||||
using Point = TPoint<Scalar>;
|
||||
using IPoint = TPoint<int64_t>;
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -7,35 +7,6 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Rect Rect::WithPoint(const Point& p) const {
|
||||
Rect copy = *this;
|
||||
if (p.x < origin.x) {
|
||||
copy.origin.x = p.x;
|
||||
copy.size.width += (origin.x - p.x);
|
||||
}
|
||||
|
||||
if (p.y < origin.y) {
|
||||
copy.origin.y = p.y;
|
||||
copy.size.height += (origin.y - p.y);
|
||||
}
|
||||
|
||||
if (p.x > (size.width + origin.x)) {
|
||||
copy.size.width += p.x - (size.width + origin.x);
|
||||
}
|
||||
|
||||
if (p.y > (size.height + origin.y)) {
|
||||
copy.size.height += p.y - (size.height + origin.y);
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
Rect Rect::WithPoints(const std::vector<Point>& points) const {
|
||||
Rect box = *this;
|
||||
for (const auto& point : points) {
|
||||
box = box.WithPoint(point);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
//
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -12,62 +12,96 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
struct Rect {
|
||||
Point origin;
|
||||
Size size;
|
||||
template <class T>
|
||||
struct TRect {
|
||||
using Type = T;
|
||||
|
||||
constexpr Rect() : origin({0.0, 0.0}), size({0.0, 0.0}) {}
|
||||
TPoint<Type> origin;
|
||||
TSize<Type> size;
|
||||
|
||||
constexpr Rect(Size size) : origin({0.0, 0.0}), size(size) {}
|
||||
constexpr TRect() : origin({0.0, 0.0}), size({0.0, 0.0}) {}
|
||||
|
||||
constexpr Rect(Point origin, Size size) : origin(origin), size(size) {}
|
||||
constexpr TRect(TSize<Type> size) : origin({0.0, 0.0}), size(size) {}
|
||||
|
||||
constexpr Rect(const Scalar components[4])
|
||||
constexpr TRect(TPoint<Type> origin, TSize<Type> size)
|
||||
: origin(origin), size(size) {}
|
||||
|
||||
constexpr TRect(const Type components[4])
|
||||
: origin(components[0], components[1]),
|
||||
size(components[2], components[3]) {}
|
||||
|
||||
constexpr Rect(Scalar x, Scalar y, Scalar width, Scalar height)
|
||||
constexpr TRect(Type x, Type y, Type width, Type height)
|
||||
: origin(x, y), size(width, height) {}
|
||||
|
||||
/*
|
||||
* Operator overloads
|
||||
*/
|
||||
constexpr Rect operator+(const Rect& r) const {
|
||||
return Rect({origin.x + r.origin.x, origin.y + r.origin.y},
|
||||
{size.width + r.size.width, size.height + r.size.height});
|
||||
template <class U>
|
||||
constexpr explicit TRect(const TRect<U>& other)
|
||||
: origin(static_cast<TPoint<Type>>(other.origin)),
|
||||
size(static_cast<TSize<Type>>(other.size)) {}
|
||||
|
||||
constexpr TRect operator+(const TRect& r) const {
|
||||
return TRect({origin.x + r.origin.x, origin.y + r.origin.y},
|
||||
{size.width + r.size.width, size.height + r.size.height});
|
||||
}
|
||||
|
||||
constexpr Rect operator-(const Rect& r) const {
|
||||
return Rect({origin.x - r.origin.x, origin.y - r.origin.y},
|
||||
{size.width - r.size.width, size.height - r.size.height});
|
||||
constexpr TRect operator-(const TRect& r) const {
|
||||
return TRect({origin.x - r.origin.x, origin.y - r.origin.y},
|
||||
{size.width - r.size.width, size.height - r.size.height});
|
||||
}
|
||||
|
||||
constexpr Rect operator*(Scalar scale) const {
|
||||
return Rect({origin.x * scale, origin.y * scale},
|
||||
{size.width * scale, size.height * scale});
|
||||
constexpr TRect operator*(Type scale) const {
|
||||
return TRect({origin.x * scale, origin.y * scale},
|
||||
{size.width * scale, size.height * scale});
|
||||
}
|
||||
|
||||
constexpr Rect operator*(const Rect& r) const {
|
||||
return Rect({origin.x * r.origin.x, origin.y * r.origin.y},
|
||||
{size.width * r.size.width, size.height * r.size.height});
|
||||
constexpr TRect operator*(const TRect& r) const {
|
||||
return TRect({origin.x * r.origin.x, origin.y * r.origin.y},
|
||||
{size.width * r.size.width, size.height * r.size.height});
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Rect& r) const {
|
||||
constexpr bool operator==(const TRect& r) const {
|
||||
return origin == r.origin && size == r.size;
|
||||
}
|
||||
|
||||
constexpr bool Contains(const Point& p) const {
|
||||
constexpr bool Contains(const TPoint<Type>& p) const {
|
||||
return p.x >= origin.x && p.x <= size.width && p.y >= origin.y &&
|
||||
p.y <= size.height;
|
||||
}
|
||||
|
||||
constexpr bool IsZero() const { return size.IsZero(); }
|
||||
|
||||
Rect WithPoint(const Point& p) const;
|
||||
constexpr TRect WithPoint(const TPoint<Type>& p) const {
|
||||
TRect copy = *this;
|
||||
if (p.x < origin.x) {
|
||||
copy.origin.x = p.x;
|
||||
copy.size.width += (origin.x - p.x);
|
||||
}
|
||||
|
||||
Rect WithPoints(const std::vector<Point>& points) const;
|
||||
if (p.y < origin.y) {
|
||||
copy.origin.y = p.y;
|
||||
copy.size.height += (origin.y - p.y);
|
||||
}
|
||||
|
||||
if (p.x > (size.width + origin.x)) {
|
||||
copy.size.width += p.x - (size.width + origin.x);
|
||||
}
|
||||
|
||||
if (p.y > (size.height + origin.y)) {
|
||||
copy.size.height += p.y - (size.height + origin.y);
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
constexpr TRect WithPoints(const std::vector<TPoint<Type>>& points) const {
|
||||
TRect box = *this;
|
||||
for (const auto& point : points) {
|
||||
box = box.WithPoint(point);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(Rect) == 4 * sizeof(Scalar));
|
||||
using Rect = TRect<Scalar>;
|
||||
using IRect = TRect<int64_t>;
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -23,6 +23,11 @@ struct TSize {
|
||||
|
||||
constexpr TSize(Type width, Type height) : width(width), height(height) {}
|
||||
|
||||
template <class U>
|
||||
explicit constexpr TSize(const TSize<U>& other)
|
||||
: TSize(static_cast<Type>(other.width), static_cast<Type>(other.height)) {
|
||||
}
|
||||
|
||||
static constexpr TSize Infinite() {
|
||||
return TSize{std::numeric_limits<Type>::max(),
|
||||
std::numeric_limits<Type>::max()};
|
||||
|
||||
Reference in New Issue
Block a user