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/point3_f.h"
15 #include "ui/gfx/point_f.h"
16 #include "ui/gfx/size.h"
17 #include "ui/gfx/transform.h"
19 namespace base
{ class Value
; }
31 struct HomogeneousCoordinate
{
32 HomogeneousCoordinate(SkMScalar x
, SkMScalar y
, SkMScalar z
, SkMScalar w
) {
39 bool ShouldBeClipped() const { return w() <= 0.0; }
41 gfx::PointF
CartesianPoint2d() const {
43 return gfx::PointF(x(), y());
45 // For now, because this code is used privately only by MathUtil, it should
46 // never be called when w == 0, and we do not yet need to handle that case.
48 double inv_w
= 1.0 / w();
49 return gfx::PointF(x() * inv_w
, y() * inv_w
);
52 gfx::Point3F
CartesianPoint3d() const {
54 return gfx::Point3F(x(), y(), z());
56 // For now, because this code is used privately only by MathUtil, it should
57 // never be called when w == 0, and we do not yet need to handle that case.
59 double inv_w
= 1.0 / w();
60 return gfx::Point3F(x() * inv_w
, y() * inv_w
, z() * inv_w
);
63 SkMScalar
x() const { return vec
[0]; }
64 SkMScalar
y() const { return vec
[1]; }
65 SkMScalar
z() const { return vec
[2]; }
66 SkMScalar
w() const { return vec
[3]; }
71 class CC_EXPORT MathUtil
{
73 static const double kPiDouble
;
74 static const float kPiFloat
;
76 static double Deg2Rad(double deg
) { return deg
* kPiDouble
/ 180.0; }
77 static double Rad2Deg(double rad
) { return rad
* 180.0 / kPiDouble
; }
79 static float Deg2Rad(float deg
) { return deg
* kPiFloat
/ 180.0f
; }
80 static float Rad2Deg(float rad
) { return rad
* 180.0f
/ kPiFloat
; }
82 static float Round(float f
) {
83 return (f
> 0.f
) ? std::floor(f
+ 0.5f
) : std::ceil(f
- 0.5f
);
85 static double Round(double d
) {
86 return (d
> 0.0) ? std::floor(d
+ 0.5) : std::ceil(d
- 0.5);
89 template <typename T
> static T
ClampToRange(T value
, T min
, T max
) {
90 return std::min(std::max(value
, min
), max
);
93 // Background: Existing transform code does not do the right thing in
94 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
95 // causes one of the transformed vertices to go to w < 0. In those cases, it
96 // is necessary to perform clipping in homogeneous coordinates, after applying
97 // the transform, before dividing-by-w to convert to cartesian coordinates.
99 // These functions return the axis-aligned rect that encloses the correctly
100 // clipped, transformed polygon.
101 static gfx::Rect
MapClippedRect(const gfx::Transform
& transform
,
103 static gfx::RectF
MapClippedRect(const gfx::Transform
& transform
,
104 const gfx::RectF
& rect
);
105 static gfx::RectF
ProjectClippedRect(const gfx::Transform
& transform
,
106 const gfx::RectF
& rect
);
108 // Returns an array of vertices that represent the clipped polygon. After
109 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
110 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
111 // which means the entire quad was clipped, and none of the vertices in the
113 static void MapClippedQuad(const gfx::Transform
& transform
,
114 const gfx::QuadF
& src_quad
,
115 gfx::PointF clipped_quad
[8],
116 int* num_vertices_in_clipped_quad
);
118 static gfx::RectF
ComputeEnclosingRectOfVertices(gfx::PointF vertices
[],
120 static gfx::RectF
ComputeEnclosingClippedRect(
121 const HomogeneousCoordinate
& h1
,
122 const HomogeneousCoordinate
& h2
,
123 const HomogeneousCoordinate
& h3
,
124 const HomogeneousCoordinate
& h4
);
126 // NOTE: These functions do not do correct clipping against w = 0 plane, but
127 // they correctly detect the clipped condition via the boolean clipped.
128 static gfx::QuadF
MapQuad(const gfx::Transform
& transform
,
129 const gfx::QuadF
& quad
,
131 static gfx::PointF
MapPoint(const gfx::Transform
& transform
,
134 static gfx::Point3F
MapPoint(const gfx::Transform
&,
137 static gfx::QuadF
ProjectQuad(const gfx::Transform
& transform
,
138 const gfx::QuadF
& quad
,
140 static gfx::PointF
ProjectPoint(const gfx::Transform
& transform
,
144 static gfx::Vector2dF
ComputeTransform2dScaleComponents(const gfx::Transform
&,
145 float fallbackValue
);
147 // Returns the smallest angle between the given two vectors in degrees.
148 // Neither vector is assumed to be normalized.
149 static float SmallestAngleBetweenVectors(gfx::Vector2dF v1
,
152 // Projects the |source| vector onto |destination|. Neither vector is assumed
154 static gfx::Vector2dF
ProjectVector(gfx::Vector2dF source
,
155 gfx::Vector2dF destination
);
157 // Conversion to value.
158 static scoped_ptr
<base::Value
> AsValue(gfx::Size s
);
159 static scoped_ptr
<base::Value
> AsValue(gfx::SizeF s
);
160 static scoped_ptr
<base::Value
> AsValue(gfx::Rect r
);
161 static bool FromValue(const base::Value
*, gfx::Rect
* out_rect
);
162 static scoped_ptr
<base::Value
> AsValue(gfx::PointF q
);
163 static scoped_ptr
<base::Value
> AsValue(const gfx::QuadF
& q
);
164 static scoped_ptr
<base::Value
> AsValue(const gfx::RectF
& rect
);
165 static scoped_ptr
<base::Value
> AsValue(const gfx::Transform
& transform
);
167 // Returns a base::Value representation of the floating point value.
168 // If the value is inf, returns max double/float representation.
169 static scoped_ptr
<base::Value
> AsValueSafely(double value
);
170 static scoped_ptr
<base::Value
> AsValueSafely(float value
);
175 #endif // CC_BASE_MATH_UTIL_H_