Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / cc / base / math_util.h
blob486a654a5e49a76e9483f350760a61b500dc9732
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/box_f.h"
16 #include "ui/gfx/point3_f.h"
17 #include "ui/gfx/point_f.h"
18 #include "ui/gfx/size.h"
19 #include "ui/gfx/transform.h"
21 namespace base {
22 class Value;
23 namespace debug {
24 class TracedValue;
28 namespace gfx {
29 class QuadF;
30 class Rect;
31 class RectF;
32 class Transform;
33 class Vector2dF;
34 class Vector2d;
37 namespace cc {
39 struct HomogeneousCoordinate {
40 HomogeneousCoordinate(SkMScalar x, SkMScalar y, SkMScalar z, SkMScalar w) {
41 vec[0] = x;
42 vec[1] = y;
43 vec[2] = z;
44 vec[3] = w;
47 bool ShouldBeClipped() const { return w() <= 0.0; }
49 gfx::PointF CartesianPoint2d() const {
50 if (w() == SK_MScalar1)
51 return gfx::PointF(x(), y());
53 // For now, because this code is used privately only by MathUtil, it should
54 // never be called when w == 0, and we do not yet need to handle that case.
55 DCHECK(w());
56 SkMScalar inv_w = SK_MScalar1 / w();
57 return gfx::PointF(x() * inv_w, y() * inv_w);
60 gfx::Point3F CartesianPoint3d() const {
61 if (w() == SK_MScalar1)
62 return gfx::Point3F(x(), y(), z());
64 // For now, because this code is used privately only by MathUtil, it should
65 // never be called when w == 0, and we do not yet need to handle that case.
66 DCHECK(w());
67 SkMScalar inv_w = SK_MScalar1 / w();
68 return gfx::Point3F(x() * inv_w, y() * inv_w, z() * inv_w);
71 SkMScalar x() const { return vec[0]; }
72 SkMScalar y() const { return vec[1]; }
73 SkMScalar z() const { return vec[2]; }
74 SkMScalar w() const { return vec[3]; }
76 SkMScalar vec[4];
79 class CC_EXPORT MathUtil {
80 public:
81 static const double kPiDouble;
82 static const float kPiFloat;
84 static double Deg2Rad(double deg) { return deg * kPiDouble / 180.0; }
85 static double Rad2Deg(double rad) { return rad * 180.0 / kPiDouble; }
87 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; }
88 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; }
90 static float Round(float f) {
91 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f);
93 static double Round(double d) {
94 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
97 template <typename T> static T ClampToRange(T value, T min, T max) {
98 return std::min(std::max(value, min), max);
101 // Background: Existing transform code does not do the right thing in
102 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that
103 // causes one of the transformed vertices to go to w < 0. In those cases, it
104 // is necessary to perform clipping in homogeneous coordinates, after applying
105 // the transform, before dividing-by-w to convert to cartesian coordinates.
107 // These functions return the axis-aligned rect that encloses the correctly
108 // clipped, transformed polygon.
109 static gfx::Rect MapEnclosingClippedRect(const gfx::Transform& transform,
110 const gfx::Rect& rect);
111 static gfx::RectF MapClippedRect(const gfx::Transform& transform,
112 const gfx::RectF& rect);
113 static gfx::Rect ProjectEnclosingClippedRect(const gfx::Transform& transform,
114 const gfx::Rect& rect);
115 static gfx::RectF ProjectClippedRect(const gfx::Transform& transform,
116 const gfx::RectF& rect);
118 // Returns an array of vertices that represent the clipped polygon. After
119 // returning, indexes from 0 to num_vertices_in_clipped_quad are valid in the
120 // clipped_quad array. Note that num_vertices_in_clipped_quad may be zero,
121 // which means the entire quad was clipped, and none of the vertices in the
122 // array are valid.
123 static void MapClippedQuad(const gfx::Transform& transform,
124 const gfx::QuadF& src_quad,
125 gfx::PointF clipped_quad[8],
126 int* num_vertices_in_clipped_quad);
127 static bool MapClippedQuad3d(const gfx::Transform& transform,
128 const gfx::QuadF& src_quad,
129 gfx::Point3F clipped_quad[8],
130 int* num_vertices_in_clipped_quad);
132 static gfx::RectF ComputeEnclosingRectOfVertices(const gfx::PointF vertices[],
133 int num_vertices);
134 static gfx::RectF ComputeEnclosingClippedRect(
135 const HomogeneousCoordinate& h1,
136 const HomogeneousCoordinate& h2,
137 const HomogeneousCoordinate& h3,
138 const HomogeneousCoordinate& h4);
140 // NOTE: These functions do not do correct clipping against w = 0 plane, but
141 // they correctly detect the clipped condition via the boolean clipped.
142 static gfx::QuadF MapQuad(const gfx::Transform& transform,
143 const gfx::QuadF& quad,
144 bool* clipped);
145 static gfx::QuadF MapQuad3d(const gfx::Transform& transform,
146 const gfx::QuadF& q,
147 gfx::Point3F* p,
148 bool* clipped);
149 static gfx::PointF MapPoint(const gfx::Transform& transform,
150 const gfx::PointF& point,
151 bool* clipped);
152 static gfx::Point3F MapPoint(const gfx::Transform&,
153 const gfx::Point3F&,
154 bool* clipped);
155 static gfx::QuadF ProjectQuad(const gfx::Transform& transform,
156 const gfx::QuadF& quad,
157 bool* clipped);
158 static gfx::PointF ProjectPoint(const gfx::Transform& transform,
159 const gfx::PointF& point,
160 bool* clipped);
161 // Identical to the above function, but coerces the homogeneous coordinate to
162 // a 3d rather than a 2d point.
163 static gfx::Point3F ProjectPoint3D(const gfx::Transform& transform,
164 const gfx::PointF& point,
165 bool* clipped);
167 static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&,
168 float fallbackValue);
170 // Makes a rect that has the same relationship to input_outer_rect as
171 // scale_inner_rect has to scale_outer_rect. scale_inner_rect should be
172 // contained within scale_outer_rect, and likewise the rectangle that is
173 // returned will be within input_outer_rect at a similar relative, scaled
174 // position.
175 static gfx::RectF ScaleRectProportional(const gfx::RectF& input_outer_rect,
176 const gfx::RectF& scale_outer_rect,
177 const gfx::RectF& scale_inner_rect);
179 // Returns the smallest angle between the given two vectors in degrees.
180 // Neither vector is assumed to be normalized.
181 static float SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
182 const gfx::Vector2dF& v2);
184 // Projects the |source| vector onto |destination|. Neither vector is assumed
185 // to be normalized.
186 static gfx::Vector2dF ProjectVector(const gfx::Vector2dF& source,
187 const gfx::Vector2dF& destination);
189 // Conversion to value.
190 static scoped_ptr<base::Value> AsValue(const gfx::Size& s);
191 static scoped_ptr<base::Value> AsValue(const gfx::Rect& r);
192 static bool FromValue(const base::Value*, gfx::Rect* out_rect);
193 static scoped_ptr<base::Value> AsValue(const gfx::PointF& q);
195 static void AddToTracedValue(const gfx::Size& s,
196 base::debug::TracedValue* res);
197 static void AddToTracedValue(const gfx::SizeF& s,
198 base::debug::TracedValue* res);
199 static void AddToTracedValue(const gfx::Rect& r,
200 base::debug::TracedValue* res);
201 static void AddToTracedValue(const gfx::PointF& q,
202 base::debug::TracedValue* res);
203 static void AddToTracedValue(const gfx::Point3F&,
204 base::debug::TracedValue* res);
205 static void AddToTracedValue(const gfx::Vector2d& v,
206 base::debug::TracedValue* res);
207 static void AddToTracedValue(const gfx::Vector2dF& v,
208 base::debug::TracedValue* res);
209 static void AddToTracedValue(const gfx::QuadF& q,
210 base::debug::TracedValue* res);
211 static void AddToTracedValue(const gfx::RectF& rect,
212 base::debug::TracedValue* res);
213 static void AddToTracedValue(const gfx::Transform& transform,
214 base::debug::TracedValue* res);
215 static void AddToTracedValue(const gfx::BoxF& box,
216 base::debug::TracedValue* res);
218 // Returns a base::Value representation of the floating point value.
219 // If the value is inf, returns max double/float representation.
220 static double AsDoubleSafely(double value);
221 static float AsFloatSafely(float value);
224 } // namespace cc
226 #endif // CC_BASE_MATH_UTIL_H_