NNGraph MSConnectivityScore::pick_graph(EdgeSet const &picked) const { EdgeSet::const_iterator p; std::map<unsigned int, int> idx_to_vtx; int n_vert = 0; for (p = picked.begin(); p != picked.end(); ++p) { std::map<unsigned int, int>::iterator q = idx_to_vtx.find(p->first); if (q == idx_to_vtx.end()) idx_to_vtx[p->first] = n_vert++; q = idx_to_vtx.find(p->second); if (q == idx_to_vtx.end()) idx_to_vtx[p->second] = n_vert++; } NNGraph ng(n_vert); boost::property_map<NNGraph, boost::vertex_name_t>::type vertex_id = boost::get(boost::vertex_name, ng); boost::property_map<NNGraph, boost::edge_weight_t>::type dist = boost::get(boost::edge_weight, ng); for (std::map<unsigned int, int>::iterator q = idx_to_vtx.begin(); q != idx_to_vtx.end(); ++q) { boost::put(vertex_id, q->second, q->first); } for (p = picked.begin(); p != picked.end(); ++p) { NNGraph::edge_descriptor e = boost::add_edge(idx_to_vtx[p->first], idx_to_vtx[p->second], ng).first; double d = restraint_.particle_matrix_.get_distance(p->first, p->second); boost::put(dist, e, d); } return ng; }
Triangulation::~Triangulation() { TriangleSet outTriangles; triangles(&outTriangles); for(auto it = outTriangles.begin(); it != outTriangles.end(); it++) delete *it; mTlist->clear(); EdgeSet outEdges; edges(&outEdges); for(auto it = outEdges.begin(); it != outEdges.end(); it++) delete *it; VertexSet outVertices; vertices(&outVertices); for(auto it = outEdges.begin(); it != outEdges.end(); it++) delete *it; mV2VMap->clear(); }
ParticlePairsTemp MSConnectivityRestraint::get_connected_pairs() const { IMP_CHECK_OBJECT(ps_.get()); tree_.finalize(); MSConnectivityScore mcs(tree_, ps_.get(), eps_, *const_cast<MSConnectivityRestraint *>(this)); EdgeSet edges = mcs.get_connected_pairs(); ParticlePairsTemp ret(edges.size()); unsigned index = 0; for (EdgeSet::iterator p = edges.begin(); p != edges.end(); ++p) { ret[index++] = ParticlePair(mcs.get_particle(p->first), mcs.get_particle(p->second)); } return ret; }
double MSConnectivityScore::score(DerivativeAccumulator *accum) const { EdgeSet edges = get_connected_pairs(); double sc = 0; for (EdgeSet::iterator p = edges.begin(); p != edges.end(); ++p) { if (accum) { sc += my_evaluate( ps_, restraint_.particle_matrix_.get_particle(p->first).get_particle(), restraint_.particle_matrix_.get_particle(p->second).get_particle(), accum); } else { sc += restraint_.particle_matrix_.get_distance(p->first, p->second); } } return sc; }
void MSConnectivityScore::add_edges_to_set(NNGraph &G, EdgeSet &edge_set) const { boost::property_map<NNGraph, boost::vertex_name_t>::type vertex_id = boost::get(boost::vertex_name, G); NNGraph ng(num_vertices(G)); Ints vertex_id_to_n(restraint_.particle_matrix_.size(), -1); for (unsigned int i = 0; i < num_vertices(ng); ++i) { unsigned int id = boost::get(vertex_id, i); vertex_id_to_n[id] = i; } for (EdgeSet::iterator p = edge_set.begin(); p != edge_set.end(); ++p) { unsigned int i_from = vertex_id_to_n[(*p).first]; unsigned int i_to = vertex_id_to_n[(*p).second]; add_edge(i_from, i_to, ng); } Ints components(num_vertices(ng)); int ncomp = boost::connected_components(ng, &components[0]); if (ncomp == 1) return; Vector<std::pair<unsigned int, unsigned int> > candidates; NNGraph::edge_iterator e, end; for (boost::tie(e, end) = edges(G); e != end; ++e) { unsigned int src = boost::get(vertex_id, source(*e, G)); unsigned int dst = boost::get(vertex_id, target(*e, G)); if (src > dst) std::swap(src, dst); std::pair<unsigned int, unsigned int> candidate = std::make_pair(src, dst); if (edge_set.find(candidate) == edge_set.end()) candidates.push_back(candidate); } std::sort(candidates.begin(), candidates.end(), EdgeScoreComparator(restraint_)); unsigned int idx = 0; while (ncomp > 1 && idx < candidates.size()) { unsigned int i_from = vertex_id_to_n[candidates[idx].first]; unsigned int i_to = vertex_id_to_n[candidates[idx].second]; if (components[i_from] != components[i_to]) { int old_comp = components[i_to]; for (unsigned int i = 0; i < components.size(); ++i) if (components[i] == old_comp) components[i] = components[i_from]; --ncomp; edge_set.insert(candidates[idx]); } ++idx; } BOOST_ASSERT(ncomp == 1); }
void Network::clearEdges() { GraphMap::iterator neit = _node_to_edges.begin(); EdgeSet edges; for(neit = _node_to_edges.begin(); neit != _node_to_edges.end(); neit++) { edges.insert( neit->second.begin(), neit->second.end() ); //Clear this set of edges: neit->second.clear(); } //Now actually remove the edges: EdgeSet::iterator eit; for( eit = edges.begin(); eit != edges.end(); eit++ ) { decrementRefCount(*eit); } }
void Delaunay::Triangulate(const VertexSet& vertices, TriangleSet& output) { if (vertices.size() < 3) { return; } cVertexIterator iterVertex = vertices.begin(); double xMin = iterVertex->GetX(); double yMin = iterVertex->GetY(); double xMax = xMin; double yMax = yMin; ++iterVertex; for (; iterVertex != vertices.end(); ++iterVertex) { xMax = iterVertex->GetX(); double y = iterVertex->GetY(); if (y < yMin) { yMin = y; } if (y > yMax) { yMax = y; } } double dx = xMax - xMin; double dy = yMax - yMin; double ddx = dx * 0.01; double ddy = dy * 0.01; xMin -= ddx; xMax += ddx; dx += 2 * ddx; yMin -= ddy; yMax += ddy; dy += 2 * ddy; Vertex vSuper[3]; vSuper[0] = Vertex(xMin - dy * sqrt3 / 3.0, yMin); vSuper[1] = Vertex(xMax + dy * sqrt3 / 3.0, yMin); vSuper[2] = Vertex((xMin + xMax) * 0.5, yMax + dx * sqrt3 * 0.5); TriangleSet workset; workset.insert(Triangle(vSuper)); for (iterVertex = vertices.begin(); iterVertex != vertices.end(); ++iterVertex) { TriangleIsCompleted pred1(iterVertex, output, vSuper); TriangleSet::iterator iter = workset.begin(); while (iter != workset.end()) { if (pred1(*iter)) { iter = workset.erase(iter); } else { ++iter; } } EdgeSet edges; VertexIsInCircumstanceCircle pred2(iterVertex, edges); iter = workset.begin(); while (iter != workset.end()) { if (pred2(*iter)) { iter = workset.erase(iter); } else { ++iter; } } for (EdgeIterator edgeIter = edges.begin(); edgeIter != edges.end(); ++edgeIter) { workset.insert(Triangle(edgeIter->m_pv0, edgeIter->m_pv1, &(*iterVertex))); } } TriangleIterator where = output.begin(); TriangleHasVertex pred(vSuper); for (auto t : workset) { if (!pred(t)) { output.insert(output.begin(), t); } } }
SEXP attribute_hidden do_provenance_graph(SEXP call, SEXP op, SEXP args, SEXP rho) { #ifndef PROVENANCE_TRACKING Rf_error(_("provenance tracking not implemented in this build")); return 0; #else int nargs = length(args); if (nargs != 1) Rf_error(_("%d arguments passed to 'provenance.graph' which requires 1"), nargs); SEXP arg1 = CAR(args); if (!arg1 || arg1->sexptype() != STRSXP) Rf_error(_("invalid 'names' argument")); Environment* env = static_cast<Environment*>(rho); Provenance::Set provs; StringVector* sv = static_cast<StringVector*>(arg1); for (size_t i = 0; i < sv->size(); i++) { const char* name = (*sv)[i]->c_str(); Symbol* sym = Symbol::obtain(name); Frame::Binding* bdg = env->findBinding(sym); if (!bdg) Rf_error(_("symbol '%s' not found"), name); else { Provenance* prov = const_cast<Provenance*>(bdg->provenance()); if (!prov) Rf_warning(_("'%s' does not have provenance information"), name); else provs.insert(prov); } } Provenance::Set* ancestors = Provenance::ancestors(provs); GCStackRoot<ListVector> ans(CXXR_NEW(ListVector(7))); std::map<const Provenance*, unsigned int> ancestor_index; std::vector<std::pair<unsigned int, const RObject*> > xenogenous_bdgs; // Assemble information on graph nodes: { size_t n = ancestors->size(); GCStackRoot<ListVector> symbols(CXXR_NEW(ListVector(n))); GCStackRoot<ListVector> commands(CXXR_NEW(ListVector(n))); GCStackRoot<RealVector> timestamps(CXXR_NEW(RealVector(n))); size_t i = 0; for (Provenance::Set::iterator it = ancestors->begin(); it != ancestors->end(); ++it) { const Provenance* p = *it; (*symbols)[i] = const_cast<Symbol*>(p->symbol()); (*commands)[i] = const_cast<RObject*>(p->command()); (*timestamps)[i] = p->timestamp(); ++i; ancestor_index[p] = i; if (p->isXenogenous()) xenogenous_bdgs.push_back(std::make_pair(i, p->value())); } (*ans)[0] = symbols; (*ans)[1] = commands; (*ans)[2] = timestamps; } // Record information on xenogenous bindings: { size_t xn = xenogenous_bdgs.size(); GCStackRoot<IntVector> xenogenous(CXXR_NEW(IntVector(xn))); GCStackRoot<ListVector> values(CXXR_NEW(ListVector(xn))); for (unsigned int i = 0; i < xn; ++i) { std::pair<unsigned int, const RObject*>& pr = xenogenous_bdgs[i]; (*xenogenous)[i] = pr.first; (*values)[i] = const_cast<RObject*>(pr.second); } (*ans)[3] = xenogenous; (*ans)[4] = values; } // Assemble information on graph edges: { typedef std::set<std::pair<unsigned int, unsigned int> > EdgeSet; EdgeSet edges; for (Provenance::Set::iterator it = ancestors->begin(); it != ancestors->end(); ++it) { const Provenance* child = *it; unsigned int child_idx = ancestor_index[child]; std::pair<CommandChronicle::ParentVector::const_iterator, CommandChronicle::ParentVector::const_iterator> pr = child->parents(); for (CommandChronicle::ParentVector::const_iterator it = pr.first; it != pr.second; ++it) { const Provenance* parent = *it; unsigned int parent_idx = ancestor_index[parent]; edges.insert(std::make_pair(parent_idx, child_idx)); } } size_t en = edges.size(); GCStackRoot<IntVector> parents(CXXR_NEW(IntVector(en))); GCStackRoot<IntVector> children(CXXR_NEW(IntVector(en))); unsigned int i = 0; for (EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) { const std::pair<unsigned int, unsigned int>& edge = *it; (*parents)[i] = edge.first; (*children)[i] = edge.second; ++i; } (*ans)[5] = parents; (*ans)[6] = children; } delete ancestors; return ans; #endif // PROVENANCE_TRACKING }
Network::NeighborIterator::NeighborIterator(Node* n, const EdgeSet& edges) { _beg = edges.begin(); _end = edges.end(); _neighbors_of = n; reset(); }