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