Remove WebKitTestRunner::setClientWindowRect.
[chromium-blink-merge.git] / cc / base / math_util.h
blob67a93709d0fe57efdc9d90d89466a665d268f4b6
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>
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; }
21 namespace gfx {
22 class QuadF;
23 class Rect;
24 class RectF;
25 class Transform;
26 class Vector2dF;
29 namespace cc {
31 struct HomogeneousCoordinate {
32 HomogeneousCoordinate(SkMScalar x, SkMScalar y, SkMScalar z, SkMScalar w) {
33 vec[0] = x;
34 vec[1] = y;
35 vec[2] = z;
36 vec[3] = w;
39 bool ShouldBeClipped() const { return w() <= 0.0; }
41 gfx::PointF CartesianPoint2d() const {
42 if (w() == SK_MScalar1)
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.
47 DCHECK(w());
48 SkMScalar inv_w = SK_MScalar1 / w();
49 return gfx::PointF(x() * inv_w, y() * inv_w);
52 gfx::Point3F CartesianPoint3d() const {
53 if (w() == SK_MScalar1)
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.
58 DCHECK(w());
59 SkMScalar inv_w = SK_MScalar1 / 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]; }
68 SkMScalar vec[4];
71 class CC_EXPORT MathUtil {
72 public:
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,
102 gfx::Rect rect);
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
112 // array are valid.
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[],
119 int num_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,
130 bool* clipped);
131 static gfx::PointF MapPoint(const gfx::Transform& transform,
132 gfx::PointF point,
133 bool* clipped);
134 static gfx::Point3F MapPoint(const gfx::Transform&,
135 const gfx::Point3F&,
136 bool* clipped);
137 static gfx::QuadF ProjectQuad(const gfx::Transform& transform,
138 const gfx::QuadF& quad,
139 bool* clipped);
140 static gfx::PointF ProjectPoint(const gfx::Transform& transform,
141 gfx::PointF point,
142 bool* clipped);
144 static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&,
145 float fallbackValue);
147 // Makes a rect that has the same relationship to input_outer_rect as
148 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
149 // contained within scale_outer_rect, and likewise the rectangle that is
150 // returned will be within input_outer_rect at a similar relative, scaled
151 // position.
152 static gfx::RectF ScaleRectProportional(const gfx::RectF& input_outer_rect,
153 const gfx::RectF& scale_outer_rect,
154 const gfx::RectF& scale_inner_rect);
156 // Returns the smallest angle between the given two vectors in degrees.
157 // Neither vector is assumed to be normalized.
158 static float SmallestAngleBetweenVectors(gfx::Vector2dF v1,
159 gfx::Vector2dF v2);
161 // Projects the |source| vector onto |destination|. Neither vector is assumed
162 // to be normalized.
163 static gfx::Vector2dF ProjectVector(gfx::Vector2dF source,
164 gfx::Vector2dF destination);
166 // Conversion to value.
167 static scoped_ptr<base::Value> AsValue(gfx::Size s);
168 static scoped_ptr<base::Value> AsValue(gfx::SizeF s);
169 static scoped_ptr<base::Value> AsValue(gfx::Rect r);
170 static bool FromValue(const base::Value*, gfx::Rect* out_rect);
171 static scoped_ptr<base::Value> AsValue(gfx::PointF q);
172 static scoped_ptr<base::Value> AsValue(const gfx::QuadF& q);
173 static scoped_ptr<base::Value> AsValue(const gfx::RectF& rect);
174 static scoped_ptr<base::Value> AsValue(const gfx::Transform& transform);
176 // Returns a base::Value representation of the floating point value.
177 // If the value is inf, returns max double/float representation.
178 static scoped_ptr<base::Value> AsValueSafely(double value);
179 static scoped_ptr<base::Value> AsValueSafely(float value);
182 } // namespace cc
184 #endif // CC_BASE_MATH_UTIL_H_