CubicToQuadratics.cpp revision 05c4bad470722bc4e5e6ae3d79aa8bcf9e732f06
16d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com/* 26d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comhttp://stackoverflow.com/questions/2009160/how-do-i-convert-the-2-control-points-of-a-cubic-curve-to-the-single-control-poi 36d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com*/ 46d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 56d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com/* 68ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.comLet's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2. 76d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comThen for degree elevation, the equations are: 86d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 96d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ0 = P0 106d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ1 = 1/3 P0 + 2/3 P1 116d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ2 = 2/3 P1 + 1/3 P2 126d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ3 = P2 136d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comIn your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from 146d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com the equations above: 156d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 166d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = 3/2 Q1 - 1/2 Q0 176d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = 3/2 Q2 - 1/2 Q3 186d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comIf this is a degree-elevated cubic, then both equations will give the same answer for P1. Since 196d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com it's likely not, your best bet is to average them. So, 206d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 216d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3 226d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 236d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 246d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comCubic defined by: P1/2 - anchor points, C1/C2 control points 256d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com|x| is the euclidean norm of x 266d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.commid-point approx of cubic: a quad that shares the same anchors with the cubic and has the 276d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com control point at C = (3·C2 - P2 + 3·C1 - P1)/4 288ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.com 296d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comAlgorithm 306d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 316d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.compick an absolute precision (prec) 328ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.comCompute the Tdiv as the root of (cubic) equation 336d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comsqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec 346d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comif Tdiv < 0.5 divide the cubic at Tdiv. First segment [0..Tdiv] can be approximated with by a 356d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com quadratic, with a defect less than prec, by the mid-point approximation. 366d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com Repeat from step 2 with the second resulted segment (corresponding to 1-Tdiv) 376d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com0.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point 386d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com approximation 396d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comTdiv>=1 - the entire cubic can be approximated by the mid-point approximation 406d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 416d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comconfirmed by (maybe stolen from) 426d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comhttp://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html 4373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// maybe in turn derived from http://www.cccg.ca/proceedings/2004/36.pdf 4473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// also stored at http://www.cis.usouthal.edu/~hain/general/Publications/Bezier/bezier%20cccg04%20paper.pdf 456d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 466d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com*/ 476d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 486d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com#include "CubicUtilities.h" 496d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com#include "CurveIntersection.h" 5073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com#include "LineIntersection.h" 5173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com 5273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comconst bool AVERAGE_END_POINTS = true; // results in better fitting curves 5373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com 5473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com#define USE_CUBIC_END_POINTS 1 556d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 56d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.comstatic double calcTDiv(const Cubic& cubic, double precision, double start) { 576d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com const double adjust = sqrt(3) / 36; 586d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com Cubic sub; 596d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com const Cubic* cPtr; 606d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com if (start == 0) { 616d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com cPtr = &cubic; 626d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } else { 636d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com // OPTIMIZE: special-case half-split ? 646d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com sub_divide(cubic, start, 1, sub); 656d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com cPtr = ⊂ 666d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } 676d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com const Cubic& c = *cPtr; 686d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com double dx = c[3].x - 3 * (c[2].x - c[1].x) - c[0].x; 696d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com double dy = c[3].y - 3 * (c[2].y - c[1].y) - c[0].y; 706d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com double dist = sqrt(dx * dx + dy * dy); 71d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.com double tDiv3 = precision / (adjust * dist); 7273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double t = cube_root(tDiv3); 7373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (start > 0) { 7473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com t = start + (1 - start) * t; 7573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 7673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return t; 776d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com} 786d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 7973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comvoid demote_cubic_to_quad(const Cubic& cubic, Quadratic& quad) { 806d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com quad[0] = cubic[0]; 8173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comif (AVERAGE_END_POINTS) { 8273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com const _Point fromC1 = { (3 * cubic[1].x - cubic[0].x) / 2, (3 * cubic[1].y - cubic[0].y) / 2 }; 8373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com const _Point fromC2 = { (3 * cubic[2].x - cubic[3].x) / 2, (3 * cubic[2].y - cubic[3].y) / 2 }; 8473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com quad[1].x = (fromC1.x + fromC2.x) / 2; 8573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com quad[1].y = (fromC1.y + fromC2.y) / 2; 8673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} else { 8773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com lineIntersect((const _Line&) cubic[0], (const _Line&) cubic[2], quad[1]); 8873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} 896d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com quad[2] = cubic[3]; 906d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com} 916d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com 92d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.comint cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) { 9373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com SkTDArray<double> ts; 9473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com cubic_to_quadratics(cubic, precision, ts); 9573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com int tsCount = ts.count(); 9673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double t1Start = 0; 9773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com int order = 0; 9873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com for (int idx = 0; idx <= tsCount; ++idx) { 9973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double t1 = idx < tsCount ? ts[idx] : 1; 10073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com Cubic part; 10173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com sub_divide(cubic, t1Start, t1, part); 10273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com Quadratic q1; 10373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com demote_cubic_to_quad(part, q1); 10473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com Quadratic s1; 10573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com int o1 = reduceOrder(q1, s1); 10673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (order < o1) { 10773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com order = o1; 10873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 10973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com memcpy(quadratics.append(), o1 < 2 ? s1 : q1, sizeof(Quadratic)); 11073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com t1Start = t1; 11173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 11273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return order; 11373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} 11473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com 11573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comstatic bool addSimpleTs(const Cubic& cubic, double precision, SkTDArray<double>& ts) { 11673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double tDiv = calcTDiv(cubic, precision, 0); 11773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (tDiv >= 1) { 11873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return true; 11973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 12073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (tDiv >= 0.5) { 12173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com *ts.append() = 0.5; 12273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return true; 12373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 12473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return false; 12573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} 12673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com 12773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comstatic void addTs(const Cubic& cubic, double precision, double start, double end, 12873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com SkTDArray<double>& ts) { 12973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double tDiv = calcTDiv(cubic, precision, 0); 13073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double parts = ceil(1.0 / tDiv); 13173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com for (double index = 0; index < parts; ++index) { 13273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double newT = start + (index / parts) * (end - start); 13373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (newT > 0 && newT < 1) { 13473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com *ts.append() = newT; 13573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 13673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 13773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} 13873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com 13973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// flavor that returns T values only, deferring computing the quads until they are needed 14005c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// FIXME: when called from recursive intersect 2, this could take the original cubic 14105c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// and do a more precise job when calling chop at and sub divide by computing the fractional ts. 14205c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// it would still take the prechopped cubic for reduce order and find cubic inflections 14373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comvoid cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<double>& ts) { 1446d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com Cubic reduced; 1456d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed); 1466d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com if (order < 3) { 14773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return; 1486d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } 14973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com double inflectT[2]; 15073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com int inflections = find_cubic_inflections(cubic, inflectT); 15173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com SkASSERT(inflections <= 2); 15273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (inflections == 0 && addSimpleTs(cubic, precision, ts)) { 15373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return; 1546d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } 15573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (inflections == 1) { 1566d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com CubicPair pair; 15773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com chop_at(cubic, pair, inflectT[0]); 15873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(pair.first(), precision, 0, inflectT[0], ts); 15973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(pair.second(), precision, inflectT[0], 1, ts); 16073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return; 1616d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } 16273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (inflections == 2) { 16373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com if (inflectT[0] > inflectT[1]) { 16473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com SkTSwap(inflectT[0], inflectT[1]); 1656d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com } 16673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com Cubic part; 16773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com sub_divide(cubic, 0, inflectT[0], part); 16873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(part, precision, 0, inflectT[0], ts); 16973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com sub_divide(cubic, inflectT[0], inflectT[1], part); 17073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(part, precision, inflectT[0], inflectT[1], ts); 17173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com sub_divide(cubic, inflectT[1], 1, part); 17273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(part, precision, inflectT[1], 1, ts); 17373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com return; 17473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com } 17573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com addTs(cubic, precision, 0, 1, ts); 1766d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com} 177