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
{
40 struct HomogeneousCoordinate
{
41 HomogeneousCoordinate(SkMScalar x
, SkMScalar y
, SkMScalar z
, SkMScalar w
) {
48 bool ShouldBeClipped() const { return w() <= 0.0; }
50 gfx::PointF
CartesianPoint2d() const {
51 if (w() == SK_MScalar1
)
52 return gfx::PointF(x(), y());
54 // For now, because this code is used privately only by MathUtil, it should
55 // never be called when w == 0, and we do not yet need to handle that case.
57 SkMScalar inv_w
= SK_MScalar1
/ w();
58 return gfx::PointF(x() * inv_w
, y() * inv_w
);
61 gfx::Point3F
CartesianPoint3d() const {
62 if (w() == SK_MScalar1
)
63 return gfx::Point3F(x(), y(), z());
65 // For now, because this code is used privately only by MathUtil, it should
66 // never be called when w == 0, and we do not yet need to handle that case.
68 SkMScalar inv_w
= SK_MScalar1
/ w();
69 return gfx::Point3F(x() * inv_w
, y() * inv_w
, z() * inv_w
);
72 SkMScalar
x() const { return vec
[0]; }
73 SkMScalar
y() const { return vec
[1]; }
74 SkMScalar
z() const { return vec
[2]; }
75 SkMScalar
w() const { return vec
[3]; }
80 class CC_EXPORT MathUtil
{
82 static const double kPiDouble
;
83 static const float kPiFloat
;
85 static double Deg2Rad(double deg
) { return deg
* kPiDouble
/ 180.0; }
86 static double Rad2Deg(double rad
) { return rad
* 180.0 / kPiDouble
; }
88 static float Deg2Rad(float deg
) { return deg
* kPiFloat
/ 180.0f
; }
89 static float Rad2Deg(float rad
) { return rad
* 180.0f
/ kPiFloat
; }
91 static float Round(float f
) {
92 return (f
> 0.f
) ? std::floor(f
+ 0.5f
) : std::ceil(f
- 0.5f
);
94 static double Round(double d
) {
95 return (d
> 0.0) ? std::floor(d
+ 0.5) : std::ceil(d
- 0.5);
98 template <typename T
> static T
ClampToRange(T value
, T min
, T max
) {
99 return std::min(std::max(value
, min
), max
);
102 // Background: Existing transform code does not do the right thing in
103 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
104 // causes one of the transformed vertices to go to w < 0. In those cases, it
105 // is necessary to perform clipping in homogeneous coordinates, after applying
106 // the transform, before dividing-by-w to convert to cartesian coordinates.
108 // These functions return the axis-aligned rect that encloses the correctly
109 // clipped, transformed polygon.
110 static gfx::Rect
MapEnclosingClippedRect(const gfx::Transform
& transform
,
111 const gfx::Rect
& rect
);
112 static gfx::RectF
MapClippedRect(const gfx::Transform
& transform
,
113 const gfx::RectF
& rect
);
114 static gfx::Rect
ProjectEnclosingClippedRect(const gfx::Transform
& transform
,
115 const gfx::Rect
& rect
);
116 static gfx::RectF
ProjectClippedRect(const gfx::Transform
& transform
,
117 const gfx::RectF
& rect
);
119 // This function is only valid when the transform preserves 2d axis
120 // alignment and the resulting rect will not be clipped.
121 static gfx::Rect
MapEnclosedRectWith2dAxisAlignedTransform(
122 const gfx::Transform
& transform
,
123 const gfx::Rect
& rect
);
125 // Returns an array of vertices that represent the clipped polygon. After
126 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
127 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
128 // which means the entire quad was clipped, and none of the vertices in the
130 static void MapClippedQuad(const gfx::Transform
& transform
,
131 const gfx::QuadF
& src_quad
,
132 gfx::PointF clipped_quad
[8],
133 int* num_vertices_in_clipped_quad
);
134 static bool MapClippedQuad3d(const gfx::Transform
& transform
,
135 const gfx::QuadF
& src_quad
,
136 gfx::Point3F clipped_quad
[8],
137 int* num_vertices_in_clipped_quad
);
139 static gfx::RectF
ComputeEnclosingRectOfVertices(const gfx::PointF vertices
[],
141 static gfx::RectF
ComputeEnclosingClippedRect(
142 const HomogeneousCoordinate
& h1
,
143 const HomogeneousCoordinate
& h2
,
144 const HomogeneousCoordinate
& h3
,
145 const HomogeneousCoordinate
& h4
);
147 // NOTE: These functions do not do correct clipping against w = 0 plane, but
148 // they correctly detect the clipped condition via the boolean clipped.
149 static gfx::QuadF
MapQuad(const gfx::Transform
& transform
,
150 const gfx::QuadF
& quad
,
152 static gfx::QuadF
MapQuad3d(const gfx::Transform
& transform
,
156 static gfx::PointF
MapPoint(const gfx::Transform
& transform
,
157 const gfx::PointF
& point
,
159 static gfx::Point3F
MapPoint(const gfx::Transform
&,
162 static gfx::QuadF
ProjectQuad(const gfx::Transform
& transform
,
163 const gfx::QuadF
& quad
,
165 static gfx::PointF
ProjectPoint(const gfx::Transform
& transform
,
166 const gfx::PointF
& point
,
168 // Identical to the above function, but coerces the homogeneous coordinate to
169 // a 3d rather than a 2d point.
170 static gfx::Point3F
ProjectPoint3D(const gfx::Transform
& transform
,
171 const gfx::PointF
& point
,
174 static gfx::Vector2dF
ComputeTransform2dScaleComponents(const gfx::Transform
&,
175 float fallbackValue
);
177 // Makes a rect that has the same relationship to input_outer_rect as
178 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
179 // contained within scale_outer_rect, and likewise the rectangle that is
180 // returned will be within input_outer_rect at a similar relative, scaled
182 static gfx::RectF
ScaleRectProportional(const gfx::RectF
& input_outer_rect
,
183 const gfx::RectF
& scale_outer_rect
,
184 const gfx::RectF
& scale_inner_rect
);
186 // Returns the smallest angle between the given two vectors in degrees.
187 // Neither vector is assumed to be normalized.
188 static float SmallestAngleBetweenVectors(const gfx::Vector2dF
& v1
,
189 const gfx::Vector2dF
& v2
);
191 // Projects the |source| vector onto |destination|. Neither vector is assumed
193 static gfx::Vector2dF
ProjectVector(const gfx::Vector2dF
& source
,
194 const gfx::Vector2dF
& destination
);
196 // Conversion to value.
197 static scoped_ptr
<base::Value
> AsValue(const gfx::Size
& s
);
198 static scoped_ptr
<base::Value
> AsValue(const gfx::Rect
& r
);
199 static bool FromValue(const base::Value
*, gfx::Rect
* out_rect
);
200 static scoped_ptr
<base::Value
> AsValue(const gfx::PointF
& q
);
202 static void AddToTracedValue(const char* name
,
204 base::trace_event::TracedValue
* res
);
205 static void AddToTracedValue(const char* name
,
207 base::trace_event::TracedValue
* res
);
208 static void AddToTracedValue(const char* name
,
210 base::trace_event::TracedValue
* res
);
211 static void AddToTracedValue(const char* name
,
212 const gfx::PointF
& q
,
213 base::trace_event::TracedValue
* res
);
214 static void AddToTracedValue(const char* name
,
216 base::trace_event::TracedValue
* res
);
217 static void AddToTracedValue(const char* name
,
218 const gfx::Vector2d
& v
,
219 base::trace_event::TracedValue
* res
);
220 static void AddToTracedValue(const char* name
,
221 const gfx::Vector2dF
& v
,
222 base::trace_event::TracedValue
* res
);
223 static void AddToTracedValue(const char* name
,
224 const gfx::ScrollOffset
& v
,
225 base::trace_event::TracedValue
* res
);
226 static void AddToTracedValue(const char* name
,
228 base::trace_event::TracedValue
* res
);
229 static void AddToTracedValue(const char* name
,
230 const gfx::RectF
& rect
,
231 base::trace_event::TracedValue
* res
);
232 static void AddToTracedValue(const char* name
,
233 const gfx::Transform
& transform
,
234 base::trace_event::TracedValue
* res
);
235 static void AddToTracedValue(const char* name
,
236 const gfx::BoxF
& box
,
237 base::trace_event::TracedValue
* res
);
239 // Returns a base::Value representation of the floating point value.
240 // If the value is inf, returns max double/float representation.
241 static double AsDoubleSafely(double value
);
242 static float AsFloatSafely(float value
);
247 #endif // CC_BASE_MATH_UTIL_H_