BasePoint.h (4050B)
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/. */ 6 7 #ifndef MOZILLA_GFX_BASEPOINT_H_ 8 #define MOZILLA_GFX_BASEPOINT_H_ 9 10 #include <cmath> 11 #include <ostream> 12 #include <type_traits> 13 #include "mozilla/Attributes.h" 14 #include "Coord.h" 15 16 namespace mozilla { 17 namespace gfx { 18 19 template <class T, typename EnableT = void> 20 struct FloatType; 21 22 template <typename T> 23 struct FloatType<T, typename std::enable_if_t<std::is_integral_v<T>>> { 24 using type = float; 25 }; 26 27 template <typename T> 28 struct FloatType<T, typename std::enable_if_t<std::is_floating_point_v<T>>> { 29 using type = T; 30 }; 31 32 template <typename Units, typename Rep> 33 struct FloatType<IntCoordTyped<Units, Rep>> { 34 using type = CoordTyped<Units, float>; 35 }; 36 37 template <typename Units, typename Rep> 38 struct FloatType<CoordTyped<Units, Rep>> { 39 using type = CoordTyped<Units, Rep>; 40 }; 41 42 template <typename T> 43 using FloatType_t = typename FloatType<T>::type; 44 45 /** 46 * Do not use this class directly. Subclass it, pass that subclass as the 47 * Sub parameter, and only use that subclass. This allows methods to safely 48 * cast 'this' to 'Sub*'. 49 */ 50 template <class T, class Sub, class Coord = T> 51 struct BasePoint { 52 union { 53 struct { 54 Coord x, y; 55 }; 56 Coord components[2]; 57 }; 58 59 // Constructors 60 constexpr BasePoint() : x(0), y(0) {} 61 constexpr BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {} 62 63 MOZ_ALWAYS_INLINE Coord X() const { return x; } 64 MOZ_ALWAYS_INLINE Coord Y() const { return y; } 65 66 void MoveTo(Coord aX, Coord aY) { 67 x = aX; 68 y = aY; 69 } 70 void MoveBy(Coord aDx, Coord aDy) { 71 x += aDx; 72 y += aDy; 73 } 74 75 // Note that '=' isn't defined so we'll get the 76 // compiler generated default assignment operator 77 78 bool operator==(const Sub& aPoint) const { 79 return x == aPoint.x && y == aPoint.y; 80 } 81 bool operator!=(const Sub& aPoint) const { 82 return x != aPoint.x || y != aPoint.y; 83 } 84 85 Sub operator+(const Sub& aPoint) const { 86 return Sub(x + aPoint.x, y + aPoint.y); 87 } 88 Sub operator-(const Sub& aPoint) const { 89 return Sub(x - aPoint.x, y - aPoint.y); 90 } 91 Sub& operator+=(const Sub& aPoint) { 92 x += aPoint.x; 93 y += aPoint.y; 94 return *static_cast<Sub*>(this); 95 } 96 Sub& operator-=(const Sub& aPoint) { 97 x -= aPoint.x; 98 y -= aPoint.y; 99 return *static_cast<Sub*>(this); 100 } 101 102 Sub operator*(T aScale) const { return Sub(x * aScale, y * aScale); } 103 Sub operator/(T aScale) const { return Sub(x / aScale, y / aScale); } 104 105 Sub operator-() const { return Sub(-x, -y); } 106 107 T DotProduct(const Sub& aPoint) const { 108 return x.value * aPoint.x.value + y.value * aPoint.y.value; 109 } 110 111 FloatType_t<Coord> Length() const { 112 return FloatType_t<Coord>(hypot(x.value, y.value)); 113 } 114 115 T LengthSquare() const { return x.value * x.value + y.value * y.value; } 116 117 // Round() is *not* rounding to nearest integer if the values are negative. 118 // They are always rounding as floor(n + 0.5). 119 // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14 120 Sub& Round() { 121 x = Coord(std::floor(T(x) + T(0.5f))); 122 y = Coord(std::floor(T(y) + T(0.5f))); 123 return *static_cast<Sub*>(this); 124 } 125 126 // "Finite" means not inf and not NaN 127 bool IsFinite() const { 128 using FloatType = 129 std::conditional_t<std::is_same_v<T, float>, float, double>; 130 return (std::isfinite(FloatType(x)) && std::isfinite(FloatType(y))); 131 } 132 133 void Clamp(Coord aMaxAbsValue) { 134 x = std::clamp(x, -aMaxAbsValue, aMaxAbsValue); 135 y = std::clamp(y, -aMaxAbsValue, aMaxAbsValue); 136 } 137 138 friend std::ostream& operator<<(std::ostream& stream, 139 const BasePoint<T, Sub, Coord>& aPoint) { 140 return stream << '(' << aPoint.x << ',' << aPoint.y << ')'; 141 } 142 }; 143 144 } // namespace gfx 145 } // namespace mozilla 146 147 #endif /* MOZILLA_GFX_BASEPOINT_H_ */