2011-05-26 23:41:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#ifndef MOZILLA_GFX_BASEPOINT_H_
|
|
|
|
#define MOZILLA_GFX_BASEPOINT_H_
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2014-04-01 08:02:10 +04:00
|
|
|
#include <cmath>
|
2015-10-26 18:04:14 +03:00
|
|
|
#include <ostream>
|
2013-11-22 23:45:50 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-01-19 22:19:25 +03:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-11-22 23:45:50 +04:00
|
|
|
|
2011-05-26 23:41:33 +04:00
|
|
|
namespace mozilla {
|
2011-06-24 21:41:16 +04:00
|
|
|
namespace gfx {
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not use this class directly. Subclass it, pass that subclass as the
|
|
|
|
* Sub parameter, and only use that subclass. This allows methods to safely
|
|
|
|
* cast 'this' to 'Sub*'.
|
|
|
|
*/
|
2014-08-19 21:08:16 +04:00
|
|
|
template <class T, class Sub, class Coord = T>
|
2011-05-26 23:41:33 +04:00
|
|
|
struct BasePoint {
|
2016-04-18 22:12:36 +03:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
T x, y;
|
|
|
|
};
|
|
|
|
T components[2];
|
|
|
|
};
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
// Constructors
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr BasePoint() : x(0), y(0) {}
|
|
|
|
constexpr BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
void MoveTo(T aX, T aY) { x = aX; y = aY; }
|
|
|
|
void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; }
|
|
|
|
|
|
|
|
// Note that '=' isn't defined so we'll get the
|
|
|
|
// compiler generated default assignment operator
|
|
|
|
|
|
|
|
bool operator==(const Sub& aPoint) const {
|
|
|
|
return x == aPoint.x && y == aPoint.y;
|
|
|
|
}
|
|
|
|
bool operator!=(const Sub& aPoint) const {
|
|
|
|
return x != aPoint.x || y != aPoint.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sub operator+(const Sub& aPoint) const {
|
|
|
|
return Sub(x + aPoint.x, y + aPoint.y);
|
|
|
|
}
|
|
|
|
Sub operator-(const Sub& aPoint) const {
|
|
|
|
return Sub(x - aPoint.x, y - aPoint.y);
|
|
|
|
}
|
|
|
|
Sub& operator+=(const Sub& aPoint) {
|
|
|
|
x += aPoint.x;
|
|
|
|
y += aPoint.y;
|
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
|
|
|
Sub& operator-=(const Sub& aPoint) {
|
|
|
|
x -= aPoint.x;
|
|
|
|
y -= aPoint.y;
|
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sub operator*(T aScale) const {
|
|
|
|
return Sub(x * aScale, y * aScale);
|
|
|
|
}
|
|
|
|
Sub operator/(T aScale) const {
|
|
|
|
return Sub(x / aScale, y / aScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sub operator-() const {
|
|
|
|
return Sub(-x, -y);
|
|
|
|
}
|
2014-04-01 08:02:10 +04:00
|
|
|
|
2015-07-20 21:53:24 +03:00
|
|
|
T DotProduct(const Sub& aPoint) const {
|
|
|
|
return x * aPoint.x + y * aPoint.y;
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:34:36 +03:00
|
|
|
Coord Length() const {
|
2014-08-28 20:45:48 +04:00
|
|
|
return hypot(x, y);
|
2014-06-27 02:37:44 +04:00
|
|
|
}
|
|
|
|
|
2015-08-06 05:42:09 +03:00
|
|
|
T LengthSquare() const {
|
|
|
|
return x * x + y * y;
|
|
|
|
}
|
|
|
|
|
2014-04-01 08:02:10 +04:00
|
|
|
// Round() is *not* rounding to nearest integer if the values are negative.
|
|
|
|
// They are always rounding as floor(n + 0.5).
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
|
|
|
|
Sub& Round() {
|
2014-08-19 21:08:16 +04:00
|
|
|
x = Coord(floor(T(x) + T(0.5)));
|
|
|
|
y = Coord(floor(T(y) + T(0.5)));
|
2014-04-01 08:02:10 +04:00
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:19:25 +03:00
|
|
|
// "Finite" means not inf and not NaN
|
|
|
|
bool IsFinite() const
|
|
|
|
{
|
|
|
|
typedef typename mozilla::Conditional<mozilla::IsSame<T, float>::value, float, double>::Type FloatType;
|
|
|
|
return (mozilla::IsFinite(FloatType(x)) && mozilla::IsFinite(FloatType(y)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 02:51:08 +03:00
|
|
|
void Clamp(T aMaxAbsValue)
|
|
|
|
{
|
|
|
|
x = std::max(std::min(x, aMaxAbsValue), -aMaxAbsValue);
|
|
|
|
y = std::max(std::min(y, aMaxAbsValue), -aMaxAbsValue);
|
|
|
|
}
|
|
|
|
|
2014-08-28 03:25:53 +04:00
|
|
|
friend std::ostream& operator<<(std::ostream& stream, const BasePoint<T, Sub, Coord>& aPoint) {
|
|
|
|
return stream << '(' << aPoint.x << ',' << aPoint.y << ')';
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:41:33 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#endif /* MOZILLA_GFX_BASEPOINT_H_ */
|