2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "src/pathops/SkIntersections.h"
8 #include "src/pathops/SkLineParameters.h"
9 #include "src/pathops/SkPathOpsConic.h"
10 #include "src/pathops/SkPathOpsCubic.h"
11 #include "src/pathops/SkPathOpsQuad.h"
12 #include "src/pathops/SkPathOpsRect.h"
14 // cribbed from the float version in SkGeometry.cpp
15 static void conic_deriv_coeff(const double src[],
18 const double P20 = src[4] - src[0];
19 const double P10 = src[2] - src[0];
20 const double wP10 = w * P10;
21 coeff[0] = w * P20 - P20;
22 coeff[1] = P20 - 2 * wP10;
26 static double conic_eval_tan(const double coord[], SkScalar w, double t) {
28 conic_deriv_coeff(coord, w, coeff);
29 return t * (t * coeff[0] + coeff[1]) + coeff[2];
32 int SkDConic::FindExtrema(const double src[], SkScalar w, double t[1]) {
34 conic_deriv_coeff(src, w, coeff);
37 int roots = SkDQuad::RootsValidT(coeff[0], coeff[1], coeff[2], tValues);
38 // In extreme cases, the number of roots returned can be 2. Pathops
39 // will fail later on, so there's no advantage to plumbing in an error
41 // SkASSERT(0 == roots || 1 == roots);
50 SkDVector SkDConic::dxdyAtT(double t) const {
52 conic_eval_tan(&fPts[0].fX, fWeight, t),
53 conic_eval_tan(&fPts[0].fY, fWeight, t)
55 if (result.fX == 0 && result.fY == 0) {
57 result = fPts[2] - fPts[0];
66 static double conic_eval_numerator(const double src[], SkScalar w, double t) {
68 SkASSERT(t >= 0 && t <= 1);
69 double src2w = src[2] * w;
71 double A = src[4] - 2 * src2w + C;
72 double B = 2 * (src2w - C);
73 return (A * t + B) * t + C;
77 static double conic_eval_denominator(SkScalar w, double t) {
78 double B = 2 * (w - 1);
81 return (A * t + B) * t + C;
84 bool SkDConic::hullIntersects(const SkDCubic& cubic, bool* isLinear) const {
85 return cubic.hullIntersects(*this, isLinear);
88 SkDPoint SkDConic::ptAtT(double t) const {
95 double denominator = conic_eval_denominator(fWeight, t);
97 sk_ieee_double_divide(conic_eval_numerator(&fPts[0].fX, fWeight, t), denominator),
98 sk_ieee_double_divide(conic_eval_numerator(&fPts[0].fY, fWeight, t), denominator)
103 /* see quad subdivide for point rationale */
104 /* w rationale : the mid point between t1 and t2 could be determined from the computed a/b/c
105 values if the computed w was known. Since we know the mid point at (t1+t2)/2, we'll assume
106 that it is the same as the point on the new curve t==(0+1)/2.
108 d / dz == conic_poly(dst, unknownW, .5) / conic_weight(unknownW, .5);
110 conic_poly(dst, unknownW, .5)
111 = a / 4 + (b * unknownW) / 2 + c / 4
112 = (a + c) / 4 + (bx * unknownW) / 2
114 conic_weight(unknownW, .5)
115 = unknownW / 2 + 1 / 2
117 d / dz == ((a + c) / 2 + b * unknownW) / (unknownW + 1)
118 d / dz * (unknownW + 1) == (a + c) / 2 + b * unknownW
119 unknownW = ((a + c) / 2 - d / dz) / (d / dz - b)
121 Thus, w is the ratio of the distance from the mid of end points to the on-curve point, and the
122 distance of the on-curve point to the control point.
124 SkDConic SkDConic::subDivide(double t1, double t2) const {
130 } else if (t1 != 1) {
131 ax = conic_eval_numerator(&fPts[0].fX, fWeight, t1);
132 ay = conic_eval_numerator(&fPts[0].fY, fWeight, t1);
133 az = conic_eval_denominator(fWeight, t1);
139 double midT = (t1 + t2) / 2;
140 double dx = conic_eval_numerator(&fPts[0].fX, fWeight, midT);
141 double dy = conic_eval_numerator(&fPts[0].fY, fWeight, midT);
142 double dz = conic_eval_denominator(fWeight, midT);
148 } else if (t2 != 0) {
149 cx = conic_eval_numerator(&fPts[0].fX, fWeight, t2);
150 cy = conic_eval_numerator(&fPts[0].fY, fWeight, t2);
151 cz = conic_eval_denominator(fWeight, t2);
157 double bx = 2 * dx - (ax + cx) / 2;
158 double by = 2 * dy - (ay + cy) / 2;
159 double bz = 2 * dz - (az + cz) / 2;
161 bz = 1; // if bz is 0, weight is 0, control point has no effect: any value will do
163 SkDConic dst = {{{{ax / az, ay / az}, {bx / bz, by / bz}, {cx / cz, cy / cz}}
164 SkDEBUGPARAMS(fPts.fDebugGlobalState) },
165 SkDoubleToScalar(bz / sqrt(az * cz)) };
169 SkDPoint SkDConic::subDivide(const SkDPoint& a, const SkDPoint& c, double t1, double t2,
170 SkScalar* weight) const {
171 SkDConic chopped = this->subDivide(t1, t2);
172 *weight = chopped.fWeight;
176 int SkTConic::intersectRay(SkIntersections* i, const SkDLine& line) const {
177 return i->intersectRay(fConic, line);
180 bool SkTConic::hullIntersects(const SkDQuad& quad, bool* isLinear) const {
181 return quad.hullIntersects(fConic, isLinear);
184 bool SkTConic::hullIntersects(const SkDCubic& cubic, bool* isLinear) const {
185 return cubic.hullIntersects(fConic, isLinear);
188 void SkTConic::setBounds(SkDRect* rect) const {
189 rect->setBounds(fConic);