AMI_STREAM<elevation_type> * classifyNodata(AMI_STREAM<elevation_type> *elstr) { Rtimer rt; rt_start(rt); if (stats) stats->comment("finding nodata", opt->verbose); detectEdgeNodata md(nrows, ncols, nodataType::ELEVATION_NODATA); md.generateNodata(*elstr); if (stats) *stats << "nodata stream length = " << md.getNodata()->stream_len() << endl; { char * foo; md.getNodata()->name(&foo); if (stats) *stats << "nodata stream name: " << foo << endl; } rt_stop(rt); if (stats) stats->recordTime("classifyNodata::generate nodata", rt); rt_start(rt); if (stats) stats->comment("relabeling nodata", opt->verbose); md.relabelNodata(); /* re-assign labels (combine connected plateaus) */ rt_stop(rt); if (stats) stats->recordTime("classifyNodata::relabeling", rt); rt_start(rt); if (stats) stats->comment("merging relabeled grid", opt->verbose); AMI_STREAM<elevation_type> *mergeStr; mergeStr = md.merge(); rt_stop(rt); if (stats) stats->recordTime("classifyNodata::merge", rt); mergeStr->seek(0); return mergeStr; }
void ccforest<T>::findAllRoots(int depth) { if(foundAllRoots) return; foundAllRoots = 1; Rtimer rt; rt_start(rt); if(depth > 5) { cerr << "WARNING: excessive recursion in ccforest (ignored)" << endl; } int explicitRootCount = 0; assert(!superTree); superTree = new ccforest<T>(); if (stats) DEBUG_CCFOREST *stats << "sort edgeStream (by cclabel)): "; keyCmpKeyvalueType<T> fo; sort(&edgeStream, fo); /* XXX -changed this to use a cmp obj */ /* time forward processing */ EMPQueueAdaptive<cckeyvalue,T> *pq = new EMPQueueAdaptive<cckeyvalue,T>(); /* parent queue */ size_t streamLength = edgeStream->stream_len(); T prevSrc = T(-1); T parent = T(-1); ccedge prevEdge; for(unsigned int i=0; i<streamLength; i++) { ccedge *e; AMI_err ae = edgeStream->read_item(&e); assert(ae == AMI_ERROR_NO_ERROR); #if(0) if (stats) { DEBUG_CCFOREST *stats << "------------------------------" << endl; DEBUG_CCFOREST *stats << "processing edge " << *e << endl; } DEBUG_CCFOREST pq->print(); #endif if(*e == prevEdge) { if (stats) DEBUG_CCFOREST *stats << "\tduplicate " << *e << " removed\n"; continue; /* already have this done */ } prevEdge = *e; if (stats) DEBUG_CCFOREST *stats << "processing edge " << *e << endl; /* find root (assign parent) */ if(e->src() != prevSrc) { prevSrc = e->src(); cckeyvalue kv; /* check if we have a key we don't use. */ while(pq->min(kv) && (kv.getPriority() < e->src())) { pq->extract_min(kv); assert(kv.src() >= kv.dst()); removeDuplicates(kv.src(), kv.dst(), *pq); ae = rootStream->write_item(kv); /* save root */ assert(ae == AMI_ERROR_NO_ERROR); } /* try to find our root */ if(pq->min(kv) && ((e->src() == kv.getPriority()))) { pq->extract_min(kv); parent = kv.getValue(); removeDuplicates(e->src(), parent, *pq); } else { parent = e->src(); /* we are root */ explicitRootCount++; /* technically, we could skip this part. the lookup function automatically assumes that values without parents are roots */ } /* save result */ cckeyvalue kroot(e->src(), parent); assert(kroot.src() >= kroot.dst()); ae = rootStream->write_item(kroot); assert(ae == AMI_ERROR_NO_ERROR); } #ifndef NDEBUG cckeyvalue kv2; assert(pq->is_empty() || (pq->min(kv2) && kv2.getPriority() > e->src())); #endif /* insert */ cckeyvalue kv(e->dst(), parent); assert(kv.src() >= kv.dst()); pq->insert(kv); /* cout << "identified: " << kroot << endl; */ } /* drain the priority queue */ if (stats) DEBUG_CCFOREST *stats << "draining priority queue" << endl; while (!pq->is_empty()) { cckeyvalue kv; pq->extract_min(kv); assert(kv.src() >= kv.dst()); if (stats) DEBUG_CCFOREST *stats << "processing edge " << kv << endl; removeDuplicates(kv.src(), kv.dst(), *pq); AMI_err ae = rootStream->write_item(kv); assert(ae == AMI_ERROR_NO_ERROR); } delete pq; /* note that rootStream is naturally ordered by src */ if(superTree->size()) { if (stats) { DEBUG_CCFOREST *stats << "resolving cycles..." << endl; /* printStream(rootStream); */ DEBUG_CCFOREST *stats << "sort rootStream: "; } AMI_STREAM<cckeyvalue> *sortedRootStream; dstCmpKeyvalueType<T> dstfo; sortedRootStream = sort(rootStream, dstfo); /* XXX replaced this to use a cmp object -- laura AMI_STREAM<cckeyvalue>*sortedRootStream=new AMI_STREAM<cckeyvalue>(); AMI_err ae = AMI_sort(rootStream, sortedRootStream, valueCmp); assert(ae == AMI_ERROR_NO_ERROR); */ delete rootStream; cckeyvalue *kv; T parent; AMI_err ae; AMI_STREAM<cckeyvalue>* relabeledRootStream = new AMI_STREAM<cckeyvalue>(); ae = sortedRootStream->seek(0); superTree->findAllRoots(depth+1); while((ae = sortedRootStream->read_item(&kv)) == AMI_ERROR_NO_ERROR) { parent = superTree->findNextRoot(kv->dst()); ae = relabeledRootStream->write_item(cckeyvalue(kv->src(), parent)); assert(ae == AMI_ERROR_NO_ERROR); } delete sortedRootStream; if (stats) DEBUG_CCFOREST *stats << "sort relabeledRootStream: "; rootStream = sort(relabeledRootStream, fo); /* laura: changed this rootStream = new AMI_STREAM<cckeyvalue>(); ae = AMI_sort(relabeledRootStream, rootStream); assert(ae == AMI_ERROR_NO_ERROR); */ delete relabeledRootStream; if (stats) DEBUG_CCFOREST *stats << "resolving cycles... done." << endl; } rootStream->seek(0); if (stats){ DEBUG_CCFOREST *stats << "Rootstream length=" << rootStream->stream_len() << endl; DEBUG_CCFOREST printStream(*stats, rootStream); DEBUG_CCFOREST *stats << "Explicit root count=" << explicitRootCount << endl; } rt_stop(rt); if (stats) stats->recordTime("ccforest::findAllRoots", (long int)rt_seconds(rt)); }