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/point3_f.h" 15 #include "ui/gfx/point_f.h" 16 #include "ui/gfx/size.h" 17 #include "ui/gfx/transform.h" 18 19 namespace base { class Value; } 20 21 namespace gfx { 22 class QuadF; 23 class Rect; 24 class RectF; 25 class Transform; 26 class Vector2dF; 27 } 28 29 namespace cc { 30 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; 37 } 38 39 bool ShouldBeClipped() const { return w() <= 0.0; } 40 41 gfx::PointF CartesianPoint2d() const { 42 if (w() == 1.0) 43 return gfx::PointF(x(), y()); 44 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 double inv_w = 1.0 / w(); 49 return gfx::PointF(x() * inv_w, y() * inv_w); 50 } 51 52 gfx::Point3F CartesianPoint3d() const { 53 if (w() == 1) 54 return gfx::Point3F(x(), y(), z()); 55 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 double inv_w = 1.0 / w(); 60 return gfx::Point3F(x() * inv_w, y() * inv_w, z() * inv_w); 61 } 62 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]; } 67 68 SkMScalar vec[4]; 69 }; 70 71 class CC_EXPORT MathUtil { 72 public: 73 static const double kPiDouble; 74 static const float kPiFloat; 75 76 static double Deg2Rad(double deg) { return deg * kPiDouble / 180.0; } 77 static double Rad2Deg(double rad) { return rad * 180.0 / kPiDouble; } 78 79 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } 80 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } 81 82 static float Round(float f) { 83 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); 84 } 85 static double Round(double d) { 86 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); 87 } 88 89 template <typename T> static T ClampToRange(T value, T min, T max) { 90 return std::min(std::max(value, min), max); 91 } 92 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. 98 // 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); 107 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); 117 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); 125 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); 143 144 static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&, 145 float fallbackValue); 146 147 // Returns the smallest angle between the given two vectors in degrees. 148 // Neither vector is assumed to be normalized. 149 static float SmallestAngleBetweenVectors(gfx::Vector2dF v1, 150 gfx::Vector2dF v2); 151 152 // Projects the |source| vector onto |destination|. Neither vector is assumed 153 // to be normalized. 154 static gfx::Vector2dF ProjectVector(gfx::Vector2dF source, 155 gfx::Vector2dF destination); 156 157 // Conversion to value. 158 static scoped_ptr<base::Value> AsValue(gfx::Size s); 159 static scoped_ptr<base::Value> AsValue(gfx::SizeF s); 160 static scoped_ptr<base::Value> AsValue(gfx::Rect r); 161 static bool FromValue(const base::Value*, gfx::Rect* out_rect); 162 static scoped_ptr<base::Value> AsValue(gfx::PointF q); 163 static scoped_ptr<base::Value> AsValue(const gfx::QuadF& q); 164 static scoped_ptr<base::Value> AsValue(const gfx::RectF& rect); 165 static scoped_ptr<base::Value> AsValue(const gfx::Transform& transform); 166 167 // Returns a base::Value representation of the floating point value. 168 // If the value is inf, returns max double/float representation. 169 static scoped_ptr<base::Value> AsValueSafely(double value); 170 static scoped_ptr<base::Value> AsValueSafely(float value); 171 }; 172 173 } // namespace cc 174 175 #endif // CC_BASE_MATH_UTIL_H_ 176