Skip to content

Commit

Permalink
Performance fix for footprint dilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdswinbank committed May 13, 2015
1 parent ce19365 commit 63bafa5
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/detection/Footprint.cc
Expand Up @@ -1318,17 +1318,45 @@ PTR(Footprint) growFootprintImpl(
// Create an empty footprint covering foot's region.
PTR(Footprint) grown(new Footprint(0, foot.getRegion()));

// Iterate over foot & structuring element adding spans to the empty
// footprint.
for (auto spanIter = foot.getSpans().begin(); spanIter != foot.getSpans().end(); spanIter++) {
for (auto it = element.begin(); it != element.end(); it++) {
// We use a map of (y coordinate) to set of (xmin, xmax) pairs to describe
// the spans being constructed. In this way, we ensure that the spans are
// always sorted by increasing y, xmin.
std::map<int, std::set<std::pair<int, int>>> spans;

// Iterate over foot & structuring element building up a collection of
// spans which will be added to the footprint.
for (auto spanIter = foot.getSpans().begin(); spanIter != foot.getSpans().end(); ++spanIter) {
for (auto it = element.begin(); it != element.end(); ++it) {
int xmin = (*spanIter)->getX0() + it->getX0();
int xmax = (*spanIter)->getX1() + it->getX1();
grown->addSpan((*spanIter)->getY() + it->getY(), xmin, xmax);
int yval = (*spanIter)->getY() + it->getY();
spans[yval].insert(std::make_pair(xmin, xmax));

// Merge overlapping spans at this y coordinate, thereby ensuring
// that the proto-footprint remains normalized.
std::set<std::pair<int, int>> newSpans;
for (auto span = spans[yval].cbegin(); span != spans[yval].cend(); ++span) {
int start = span->first, end = span->second;
// NB check for end+1 because end value is inclusive. That is,
// if one span terminates at x=N and another begins at x=N+1,
// those spans are contiguous.
while (span != spans[yval].cend() && span->first <= (end+1)) {
end = std::max(end, span++->second);
}
newSpans.insert(std::make_pair(start, end));
--span; // "rewind" to the last span included
}
std::swap(spans[yval], newSpans);
}
}

grown->normalize();
// Now append the spans to the output footprint.
for (auto y = spans.cbegin(); y != spans.cend(); ++y) {
int yval = y->first;
for (auto x = y->second.cbegin(); x != y->second.cend(); ++x) {
grown->addSpanInSeries(yval, x->first, x->second);
}
}

// Copy over peaks from the original footprint
grown->getPeaks() = PeakCatalog(foot.getPeaks().getTable(), foot.getPeaks().begin(),
Expand Down

0 comments on commit 63bafa5

Please sign in to comment.