2012-09-14 14:19:30 +00:00
|
|
|
// Another approach is to start with the implicit form of one curve and solve
|
|
|
|
// (seek implicit coefficients in QuadraticParameter.cpp
|
|
|
|
// by substituting in the parametric form of the other.
|
|
|
|
// The downside of this approach is that early rejects are difficult to come by.
|
|
|
|
// http://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html#step
|
|
|
|
|
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
#include "CubicUtilities.h"
|
2012-09-14 14:19:30 +00:00
|
|
|
#include "CurveIntersection.h"
|
|
|
|
#include "Intersections.h"
|
|
|
|
#include "QuadraticParameterization.h"
|
|
|
|
#include "QuarticRoot.h"
|
|
|
|
#include "QuadraticUtilities.h"
|
2013-01-17 21:02:47 +00:00
|
|
|
#include "TSearch.h"
|
|
|
|
|
2013-01-28 19:25:51 +00:00
|
|
|
#if SK_DEBUG
|
|
|
|
#include "LineUtilities.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-14 14:19:30 +00:00
|
|
|
/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
|
|
|
|
* and given x = at^2 + bt + c (the parameterized form)
|
|
|
|
* y = dt^2 + et + f
|
2012-09-15 02:01:41 +00:00
|
|
|
* then
|
2012-09-14 14:19:30 +00:00
|
|
|
* 0 = A(at^2+bt+c)(at^2+bt+c)+B(at^2+bt+c)(dt^2+et+f)+C(dt^2+et+f)(dt^2+et+f)+D(at^2+bt+c)+E(dt^2+et+f)+F
|
|
|
|
*/
|
2013-01-18 07:07:28 +00:00
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4],
|
2013-02-20 12:51:37 +00:00
|
|
|
bool oneHint, int firstCubicRoot) {
|
2012-09-14 14:19:30 +00:00
|
|
|
double a, b, c;
|
|
|
|
set_abc(&q2[0].x, a, b, c);
|
|
|
|
double d, e, f;
|
|
|
|
set_abc(&q2[0].y, d, e, f);
|
|
|
|
const double t4 = i.x2() * a * a
|
|
|
|
+ i.xy() * a * d
|
|
|
|
+ i.y2() * d * d;
|
|
|
|
const double t3 = 2 * i.x2() * a * b
|
|
|
|
+ i.xy() * (a * e + b * d)
|
|
|
|
+ 2 * i.y2() * d * e;
|
|
|
|
const double t2 = i.x2() * (b * b + 2 * a * c)
|
|
|
|
+ i.xy() * (c * d + b * e + a * f)
|
|
|
|
+ i.y2() * (e * e + 2 * d * f)
|
|
|
|
+ i.x() * a
|
|
|
|
+ i.y() * d;
|
|
|
|
const double t1 = 2 * i.x2() * b * c
|
|
|
|
+ i.xy() * (c * e + b * f)
|
|
|
|
+ 2 * i.y2() * e * f
|
|
|
|
+ i.x() * b
|
|
|
|
+ i.y() * e;
|
|
|
|
const double t0 = i.x2() * c * c
|
|
|
|
+ i.xy() * c * f
|
|
|
|
+ i.y2() * f * f
|
|
|
|
+ i.x() * c
|
|
|
|
+ i.y() * f
|
|
|
|
+ i.c();
|
2013-01-24 21:47:16 +00:00
|
|
|
int rootCount = reducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots);
|
|
|
|
if (rootCount >= 0) {
|
|
|
|
return rootCount;
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
2013-02-20 12:51:37 +00:00
|
|
|
return quarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 15:29:11 +00:00
|
|
|
static int addValidRoots(const double roots[4], const int count, double valid[4]) {
|
|
|
|
int result = 0;
|
2012-09-14 14:19:30 +00:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < count; ++index) {
|
|
|
|
if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double t = 1 - roots[index];
|
|
|
|
if (approximately_less_than_zero(t)) {
|
|
|
|
t = 0;
|
|
|
|
} else if (approximately_greater_than_one(t)) {
|
|
|
|
t = 1;
|
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
valid[result++] = t;
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
return result;
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 14:11:58 +00:00
|
|
|
static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
|
|
|
|
// the idea here is to see at minimum do a quick reject by rotating all points
|
|
|
|
// to either side of the line formed by connecting the endpoints
|
|
|
|
// if the opposite curves points are on the line or on the other side, the
|
|
|
|
// curves at most intersect at the endpoints
|
|
|
|
for (int oddMan = 0; oddMan < 3; ++oddMan) {
|
|
|
|
const _Point* endPt[2];
|
|
|
|
for (int opp = 1; opp < 3; ++opp) {
|
|
|
|
int end = oddMan ^ opp;
|
|
|
|
if (end == 3) {
|
|
|
|
end = opp;
|
|
|
|
}
|
|
|
|
endPt[opp - 1] = &q1[end];
|
|
|
|
}
|
|
|
|
double origX = endPt[0]->x;
|
|
|
|
double origY = endPt[0]->y;
|
|
|
|
double adj = endPt[1]->x - origX;
|
|
|
|
double opp = endPt[1]->y - origY;
|
|
|
|
double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp;
|
2013-01-17 21:02:47 +00:00
|
|
|
if (approximately_zero(sign)) {
|
|
|
|
goto tryNextHalfPlane;
|
|
|
|
}
|
2012-10-09 14:11:58 +00:00
|
|
|
for (int n = 0; n < 3; ++n) {
|
|
|
|
double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp;
|
|
|
|
if (test * sign > 0) {
|
|
|
|
goto tryNextHalfPlane;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i1 = 0; i1 < 3; i1 += 2) {
|
|
|
|
for (int i2 = 0; i2 < 3; i2 += 2) {
|
|
|
|
if (q1[i1] == q2[i2]) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insert(i1 >> 1, i2 >> 1, q1[i1]);
|
2012-10-09 14:11:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 20:28:49 +00:00
|
|
|
SkASSERT(i.fUsed < 3);
|
2012-10-09 14:11:58 +00:00
|
|
|
return true;
|
|
|
|
tryNextHalfPlane:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:25:51 +00:00
|
|
|
// returns false if there's more than one intercept or the intercept doesn't match the point
|
2013-01-29 07:05:52 +00:00
|
|
|
// returns true if the intercept was successfully added or if the
|
2013-01-28 19:25:51 +00:00
|
|
|
// original quads need to be subdivided
|
2013-01-17 21:02:47 +00:00
|
|
|
static bool addIntercept(const Quadratic& q1, const Quadratic& q2, double tMin, double tMax,
|
2013-01-28 19:25:51 +00:00
|
|
|
Intersections& i, bool* subDivide) {
|
2013-01-17 21:02:47 +00:00
|
|
|
double tMid = (tMin + tMax) / 2;
|
|
|
|
_Point mid;
|
|
|
|
xy_at_t(q2, tMid, mid.x, mid.y);
|
|
|
|
_Line line;
|
|
|
|
line[0] = line[1] = mid;
|
2013-02-26 15:56:05 +00:00
|
|
|
_Vector dxdy = dxdy_at_t(q2, tMid);
|
|
|
|
line[0] -= dxdy;
|
|
|
|
line[1] += dxdy;
|
2013-01-17 21:02:47 +00:00
|
|
|
Intersections rootTs;
|
|
|
|
int roots = intersect(q1, line, rootTs);
|
2013-01-28 19:25:51 +00:00
|
|
|
if (roots == 0) {
|
|
|
|
if (subDivide) {
|
|
|
|
*subDivide = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-24 21:47:16 +00:00
|
|
|
if (roots == 2) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-17 21:02:47 +00:00
|
|
|
_Point pt2;
|
|
|
|
xy_at_t(q1, rootTs.fT[0][0], pt2.x, pt2.y);
|
2013-02-14 15:29:11 +00:00
|
|
|
if (!pt2.approximatelyEqualHalf(mid)) {
|
2013-01-17 21:02:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insertSwap(rootTs.fT[0][0], tMid, pt2);
|
2013-01-17 21:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isLinearInner(const Quadratic& q1, double t1s, double t1e, const Quadratic& q2,
|
2013-01-28 19:25:51 +00:00
|
|
|
double t2s, double t2e, Intersections& i, bool* subDivide) {
|
2013-01-17 21:02:47 +00:00
|
|
|
Quadratic hull;
|
|
|
|
sub_divide(q1, t1s, t1e, hull);
|
|
|
|
_Line line = {hull[2], hull[0]};
|
|
|
|
const _Line* testLines[] = { &line, (const _Line*) &hull[0], (const _Line*) &hull[1] };
|
|
|
|
size_t testCount = sizeof(testLines) / sizeof(testLines[0]);
|
|
|
|
SkTDArray<double> tsFound;
|
|
|
|
for (size_t index = 0; index < testCount; ++index) {
|
|
|
|
Intersections rootTs;
|
|
|
|
int roots = intersect(q2, *testLines[index], rootTs);
|
|
|
|
for (int idx2 = 0; idx2 < roots; ++idx2) {
|
|
|
|
double t = rootTs.fT[0][idx2];
|
2013-01-28 19:25:51 +00:00
|
|
|
#if SK_DEBUG
|
|
|
|
_Point qPt, lPt;
|
|
|
|
xy_at_t(q2, t, qPt.x, qPt.y);
|
|
|
|
xy_at_t(*testLines[index], rootTs.fT[1][idx2], lPt.x, lPt.y);
|
|
|
|
SkASSERT(qPt.approximatelyEqual(lPt));
|
|
|
|
#endif
|
2013-01-17 21:02:47 +00:00
|
|
|
if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*tsFound.append() = rootTs.fT[0][idx2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int tCount = tsFound.count();
|
|
|
|
if (!tCount) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
double tMin, tMax;
|
|
|
|
if (tCount == 1) {
|
|
|
|
tMin = tMax = tsFound[0];
|
|
|
|
} else if (tCount > 1) {
|
|
|
|
QSort<double>(tsFound.begin(), tsFound.end() - 1);
|
|
|
|
tMin = tsFound[0];
|
2013-03-13 20:29:41 +00:00
|
|
|
tMax = tsFound[tsFound.count() - 1];
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
_Point end;
|
|
|
|
xy_at_t(q2, t2s, end.x, end.y);
|
2013-02-07 13:13:41 +00:00
|
|
|
bool startInTriangle = point_in_hull(hull, end);
|
2013-01-17 21:02:47 +00:00
|
|
|
if (startInTriangle) {
|
|
|
|
tMin = t2s;
|
2013-01-18 07:07:28 +00:00
|
|
|
}
|
2013-01-17 21:02:47 +00:00
|
|
|
xy_at_t(q2, t2e, end.x, end.y);
|
2013-02-07 13:13:41 +00:00
|
|
|
bool endInTriangle = point_in_hull(hull, end);
|
2013-01-17 21:02:47 +00:00
|
|
|
if (endInTriangle) {
|
|
|
|
tMax = t2e;
|
|
|
|
}
|
|
|
|
int split = 0;
|
2013-02-26 15:56:05 +00:00
|
|
|
_Vector dxy1, dxy2;
|
2013-01-17 21:02:47 +00:00
|
|
|
if (tMin != tMax || tCount > 2) {
|
2013-02-26 15:56:05 +00:00
|
|
|
dxy2 = dxdy_at_t(q2, tMin);
|
2013-01-17 21:02:47 +00:00
|
|
|
for (int index = 1; index < tCount; ++index) {
|
|
|
|
dxy1 = dxy2;
|
2013-02-26 15:56:05 +00:00
|
|
|
dxy2 = dxdy_at_t(q2, tsFound[index]);
|
2013-01-17 21:02:47 +00:00
|
|
|
double dot = dxy1.dot(dxy2);
|
|
|
|
if (dot < 0) {
|
|
|
|
split = index - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-18 07:07:28 +00:00
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
if (split == 0) { // there's one point
|
2013-01-28 19:25:51 +00:00
|
|
|
if (addIntercept(q1, q2, tMin, tMax, i, subDivide)) {
|
2013-01-17 21:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
i.swap();
|
2013-01-28 19:25:51 +00:00
|
|
|
return isLinearInner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
// At this point, we have two ranges of t values -- treat each separately at the split
|
|
|
|
bool result;
|
2013-01-28 19:25:51 +00:00
|
|
|
if (addIntercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
|
2013-01-17 21:02:47 +00:00
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
i.swap();
|
2013-01-28 19:25:51 +00:00
|
|
|
result = isLinearInner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
2013-01-28 19:25:51 +00:00
|
|
|
if (addIntercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
|
2013-01-17 21:02:47 +00:00
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
i.swap();
|
2013-01-28 19:25:51 +00:00
|
|
|
result |= isLinearInner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double flatMeasure(const Quadratic& q) {
|
2013-02-26 15:56:05 +00:00
|
|
|
_Vector mid = q[1] - q[0];
|
|
|
|
_Vector dxy = q[2] - q[0];
|
2013-01-24 21:47:16 +00:00
|
|
|
double length = dxy.length(); // OPTIMIZE: get rid of sqrt
|
|
|
|
return fabs(mid.cross(dxy) / length);
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME ? should this measure both and then use the quad that is the flattest as the line?
|
|
|
|
static bool isLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
|
|
|
|
double measure = flatMeasure(q1);
|
|
|
|
// OPTIMIZE: (get rid of sqrt) use approximately_zero
|
|
|
|
if (!approximately_zero_sqrt(measure)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 19:25:51 +00:00
|
|
|
return isLinearInner(q1, 0, 1, q2, 0, 1, i, NULL);
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 21:47:16 +00:00
|
|
|
// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
|
2013-01-28 19:25:51 +00:00
|
|
|
static void relaxedIsLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
|
2013-01-17 21:02:47 +00:00
|
|
|
double m1 = flatMeasure(q1);
|
|
|
|
double m2 = flatMeasure(q2);
|
2013-01-24 21:47:16 +00:00
|
|
|
#if SK_DEBUG
|
|
|
|
double min = SkTMin(m1, m2);
|
|
|
|
if (min > 5) {
|
|
|
|
SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-19 13:22:39 +00:00
|
|
|
i.reset();
|
2013-01-28 19:25:51 +00:00
|
|
|
const Quadratic& rounder = m2 < m1 ? q1 : q2;
|
|
|
|
const Quadratic& flatter = m2 < m1 ? q2 : q1;
|
|
|
|
bool subDivide = false;
|
|
|
|
isLinearInner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
|
|
|
|
if (subDivide) {
|
|
|
|
QuadraticPair pair;
|
|
|
|
chop_at(flatter, pair, 0.5);
|
|
|
|
Intersections firstI, secondI;
|
|
|
|
relaxedIsLinear(pair.first(), rounder, firstI);
|
|
|
|
for (int index = 0; index < firstI.used(); ++index) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insert(firstI.fT[0][index] * 0.5, firstI.fT[1][index], firstI.fPt[index]);
|
2013-01-28 19:25:51 +00:00
|
|
|
}
|
|
|
|
relaxedIsLinear(pair.second(), rounder, secondI);
|
|
|
|
for (int index = 0; index < secondI.used(); ++index) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insert(0.5 + secondI.fT[0][index] * 0.5, secondI.fT[1][index], secondI.fPt[index]);
|
2013-01-28 19:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m2 < m1) {
|
|
|
|
i.swapPts();
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
|
|
|
|
const Quadratic* qs[2] = { &q1, &q2 };
|
|
|
|
// need t values for start and end of unsortable expanse on both curves
|
|
|
|
// try projecting lines parallel to the end points
|
|
|
|
i.fT[0][0] = 0;
|
|
|
|
i.fT[0][1] = 1;
|
|
|
|
int flip = -1; // undecided
|
|
|
|
for (int qIdx = 0; qIdx < 2; qIdx++) {
|
|
|
|
for (int t = 0; t < 2; t++) {
|
|
|
|
_Point dxdy;
|
2013-01-19 13:22:39 +00:00
|
|
|
dxdy_at_t(*qs[qIdx], t, dxdy);
|
2013-01-17 21:02:47 +00:00
|
|
|
_Line perp;
|
|
|
|
perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2];
|
|
|
|
perp[0].x += dxdy.y;
|
|
|
|
perp[0].y -= dxdy.x;
|
|
|
|
perp[1].x -= dxdy.y;
|
|
|
|
perp[1].y += dxdy.x;
|
|
|
|
Intersections hitData;
|
|
|
|
int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData);
|
2013-01-29 20:28:49 +00:00
|
|
|
SkASSERT(hits <= 1);
|
2013-01-17 21:02:47 +00:00
|
|
|
if (hits) {
|
|
|
|
if (flip < 0) {
|
|
|
|
_Point dxdy2;
|
2013-01-19 13:22:39 +00:00
|
|
|
dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2);
|
2013-01-17 21:02:47 +00:00
|
|
|
double dot = dxdy.dot(dxdy2);
|
|
|
|
flip = dot < 0;
|
|
|
|
i.fT[1][0] = flip;
|
|
|
|
i.fT[1][1] = !flip;
|
|
|
|
}
|
|
|
|
i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.fUnsortable = true; // failed, probably coincident or near-coincident
|
|
|
|
i.fUsed = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-20 12:51:37 +00:00
|
|
|
// each time through the loop, this computes values it had from the last loop
|
|
|
|
// if i == j == 1, the center values are still good
|
|
|
|
// otherwise, for i != 1 or j != 1, four of the values are still good
|
|
|
|
// and if i == 1 ^ j == 1, an additional value is good
|
|
|
|
static bool binarySearch(const Quadratic& quad1, const Quadratic& quad2, double& t1Seed,
|
|
|
|
double& t2Seed, _Point& pt) {
|
|
|
|
double tStep = ROUGH_EPSILON;
|
|
|
|
_Point t1[3], t2[3];
|
|
|
|
int calcMask = ~0;
|
|
|
|
do {
|
|
|
|
if (calcMask & (1 << 1)) t1[1] = xy_at_t(quad1, t1Seed);
|
|
|
|
if (calcMask & (1 << 4)) t2[1] = xy_at_t(quad2, t2Seed);
|
|
|
|
if (t1[1].approximatelyEqual(t2[1])) {
|
|
|
|
pt = t1[1];
|
|
|
|
#if ONE_OFF_DEBUG
|
|
|
|
SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__,
|
|
|
|
t1Seed, t2Seed, t1[1].x, t1[1].y, t1[2].x, t1[2].y);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (calcMask & (1 << 0)) t1[0] = xy_at_t(quad1, t1Seed - tStep);
|
|
|
|
if (calcMask & (1 << 2)) t1[2] = xy_at_t(quad1, t1Seed + tStep);
|
|
|
|
if (calcMask & (1 << 3)) t2[0] = xy_at_t(quad2, t2Seed - tStep);
|
|
|
|
if (calcMask & (1 << 5)) t2[2] = xy_at_t(quad2, t2Seed + tStep);
|
|
|
|
double dist[3][3];
|
|
|
|
// OPTIMIZE: using calcMask value permits skipping some distance calcuations
|
|
|
|
// if prior loop's results are moved to correct slot for reuse
|
|
|
|
dist[1][1] = t1[1].distanceSquared(t2[1]);
|
|
|
|
int best_i = 1, best_j = 1;
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
for (int j = 0; j < 3; ++j) {
|
|
|
|
if (i == 1 && j == 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dist[i][j] = t1[i].distanceSquared(t2[j]);
|
|
|
|
if (dist[best_i][best_j] > dist[i][j]) {
|
|
|
|
best_i = i;
|
|
|
|
best_j = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_i == 1 && best_j == 1) {
|
|
|
|
tStep /= 2;
|
|
|
|
if (tStep < FLT_EPSILON_HALF) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5);
|
2013-02-20 12:51:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (best_i == 0) {
|
|
|
|
t1Seed -= tStep;
|
|
|
|
t1[2] = t1[1];
|
|
|
|
t1[1] = t1[0];
|
|
|
|
calcMask = 1 << 0;
|
|
|
|
} else if (best_i == 2) {
|
|
|
|
t1Seed += tStep;
|
|
|
|
t1[0] = t1[1];
|
|
|
|
t1[1] = t1[2];
|
|
|
|
calcMask = 1 << 2;
|
|
|
|
} else {
|
|
|
|
calcMask = 0;
|
|
|
|
}
|
|
|
|
if (best_j == 0) {
|
|
|
|
t2Seed -= tStep;
|
|
|
|
t2[2] = t2[1];
|
|
|
|
t2[1] = t2[0];
|
|
|
|
calcMask |= 1 << 3;
|
|
|
|
} else if (best_j == 2) {
|
|
|
|
t2Seed += tStep;
|
|
|
|
t2[0] = t2[1];
|
|
|
|
t2[1] = t2[2];
|
|
|
|
calcMask |= 1 << 5;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
#if ONE_OFF_DEBUG
|
|
|
|
SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__,
|
|
|
|
t1Seed, t2Seed, t1[1].x, t1[1].y, t1[2].x, t1[2].y);
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:19:30 +00:00
|
|
|
bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
|
2012-10-09 14:11:58 +00:00
|
|
|
// if the quads share an end point, check to see if they overlap
|
|
|
|
|
|
|
|
if (onlyEndPtsInCommon(q1, q2, i)) {
|
|
|
|
return i.intersected();
|
|
|
|
}
|
2013-01-17 21:02:47 +00:00
|
|
|
if (onlyEndPtsInCommon(q2, q1, i)) {
|
|
|
|
i.swapPts();
|
|
|
|
return i.intersected();
|
|
|
|
}
|
|
|
|
// see if either quad is really a line
|
|
|
|
if (isLinear(q1, q2, i)) {
|
|
|
|
return i.intersected();
|
|
|
|
}
|
|
|
|
if (isLinear(q2, q1, i)) {
|
|
|
|
i.swapPts();
|
|
|
|
return i.intersected();
|
|
|
|
}
|
2012-09-14 14:19:30 +00:00
|
|
|
QuadImplicitForm i1(q1);
|
|
|
|
QuadImplicitForm i2(q2);
|
|
|
|
if (i1.implicit_match(i2)) {
|
|
|
|
// FIXME: compute T values
|
|
|
|
// compute the intersections of the ends to find the coincident span
|
|
|
|
bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
|
|
|
|
double t;
|
|
|
|
if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insertCoincident(t, 0, q2[0]);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insertCoincident(t, 1, q2[2]);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
|
|
|
|
if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insertCoincident(0, t, q1[0]);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insertCoincident(1, t, q1[2]);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
SkASSERT(i.coincidentUsed() <= 2);
|
|
|
|
return i.coincidentUsed() > 0;
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
int index;
|
2013-01-17 21:02:47 +00:00
|
|
|
bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
|
2013-02-14 15:29:11 +00:00
|
|
|
double roots1[4];
|
2013-02-20 12:51:37 +00:00
|
|
|
int rootCount = findRoots(i2, q1, roots1, useCubic, 0);
|
2012-09-14 14:19:30 +00:00
|
|
|
// OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
|
2013-02-14 15:29:11 +00:00
|
|
|
double roots1Copy[4];
|
|
|
|
int r1Count = addValidRoots(roots1, rootCount, roots1Copy);
|
|
|
|
_Point pts1[4];
|
|
|
|
for (index = 0; index < r1Count; ++index) {
|
|
|
|
xy_at_t(q1, roots1Copy[index], pts1[index].x, pts1[index].y);
|
|
|
|
}
|
|
|
|
double roots2[4];
|
2013-02-20 12:51:37 +00:00
|
|
|
int rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
|
2013-02-14 15:29:11 +00:00
|
|
|
double roots2Copy[4];
|
|
|
|
int r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
|
|
|
|
_Point pts2[4];
|
|
|
|
for (index = 0; index < r2Count; ++index) {
|
|
|
|
xy_at_t(q2, roots2Copy[index], pts2[index].x, pts2[index].y);
|
|
|
|
}
|
|
|
|
if (r1Count == r2Count && r1Count <= 1) {
|
|
|
|
if (r1Count == 1) {
|
|
|
|
if (pts1[0].approximatelyEqualHalf(pts2[0])) {
|
|
|
|
i.insert(roots1Copy[0], roots2Copy[0], pts1[0]);
|
2013-03-13 20:29:41 +00:00
|
|
|
} else if (pts1[0].moreRoughlyEqual(pts2[0])) {
|
2013-02-20 12:51:37 +00:00
|
|
|
// experiment: see if a different cubic solution provides the correct quartic answer
|
|
|
|
#if 0
|
|
|
|
for (int cu1 = 0; cu1 < 3; ++cu1) {
|
|
|
|
rootCount = findRoots(i2, q1, roots1, useCubic, cu1);
|
|
|
|
r1Count = addValidRoots(roots1, rootCount, roots1Copy);
|
|
|
|
if (r1Count == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int cu2 = 0; cu2 < 3; ++cu2) {
|
|
|
|
if (cu1 == 0 && cu2 == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rootCount2 = findRoots(i1, q2, roots2, useCubic, cu2);
|
|
|
|
r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
|
|
|
|
if (r2Count == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkASSERT(r1Count == 1 && r2Count == 1);
|
2013-02-21 07:15:03 +00:00
|
|
|
SkDebugf("*** [%d,%d] (%1.9g,%1.9g) %s (%1.9g,%1.9g)\n", cu1, cu2,
|
2013-02-20 12:51:37 +00:00
|
|
|
pts1[0].x, pts1[0].y, pts1[0].approximatelyEqualHalf(pts2[0])
|
|
|
|
? "==" : "!=", pts2[0].x, pts2[0].y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// experiment: try to find intersection by chasing t
|
|
|
|
rootCount = findRoots(i2, q1, roots1, useCubic, 0);
|
|
|
|
r1Count = addValidRoots(roots1, rootCount, roots1Copy);
|
|
|
|
rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
|
|
|
|
r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
|
|
|
|
if (binarySearch(q1, q2, roots1Copy[0], roots2Copy[0], pts1[0])) {
|
|
|
|
i.insert(roots1Copy[0], roots2Copy[0], pts1[0]);
|
|
|
|
}
|
2012-10-31 19:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return i.intersected();
|
|
|
|
}
|
|
|
|
int closest[4];
|
|
|
|
double dist[4];
|
2013-01-17 21:02:47 +00:00
|
|
|
bool foundSomething = false;
|
2013-02-14 15:29:11 +00:00
|
|
|
for (index = 0; index < r1Count; ++index) {
|
2012-10-31 19:00:20 +00:00
|
|
|
dist[index] = DBL_MAX;
|
|
|
|
closest[index] = -1;
|
2013-02-14 15:29:11 +00:00
|
|
|
for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) {
|
|
|
|
if (!pts2[ndex2].approximatelyEqualHalf(pts1[index])) {
|
2012-10-31 19:00:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
double dx = pts2[ndex2].x - pts1[index].x;
|
|
|
|
double dy = pts2[ndex2].y - pts1[index].y;
|
2012-10-31 19:00:20 +00:00
|
|
|
double distance = dx * dx + dy * dy;
|
|
|
|
if (dist[index] <= distance) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int outer = 0; outer < index; ++outer) {
|
|
|
|
if (closest[outer] != ndex2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dist[outer] < distance) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
closest[outer] = -1;
|
|
|
|
}
|
|
|
|
dist[index] = distance;
|
|
|
|
closest[index] = ndex2;
|
2013-01-17 21:02:47 +00:00
|
|
|
foundSomething = true;
|
2012-10-31 19:00:20 +00:00
|
|
|
next:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
if (r1Count && r2Count && !foundSomething) {
|
2013-01-28 19:25:51 +00:00
|
|
|
relaxedIsLinear(q1, q2, i);
|
2013-01-17 21:02:47 +00:00
|
|
|
return i.intersected();
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
2013-01-17 21:02:47 +00:00
|
|
|
int used = 0;
|
|
|
|
do {
|
|
|
|
double lowest = DBL_MAX;
|
|
|
|
int lowestIndex = -1;
|
2013-02-14 15:29:11 +00:00
|
|
|
for (index = 0; index < r1Count; ++index) {
|
2013-01-17 21:02:47 +00:00
|
|
|
if (closest[index] < 0) {
|
2012-09-14 14:19:30 +00:00
|
|
|
continue;
|
2013-01-17 21:02:47 +00:00
|
|
|
}
|
|
|
|
if (roots1Copy[index] < lowest) {
|
|
|
|
lowestIndex = index;
|
|
|
|
lowest = roots1Copy[index];
|
|
|
|
}
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
2013-01-17 21:02:47 +00:00
|
|
|
if (lowestIndex < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-14 15:29:11 +00:00
|
|
|
i.insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]],
|
|
|
|
pts1[lowestIndex]);
|
2013-01-17 21:02:47 +00:00
|
|
|
closest[lowestIndex] = -1;
|
2013-02-14 15:29:11 +00:00
|
|
|
} while (++used < r1Count);
|
2013-01-17 21:02:47 +00:00
|
|
|
i.fFlip = false;
|
2012-09-14 14:19:30 +00:00
|
|
|
return i.intersected();
|
|
|
|
}
|