/* * Copyright 2012 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file.
*/ #include"src/pathops/SkOpAngle.h"
/* Angles are sorted counterclockwise. The smallest angle has a positive x and the smallest
positive y. The largest angle has a positive x and a zero y. */
31 x > 0, y == 0 horizontal line (to the right) 0 x > 0, y == epsilon quad/cubic horizontal tangent eventually going +y 1 x > 0, y > 0, x > y nearer horizontal angle 2 x + e == y quad/cubic 45 going horiz 3 x > 0, y > 0, x == y 45 angle 4 x == y + e quad/cubic 45 going vert 5 x > 0, y > 0, x < y nearer vertical angle 6 x == epsilon, y > 0 quad/cubic vertical tangent eventually going +x 7 x == 0, y > 0 vertical line (to the top)
// given a line, see if the opposite curve's convex hull is all on one side // returns -1=not on one side 0=this CW of test 1=this CCW of test int SkOpAngle::lineOnOneSide(const SkOpAngle* test, bool useOriginal) {
SkASSERT(!fPart.isCurve());
SkASSERT(test->fPart.isCurve());
SkDPoint origin = fPart.fCurve[0];
SkDVector line = fPart.fCurve[1] - origin; int result = this->lineOnOneSide(origin, line, test, useOriginal); if (-2 == result) {
fUnorderable = true;
result = -1;
} return result;
}
// experiment works only with lines for now int SkOpAngle::linesOnOriginalSide(const SkOpAngle* test) {
SkASSERT(!fPart.isCurve());
SkASSERT(!test->fPart.isCurve());
SkDPoint origin = fOriginalCurvePart[0];
SkDVector line = fOriginalCurvePart[1] - origin; double dots[2]; double crosses[2]; const SkDCurve& testCurve = test->fOriginalCurvePart; for (int index = 0; index < 2; ++index) {
SkDVector testLine = testCurve[index] - origin; double xy1 = line.fX * testLine.fY; double xy2 = line.fY * testLine.fX;
dots[index] = line.fX * testLine.fX + line.fY * testLine.fY;
crosses[index] = AlmostBequalUlps(xy1, xy2) ? 0 : xy1 - xy2;
} if (crosses[0] * crosses[1] < 0) { return -1;
} if (crosses[0]) { return crosses[0] < 0;
} if (crosses[1]) { return crosses[1] < 0;
} if ((!dots[0] && dots[1] < 0) || (dots[0] < 0 && !dots[1])) { return 2; // 180 degrees apart
}
fUnorderable = true; return -1;
}
// To sort the angles, all curves are translated to have the same starting point. // If the curve's control point in its original position is on one side of a compared line, // and translated is on the opposite side, reverse the previously computed order. void SkOpAngle::alignmentSameSide(const SkOpAngle* test, int* order) const { if (*order < 0) { return;
} if (fPart.isCurve()) { // This should support all curve types, but only bug that requires this has lines // Turning on for curves causes existing tests to fail return;
} if (test->fPart.isCurve()) { return;
} const SkDPoint& xOrigin = test->fPart.fCurve.fLine[0]; const SkDPoint& oOrigin = test->fOriginalCurvePart.fLine[0]; if (xOrigin == oOrigin) { return;
} int iMax = SkPathOpsVerbToPoints(this->segment()->verb());
SkDVector xLine = test->fPart.fCurve.fLine[1] - xOrigin;
SkDVector oLine = test->fOriginalCurvePart.fLine[1] - oOrigin; for (int index = 1; index <= iMax; ++index) { const SkDPoint& testPt = fPart.fCurve[index]; double xCross = oLine.crossCheck(testPt - xOrigin); double oCross = xLine.crossCheck(testPt - oOrigin); if (oCross * xCross < 0) {
*order ^= 1; break;
}
}
}
bool SkOpAngle::checkCrossesZero() const { int start = std::min(fSectorStart, fSectorEnd); int end = std::max(fSectorStart, fSectorEnd); bool crossesZero = end - start > 16; return crossesZero;
}
bool SkOpAngle::checkParallel(SkOpAngle* rh) {
SkDVector scratch[2]; const SkDVector* sweep, * tweep; if (this->fPart.isOrdered()) {
sweep = this->fPart.fSweep;
} else {
scratch[0] = this->fPart.fCurve[1] - this->fPart.fCurve[0];
sweep = &scratch[0];
} if (rh->fPart.isOrdered()) {
tweep = rh->fPart.fSweep;
} else {
scratch[1] = rh->fPart.fCurve[1] - rh->fPart.fCurve[0];
tweep = &scratch[1];
} double s0xt0 = sweep->crossCheck(*tweep); if (tangentsDiverge(rh, s0xt0)) { return s0xt0 < 0;
} // compute the perpendicular to the endpoints and see where it intersects the opposite curve // if the intersections within the t range, do a cross check on those bool inside; if (!fEnd->contains(rh->fEnd)) { if (this->endToSide(rh, &inside)) { return inside;
} if (rh->endToSide(this, &inside)) { return !inside;
}
} if (this->midToSide(rh, &inside)) { return inside;
} if (rh->midToSide(this, &inside)) { return !inside;
} // compute the cross check from the mid T values (last resort)
SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0]; double m0xm1 = m0.crossCheck(m1); if (m0xm1 == 0) {
this->fUnorderable = true;
rh->fUnorderable = true; returntrue;
} return m0xm1 < 0;
}
// the original angle is too short to get meaningful sector information // lengthen it until it is long enough to be meaningful or leave it unset if lengthening it // would cause it to intersect one of the adjacent angles bool SkOpAngle::computeSector() { if (fComputedSector) { return !fUnorderable;
}
fComputedSector = true; bool stepUp = fStart->t() < fEnd->t();
SkOpSpanBase* checkEnd = fEnd; if (checkEnd->final() && stepUp) {
fUnorderable = true; returnfalse;
} do { // advance end const SkOpSegment* other = checkEnd->segment(); const SkOpSpanBase* oSpan = other->head(); do { if (oSpan->segment() != segment()) { continue;
} if (oSpan == checkEnd) { continue;
} if (!approximately_equal(oSpan->t(), checkEnd->t())) { continue;
} goto recomputeSector;
} while (!oSpan->final() && (oSpan = oSpan->upCast()->next()));
checkEnd = stepUp ? !checkEnd->final()
? checkEnd->upCast()->next() : nullptr
: checkEnd->prev();
} while (checkEnd);
recomputeSector:
SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head()
: checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail(); if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) {
fUnorderable = true; returnfalse;
} if (stepUp != (fStart->t() < computedEnd->t())) {
fUnorderable = true; returnfalse;
}
SkOpSpanBase* saveEnd = fEnd;
fComputedEnd = fEnd = computedEnd;
setSpans();
setSector();
fEnd = saveEnd; return !fUnorderable;
}
int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) { const SkDVector* sweep = this->fPart.fSweep; const SkDVector* tweep = rh->fPart.fSweep; double s0xs1 = sweep[0].crossCheck(sweep[1]); double s0xt0 = sweep[0].crossCheck(tweep[0]); double s1xt0 = sweep[1].crossCheck(tweep[0]); bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0; double s0xt1 = sweep[0].crossCheck(tweep[1]); double s1xt1 = sweep[1].crossCheck(tweep[1]);
tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0; double t0xt1 = tweep[0].crossCheck(tweep[1]); if (tBetweenS) { return -1;
} if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1 return -1;
} bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0; if (sBetweenT) { return -1;
} // if all of the sweeps are in the same half plane, then the order of any pair is enough if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) { return 0;
} if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) { return 1;
} // if the outside sweeps are greater than 180 degress: // first assume the inital tangents are the ordering // if the midpoint direction matches the inital order, that is enough
SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0]; double m0xm1 = m0.crossCheck(m1); if (s0xt0 > 0 && m0xm1 > 0) { return 0;
} if (s0xt0 < 0 && m0xm1 < 0) { return 1;
} if (tangentsDiverge(rh, s0xt0)) { return s0xt0 < 0;
} return m0xm1 < 0;
}
// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup double SkOpAngle::distEndRatio(double dist) const { double longest = 0; const SkOpSegment& segment = *this->segment(); int ptCount = SkPathOpsVerbToPoints(segment.verb()); const SkPoint* pts = segment.pts(); for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) { for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) { if (idx1 == idx2) { continue;
}
SkDVector v;
v.set(pts[idx2] - pts[idx1]); double lenSq = v.lengthSquared();
longest = std::max(longest, lenSq);
}
} return sqrt(longest) / dist;
}
bool SkOpAngle::endsIntersect(SkOpAngle* rh) {
SkPath::Verb lVerb = this->segment()->verb();
SkPath::Verb rVerb = rh->segment()->verb(); int lPts = SkPathOpsVerbToPoints(lVerb); int rPts = SkPathOpsVerbToPoints(rVerb);
SkDLine rays[] = {{{this->fPart.fCurve[0], rh->fPart.fCurve[rPts]}},
{{this->fPart.fCurve[0], this->fPart.fCurve[lPts]}}}; if (this->fEnd->contains(rh->fEnd)) { return checkParallel(rh);
} double smallTs[2] = {-1, -1}; bool limited[2] = {false, false}; for (int index = 0; index < 2; ++index) {
SkPath::Verb cVerb = index ? rVerb : lVerb; // if the curve is a line, then the line and the ray intersect only at their crossing if (cVerb == SkPath::kLine_Verb) { continue;
} const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
SkIntersections i;
(*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i); double tStart = index ? rh->fStart->t() : this->fStart->t(); double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t(); bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t()); double t = testAscends ? 0 : 1; for (int idx2 = 0; idx2 < i.used(); ++idx2) { double testT = i[0][idx2]; if (!approximately_between_orderable(tStart, testT, tEnd)) { continue;
} if (approximately_equal_orderable(tStart, testT)) { continue;
}
smallTs[index] = t = testAscends ? std::max(t, testT) : std::min(t, testT);
limited[index] = approximately_equal_orderable(t, tEnd);
}
} bool sRayLonger = false;
SkDVector sCept = {0, 0}; double sCeptT = -1; int sIndex = -1; bool useIntersect = false; for (int index = 0; index < 2; ++index) { if (smallTs[index] < 0) { continue;
} const SkOpSegment& segment = index ? *rh->segment() : *this->segment(); const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
SkDVector cept = dPt - rays[index][0]; // If this point is on the curve, it should have been detected earlier by ordinary // curve intersection. This may be hard to determine in general, but for lines, // the point could be close to or equal to its end, but shouldn't be near the start. if ((index ? lPts : rPts) == 1) {
SkDVector total = rays[index][1] - rays[index][0]; if (cept.lengthSquared() * 2 < total.lengthSquared()) { continue;
}
}
SkDVector end = rays[index][1] - rays[index][0]; if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) { continue;
} double rayDist = cept.length(); double endDist = end.length(); bool rayLonger = rayDist > endDist; if (limited[0] && limited[1] && rayLonger) {
useIntersect = true;
sRayLonger = rayLonger;
sCept = cept;
sCeptT = smallTs[index];
sIndex = index; break;
} double delta = fabs(rayDist - endDist); double minX, minY, maxX, maxY;
minX = minY = SK_ScalarInfinity;
maxX = maxY = -SK_ScalarInfinity; const SkDCurve& curve = index ? rh->fPart.fCurve : this->fPart.fCurve; int ptCount = index ? rPts : lPts; for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
minX = std::min(minX, curve[idx2].fX);
minY = std::min(minY, curve[idx2].fY);
maxX = std::max(maxX, curve[idx2].fX);
maxY = std::max(maxY, curve[idx2].fY);
} double maxWidth = std::max(maxX - minX, maxY - minY);
delta = sk_ieee_double_divide(delta, maxWidth); // FIXME: move these magic numbers // This fixes skbug.com/8380 // Larger changes (like changing the constant in the next block) cause other // tests to fail as documented in the bug. // This could probably become a more general test: e.g., if translating the // curve causes the cross product of any control point or end point to change // sign with regard to the opposite curve's hull, treat the curves as parallel.
// Moreso, this points to the general fragility of this approach of assigning // winding by sorting the angles of curves sharing a common point, as mentioned // in the bug. if (delta < 4e-3 && delta > 1e-3 && !useIntersect && fPart.isCurve()
&& rh->fPart.isCurve() && fOriginalCurvePart[0] != fPart.fCurve.fLine[0]) { // see if original curve is on one side of hull; translated is on the other const SkDPoint& origin = rh->fOriginalCurvePart[0]; int count = SkPathOpsVerbToPoints(rh->segment()->verb()); const SkDVector line = rh->fOriginalCurvePart[count] - origin; int originalSide = rh->lineOnOneSide(origin, line, this, true); if (originalSide >= 0) { int translatedSide = rh->lineOnOneSide(origin, line, this, false); if (originalSide != translatedSide) { continue;
}
}
} if (delta > 1e-3 && (useIntersect ^= true)) {
sRayLonger = rayLonger;
sCept = cept;
sCeptT = smallTs[index];
sIndex = index;
}
} if (useIntersect) { const SkDCurve& curve = sIndex ? rh->fPart.fCurve : this->fPart.fCurve; const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment(); double tStart = sIndex ? rh->fStart->t() : fStart->t();
SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0]; double septDir = mid.crossCheck(sCept); if (!septDir) { return checkParallel(rh);
} return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
} else { return checkParallel(rh);
}
}
/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0 0 x x x 1 x x x 2 x x x 3 x x x 4 x x x 5 x x x 6 x x x 7 x x x 8 x x x 9 x x x 10 x x x 11 x x x 12 x x x 13 x x x 14 x x x 15 x x x
*/ int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const { double absX = fabs(x); double absY = fabs(y); double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0; // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim, // one could coin the term sedecimant for a space divided into 16 sections. // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts staticconstint sedecimant[3][3][3] = { // y<0 y==0 y>0 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
{{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
{{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
{{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
}; int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1; // SkASSERT(SkPath::kLine_Verb == verb || sector >= 0); return sector;
}
// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side // OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side bool SkOpAngle::insert(SkOpAngle* angle) { if (angle->fNext) { if (loopCount() >= angle->loopCount()) { if (!merge(angle)) { returntrue;
}
} elseif (fNext) { if (!angle->merge(this)) { returntrue;
}
} else {
angle->insert(this);
} returntrue;
} bool singleton = nullptr == fNext; if (singleton) {
fNext = this;
}
SkOpAngle* next = fNext; if (next->fNext == this) { if (singleton || angle->after(this)) {
this->fNext = angle;
angle->fNext = next;
} else {
next->fNext = angle;
angle->fNext = this;
}
debugValidateNext(); returntrue;
}
SkOpAngle* last = this; bool flipAmbiguity = false; do {
SkASSERT(last->fNext == next); if (angle->after(last) ^ (angle->tangentsAmbiguous() & flipAmbiguity)) {
last->fNext = angle;
angle->fNext = next;
debugValidateNext(); break;
}
last = next; if (last == this) {
FAIL_IF(flipAmbiguity); // We're in a loop. If a sort was ambiguous, flip it to end the loop.
flipAmbiguity = true;
}
next = next->fNext;
} while (true); returntrue;
}
SkOpSpanBase* SkOpAngle::lastMarked() const { if (fLastMarked) { if (fLastMarked->chased()) { return nullptr;
}
fLastMarked->setChased(true);
} return fLastMarked;
}
int SkOpAngle::loopCount() const { int count = 0; const SkOpAngle* first = this; const SkOpAngle* next = this; do {
next = next->fNext;
++count;
} while (next && next != first); return count;
}
bool SkOpAngle::merge(SkOpAngle* angle) {
SkASSERT(fNext);
SkASSERT(angle->fNext);
SkOpAngle* working = angle; do { if (this == working) { returnfalse;
}
working = working->fNext;
} while (working != angle); do {
SkOpAngle* next = working->fNext;
working->fNext = nullptr;
insert(working);
working = next;
} while (working != angle); // it's likely that a pair of the angles are unorderable
debugValidateNext(); returntrue;
}
// OPTIMIZE: if this shows up in a profile, add a previous pointer // as is, this should be rarely called
SkOpAngle* SkOpAngle::previous() const {
SkOpAngle* last = fNext; do {
SkOpAngle* next = last->fNext; if (next == this) { return last;
}
last = next;
} while (true);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.