BezierUtils.h (5493B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_BezierUtils_h_ 8 #define mozilla_BezierUtils_h_ 9 10 #include "mozilla/gfx/Point.h" 11 #include "mozilla/gfx/Types.h" 12 13 namespace mozilla { 14 namespace gfx { 15 16 // Control points for bezier curve 17 // 18 // mPoints[2] 19 // +-----___---+ mPoints[3] 20 // __-- 21 // _-- 22 // / 23 // / 24 // mPoints[1] + | 25 // | | 26 // || 27 // || 28 // | 29 // | 30 // | 31 // | 32 // mPoints[0] + 33 struct Bezier { 34 Point mPoints[4]; 35 }; 36 37 // Calculate a point or it's differential of a bezier curve formed by 38 // aBezier and parameter t. 39 // 40 // GetBezierPoint = P(t) 41 // GetBezierDifferential = P'(t) 42 // GetBezierDifferential2 = P''(t) 43 // 44 // mPoints[2] 45 // +-----___---+ mPoints[3] 46 // __-- P(1) 47 // _-- 48 // + 49 // / P(t) 50 // mPoints[1] + | 51 // | | 52 // || 53 // || 54 // | 55 // | 56 // | 57 // | 58 // mPoints[0] + P(0) 59 Point GetBezierPoint(const Bezier& aBezier, Float t); 60 Point GetBezierDifferential(const Bezier& aBezier, Float t); 61 Point GetBezierDifferential2(const Bezier& aBezier, Float t); 62 63 // Calculate length of a simple bezier curve formed by aBezier and range [a, b]. 64 Float GetBezierLength(const Bezier& aBezier, Float a, Float b); 65 66 // Split bezier curve formed by aBezier into [0,t1], [t1,t2], [t2,1] parts, and 67 // stores control points for [t1,t2] to aSubBezier. 68 // 69 // ___---+ 70 // __+- P(1) 71 // _-- P(t2) 72 // - 73 // / <-- aSubBezier 74 // | 75 // | 76 // + 77 // | P(t1) 78 // | 79 // | 80 // | 81 // | 82 // + P(0) 83 void GetSubBezier(Bezier* aSubBezier, const Bezier& aBezier, Float t1, 84 Float t2); 85 86 // Find a nearest point on bezier curve formed by aBezier to a point aTarget. 87 // aInitialT is a hint to find the parameter t for the nearest point. 88 // If aT is non-null, parameter for the nearest point is stored to *aT. 89 // This function expects a bezier curve to be an approximation of elliptic arc. 90 // Otherwise it will return wrong point. 91 // 92 // aTarget 93 // + ___---+ 94 // __-- 95 // _-- 96 // + 97 // / nearest point = P(t = *aT) 98 // | 99 // | 100 // | 101 // + P(aInitialT) 102 // | 103 // | 104 // | 105 // | 106 // + 107 Point FindBezierNearestPoint(const Bezier& aBezier, const Point& aTarget, 108 Float aInitialT, Float* aT = nullptr); 109 110 // Calculate control points for a bezier curve that is an approximation of 111 // an elliptic arc. 112 // 113 // aCornerSize.width 114 // |<----------------->| 115 // | | 116 // aCornerPoint| mPoints[2] | 117 // -------------+-------+-----___---+ mPoints[3] 118 // ^ | __-- 119 // | | _-- 120 // | | - 121 // | | / 122 // aCornerSize.height | mPoints[1] + | 123 // | | | 124 // | || 125 // | || 126 // | | 127 // | | 128 // | | 129 // v mPoints[0] | 130 // -------------+ 131 void GetBezierPointsForCorner(Bezier* aBezier, mozilla::Corner aCorner, 132 const Point& aCornerPoint, 133 const Size& aCornerSize); 134 135 // Calculate the approximate length of a quarter elliptic arc formed by radii 136 // (a, b). 137 // 138 // a 139 // |<----------------->| 140 // | | 141 // ---+-------------___---+ 142 // ^ | __-- 143 // | | _-- 144 // | | - 145 // | | / 146 // b | | | 147 // | | | 148 // | || 149 // | || 150 // | | 151 // | | 152 // | | 153 // v | 154 // ---+ 155 Float GetQuarterEllipticArcLength(Float a, Float b); 156 157 // Calculate the distance between an elliptic arc formed by (origin, width, 158 // height), and a point P, along a line formed by |P + n * normal|. 159 // P should be outside of the ellipse, and the line should cross with the 160 // ellipse twice at n > 0 points. 161 // 162 // width 163 // |<----------------->| 164 // origin | | 165 // -----------+-------------___---+ 166 // ^ normal | __-- 167 // | P +->__ | _-- 168 // | --__ - 169 // | | --+ 170 // height | | | 171 // | | | 172 // | || 173 // | || 174 // | | 175 // | | 176 // | | 177 // v | 178 // -----------+ 179 Float CalculateDistanceToEllipticArc(const Point& P, const Point& normal, 180 const Point& origin, Float width, 181 Float height); 182 183 } // namespace gfx 184 } // namespace mozilla 185 186 #endif /* mozilla_BezierUtils_h_ */