1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_BASE_MATH_UTIL_H_
6 #define CC_BASE_MATH_UTIL_H_
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "cc/base/cc_export.h"
15 #include "ui/gfx/geometry/box_f.h"
16 #include "ui/gfx/geometry/point3_f.h"
17 #include "ui/gfx/geometry/point_f.h"
18 #include "ui/gfx/geometry/scroll_offset.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/transform.h"
24 namespace trace_event
{
41 struct HomogeneousCoordinate
{
42 HomogeneousCoordinate(SkMScalar x
, SkMScalar y
, SkMScalar z
, SkMScalar w
) {
49 bool ShouldBeClipped() const { return w() <= 0.0; }
51 gfx::PointF
CartesianPoint2d() const {
52 if (w() == SK_MScalar1
)
53 return gfx::PointF(x(), y());
55 // For now, because this code is used privately only by MathUtil, it should
56 // never be called when w == 0, and we do not yet need to handle that case.
58 SkMScalar inv_w
= SK_MScalar1
/ w();
59 return gfx::PointF(x() * inv_w
, y() * inv_w
);
62 gfx::Point3F
CartesianPoint3d() const {
63 if (w() == SK_MScalar1
)
64 return gfx::Point3F(x(), y(), z());
66 // For now, because this code is used privately only by MathUtil, it should
67 // never be called when w == 0, and we do not yet need to handle that case.
69 SkMScalar inv_w
= SK_MScalar1
/ w();
70 return gfx::Point3F(x() * inv_w
, y() * inv_w
, z() * inv_w
);
73 SkMScalar
x() const { return vec
[0]; }
74 SkMScalar
y() const { return vec
[1]; }
75 SkMScalar
z() const { return vec
[2]; }
76 SkMScalar
w() const { return vec
[3]; }
81 class CC_EXPORT MathUtil
{
83 static const double kPiDouble
;
84 static const float kPiFloat
;
86 static double Deg2Rad(double deg
) { return deg
* kPiDouble
/ 180.0; }
87 static double Rad2Deg(double rad
) { return rad
* 180.0 / kPiDouble
; }
89 static float Deg2Rad(float deg
) { return deg
* kPiFloat
/ 180.0f
; }
90 static float Rad2Deg(float rad
) { return rad
* 180.0f
/ kPiFloat
; }
92 static float Round(float f
) {
93 return (f
> 0.f
) ? std::floor(f
+ 0.5f
) : std::ceil(f
- 0.5f
);
95 static double Round(double d
) {
96 return (d
> 0.0) ? std::floor(d
+ 0.5) : std::ceil(d
- 0.5);
99 // RoundUp rounds up a given |n| to be a multiple of |mul|.
101 // - RoundUp(123, 50) returns 150.
102 // - RoundUp(-123, 50) returns -100.
103 template <typename T
>
104 static T
RoundUp(T n
, T mul
) {
105 static_assert(std::numeric_limits
<T
>::is_integer
,
106 "T must be an integer type");
107 return (n
> 0) ? ((n
+ mul
- 1) / mul
) * mul
: (n
/ mul
) * mul
;
110 // RoundDown rounds down a given |n| to be a multiple of |mul|.
112 // - RoundDown(123, 50) returns 100.
113 // - RoundDown(-123, 50) returns -150.
114 template <typename T
>
115 static T
RoundDown(T n
, T mul
) {
116 static_assert(std::numeric_limits
<T
>::is_integer
,
117 "T must be an integer type");
118 return (n
> 0) ? (n
/ mul
) * mul
: (n
== 0) ? 0
119 : ((n
- mul
+ 1) / mul
) * mul
;
122 template <typename T
> static T
ClampToRange(T value
, T min
, T max
) {
123 return std::min(std::max(value
, min
), max
);
126 // Background: Existing transform code does not do the right thing in
127 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
128 // causes one of the transformed vertices to go to w < 0. In those cases, it
129 // is necessary to perform clipping in homogeneous coordinates, after applying
130 // the transform, before dividing-by-w to convert to cartesian coordinates.
132 // These functions return the axis-aligned rect that encloses the correctly
133 // clipped, transformed polygon.
134 static gfx::Rect
MapEnclosingClippedRect(const gfx::Transform
& transform
,
135 const gfx::Rect
& rect
);
136 static gfx::RectF
MapClippedRect(const gfx::Transform
& transform
,
137 const gfx::RectF
& rect
);
138 static gfx::Rect
ProjectEnclosingClippedRect(const gfx::Transform
& transform
,
139 const gfx::Rect
& rect
);
140 static gfx::RectF
ProjectClippedRect(const gfx::Transform
& transform
,
141 const gfx::RectF
& rect
);
143 // This function is only valid when the transform preserves 2d axis
144 // alignment and the resulting rect will not be clipped.
145 static gfx::Rect
MapEnclosedRectWith2dAxisAlignedTransform(
146 const gfx::Transform
& transform
,
147 const gfx::Rect
& rect
);
149 // Returns an array of vertices that represent the clipped polygon. After
150 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
151 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
152 // which means the entire quad was clipped, and none of the vertices in the
154 static void MapClippedQuad(const gfx::Transform
& transform
,
155 const gfx::QuadF
& src_quad
,
156 gfx::PointF clipped_quad
[8],
157 int* num_vertices_in_clipped_quad
);
158 static bool MapClippedQuad3d(const gfx::Transform
& transform
,
159 const gfx::QuadF
& src_quad
,
160 gfx::Point3F clipped_quad
[8],
161 int* num_vertices_in_clipped_quad
);
163 static gfx::RectF
ComputeEnclosingRectOfVertices(const gfx::PointF vertices
[],
165 static gfx::RectF
ComputeEnclosingClippedRect(
166 const HomogeneousCoordinate
& h1
,
167 const HomogeneousCoordinate
& h2
,
168 const HomogeneousCoordinate
& h3
,
169 const HomogeneousCoordinate
& h4
);
171 // NOTE: These functions do not do correct clipping against w = 0 plane, but
172 // they correctly detect the clipped condition via the boolean clipped.
173 static gfx::QuadF
MapQuad(const gfx::Transform
& transform
,
174 const gfx::QuadF
& quad
,
176 static gfx::QuadF
MapQuad3d(const gfx::Transform
& transform
,
180 static gfx::PointF
MapPoint(const gfx::Transform
& transform
,
181 const gfx::PointF
& point
,
183 static gfx::Point3F
MapPoint(const gfx::Transform
&,
186 static gfx::QuadF
ProjectQuad(const gfx::Transform
& transform
,
187 const gfx::QuadF
& quad
,
189 static gfx::PointF
ProjectPoint(const gfx::Transform
& transform
,
190 const gfx::PointF
& point
,
192 // Identical to the above function, but coerces the homogeneous coordinate to
193 // a 3d rather than a 2d point.
194 static gfx::Point3F
ProjectPoint3D(const gfx::Transform
& transform
,
195 const gfx::PointF
& point
,
198 static gfx::Vector2dF
ComputeTransform2dScaleComponents(const gfx::Transform
&,
199 float fallbackValue
);
201 // Makes a rect that has the same relationship to input_outer_rect as
202 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
203 // contained within scale_outer_rect, and likewise the rectangle that is
204 // returned will be within input_outer_rect at a similar relative, scaled
206 static gfx::RectF
ScaleRectProportional(const gfx::RectF
& input_outer_rect
,
207 const gfx::RectF
& scale_outer_rect
,
208 const gfx::RectF
& scale_inner_rect
);
210 // Returns the smallest angle between the given two vectors in degrees.
211 // Neither vector is assumed to be normalized.
212 static float SmallestAngleBetweenVectors(const gfx::Vector2dF
& v1
,
213 const gfx::Vector2dF
& v2
);
215 // Projects the |source| vector onto |destination|. Neither vector is assumed
217 static gfx::Vector2dF
ProjectVector(const gfx::Vector2dF
& source
,
218 const gfx::Vector2dF
& destination
);
220 // Conversion to value.
221 static scoped_ptr
<base::Value
> AsValue(const gfx::Size
& s
);
222 static scoped_ptr
<base::Value
> AsValue(const gfx::Rect
& r
);
223 static bool FromValue(const base::Value
*, gfx::Rect
* out_rect
);
224 static scoped_ptr
<base::Value
> AsValue(const gfx::PointF
& q
);
226 static void AddToTracedValue(const char* name
,
228 base::trace_event::TracedValue
* res
);
229 static void AddToTracedValue(const char* name
,
231 base::trace_event::TracedValue
* res
);
232 static void AddToTracedValue(const char* name
,
234 base::trace_event::TracedValue
* res
);
235 static void AddToTracedValue(const char* name
,
236 const gfx::PointF
& q
,
237 base::trace_event::TracedValue
* res
);
238 static void AddToTracedValue(const char* name
,
240 base::trace_event::TracedValue
* res
);
241 static void AddToTracedValue(const char* name
,
242 const gfx::Vector2d
& v
,
243 base::trace_event::TracedValue
* res
);
244 static void AddToTracedValue(const char* name
,
245 const gfx::Vector2dF
& v
,
246 base::trace_event::TracedValue
* res
);
247 static void AddToTracedValue(const char* name
,
248 const gfx::ScrollOffset
& v
,
249 base::trace_event::TracedValue
* res
);
250 static void AddToTracedValue(const char* name
,
252 base::trace_event::TracedValue
* res
);
253 static void AddToTracedValue(const char* name
,
254 const gfx::RectF
& rect
,
255 base::trace_event::TracedValue
* res
);
256 static void AddToTracedValue(const char* name
,
257 const gfx::Transform
& transform
,
258 base::trace_event::TracedValue
* res
);
259 static void AddToTracedValue(const char* name
,
260 const gfx::BoxF
& box
,
261 base::trace_event::TracedValue
* res
);
263 // Returns a base::Value representation of the floating point value.
264 // If the value is inf, returns max double/float representation.
265 static double AsDoubleSafely(double value
);
266 static float AsFloatSafely(float value
);
268 // Returns vector that x axis (1,0,0) transforms to under given transform.
269 static gfx::Vector3dF
GetXAxis(const gfx::Transform
& transform
);
271 // Returns vector that y axis (0,1,0) transforms to under given transform.
272 static gfx::Vector3dF
GetYAxis(const gfx::Transform
& transform
);
277 #endif // CC_BASE_MATH_UTIL_H_