/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */ #ifndef MOZILLA_GFX_POINT_H_ #define MOZILLA_GFX_POINT_H_ #include "mozilla/Attributes.h" #include "Types.h" #include "Coord.h" #include "BaseCoord.h" #include "BasePoint.h" #include "BasePoint3D.h" #include "BasePoint4D.h" #include "BaseSize.h" #include "mozilla/Maybe.h" #include #include namespace mozilla { template struct IsPixel; namespace gfx { // This should only be used by the typedefs below. struct UnknownUnits {}; } // namespace gfx template <> struct IsPixel : std::true_type {}; namespace gfx { /// Use this for parameters of functions to allow implicit conversions to /// integer types but not floating point types. /// We use this wrapper to prevent IntSize and IntPoint's constructors to /// take foating point values as parameters, and not require their constructors /// to have implementations for each permutation of integer types. template struct IntParam { constexpr MOZ_IMPLICIT IntParam(char val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(unsigned char val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(short val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(unsigned short val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(int val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(unsigned int val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(long val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(unsigned long val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(long long val) : value(val) {} constexpr MOZ_IMPLICIT IntParam(unsigned long long val) : value(val) {} template constexpr MOZ_IMPLICIT IntParam(IntCoordTyped val) : value(val) {} // Disable the evil ones! MOZ_IMPLICIT IntParam(float val) = delete; MOZ_IMPLICIT IntParam(double val) = delete; T value; }; template struct PointTyped; template struct SizeTyped; template struct IntPointTyped : public BasePoint, IntCoordTyped >, public units { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef IntParam ToInt; typedef IntCoordTyped Coord; typedef BasePoint, IntCoordTyped > Super; constexpr IntPointTyped() : Super() {} constexpr IntPointTyped(ToInt aX, ToInt aY) : Super(Coord(aX.value), Coord(aY.value)) {} static IntPointTyped Round(float aX, float aY) { return IntPointTyped(int32_t(floorf(aX + 0.5)), int32_t(floorf(aY + 0.5))); } static IntPointTyped Ceil(float aX, float aY) { return IntPointTyped(int32_t(ceil(aX)), int32_t(ceil(aY))); } static IntPointTyped Floor(float aX, float aY) { return IntPointTyped(int32_t(floorf(aX)), int32_t(floorf(aY))); } static IntPointTyped Truncate(float aX, float aY) { return IntPointTyped(int32_t(aX), int32_t(aY)); } static IntPointTyped Round(const PointTyped& aPoint); static IntPointTyped Ceil(const PointTyped& aPoint); static IntPointTyped Floor(const PointTyped& aPoint); static IntPointTyped Truncate(const PointTyped& aPoint); // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static IntPointTyped FromUnknownPoint( const IntPointTyped& aPoint) { return IntPointTyped(aPoint.x, aPoint.y); } IntPointTyped ToUnknownPoint() const { return IntPointTyped(this->x, this->y); } }; typedef IntPointTyped IntPoint; template struct PointTyped : public BasePoint, CoordTyped >, public units { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef CoordTyped Coord; typedef BasePoint, CoordTyped > Super; constexpr PointTyped() : Super() {} constexpr PointTyped(F aX, F aY) : Super(Coord(aX), Coord(aY)) {} // The mixed-type constructors (Float, Coord) and (Coord, Float) are needed to // avoid ambiguities because Coord is implicitly convertible to Float. constexpr PointTyped(F aX, Coord aY) : Super(Coord(aX), aY) {} constexpr PointTyped(Coord aX, F aY) : Super(aX, Coord(aY)) {} constexpr PointTyped(Coord aX, Coord aY) : Super(aX.value, aY.value) {} constexpr MOZ_IMPLICIT PointTyped(const IntPointTyped& point) : Super(F(point.x), F(point.y)) {} bool WithinEpsilonOf(const PointTyped& aPoint, F aEpsilon) const { return fabs(aPoint.x - this->x) < aEpsilon && fabs(aPoint.y - this->y) < aEpsilon; } // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static PointTyped FromUnknownPoint( const PointTyped& aPoint) { return PointTyped(aPoint.x, aPoint.y); } PointTyped ToUnknownPoint() const { return PointTyped(this->x, this->y); } }; typedef PointTyped Point; typedef PointTyped PointDouble; template IntPointTyped RoundedToInt(const PointTyped& aPoint) { return IntPointTyped::Round(aPoint.x, aPoint.y); } template IntPointTyped TruncatedToInt(const PointTyped& aPoint) { return IntPointTyped::Truncate(aPoint.x, aPoint.y); } template struct Point3DTyped : public BasePoint3D > { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef BasePoint3D > Super; Point3DTyped() : Super() {} Point3DTyped(F aX, F aY, F aZ) : Super(aX, aY, aZ) {} // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static Point3DTyped FromUnknownPoint( const Point3DTyped& aPoint) { return Point3DTyped(aPoint.x, aPoint.y, aPoint.z); } Point3DTyped ToUnknownPoint() const { return Point3DTyped(this->x, this->y, this->z); } }; typedef Point3DTyped Point3D; typedef Point3DTyped PointDouble3D; template IntPointTyped IntPointTyped::Round( const PointTyped& aPoint) { return IntPointTyped::Round(aPoint.x, aPoint.y); } template IntPointTyped IntPointTyped::Ceil( const PointTyped& aPoint) { return IntPointTyped::Ceil(aPoint.x, aPoint.y); } template IntPointTyped IntPointTyped::Floor( const PointTyped& aPoint) { return IntPointTyped::Floor(aPoint.x, aPoint.y); } template IntPointTyped IntPointTyped::Truncate( const PointTyped& aPoint) { return IntPointTyped::Truncate(aPoint.x, aPoint.y); } template struct Point4DTyped : public BasePoint4D > { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef BasePoint4D > Super; Point4DTyped() : Super() {} Point4DTyped(F aX, F aY, F aZ, F aW) : Super(aX, aY, aZ, aW) {} explicit Point4DTyped(const Point3DTyped& aPoint) : Super(aPoint.x, aPoint.y, aPoint.z, 1) {} // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static Point4DTyped FromUnknownPoint( const Point4DTyped& aPoint) { return Point4DTyped(aPoint.x, aPoint.y, aPoint.z, aPoint.w); } Point4DTyped ToUnknownPoint() const { return Point4DTyped(this->x, this->y, this->z, this->w); } PointTyped As2DPoint() const { return PointTyped(this->x / this->w, this->y / this->w); } Point3DTyped As3DPoint() const { return Point3DTyped(this->x / this->w, this->y / this->w, this->z / this->w); } }; typedef Point4DTyped Point4D; typedef Point4DTyped PointDouble4D; template struct IntSizeTyped : public BaseSize >, public units { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef IntParam ToInt; typedef BaseSize > Super; constexpr IntSizeTyped() : Super() {} constexpr IntSizeTyped(ToInt aWidth, ToInt aHeight) : Super(aWidth.value, aHeight.value) {} static IntSizeTyped Round(float aWidth, float aHeight) { return IntSizeTyped(int32_t(floorf(aWidth + 0.5)), int32_t(floorf(aHeight + 0.5))); } static IntSizeTyped Truncate(float aWidth, float aHeight) { return IntSizeTyped(int32_t(aWidth), int32_t(aHeight)); } static IntSizeTyped Ceil(float aWidth, float aHeight) { return IntSizeTyped(int32_t(ceil(aWidth)), int32_t(ceil(aHeight))); } static IntSizeTyped Floor(float aWidth, float aHeight) { return IntSizeTyped(int32_t(floorf(aWidth)), int32_t(floorf(aHeight))); } static IntSizeTyped Round(const SizeTyped& aSize); static IntSizeTyped Ceil(const SizeTyped& aSize); static IntSizeTyped Floor(const SizeTyped& aSize); static IntSizeTyped Truncate(const SizeTyped& aSize); // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static IntSizeTyped FromUnknownSize( const IntSizeTyped& aSize) { return IntSizeTyped(aSize.width, aSize.height); } IntSizeTyped ToUnknownSize() const { return IntSizeTyped(this->width, this->height); } }; typedef IntSizeTyped IntSize; typedef Maybe MaybeIntSize; template struct SizeTyped : public BaseSize >, public units { static_assert(IsPixel::value, "'units' must be a coordinate system tag"); typedef BaseSize > Super; constexpr SizeTyped() : Super() {} constexpr SizeTyped(F aWidth, F aHeight) : Super(aWidth, aHeight) {} explicit SizeTyped(const IntSizeTyped& size) : Super(F(size.width), F(size.height)) {} // XXX When all of the code is ported, the following functions to convert to // and from unknown types should be removed. static SizeTyped FromUnknownSize( const SizeTyped& aSize) { return SizeTyped(aSize.width, aSize.height); } SizeTyped ToUnknownSize() const { return SizeTyped(this->width, this->height); } }; typedef SizeTyped Size; typedef SizeTyped SizeDouble; template IntSizeTyped RoundedToInt(const SizeTyped& aSize) { return IntSizeTyped(int32_t(floorf(aSize.width + 0.5f)), int32_t(floorf(aSize.height + 0.5f))); } template IntSizeTyped IntSizeTyped::Round( const SizeTyped& aSize) { return IntSizeTyped::Round(aSize.width, aSize.height); } template IntSizeTyped IntSizeTyped::Ceil( const SizeTyped& aSize) { return IntSizeTyped::Ceil(aSize.width, aSize.height); } template IntSizeTyped IntSizeTyped::Floor( const SizeTyped& aSize) { return IntSizeTyped::Floor(aSize.width, aSize.height); } template IntSizeTyped IntSizeTyped::Truncate( const SizeTyped& aSize) { return IntSizeTyped::Truncate(aSize.width, aSize.height); } } // namespace gfx } // namespace mozilla #endif /* MOZILLA_GFX_POINT_H_ */