1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_GFX_POINT_H_
7 #define MOZILLA_GFX_POINT_H_
9 #include "mozilla/Attributes.h"
12 #include "BaseCoord.h"
13 #include "BasePoint.h"
14 #include "BasePoint3D.h"
15 #include "BasePoint4D.h"
17 #include "mozilla/TypeTraits.h"
23 template <typename
> struct IsPixel
;
27 // This should only be used by the typedefs below.
28 struct UnknownUnits
{};
30 } // close namespace 'gfx' because IsPixel specialization must be in 'mozilla'
32 template<> struct IsPixel
<gfx::UnknownUnits
> : TrueType
{};
37 struct IntPointTyped
:
38 public BasePoint
< int32_t, IntPointTyped
<units
>, IntCoordTyped
<units
> >,
40 static_assert(IsPixel
<units
>::value
,
41 "'units' must be a coordinate system tag");
43 typedef IntCoordTyped
<units
> Coord
;
44 typedef BasePoint
< int32_t, IntPointTyped
<units
>, IntCoordTyped
<units
> > Super
;
46 MOZ_CONSTEXPR
IntPointTyped() : Super() {}
47 MOZ_CONSTEXPR
IntPointTyped(int32_t aX
, int32_t aY
) : Super(Coord(aX
), Coord(aY
)) {}
48 // The mixed-type constructors (int, Coord) and (Coord, int) are needed to
49 // avoid ambiguities because Coord is implicitly convertible to int.
50 MOZ_CONSTEXPR
IntPointTyped(int32_t aX
, Coord aY
) : Super(Coord(aX
), aY
) {}
51 MOZ_CONSTEXPR
IntPointTyped(Coord aX
, int32_t aY
) : Super(aX
, Coord(aY
)) {}
52 MOZ_CONSTEXPR
IntPointTyped(Coord aX
, Coord aY
) : Super(aX
, aY
) {}
54 // XXX When all of the code is ported, the following functions to convert to and from
55 // unknown types should be removed.
57 static IntPointTyped
<units
> FromUnknownPoint(const IntPointTyped
<UnknownUnits
>& aPoint
) {
58 return IntPointTyped
<units
>(aPoint
.x
, aPoint
.y
);
61 IntPointTyped
<UnknownUnits
> ToUnknownPoint() const {
62 return IntPointTyped
<UnknownUnits
>(this->x
, this->y
);
65 typedef IntPointTyped
<UnknownUnits
> IntPoint
;
69 public BasePoint
< Float
, PointTyped
<units
>, CoordTyped
<units
> >,
71 static_assert(IsPixel
<units
>::value
,
72 "'units' must be a coordinate system tag");
74 typedef CoordTyped
<units
> Coord
;
75 typedef BasePoint
< Float
, PointTyped
<units
>, CoordTyped
<units
> > Super
;
77 MOZ_CONSTEXPR
PointTyped() : Super() {}
78 MOZ_CONSTEXPR
PointTyped(Float aX
, Float aY
) : Super(Coord(aX
), Coord(aY
)) {}
79 // The mixed-type constructors (Float, Coord) and (Coord, Float) are needed to
80 // avoid ambiguities because Coord is implicitly convertible to Float.
81 MOZ_CONSTEXPR
PointTyped(Float aX
, Coord aY
) : Super(Coord(aX
), aY
) {}
82 MOZ_CONSTEXPR
PointTyped(Coord aX
, Float aY
) : Super(aX
, Coord(aY
)) {}
83 MOZ_CONSTEXPR
PointTyped(Coord aX
, Coord aY
) : Super(aX
.value
, aY
.value
) {}
84 MOZ_CONSTEXPR MOZ_IMPLICIT
PointTyped(const IntPointTyped
<units
>& point
) : Super(float(point
.x
), float(point
.y
)) {}
86 // XXX When all of the code is ported, the following functions to convert to and from
87 // unknown types should be removed.
89 static PointTyped
<units
> FromUnknownPoint(const PointTyped
<UnknownUnits
>& aPoint
) {
90 return PointTyped
<units
>(aPoint
.x
, aPoint
.y
);
93 PointTyped
<UnknownUnits
> ToUnknownPoint() const {
94 return PointTyped
<UnknownUnits
>(this->x
, this->y
);
97 typedef PointTyped
<UnknownUnits
> Point
;
100 IntPointTyped
<units
> RoundedToInt(const PointTyped
<units
>& aPoint
) {
101 return IntPointTyped
<units
>(int32_t(floorf(aPoint
.x
+ 0.5f
)),
102 int32_t(floorf(aPoint
.y
+ 0.5f
)));
105 template<class units
>
106 IntPointTyped
<units
> TruncatedToInt(const PointTyped
<units
>& aPoint
) {
107 return IntPointTyped
<units
>(int32_t(aPoint
.x
),
111 template<class units
>
112 struct Point3DTyped
:
113 public BasePoint3D
< Float
, Point3DTyped
<units
> > {
114 static_assert(IsPixel
<units
>::value
,
115 "'units' must be a coordinate system tag");
117 typedef BasePoint3D
< Float
, Point3DTyped
<units
> > Super
;
119 Point3DTyped() : Super() {}
120 Point3DTyped(Float aX
, Float aY
, Float aZ
) : Super(aX
, aY
, aZ
) {}
122 // XXX When all of the code is ported, the following functions to convert to and from
123 // unknown types should be removed.
125 static Point3DTyped
<units
> FromUnknownPoint(const Point3DTyped
<UnknownUnits
>& aPoint
) {
126 return Point3DTyped
<units
>(aPoint
.x
, aPoint
.y
, aPoint
.z
);
129 Point3DTyped
<UnknownUnits
> ToUnknownPoint() const {
130 return Point3DTyped
<UnknownUnits
>(this->x
, this->y
, this->z
);
133 typedef Point3DTyped
<UnknownUnits
> Point3D
;
135 template<class units
>
136 struct Point4DTyped
:
137 public BasePoint4D
< Float
, Point4DTyped
<units
> > {
138 static_assert(IsPixel
<units
>::value
,
139 "'units' must be a coordinate system tag");
141 typedef BasePoint4D
< Float
, Point4DTyped
<units
> > Super
;
143 Point4DTyped() : Super() {}
144 Point4DTyped(Float aX
, Float aY
, Float aZ
, Float aW
) : Super(aX
, aY
, aZ
, aW
) {}
146 // XXX When all of the code is ported, the following functions to convert to and from
147 // unknown types should be removed.
149 static Point4DTyped
<units
> FromUnknownPoint(const Point4DTyped
<UnknownUnits
>& aPoint
) {
150 return Point4DTyped
<units
>(aPoint
.x
, aPoint
.y
, aPoint
.z
, aPoint
.w
);
153 Point4DTyped
<UnknownUnits
> ToUnknownPoint() const {
154 return Point4DTyped
<UnknownUnits
>(this->x
, this->y
, this->z
, this->w
);
157 PointTyped
<units
> As2DPoint() {
158 return PointTyped
<units
>(this->x
/ this->w
, this->y
/ this->w
);
161 typedef Point4DTyped
<UnknownUnits
> Point4D
;
163 template<class units
>
164 struct IntSizeTyped
:
165 public BaseSize
< int32_t, IntSizeTyped
<units
> >,
167 static_assert(IsPixel
<units
>::value
,
168 "'units' must be a coordinate system tag");
170 typedef BaseSize
< int32_t, IntSizeTyped
<units
> > Super
;
172 MOZ_CONSTEXPR
IntSizeTyped() : Super() {}
173 MOZ_CONSTEXPR
IntSizeTyped(int32_t aWidth
, int32_t aHeight
) : Super(aWidth
, aHeight
) {}
175 // XXX When all of the code is ported, the following functions to convert to and from
176 // unknown types should be removed.
178 static IntSizeTyped
<units
> FromUnknownSize(const IntSizeTyped
<UnknownUnits
>& aSize
) {
179 return IntSizeTyped
<units
>(aSize
.width
, aSize
.height
);
182 IntSizeTyped
<UnknownUnits
> ToUnknownSize() const {
183 return IntSizeTyped
<UnknownUnits
>(this->width
, this->height
);
186 typedef IntSizeTyped
<UnknownUnits
> IntSize
;
188 template<class units
>
190 public BaseSize
< Float
, SizeTyped
<units
> >,
192 static_assert(IsPixel
<units
>::value
,
193 "'units' must be a coordinate system tag");
195 typedef BaseSize
< Float
, SizeTyped
<units
> > Super
;
197 MOZ_CONSTEXPR
SizeTyped() : Super() {}
198 MOZ_CONSTEXPR
SizeTyped(Float aWidth
, Float aHeight
) : Super(aWidth
, aHeight
) {}
199 explicit SizeTyped(const IntSizeTyped
<units
>& size
) :
200 Super(float(size
.width
), float(size
.height
)) {}
202 // XXX When all of the code is ported, the following functions to convert to and from
203 // unknown types should be removed.
205 static SizeTyped
<units
> FromUnknownSize(const SizeTyped
<UnknownUnits
>& aSize
) {
206 return SizeTyped
<units
>(aSize
.width
, aSize
.height
);
209 SizeTyped
<UnknownUnits
> ToUnknownSize() const {
210 return SizeTyped
<UnknownUnits
>(this->width
, this->height
);
213 typedef SizeTyped
<UnknownUnits
> Size
;
215 template<class units
>
216 IntSizeTyped
<units
> RoundedToInt(const SizeTyped
<units
>& aSize
) {
217 return IntSizeTyped
<units
>(int32_t(floorf(aSize
.width
+ 0.5f
)),
218 int32_t(floorf(aSize
.height
+ 0.5f
)));
224 #endif /* MOZILLA_GFX_POINT_H_ */