in src/pathops/SkPathWriter.cpp [207:435]
void SkPathWriter::assemble() {
if (!this->someAssemblyRequired()) {
return;
}
#if DEBUG_PATH_CONSTRUCTION
SkDebugf("%s\n", __FUNCTION__);
#endif
SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
int endCount = fEndPtTs.size(); // all starts and ends
SkASSERT(endCount > 0);
SkASSERT(endCount == fPartials.size() * 2);
#if DEBUG_ASSEMBLE
for (int index = 0; index < endCount; index += 2) {
const SkOpPtT* eStart = runs[index];
const SkOpPtT* eEnd = runs[index + 1];
SkASSERT(eStart != eEnd);
SkASSERT(!eStart->contains(eEnd));
SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
}
#endif
// lengthen any partial contour adjacent to a simple segment
for (int pIndex = 0; pIndex < endCount; pIndex++) {
SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
SkPath p;
SkPathWriter partWriter(p);
do {
if (!zero_or_one(opPtT->fT)) {
break;
}
SkOpSpanBase* opSpanBase = opPtT->span();
SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
int step = opPtT->fT ? 1 : -1;
const SkOpSegment* opSegment = opSpanBase->segment();
const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
if (!nextSegment) {
break;
}
SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
if (start->starter(opSpanEnd)->alreadyAdded()) {
break;
}
nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
opPtT = opSpanEnd->ptT();
SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
*runsPtr = opPtT;
} while (true);
partWriter.finishContour();
const TArray<SkPath>& partPartials = partWriter.partials();
if (partPartials.empty()) {
continue;
}
// if pIndex is even, reverse and prepend to fPartials; otherwise, append
SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]);
const SkPath& part = partPartials[0];
if (pIndex & 1) {
partial.addPath(part, SkPath::kExtend_AddPathMode);
} else {
SkPath reverse;
reverse.reverseAddPath(part);
reverse.addPath(partial, SkPath::kExtend_AddPathMode);
partial = reverse;
}
}
SkTDArray<int> sLink, eLink;
int linkCount = endCount / 2; // number of partial contours
sLink.append(linkCount);
eLink.append(linkCount);
int rIndex, iIndex;
for (rIndex = 0; rIndex < linkCount; ++rIndex) {
sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
}
const int entries = endCount * (endCount - 1) / 2; // folded triangle
STArray<8, double, true> distances(entries);
STArray<8, int, true> sortedDist(entries);
STArray<8, int, true> distLookup(entries);
int rRow = 0;
int dIndex = 0;
for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
const SkOpPtT* oPtT = runs[rIndex];
for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
const SkOpPtT* iPtT = runs[iIndex];
double dx = iPtT->fPt.fX - oPtT->fPt.fX;
double dy = iPtT->fPt.fY - oPtT->fPt.fY;
double dist = dx * dx + dy * dy;
distLookup.push_back(rRow + iIndex);
distances.push_back(dist); // oStart distance from iStart
sortedDist.push_back(dIndex++);
}
rRow += endCount;
}
SkASSERT(dIndex == entries);
SkTQSort<int>(sortedDist.begin(), sortedDist.end(), DistanceLessThan(distances.begin()));
int remaining = linkCount; // number of start/end pairs
for (rIndex = 0; rIndex < entries; ++rIndex) {
int pair = sortedDist[rIndex];
pair = distLookup[pair];
int row = pair / endCount;
int col = pair - row * endCount;
int ndxOne = row >> 1;
bool endOne = row & 1;
int* linkOne = endOne ? eLink.begin() : sLink.begin();
if (linkOne[ndxOne] != SK_MaxS32) {
continue;
}
int ndxTwo = col >> 1;
bool endTwo = col & 1;
int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
if (linkTwo[ndxTwo] != SK_MaxS32) {
continue;
}
SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
bool flip = endOne == endTwo;
linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
if (!--remaining) {
break;
}
}
SkASSERT(!remaining);
#if DEBUG_ASSEMBLE
for (rIndex = 0; rIndex < linkCount; ++rIndex) {
int s = sLink[rIndex];
int e = eLink[rIndex];
SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
}
#endif
rIndex = 0;
do {
bool forward = true;
bool first = true;
int sIndex = sLink[rIndex];
SkASSERT(sIndex != SK_MaxS32);
sLink[rIndex] = SK_MaxS32;
int eIndex;
if (sIndex < 0) {
eIndex = sLink[~sIndex];
sLink[~sIndex] = SK_MaxS32;
} else {
eIndex = eLink[sIndex];
eLink[sIndex] = SK_MaxS32;
}
SkASSERT(eIndex != SK_MaxS32);
#if DEBUG_ASSEMBLE
SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
eIndex < 0 ? ~eIndex : eIndex);
#endif
do {
const SkPath& contour = fPartials[rIndex];
if (!first) {
SkPoint prior, next;
if (!fPathPtr->getLastPt(&prior)) {
return;
}
if (forward) {
next = contour.getPoint(0);
} else {
SkAssertResult(contour.getLastPt(&next));
}
if (prior != next) {
/* TODO: if there is a gap between open path written so far and path to come,
connect by following segments from one to the other, rather than introducing
a diagonal to connect the two.
*/
}
}
if (forward) {
fPathPtr->addPath(contour,
first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
} else {
SkASSERT(!first);
fPathPtr->reversePathTo(contour);
}
if (first) {
first = false;
}
#if DEBUG_ASSEMBLE
SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
#endif
if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
fPathPtr->close();
break;
}
if (forward) {
eIndex = eLink[rIndex];
SkASSERT(eIndex != SK_MaxS32);
eLink[rIndex] = SK_MaxS32;
if (eIndex >= 0) {
SkASSERT(sLink[eIndex] == rIndex);
sLink[eIndex] = SK_MaxS32;
} else {
SkASSERT(eLink[~eIndex] == ~rIndex);
eLink[~eIndex] = SK_MaxS32;
}
} else {
eIndex = sLink[rIndex];
SkASSERT(eIndex != SK_MaxS32);
sLink[rIndex] = SK_MaxS32;
if (eIndex >= 0) {
SkASSERT(eLink[eIndex] == rIndex);
eLink[eIndex] = SK_MaxS32;
} else {
SkASSERT(sLink[~eIndex] == ~rIndex);
sLink[~eIndex] = SK_MaxS32;
}
}
rIndex = eIndex;
if (rIndex < 0) {
forward ^= 1;
rIndex = ~rIndex;
}
} while (true);
for (rIndex = 0; rIndex < linkCount; ++rIndex) {
if (sLink[rIndex] != SK_MaxS32) {
break;
}
}
} while (rIndex < linkCount);
#if DEBUG_ASSEMBLE
for (rIndex = 0; rIndex < linkCount; ++rIndex) {
SkASSERT(sLink[rIndex] == SK_MaxS32);
SkASSERT(eLink[rIndex] == SK_MaxS32);
}
#endif
}