/src/mozilla-central/gfx/2d/BasePoint.h
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/Attributes.h" |
13 | | #include "mozilla/FloatingPoint.h" |
14 | | #include "mozilla/TypeTraits.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace gfx { |
18 | | |
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*'. |
23 | | */ |
24 | | template <class T, class Sub, class Coord = T> |
25 | | struct BasePoint { |
26 | | union { |
27 | | struct { |
28 | | T x, y; |
29 | | }; |
30 | | T components[2]; |
31 | | }; |
32 | | |
33 | | // Constructors |
34 | | constexpr BasePoint() : x(0), y(0) {} |
35 | 0 | constexpr BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {} Unexecuted instantiation: mozilla::gfx::BasePoint<int, mozilla::gfx::IntPointTyped<mozilla::gfx::UnknownUnits>, mozilla::gfx::IntCoordTyped<mozilla::gfx::UnknownUnits> >::BasePoint(mozilla::gfx::IntCoordTyped<mozilla::gfx::UnknownUnits>, mozilla::gfx::IntCoordTyped<mozilla::gfx::UnknownUnits>) Unexecuted instantiation: mozilla::gfx::BasePoint<float, mozilla::gfx::PointTyped<mozilla::gfx::UnknownUnits, float>, mozilla::gfx::CoordTyped<mozilla::gfx::UnknownUnits, float> >::BasePoint(mozilla::gfx::CoordTyped<mozilla::gfx::UnknownUnits, float>, mozilla::gfx::CoordTyped<mozilla::gfx::UnknownUnits, float>) |
36 | | |
37 | | MOZ_ALWAYS_INLINE T X() const { return x; } |
38 | | MOZ_ALWAYS_INLINE T Y() const { return y; } |
39 | | |
40 | | void MoveTo(T aX, T aY) { x = aX; y = aY; } |
41 | | void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; } |
42 | | |
43 | | // Note that '=' isn't defined so we'll get the |
44 | | // compiler generated default assignment operator |
45 | | |
46 | | bool operator==(const Sub& aPoint) const { |
47 | | return x == aPoint.x && y == aPoint.y; |
48 | | } |
49 | 0 | bool operator!=(const Sub& aPoint) const { |
50 | 0 | return x != aPoint.x || y != aPoint.y; |
51 | 0 | } |
52 | | |
53 | 0 | Sub operator+(const Sub& aPoint) const { |
54 | 0 | return Sub(x + aPoint.x, y + aPoint.y); |
55 | 0 | } Unexecuted instantiation: mozilla::gfx::BasePoint<float, mozilla::gfx::PointTyped<mozilla::gfx::UnknownUnits, float>, mozilla::gfx::CoordTyped<mozilla::gfx::UnknownUnits, float> >::operator+(mozilla::gfx::PointTyped<mozilla::gfx::UnknownUnits, float> const&) const Unexecuted instantiation: mozilla::gfx::BasePoint<int, mozilla::gfx::IntPointTyped<mozilla::gfx::UnknownUnits>, mozilla::gfx::IntCoordTyped<mozilla::gfx::UnknownUnits> >::operator+(mozilla::gfx::IntPointTyped<mozilla::gfx::UnknownUnits> const&) const |
56 | 0 | Sub operator-(const Sub& aPoint) const { |
57 | 0 | return Sub(x - aPoint.x, y - aPoint.y); |
58 | 0 | } |
59 | | Sub& operator+=(const Sub& aPoint) { |
60 | | x += aPoint.x; |
61 | | y += aPoint.y; |
62 | | return *static_cast<Sub*>(this); |
63 | | } |
64 | | Sub& operator-=(const Sub& aPoint) { |
65 | | x -= aPoint.x; |
66 | | y -= aPoint.y; |
67 | | return *static_cast<Sub*>(this); |
68 | | } |
69 | | |
70 | 0 | Sub operator*(T aScale) const { |
71 | 0 | return Sub(x * aScale, y * aScale); |
72 | 0 | } |
73 | | Sub operator/(T aScale) const { |
74 | | return Sub(x / aScale, y / aScale); |
75 | | } |
76 | | |
77 | 0 | Sub operator-() const { |
78 | 0 | return Sub(-x, -y); |
79 | 0 | } |
80 | | |
81 | | T DotProduct(const Sub& aPoint) const { |
82 | | return x * aPoint.x + y * aPoint.y; |
83 | | } |
84 | | |
85 | | Coord Length() const { |
86 | | return hypot(x, y); |
87 | | } |
88 | | |
89 | | T LengthSquare() const { |
90 | | return x * x + y * y; |
91 | | } |
92 | | |
93 | | // Round() is *not* rounding to nearest integer if the values are negative. |
94 | | // They are always rounding as floor(n + 0.5). |
95 | | // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14 |
96 | | Sub& Round() { |
97 | | x = Coord(std::floor(T(x) + T(0.5f))); |
98 | | y = Coord(std::floor(T(y) + T(0.5f))); |
99 | | return *static_cast<Sub*>(this); |
100 | | } |
101 | | |
102 | | // "Finite" means not inf and not NaN |
103 | | bool IsFinite() const |
104 | 0 | { |
105 | 0 | typedef typename mozilla::Conditional<mozilla::IsSame<T, float>::value, float, double>::Type FloatType; |
106 | 0 | return (mozilla::IsFinite(FloatType(x)) && mozilla::IsFinite(FloatType(y))); |
107 | 0 | return true; |
108 | 0 | } |
109 | | |
110 | | void Clamp(T aMaxAbsValue) |
111 | | { |
112 | | x = std::max(std::min(x, aMaxAbsValue), -aMaxAbsValue); |
113 | | y = std::max(std::min(y, aMaxAbsValue), -aMaxAbsValue); |
114 | | } |
115 | | |
116 | | friend std::ostream& operator<<(std::ostream& stream, const BasePoint<T, Sub, Coord>& aPoint) { |
117 | | return stream << '(' << aPoint.x << ',' << aPoint.y << ')'; |
118 | | } |
119 | | |
120 | | }; |
121 | | |
122 | | } // namespace gfx |
123 | | } // namespace mozilla |
124 | | |
125 | | #endif /* MOZILLA_GFX_BASEPOINT_H_ */ |