Update comments of TabObserver#onLoadStarted and rename onContentChanged
[chromium-blink-merge.git] / cc / base / math_util.h
blob749b9bdbae1d20872daedc8036850ea859d53c71
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;
36 class Vector3dF;
39 namespace cc {
41 struct HomogeneousCoordinate {
42 HomogeneousCoordinate(SkMScalar x, SkMScalar y, SkMScalar z, SkMScalar w) {
43 vec[0] = x;
44 vec[1] = y;
45 vec[2] = z;
46 vec[3] = 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.
57 DCHECK(w());
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.
68 DCHECK(w());
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]; }
78 SkMScalar vec[4];
81 class CC_EXPORT MathUtil {
82 public:
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 template <typename T> static T ClampToRange(T value, T min, T max) {
100 return std::min(std::max(value, min), max);
103 // Background: Existing transform code does not do the right thing in
104 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
105 // causes one of the transformed vertices to go to w < 0. In those cases, it
106 // is necessary to perform clipping in homogeneous coordinates, after applying
107 // the transform, before dividing-by-w to convert to cartesian coordinates.
109 // These functions return the axis-aligned rect that encloses the correctly
110 // clipped, transformed polygon.
111 static gfx::Rect MapEnclosingClippedRect(const gfx::Transform& transform,
112 const gfx::Rect& rect);
113 static gfx::RectF MapClippedRect(const gfx::Transform& transform,
114 const gfx::RectF& rect);
115 static gfx::Rect ProjectEnclosingClippedRect(const gfx::Transform& transform,
116 const gfx::Rect& rect);
117 static gfx::RectF ProjectClippedRect(const gfx::Transform& transform,
118 const gfx::RectF& rect);
120 // This function is only valid when the transform preserves 2d axis
121 // alignment and the resulting rect will not be clipped.
122 static gfx::Rect MapEnclosedRectWith2dAxisAlignedTransform(
123 const gfx::Transform& transform,
124 const gfx::Rect& rect);
126 // Returns an array of vertices that represent the clipped polygon. After
127 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
128 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
129 // which means the entire quad was clipped, and none of the vertices in the
130 // array are valid.
131 static void MapClippedQuad(const gfx::Transform& transform,
132 const gfx::QuadF& src_quad,
133 gfx::PointF clipped_quad[8],
134 int* num_vertices_in_clipped_quad);
135 static bool MapClippedQuad3d(const gfx::Transform& transform,
136 const gfx::QuadF& src_quad,
137 gfx::Point3F clipped_quad[8],
138 int* num_vertices_in_clipped_quad);
140 static gfx::RectF ComputeEnclosingRectOfVertices(const gfx::PointF vertices[],
141 int num_vertices);
142 static gfx::RectF ComputeEnclosingClippedRect(
143 const HomogeneousCoordinate& h1,
144 const HomogeneousCoordinate& h2,
145 const HomogeneousCoordinate& h3,
146 const HomogeneousCoordinate& h4);
148 // NOTE: These functions do not do correct clipping against w = 0 plane, but
149 // they correctly detect the clipped condition via the boolean clipped.
150 static gfx::QuadF MapQuad(const gfx::Transform& transform,
151 const gfx::QuadF& quad,
152 bool* clipped);
153 static gfx::QuadF MapQuad3d(const gfx::Transform& transform,
154 const gfx::QuadF& q,
155 gfx::Point3F* p,
156 bool* clipped);
157 static gfx::PointF MapPoint(const gfx::Transform& transform,
158 const gfx::PointF& point,
159 bool* clipped);
160 static gfx::Point3F MapPoint(const gfx::Transform&,
161 const gfx::Point3F&,
162 bool* clipped);
163 static gfx::QuadF ProjectQuad(const gfx::Transform& transform,
164 const gfx::QuadF& quad,
165 bool* clipped);
166 static gfx::PointF ProjectPoint(const gfx::Transform& transform,
167 const gfx::PointF& point,
168 bool* clipped);
169 // Identical to the above function, but coerces the homogeneous coordinate to
170 // a 3d rather than a 2d point.
171 static gfx::Point3F ProjectPoint3D(const gfx::Transform& transform,
172 const gfx::PointF& point,
173 bool* clipped);
175 static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&,
176 float fallbackValue);
178 // Makes a rect that has the same relationship to input_outer_rect as
179 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
180 // contained within scale_outer_rect, and likewise the rectangle that is
181 // returned will be within input_outer_rect at a similar relative, scaled
182 // position.
183 static gfx::RectF ScaleRectProportional(const gfx::RectF& input_outer_rect,
184 const gfx::RectF& scale_outer_rect,
185 const gfx::RectF& scale_inner_rect);
187 // Returns the smallest angle between the given two vectors in degrees.
188 // Neither vector is assumed to be normalized.
189 static float SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
190 const gfx::Vector2dF& v2);
192 // Projects the |source| vector onto |destination|. Neither vector is assumed
193 // to be normalized.
194 static gfx::Vector2dF ProjectVector(const gfx::Vector2dF& source,
195 const gfx::Vector2dF& destination);
197 // Conversion to value.
198 static scoped_ptr<base::Value> AsValue(const gfx::Size& s);
199 static scoped_ptr<base::Value> AsValue(const gfx::Rect& r);
200 static bool FromValue(const base::Value*, gfx::Rect* out_rect);
201 static scoped_ptr<base::Value> AsValue(const gfx::PointF& q);
203 static void AddToTracedValue(const char* name,
204 const gfx::Size& s,
205 base::trace_event::TracedValue* res);
206 static void AddToTracedValue(const char* name,
207 const gfx::SizeF& s,
208 base::trace_event::TracedValue* res);
209 static void AddToTracedValue(const char* name,
210 const gfx::Rect& r,
211 base::trace_event::TracedValue* res);
212 static void AddToTracedValue(const char* name,
213 const gfx::PointF& q,
214 base::trace_event::TracedValue* res);
215 static void AddToTracedValue(const char* name,
216 const gfx::Point3F&,
217 base::trace_event::TracedValue* res);
218 static void AddToTracedValue(const char* name,
219 const gfx::Vector2d& v,
220 base::trace_event::TracedValue* res);
221 static void AddToTracedValue(const char* name,
222 const gfx::Vector2dF& v,
223 base::trace_event::TracedValue* res);
224 static void AddToTracedValue(const char* name,
225 const gfx::ScrollOffset& v,
226 base::trace_event::TracedValue* res);
227 static void AddToTracedValue(const char* name,
228 const gfx::QuadF& q,
229 base::trace_event::TracedValue* res);
230 static void AddToTracedValue(const char* name,
231 const gfx::RectF& rect,
232 base::trace_event::TracedValue* res);
233 static void AddToTracedValue(const char* name,
234 const gfx::Transform& transform,
235 base::trace_event::TracedValue* res);
236 static void AddToTracedValue(const char* name,
237 const gfx::BoxF& box,
238 base::trace_event::TracedValue* res);
240 // Returns a base::Value representation of the floating point value.
241 // If the value is inf, returns max double/float representation.
242 static double AsDoubleSafely(double value);
243 static float AsFloatSafely(float value);
245 // Returns vector that x axis (1,0,0) transforms to under given transform.
246 static gfx::Vector3dF GetXAxis(const gfx::Transform& transform);
248 // Returns vector that y axis (0,1,0) transforms to under given transform.
249 static gfx::Vector3dF GetYAxis(const gfx::Transform& transform);
252 } // namespace cc
254 #endif // CC_BASE_MATH_UTIL_H_