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_
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "cc/base/cc_export.h"
14 #include "ui/gfx/box_f.h"
15 #include "ui/gfx/point3_f.h"
16 #include "ui/gfx/point_f.h"
17 #include "ui/gfx/size.h"
18 #include "ui/gfx/transform.h"
20 namespace base
{ class Value
; }
32 struct HomogeneousCoordinate
{
33 HomogeneousCoordinate(SkMScalar x
, SkMScalar y
, SkMScalar z
, SkMScalar w
) {
40 bool ShouldBeClipped() const { return w() <= 0.0; }
42 gfx::PointF
CartesianPoint2d() const {
43 if (w() == SK_MScalar1
)
44 return gfx::PointF(x(), y());
46 // For now, because this code is used privately only by MathUtil, it should
47 // never be called when w == 0, and we do not yet need to handle that case.
49 SkMScalar inv_w
= SK_MScalar1
/ w();
50 return gfx::PointF(x() * inv_w
, y() * inv_w
);
53 gfx::Point3F
CartesianPoint3d() const {
54 if (w() == SK_MScalar1
)
55 return gfx::Point3F(x(), y(), z());
57 // For now, because this code is used privately only by MathUtil, it should
58 // never be called when w == 0, and we do not yet need to handle that case.
60 SkMScalar inv_w
= SK_MScalar1
/ w();
61 return gfx::Point3F(x() * inv_w
, y() * inv_w
, z() * inv_w
);
64 SkMScalar
x() const { return vec
[0]; }
65 SkMScalar
y() const { return vec
[1]; }
66 SkMScalar
z() const { return vec
[2]; }
67 SkMScalar
w() const { return vec
[3]; }
72 class CC_EXPORT MathUtil
{
74 static const double kPiDouble
;
75 static const float kPiFloat
;
77 static double Deg2Rad(double deg
) { return deg
* kPiDouble
/ 180.0; }
78 static double Rad2Deg(double rad
) { return rad
* 180.0 / kPiDouble
; }
80 static float Deg2Rad(float deg
) { return deg
* kPiFloat
/ 180.0f
; }
81 static float Rad2Deg(float rad
) { return rad
* 180.0f
/ kPiFloat
; }
83 static float Round(float f
) {
84 return (f
> 0.f
) ? std::floor(f
+ 0.5f
) : std::ceil(f
- 0.5f
);
86 static double Round(double d
) {
87 return (d
> 0.0) ? std::floor(d
+ 0.5) : std::ceil(d
- 0.5);
90 template <typename T
> static T
ClampToRange(T value
, T min
, T max
) {
91 return std::min(std::max(value
, min
), max
);
94 // Background: Existing transform code does not do the right thing in
95 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
96 // causes one of the transformed vertices to go to w < 0. In those cases, it
97 // is necessary to perform clipping in homogeneous coordinates, after applying
98 // the transform, before dividing-by-w to convert to cartesian coordinates.
100 // These functions return the axis-aligned rect that encloses the correctly
101 // clipped, transformed polygon.
102 static gfx::Rect
MapClippedRect(const gfx::Transform
& transform
,
103 const gfx::Rect
& rect
);
104 static gfx::RectF
MapClippedRect(const gfx::Transform
& transform
,
105 const gfx::RectF
& rect
);
106 static gfx::RectF
ProjectClippedRect(const gfx::Transform
& transform
,
107 const gfx::RectF
& rect
);
109 // Returns an array of vertices that represent the clipped polygon. After
110 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
111 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
112 // which means the entire quad was clipped, and none of the vertices in the
114 static void MapClippedQuad(const gfx::Transform
& transform
,
115 const gfx::QuadF
& src_quad
,
116 gfx::PointF clipped_quad
[8],
117 int* num_vertices_in_clipped_quad
);
119 static gfx::RectF
ComputeEnclosingRectOfVertices(const gfx::PointF vertices
[],
121 static gfx::RectF
ComputeEnclosingClippedRect(
122 const HomogeneousCoordinate
& h1
,
123 const HomogeneousCoordinate
& h2
,
124 const HomogeneousCoordinate
& h3
,
125 const HomogeneousCoordinate
& h4
);
127 // NOTE: These functions do not do correct clipping against w = 0 plane, but
128 // they correctly detect the clipped condition via the boolean clipped.
129 static gfx::QuadF
MapQuad(const gfx::Transform
& transform
,
130 const gfx::QuadF
& quad
,
132 static gfx::PointF
MapPoint(const gfx::Transform
& transform
,
133 const gfx::PointF
& point
,
135 static gfx::Point3F
MapPoint(const gfx::Transform
&,
138 static gfx::QuadF
ProjectQuad(const gfx::Transform
& transform
,
139 const gfx::QuadF
& quad
,
141 static gfx::PointF
ProjectPoint(const gfx::Transform
& transform
,
142 const gfx::PointF
& point
,
145 static gfx::Vector2dF
ComputeTransform2dScaleComponents(const gfx::Transform
&,
146 float fallbackValue
);
148 // Makes a rect that has the same relationship to input_outer_rect as
149 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
150 // contained within scale_outer_rect, and likewise the rectangle that is
151 // returned will be within input_outer_rect at a similar relative, scaled
153 static gfx::RectF
ScaleRectProportional(const gfx::RectF
& input_outer_rect
,
154 const gfx::RectF
& scale_outer_rect
,
155 const gfx::RectF
& scale_inner_rect
);
157 // Returns the smallest angle between the given two vectors in degrees.
158 // Neither vector is assumed to be normalized.
159 static float SmallestAngleBetweenVectors(const gfx::Vector2dF
& v1
,
160 const gfx::Vector2dF
& v2
);
162 // Projects the |source| vector onto |destination|. Neither vector is assumed
164 static gfx::Vector2dF
ProjectVector(const gfx::Vector2dF
& source
,
165 const gfx::Vector2dF
& destination
);
167 // Conversion to value.
168 static scoped_ptr
<base::Value
> AsValue(gfx::Size s
);
169 static scoped_ptr
<base::Value
> AsValue(const gfx::SizeF
& s
);
170 static scoped_ptr
<base::Value
> AsValue(const gfx::Rect
& r
);
171 static bool FromValue(const base::Value
*, gfx::Rect
* out_rect
);
172 static scoped_ptr
<base::Value
> AsValue(const gfx::PointF
& q
);
173 static scoped_ptr
<base::Value
> AsValue(const gfx::QuadF
& q
);
174 static scoped_ptr
<base::Value
> AsValue(const gfx::RectF
& rect
);
175 static scoped_ptr
<base::Value
> AsValue(const gfx::Transform
& transform
);
176 static scoped_ptr
<base::Value
> AsValue(const gfx::BoxF
& box
);
178 // Returns a base::Value representation of the floating point value.
179 // If the value is inf, returns max double/float representation.
180 static scoped_ptr
<base::Value
> AsValueSafely(double value
);
181 static scoped_ptr
<base::Value
> AsValueSafely(float value
);
186 #endif // CC_BASE_MATH_UTIL_H_