Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / cc / base / math_util.h
blob56fdd1774eac48e616aa944fb0678b774402eb95
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_
8 #include <algorithm>
9 #include <cmath>
10 #include <vector>
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"
22 namespace base {
23 class Value;
24 namespace trace_event {
25 class TracedValue;
27 } // namespace base
29 namespace gfx {
30 class QuadF;
31 class Rect;
32 class RectF;
33 class Transform;
34 class Vector2dF;
35 class Vector2d;
38 namespace cc {
40 struct HomogeneousCoordinate {
41 HomogeneousCoordinate(SkMScalar x, SkMScalar y, SkMScalar z, SkMScalar w) {
42 vec[0] = x;
43 vec[1] = y;
44 vec[2] = z;
45 vec[3] = 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.
56 DCHECK(w());
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.
67 DCHECK(w());
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]; }
77 SkMScalar vec[4];
80 class CC_EXPORT MathUtil {
81 public:
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
129 // array are valid.
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[],
140 int num_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,
151 bool* clipped);
152 static gfx::QuadF MapQuad3d(const gfx::Transform& transform,
153 const gfx::QuadF& q,
154 gfx::Point3F* p,
155 bool* clipped);
156 static gfx::PointF MapPoint(const gfx::Transform& transform,
157 const gfx::PointF& point,
158 bool* clipped);
159 static gfx::Point3F MapPoint(const gfx::Transform&,
160 const gfx::Point3F&,
161 bool* clipped);
162 static gfx::QuadF ProjectQuad(const gfx::Transform& transform,
163 const gfx::QuadF& quad,
164 bool* clipped);
165 static gfx::PointF ProjectPoint(const gfx::Transform& transform,
166 const gfx::PointF& point,
167 bool* clipped);
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,
172 bool* clipped);
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
181 // position.
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
192 // to be normalized.
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,
203 const gfx::Size& s,
204 base::trace_event::TracedValue* res);
205 static void AddToTracedValue(const char* name,
206 const gfx::SizeF& s,
207 base::trace_event::TracedValue* res);
208 static void AddToTracedValue(const char* name,
209 const gfx::Rect& r,
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,
215 const gfx::Point3F&,
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,
227 const gfx::QuadF& q,
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);
245 } // namespace cc
247 #endif // CC_BASE_MATH_UTIL_H_