Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / gfx / 2d / BasePoint.h
blob7f5dd7e1e3462716fba555c361bff500f517b3a6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_BASEPOINT_H_
8 #define MOZILLA_GFX_BASEPOINT_H_
10 #include <cmath>
11 #include <ostream>
12 #include <type_traits>
13 #include "mozilla/Attributes.h"
14 #include "mozilla/FloatingPoint.h"
16 namespace mozilla {
17 namespace gfx {
19 /**
20 * Do not use this class directly. Subclass it, pass that subclass as the
21 * Sub parameter, and only use that subclass. This allows methods to safely
22 * cast 'this' to 'Sub*'.
24 template <class T, class Sub, class Coord = T>
25 struct BasePoint {
26 union {
27 struct {
28 Coord x, y;
30 Coord components[2];
33 // Constructors
34 constexpr BasePoint() : x(0), y(0) {}
35 constexpr BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {}
37 MOZ_ALWAYS_INLINE Coord X() const { return x; }
38 MOZ_ALWAYS_INLINE Coord Y() const { return y; }
40 void MoveTo(Coord aX, Coord aY) {
41 x = aX;
42 y = aY;
44 void MoveBy(Coord aDx, Coord aDy) {
45 x += aDx;
46 y += aDy;
49 // Note that '=' isn't defined so we'll get the
50 // compiler generated default assignment operator
52 bool operator==(const Sub& aPoint) const {
53 return x == aPoint.x && y == aPoint.y;
55 bool operator!=(const Sub& aPoint) const {
56 return x != aPoint.x || y != aPoint.y;
59 Sub operator+(const Sub& aPoint) const {
60 return Sub(x + aPoint.x, y + aPoint.y);
62 Sub operator-(const Sub& aPoint) const {
63 return Sub(x - aPoint.x, y - aPoint.y);
65 Sub& operator+=(const Sub& aPoint) {
66 x += aPoint.x;
67 y += aPoint.y;
68 return *static_cast<Sub*>(this);
70 Sub& operator-=(const Sub& aPoint) {
71 x -= aPoint.x;
72 y -= aPoint.y;
73 return *static_cast<Sub*>(this);
76 Sub operator*(T aScale) const { return Sub(x * aScale, y * aScale); }
77 Sub operator/(T aScale) const { return Sub(x / aScale, y / aScale); }
79 Sub operator-() const { return Sub(-x, -y); }
81 T DotProduct(const Sub& aPoint) const {
82 return x.value * aPoint.x.value + y.value * aPoint.y.value;
85 // FIXME: Maybe Length() should return a float Coord event for integer Points?
86 Coord Length() const {
87 return static_cast<decltype(x.value)>(hypot(x.value, y.value));
90 T LengthSquare() const { return x.value * x.value + y.value * y.value; }
92 // Round() is *not* rounding to nearest integer if the values are negative.
93 // They are always rounding as floor(n + 0.5).
94 // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
95 Sub& Round() {
96 x = Coord(std::floor(T(x) + T(0.5f)));
97 y = Coord(std::floor(T(y) + T(0.5f)));
98 return *static_cast<Sub*>(this);
101 // "Finite" means not inf and not NaN
102 bool IsFinite() const {
103 using FloatType =
104 std::conditional_t<std::is_same_v<T, float>, float, double>;
105 return (std::isfinite(FloatType(x)) && std::isfinite(FloatType(y)));
108 void Clamp(Coord aMaxAbsValue) {
109 x = std::max(std::min(x, aMaxAbsValue), -aMaxAbsValue);
110 y = std::max(std::min(y, aMaxAbsValue), -aMaxAbsValue);
113 friend std::ostream& operator<<(std::ostream& stream,
114 const BasePoint<T, Sub, Coord>& aPoint) {
115 return stream << '(' << aPoint.x << ',' << aPoint.y << ')';
119 } // namespace gfx
120 } // namespace mozilla
122 #endif /* MOZILLA_GFX_BASEPOINT_H_ */