2011-06-24 17:41:16 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00: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 19:41:33 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#ifndef MOZILLA_GFX_POINT_H_
|
|
|
|
#define MOZILLA_GFX_POINT_H_
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-11-22 19:45:50 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "Types.h"
|
2014-08-19 17:08:16 +00:00
|
|
|
#include "Coord.h"
|
|
|
|
#include "BaseCoord.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "BasePoint.h"
|
2013-11-27 11:22:02 +00:00
|
|
|
#include "BasePoint3D.h"
|
2014-03-18 04:06:23 +00:00
|
|
|
#include "BasePoint4D.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "BaseSize.h"
|
2014-06-10 16:43:52 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-08-08 19:56:09 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace mozilla {
|
2014-06-10 16:43:52 +00:00
|
|
|
|
|
|
|
template <typename> struct IsPixel;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace gfx {
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
// This should only be used by the typedefs below.
|
|
|
|
struct UnknownUnits {};
|
2011-11-14 04:29:01 +00:00
|
|
|
|
2014-06-10 16:43:52 +00:00
|
|
|
} // close namespace 'gfx' because IsPixel specialization must be in 'mozilla'
|
|
|
|
|
|
|
|
template<> struct IsPixel<gfx::UnknownUnits> : TrueType {};
|
|
|
|
|
|
|
|
namespace gfx {
|
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
template<class units>
|
|
|
|
struct IntPointTyped :
|
2014-08-19 17:08:16 +00:00
|
|
|
public BasePoint< int32_t, IntPointTyped<units>, IntCoordTyped<units> >,
|
2013-05-29 13:32:30 +00:00
|
|
|
public units {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2014-08-19 17:08:16 +00:00
|
|
|
typedef IntCoordTyped<units> Coord;
|
|
|
|
typedef BasePoint< int32_t, IntPointTyped<units>, IntCoordTyped<units> > Super;
|
2013-05-29 13:32:30 +00:00
|
|
|
|
2014-02-06 18:06:08 +00:00
|
|
|
MOZ_CONSTEXPR IntPointTyped() : Super() {}
|
2014-08-19 17:08:16 +00:00
|
|
|
MOZ_CONSTEXPR IntPointTyped(int32_t aX, int32_t aY) : Super(Coord(aX), Coord(aY)) {}
|
|
|
|
// The mixed-type constructors (int, Coord) and (Coord, int) are needed to
|
|
|
|
// avoid ambiguities because Coord is implicitly convertible to int.
|
|
|
|
MOZ_CONSTEXPR IntPointTyped(int32_t aX, Coord aY) : Super(Coord(aX), aY) {}
|
|
|
|
MOZ_CONSTEXPR IntPointTyped(Coord aX, int32_t aY) : Super(aX, Coord(aY)) {}
|
|
|
|
MOZ_CONSTEXPR IntPointTyped(Coord aX, Coord aY) : Super(aX, aY) {}
|
2013-06-11 22:13:11 +00:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static IntPointTyped<units> FromUnknownPoint(const IntPointTyped<UnknownUnits>& aPoint) {
|
|
|
|
return IntPointTyped<units>(aPoint.x, aPoint.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntPointTyped<UnknownUnits> ToUnknownPoint() const {
|
|
|
|
return IntPointTyped<UnknownUnits>(this->x, this->y);
|
|
|
|
}
|
2011-11-14 04:29:01 +00:00
|
|
|
};
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef IntPointTyped<UnknownUnits> IntPoint;
|
2011-11-14 04:29:01 +00:00
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
template<class units>
|
|
|
|
struct PointTyped :
|
2014-08-19 17:08:16 +00:00
|
|
|
public BasePoint< Float, PointTyped<units>, CoordTyped<units> >,
|
2013-05-29 13:32:30 +00:00
|
|
|
public units {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2014-08-19 17:08:16 +00:00
|
|
|
typedef CoordTyped<units> Coord;
|
|
|
|
typedef BasePoint< Float, PointTyped<units>, CoordTyped<units> > Super;
|
2011-11-14 04:29:01 +00:00
|
|
|
|
2014-02-06 18:06:08 +00:00
|
|
|
MOZ_CONSTEXPR PointTyped() : Super() {}
|
2014-08-19 17:08:16 +00:00
|
|
|
MOZ_CONSTEXPR PointTyped(Float aX, Float 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.
|
|
|
|
MOZ_CONSTEXPR PointTyped(Float aX, Coord aY) : Super(Coord(aX), aY) {}
|
|
|
|
MOZ_CONSTEXPR PointTyped(Coord aX, Float aY) : Super(aX, Coord(aY)) {}
|
|
|
|
MOZ_CONSTEXPR PointTyped(Coord aX, Coord aY) : Super(aX.value, aY.value) {}
|
2014-07-29 12:07:24 +00:00
|
|
|
MOZ_CONSTEXPR MOZ_IMPLICIT PointTyped(const IntPointTyped<units>& point) : Super(float(point.x), float(point.y)) {}
|
2013-05-31 01:30:13 +00:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
2013-06-11 22:13:11 +00:00
|
|
|
static PointTyped<units> FromUnknownPoint(const PointTyped<UnknownUnits>& aPoint) {
|
|
|
|
return PointTyped<units>(aPoint.x, aPoint.y);
|
2013-05-31 01:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PointTyped<UnknownUnits> ToUnknownPoint() const {
|
|
|
|
return PointTyped<UnknownUnits>(this->x, this->y);
|
|
|
|
}
|
2011-06-24 17:41:16 +00:00
|
|
|
};
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef PointTyped<UnknownUnits> Point;
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-06-14 20:11:31 +00:00
|
|
|
template<class units>
|
|
|
|
IntPointTyped<units> RoundedToInt(const PointTyped<units>& aPoint) {
|
2014-08-19 17:08:16 +00:00
|
|
|
return IntPointTyped<units>(aPoint.x.Rounded(),
|
|
|
|
aPoint.y.Rounded());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class units>
|
|
|
|
IntPointTyped<units> TruncatedToInt(const PointTyped<units>& aPoint) {
|
|
|
|
return IntPointTyped<units>(aPoint.x.Truncated(),
|
|
|
|
aPoint.y.Truncated());
|
2013-06-14 20:11:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:22:02 +00:00
|
|
|
template<class units>
|
|
|
|
struct Point3DTyped :
|
|
|
|
public BasePoint3D< Float, Point3DTyped<units> > {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-11-27 11:22:02 +00:00
|
|
|
typedef BasePoint3D< Float, Point3DTyped<units> > Super;
|
|
|
|
|
|
|
|
Point3DTyped() : Super() {}
|
|
|
|
Point3DTyped(Float aX, Float aY, Float 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<units> FromUnknownPoint(const Point3DTyped<UnknownUnits>& aPoint) {
|
|
|
|
return Point3DTyped<units>(aPoint.x, aPoint.y, aPoint.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point3DTyped<UnknownUnits> ToUnknownPoint() const {
|
|
|
|
return Point3DTyped<UnknownUnits>(this->x, this->y, this->z);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef Point3DTyped<UnknownUnits> Point3D;
|
|
|
|
|
2014-03-18 04:06:23 +00:00
|
|
|
template<class units>
|
|
|
|
struct Point4DTyped :
|
|
|
|
public BasePoint4D< Float, Point4DTyped<units> > {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2014-03-18 04:06:23 +00:00
|
|
|
typedef BasePoint4D< Float, Point4DTyped<units> > Super;
|
|
|
|
|
|
|
|
Point4DTyped() : Super() {}
|
|
|
|
Point4DTyped(Float aX, Float aY, Float aZ, Float aW) : Super(aX, aY, aZ, aW) {}
|
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static Point4DTyped<units> FromUnknownPoint(const Point4DTyped<UnknownUnits>& aPoint) {
|
|
|
|
return Point4DTyped<units>(aPoint.x, aPoint.y, aPoint.z, aPoint.w);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point4DTyped<UnknownUnits> ToUnknownPoint() const {
|
|
|
|
return Point4DTyped<UnknownUnits>(this->x, this->y, this->z, this->w);
|
|
|
|
}
|
2014-08-22 13:40:02 +00:00
|
|
|
|
|
|
|
PointTyped<units> As2DPoint() {
|
|
|
|
return PointTyped<units>(this->x / this->w, this->y / this->w);
|
|
|
|
}
|
2014-03-18 04:06:23 +00:00
|
|
|
};
|
|
|
|
typedef Point4DTyped<UnknownUnits> Point4D;
|
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
template<class units>
|
|
|
|
struct IntSizeTyped :
|
|
|
|
public BaseSize< int32_t, IntSizeTyped<units> >,
|
|
|
|
public units {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef BaseSize< int32_t, IntSizeTyped<units> > Super;
|
2011-11-14 04:29:01 +00:00
|
|
|
|
2013-09-13 16:34:20 +00:00
|
|
|
MOZ_CONSTEXPR IntSizeTyped() : Super() {}
|
|
|
|
MOZ_CONSTEXPR IntSizeTyped(int32_t aWidth, int32_t aHeight) : Super(aWidth, aHeight) {}
|
2013-06-11 22:13:11 +00:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static IntSizeTyped<units> FromUnknownSize(const IntSizeTyped<UnknownUnits>& aSize) {
|
|
|
|
return IntSizeTyped<units>(aSize.width, aSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSizeTyped<UnknownUnits> ToUnknownSize() const {
|
|
|
|
return IntSizeTyped<UnknownUnits>(this->width, this->height);
|
|
|
|
}
|
2011-06-24 17:41:16 +00:00
|
|
|
};
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef IntSizeTyped<UnknownUnits> IntSize;
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
template<class units>
|
|
|
|
struct SizeTyped :
|
|
|
|
public BaseSize< Float, SizeTyped<units> >,
|
|
|
|
public units {
|
2014-06-10 16:43:52 +00:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef BaseSize< Float, SizeTyped<units> > Super;
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2014-02-06 18:06:08 +00:00
|
|
|
MOZ_CONSTEXPR SizeTyped() : Super() {}
|
|
|
|
MOZ_CONSTEXPR SizeTyped(Float aWidth, Float aHeight) : Super(aWidth, aHeight) {}
|
2013-05-29 13:32:30 +00:00
|
|
|
explicit SizeTyped(const IntSizeTyped<units>& size) :
|
2013-03-04 14:38:11 +00:00
|
|
|
Super(float(size.width), float(size.height)) {}
|
2013-06-11 22:13:11 +00:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static SizeTyped<units> FromUnknownSize(const SizeTyped<UnknownUnits>& aSize) {
|
|
|
|
return SizeTyped<units>(aSize.width, aSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
SizeTyped<UnknownUnits> ToUnknownSize() const {
|
|
|
|
return SizeTyped<UnknownUnits>(this->width, this->height);
|
|
|
|
}
|
2011-05-26 19:41:33 +00:00
|
|
|
};
|
2013-05-29 13:32:30 +00:00
|
|
|
typedef SizeTyped<UnknownUnits> Size;
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2013-09-03 19:12:24 +00:00
|
|
|
template<class units>
|
|
|
|
IntSizeTyped<units> RoundedToInt(const SizeTyped<units>& aSize) {
|
|
|
|
return IntSizeTyped<units>(int32_t(floorf(aSize.width + 0.5f)),
|
|
|
|
int32_t(floorf(aSize.height + 0.5f)));
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#endif /* MOZILLA_GFX_POINT_H_ */
|