Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / base / math_util.h
blob3af2102152098df6b4890f1446061cc26ee4a9b8
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/box_f.h"
15 #include "ui/gfx/point3_f.h"
16 #include "ui/gfx/point_f.h"
17 #include "ui/gfx/size.h"
18 #include "ui/gfx/transform.h"
20 namespace base { class Value; }
22 namespace gfx {
23 class QuadF;
24 class Rect;
25 class RectF;
26 class Transform;
27 class Vector2dF;
28 class Vector2d;
31 namespace cc {
33 struct HomogeneousCoordinate {
34 HomogeneousCoordinate(SkMScalar x, SkMScalar y, SkMScalar z, SkMScalar w) {
35 vec[0] = x;
36 vec[1] = y;
37 vec[2] = z;
38 vec[3] = w;
41 bool ShouldBeClipped() const { return w() <= 0.0; }
43 gfx::PointF CartesianPoint2d() const {
44 if (w() == SK_MScalar1)
45 return gfx::PointF(x(), y());
47 // For now, because this code is used privately only by MathUtil, it should
48 // never be called when w == 0, and we do not yet need to handle that case.
49 DCHECK(w());
50 SkMScalar inv_w = SK_MScalar1 / w();
51 return gfx::PointF(x() * inv_w, y() * inv_w);
54 gfx::Point3F CartesianPoint3d() const {
55 if (w() == SK_MScalar1)
56 return gfx::Point3F(x(), y(), z());
58 // For now, because this code is used privately only by MathUtil, it should
59 // never be called when w == 0, and we do not yet need to handle that case.
60 DCHECK(w());
61 SkMScalar inv_w = SK_MScalar1 / w();
62 return gfx::Point3F(x() * inv_w, y() * inv_w, z() * inv_w);
65 SkMScalar x() const { return vec[0]; }
66 SkMScalar y() const { return vec[1]; }
67 SkMScalar z() const { return vec[2]; }
68 SkMScalar w() const { return vec[3]; }
70 SkMScalar vec[4];
73 class CC_EXPORT MathUtil {
74 public:
75 static const double kPiDouble;
76 static const float kPiFloat;
78 static double Deg2Rad(double deg) { return deg * kPiDouble / 180.0; }
79 static double Rad2Deg(double rad) { return rad * 180.0 / kPiDouble; }
81 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; }
82 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; }
84 static float Round(float f) {
85 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f);
87 static double Round(double d) {
88 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
91 template <typename T> static T ClampToRange(T value, T min, T max) {
92 return std::min(std::max(value, min), max);
95 // Background: Existing transform code does not do the right thing in
96 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
97 // causes one of the transformed vertices to go to w < 0. In those cases, it
98 // is necessary to perform clipping in homogeneous coordinates, after applying
99 // the transform, before dividing-by-w to convert to cartesian coordinates.
101 // These functions return the axis-aligned rect that encloses the correctly
102 // clipped, transformed polygon.
103 static gfx::Rect MapEnclosingClippedRect(const gfx::Transform& transform,
104 const gfx::Rect& rect);
105 static gfx::RectF MapClippedRect(const gfx::Transform& transform,
106 const gfx::RectF& rect);
107 static gfx::Rect ProjectEnclosingClippedRect(const gfx::Transform& transform,
108 const gfx::Rect& rect);
109 static gfx::RectF ProjectClippedRect(const gfx::Transform& transform,
110 const gfx::RectF& rect);
112 // Returns an array of vertices that represent the clipped polygon. After
113 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
114 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
115 // which means the entire quad was clipped, and none of the vertices in the
116 // array are valid.
117 static void MapClippedQuad(const gfx::Transform& transform,
118 const gfx::QuadF& src_quad,
119 gfx::PointF clipped_quad[8],
120 int* num_vertices_in_clipped_quad);
122 static gfx::RectF ComputeEnclosingRectOfVertices(const gfx::PointF vertices[],
123 int num_vertices);
124 static gfx::RectF ComputeEnclosingClippedRect(
125 const HomogeneousCoordinate& h1,
126 const HomogeneousCoordinate& h2,
127 const HomogeneousCoordinate& h3,
128 const HomogeneousCoordinate& h4);
130 // NOTE: These functions do not do correct clipping against w = 0 plane, but
131 // they correctly detect the clipped condition via the boolean clipped.
132 static gfx::QuadF MapQuad(const gfx::Transform& transform,
133 const gfx::QuadF& quad,
134 bool* clipped);
135 static gfx::PointF MapPoint(const gfx::Transform& transform,
136 const gfx::PointF& point,
137 bool* clipped);
138 static gfx::Point3F MapPoint(const gfx::Transform&,
139 const gfx::Point3F&,
140 bool* clipped);
141 static gfx::QuadF ProjectQuad(const gfx::Transform& transform,
142 const gfx::QuadF& quad,
143 bool* clipped);
144 static gfx::PointF ProjectPoint(const gfx::Transform& transform,
145 const gfx::PointF& point,
146 bool* clipped);
147 // Identical to the above function, but coerces the homogeneous coordinate to
148 // a 3d rather than a 2d point.
149 static gfx::Point3F ProjectPoint3D(const gfx::Transform& transform,
150 const gfx::PointF& point,
151 bool* clipped);
153 static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&,
154 float fallbackValue);
156 // Makes a rect that has the same relationship to input_outer_rect as
157 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
158 // contained within scale_outer_rect, and likewise the rectangle that is
159 // returned will be within input_outer_rect at a similar relative, scaled
160 // position.
161 static gfx::RectF ScaleRectProportional(const gfx::RectF& input_outer_rect,
162 const gfx::RectF& scale_outer_rect,
163 const gfx::RectF& scale_inner_rect);
165 // Returns the smallest angle between the given two vectors in degrees.
166 // Neither vector is assumed to be normalized.
167 static float SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
168 const gfx::Vector2dF& v2);
170 // Projects the |source| vector onto |destination|. Neither vector is assumed
171 // to be normalized.
172 static gfx::Vector2dF ProjectVector(const gfx::Vector2dF& source,
173 const gfx::Vector2dF& destination);
175 // Conversion to value.
176 static scoped_ptr<base::Value> AsValue(const gfx::Size& s);
177 static scoped_ptr<base::Value> AsValue(const gfx::SizeF& s);
178 static scoped_ptr<base::Value> AsValue(const gfx::Rect& r);
179 static bool FromValue(const base::Value*, gfx::Rect* out_rect);
180 static scoped_ptr<base::Value> AsValue(const gfx::PointF& q);
181 static scoped_ptr<base::Value> AsValue(const gfx::Point3F&);
182 static scoped_ptr<base::Value> AsValue(const gfx::Vector2d& v);
183 static scoped_ptr<base::Value> AsValue(const gfx::QuadF& q);
184 static scoped_ptr<base::Value> AsValue(const gfx::RectF& rect);
185 static scoped_ptr<base::Value> AsValue(const gfx::Transform& transform);
186 static scoped_ptr<base::Value> AsValue(const gfx::BoxF& box);
188 // Returns a base::Value representation of the floating point value.
189 // If the value is inf, returns max double/float representation.
190 static scoped_ptr<base::Value> AsValueSafely(double value);
191 static scoped_ptr<base::Value> AsValueSafely(float value);
194 } // namespace cc
196 #endif // CC_BASE_MATH_UTIL_H_