|
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/. */ |
|
5 |
|
6 #ifndef MOZILLA_GFX_POINT_H_ |
|
7 #define MOZILLA_GFX_POINT_H_ |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "Types.h" |
|
11 #include "BasePoint.h" |
|
12 #include "BasePoint3D.h" |
|
13 #include "BasePoint4D.h" |
|
14 #include "BaseSize.h" |
|
15 |
|
16 #include <cmath> |
|
17 |
|
18 namespace mozilla { |
|
19 namespace gfx { |
|
20 |
|
21 // This should only be used by the typedefs below. |
|
22 struct UnknownUnits {}; |
|
23 |
|
24 template<class units> |
|
25 struct IntPointTyped : |
|
26 public BasePoint< int32_t, IntPointTyped<units> >, |
|
27 public units { |
|
28 typedef BasePoint< int32_t, IntPointTyped<units> > Super; |
|
29 |
|
30 MOZ_CONSTEXPR IntPointTyped() : Super() {} |
|
31 MOZ_CONSTEXPR IntPointTyped(int32_t aX, int32_t aY) : Super(aX, aY) {} |
|
32 |
|
33 // XXX When all of the code is ported, the following functions to convert to and from |
|
34 // unknown types should be removed. |
|
35 |
|
36 static IntPointTyped<units> FromUnknownPoint(const IntPointTyped<UnknownUnits>& aPoint) { |
|
37 return IntPointTyped<units>(aPoint.x, aPoint.y); |
|
38 } |
|
39 |
|
40 IntPointTyped<UnknownUnits> ToUnknownPoint() const { |
|
41 return IntPointTyped<UnknownUnits>(this->x, this->y); |
|
42 } |
|
43 }; |
|
44 typedef IntPointTyped<UnknownUnits> IntPoint; |
|
45 |
|
46 template<class units> |
|
47 struct PointTyped : |
|
48 public BasePoint< Float, PointTyped<units> >, |
|
49 public units { |
|
50 typedef BasePoint< Float, PointTyped<units> > Super; |
|
51 |
|
52 MOZ_CONSTEXPR PointTyped() : Super() {} |
|
53 MOZ_CONSTEXPR PointTyped(Float aX, Float aY) : Super(aX, aY) {} |
|
54 MOZ_CONSTEXPR PointTyped(const IntPointTyped<units>& point) : Super(float(point.x), float(point.y)) {} |
|
55 |
|
56 // XXX When all of the code is ported, the following functions to convert to and from |
|
57 // unknown types should be removed. |
|
58 |
|
59 static PointTyped<units> FromUnknownPoint(const PointTyped<UnknownUnits>& aPoint) { |
|
60 return PointTyped<units>(aPoint.x, aPoint.y); |
|
61 } |
|
62 |
|
63 PointTyped<UnknownUnits> ToUnknownPoint() const { |
|
64 return PointTyped<UnknownUnits>(this->x, this->y); |
|
65 } |
|
66 }; |
|
67 typedef PointTyped<UnknownUnits> Point; |
|
68 |
|
69 template<class units> |
|
70 IntPointTyped<units> RoundedToInt(const PointTyped<units>& aPoint) { |
|
71 return IntPointTyped<units>(int32_t(floorf(aPoint.x + 0.5f)), |
|
72 int32_t(floorf(aPoint.y + 0.5f))); |
|
73 } |
|
74 |
|
75 template<class units> |
|
76 struct Point3DTyped : |
|
77 public BasePoint3D< Float, Point3DTyped<units> > { |
|
78 typedef BasePoint3D< Float, Point3DTyped<units> > Super; |
|
79 |
|
80 Point3DTyped() : Super() {} |
|
81 Point3DTyped(Float aX, Float aY, Float aZ) : Super(aX, aY, aZ) {} |
|
82 |
|
83 // XXX When all of the code is ported, the following functions to convert to and from |
|
84 // unknown types should be removed. |
|
85 |
|
86 static Point3DTyped<units> FromUnknownPoint(const Point3DTyped<UnknownUnits>& aPoint) { |
|
87 return Point3DTyped<units>(aPoint.x, aPoint.y, aPoint.z); |
|
88 } |
|
89 |
|
90 Point3DTyped<UnknownUnits> ToUnknownPoint() const { |
|
91 return Point3DTyped<UnknownUnits>(this->x, this->y, this->z); |
|
92 } |
|
93 }; |
|
94 typedef Point3DTyped<UnknownUnits> Point3D; |
|
95 |
|
96 template<class units> |
|
97 struct Point4DTyped : |
|
98 public BasePoint4D< Float, Point4DTyped<units> > { |
|
99 typedef BasePoint4D< Float, Point4DTyped<units> > Super; |
|
100 |
|
101 Point4DTyped() : Super() {} |
|
102 Point4DTyped(Float aX, Float aY, Float aZ, Float aW) : Super(aX, aY, aZ, aW) {} |
|
103 |
|
104 // XXX When all of the code is ported, the following functions to convert to and from |
|
105 // unknown types should be removed. |
|
106 |
|
107 static Point4DTyped<units> FromUnknownPoint(const Point4DTyped<UnknownUnits>& aPoint) { |
|
108 return Point4DTyped<units>(aPoint.x, aPoint.y, aPoint.z, aPoint.w); |
|
109 } |
|
110 |
|
111 Point4DTyped<UnknownUnits> ToUnknownPoint() const { |
|
112 return Point4DTyped<UnknownUnits>(this->x, this->y, this->z, this->w); |
|
113 } |
|
114 }; |
|
115 typedef Point4DTyped<UnknownUnits> Point4D; |
|
116 |
|
117 template<class units> |
|
118 struct IntSizeTyped : |
|
119 public BaseSize< int32_t, IntSizeTyped<units> >, |
|
120 public units { |
|
121 typedef BaseSize< int32_t, IntSizeTyped<units> > Super; |
|
122 |
|
123 MOZ_CONSTEXPR IntSizeTyped() : Super() {} |
|
124 MOZ_CONSTEXPR IntSizeTyped(int32_t aWidth, int32_t aHeight) : Super(aWidth, aHeight) {} |
|
125 |
|
126 // XXX When all of the code is ported, the following functions to convert to and from |
|
127 // unknown types should be removed. |
|
128 |
|
129 static IntSizeTyped<units> FromUnknownSize(const IntSizeTyped<UnknownUnits>& aSize) { |
|
130 return IntSizeTyped<units>(aSize.width, aSize.height); |
|
131 } |
|
132 |
|
133 IntSizeTyped<UnknownUnits> ToUnknownSize() const { |
|
134 return IntSizeTyped<UnknownUnits>(this->width, this->height); |
|
135 } |
|
136 }; |
|
137 typedef IntSizeTyped<UnknownUnits> IntSize; |
|
138 |
|
139 template<class units> |
|
140 struct SizeTyped : |
|
141 public BaseSize< Float, SizeTyped<units> >, |
|
142 public units { |
|
143 typedef BaseSize< Float, SizeTyped<units> > Super; |
|
144 |
|
145 MOZ_CONSTEXPR SizeTyped() : Super() {} |
|
146 MOZ_CONSTEXPR SizeTyped(Float aWidth, Float aHeight) : Super(aWidth, aHeight) {} |
|
147 explicit SizeTyped(const IntSizeTyped<units>& size) : |
|
148 Super(float(size.width), float(size.height)) {} |
|
149 |
|
150 // XXX When all of the code is ported, the following functions to convert to and from |
|
151 // unknown types should be removed. |
|
152 |
|
153 static SizeTyped<units> FromUnknownSize(const SizeTyped<UnknownUnits>& aSize) { |
|
154 return SizeTyped<units>(aSize.width, aSize.height); |
|
155 } |
|
156 |
|
157 SizeTyped<UnknownUnits> ToUnknownSize() const { |
|
158 return SizeTyped<UnknownUnits>(this->width, this->height); |
|
159 } |
|
160 }; |
|
161 typedef SizeTyped<UnknownUnits> Size; |
|
162 |
|
163 template<class units> |
|
164 IntSizeTyped<units> RoundedToInt(const SizeTyped<units>& aSize) { |
|
165 return IntSizeTyped<units>(int32_t(floorf(aSize.width + 0.5f)), |
|
166 int32_t(floorf(aSize.height + 0.5f))); |
|
167 } |
|
168 |
|
169 } |
|
170 } |
|
171 |
|
172 #endif /* MOZILLA_GFX_POINT_H_ */ |