void create_crag() { // useful for later: // //boost::filesystem::path dataDir = dir_of(__FILE__); //boost::filesystem::path graphfile = dataDir/"star.dat"; Crag crag; // add 100 nodes for (int i = 0; i < 100; i++) crag.addNode(); // create chains of 10 nodes for (int i = 0; i < 100; i += 10) { for (int j = i+1; j < i + 10; j++) { Crag::Node u = crag.nodeFromId(j-1); Crag::Node v = crag.nodeFromId(j); crag.addSubsetArc(u, v); } } // check levels of nodes for (int i = 0; i < 100; i++) { Crag::Node n = crag.nodeFromId(i); BOOST_CHECK_EQUAL(crag.getLevel(n), i%10); if (i%10 == 0) BOOST_CHECK(crag.isLeafNode(n)); else BOOST_CHECK(!crag.isLeafNode(n)); if (i%10 == 9) BOOST_CHECK(crag.isRootNode(n)); else BOOST_CHECK(!crag.isRootNode(n)); } }
void PlanarAdjacencyAnnotator::annotate(Crag& crag) { if (optionCragType.as<std::string>() == "empty") return; UTIL_TIME_METHOD; util::box<float, 3> cragBB = crag.getBoundingBox(); util::point<float, 3> resolution; for (Crag::NodeIt n(crag); n != lemon::INVALID; ++n) { if (!crag.isLeafNode(n)) continue; resolution = crag.getVolume(n).getResolution(); break; } // no nodes? if (resolution.isZero()) return; // create a vigra multi-array large enough to hold all volumes vigra::MultiArray<3, int> ids( vigra::Shape3( cragBB.width() /resolution.x(), cragBB.height()/resolution.y(), cragBB.depth() /resolution.z()), std::numeric_limits<int>::max()); for (Crag::NodeIt n(crag); n != lemon::INVALID; ++n) { if (!crag.isLeafNode(n)) continue; const util::point<float, 3>& volumeOffset = crag.getVolume(n).getOffset(); const util::box<unsigned int, 3>& volumeDiscreteBB = crag.getVolume(n).getDiscreteBoundingBox(); util::point<unsigned int, 3> begin = (volumeOffset - cragBB.min())/resolution; util::point<unsigned int, 3> end = begin + util::point<unsigned int, 3>( volumeDiscreteBB.width(), volumeDiscreteBB.height(), volumeDiscreteBB.depth()); vigra::combineTwoMultiArrays( crag.getVolume(n).data(), ids.subarray( vigra::Shape3( begin.x(), begin.y(), begin.z()), vigra::Shape3( end.x(), end.y(), end.z())), ids.subarray( vigra::Shape3( begin.x(), begin.y(), begin.z()), vigra::Shape3( end.x(), end.y(), end.z())), vigra::functor::ifThenElse( vigra::functor::Arg1() == vigra::functor::Param(1), vigra::functor::Param(crag.id(n)), vigra::functor::Arg2() )); } //vigra::exportImage( //ids.bind<2>(0), //vigra::ImageExportInfo("debug/ids.tif")); typedef vigra::GridGraph<3> GridGraphType; typedef vigra::AdjacencyListGraph RagType; GridGraphType grid( ids.shape(), _neighborhood == Direct ? vigra::DirectNeighborhood : vigra::IndirectNeighborhood); RagType rag; RagType::EdgeMap<std::vector<GridGraphType::Edge>> affiliatedEdges; vigra::makeRegionAdjacencyGraph( grid, ids, rag, affiliatedEdges, std::numeric_limits<int>::max()); unsigned int numAdded = 0; crag.setGridGraph(grid); for (RagType::EdgeIt e(rag); e != lemon::INVALID; ++e) { int u = rag.id(rag.u(*e)); int v = rag.id(rag.v(*e)); Crag::Edge newEdge = crag.addAdjacencyEdge( crag.nodeFromId(u), crag.nodeFromId(v)); crag.setAffiliatedEdges( newEdge, affiliatedEdges[*e]); numAdded++; LOG_ALL(planaradjacencyannotatorlog) << "adding leaf node adjacency between " << u << " and " << v << std::endl; } LOG_USER(planaradjacencyannotatorlog) << "added " << numAdded << " leaf node adjacency edges" << std::endl; if (optionCragType.as<std::string>() == "full") propagateLeafAdjacencies(crag); }
void crag_iterators() { Crag crag; int numNodes = 10; for (int i = 0; i < numNodes; i++) crag.addNode(); int numEdges = 0; for (int i = 0; i < numNodes; i++) for (int j = 0; j < numNodes; j++) if (rand() > RAND_MAX/2) { crag.addAdjacencyEdge( crag.nodeFromId(i), crag.nodeFromId(j)); numEdges++; } int numArcs = 0; for (int i = 0; i < numNodes; i += 5) { for (int j = i; j < i + 4; j++) { crag.addSubsetArc( crag.nodeFromId(j), crag.nodeFromId(j+1)); numArcs++; } } BOOST_CHECK_EQUAL(crag.nodes().size(), numNodes); BOOST_CHECK_EQUAL(crag.edges().size(), numEdges); BOOST_CHECK_EQUAL(crag.arcs().size(), numArcs); numNodes = 0; { //UTIL_TIME_SCOPE("crag node old-school iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::CragNodeIterator i = crag.nodes().begin(); i != crag.nodes().end(); i++) numNodes++; } { //UTIL_TIME_SCOPE("crag node iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::CragNode n : crag.nodes()) { dontWarnMeAboutUnusedVar(n); numNodes++; } } { //UTIL_TIME_SCOPE("lemon node iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::NodeIt n(crag); n != lemon::INVALID; ++n) numNodes -= 2; } BOOST_CHECK_EQUAL(numNodes, 0); numEdges = 0; for (Crag::CragEdge e : crag.edges()) { dontWarnMeAboutUnusedVar(e); numEdges++; } for (Crag::EdgeIt e(crag); e != lemon::INVALID; ++e) numEdges--; BOOST_CHECK_EQUAL(numEdges, 0); numArcs = 0; for (Crag::CragArc a : crag.arcs()) { dontWarnMeAboutUnusedVar(a); numArcs++; } for (Crag::SubsetArcIt a(crag); a != lemon::INVALID; ++a) numArcs--; BOOST_CHECK_EQUAL(numArcs, 0); for (Crag::CragNode n : crag.nodes()) { int numAdjEdges = 0; for (Crag::IncEdgeIt e(crag, n); e != lemon::INVALID; ++e) numAdjEdges++; BOOST_CHECK_EQUAL(crag.adjEdges(n).size(), numAdjEdges); for (Crag::CragEdge e : crag.adjEdges(n)) { dontWarnMeAboutUnusedVar(e); numAdjEdges--; } BOOST_CHECK_EQUAL(numAdjEdges, 0); int numInArcs = 0; for (Crag::SubsetInArcIt a(crag, crag.toSubset(n)); a != lemon::INVALID; ++a) numInArcs++; BOOST_CHECK_EQUAL(numInArcs, crag.inArcs(n).size()); for (Crag::CragArc a : crag.inArcs(n)) { dontWarnMeAboutUnusedVar(a); numInArcs--; } BOOST_CHECK_EQUAL(numInArcs, 0); int numOutArcs = 0; for (Crag::SubsetOutArcIt a(crag, crag.toSubset(n)); a != lemon::INVALID; ++a) numOutArcs++; BOOST_CHECK_EQUAL(numOutArcs, crag.outArcs(n).size()); for (Crag::CragArc a : crag.outArcs(n)) { dontWarnMeAboutUnusedVar(a); numOutArcs--; } BOOST_CHECK_EQUAL(numOutArcs, 0); Crag::CragEdgeIterator cei = crag.edges().begin(); Crag::EdgeIt ei(crag); while (ei != lemon::INVALID) { BOOST_CHECK((Crag::RagType::Node)((*cei).u()) == crag.getAdjacencyGraph().u(ei)); BOOST_CHECK((Crag::RagType::Node)((*cei).v()) == crag.getAdjacencyGraph().v(ei)); ++cei; ++ei; } Crag::CragArcIterator cai = crag.arcs().begin(); Crag::SubsetArcIt ai(crag); while (ai != lemon::INVALID) { BOOST_CHECK(crag.toSubset((*cai).source()) == crag.getSubsetGraph().source(ai)); BOOST_CHECK(crag.toSubset((*cai).target()) == crag.getSubsetGraph().target(ai)); ++cai; ++ai; } } }