graph_geometric.hh 5.21 KB
 Tiago Peixoto committed Oct 04, 2010 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 ``````// graph-tool -- a general graph modification and manipulation thingy // // Copyright (C) 2007-2010 Tiago de Paula Peixoto // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 3 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #ifndef GRAPH_GEOMETRIC_HH #define GRAPH_GEOMETRIC_HH #include #include #include #include "graph_util.hh" namespace graph_tool { using namespace std; using namespace boost; void get_box(const vector& p, double w, vector& box) { if (box.size() != p.size()) box.resize(p.size()); for (size_t i = 0; i < p.size(); ++i) box[i] = int(p[i] / w); } template double get_dist(const Point& p1, const Point& p2) { double r = 0; for (size_t i = 0; i < p1.size(); ++i) r += pow(p1[i] - p2[i], 2); return sqrt(r); } void periodic(int& x, int size) { if (x >= int(size)) x -= size; if (x < 0) x += size; } bool is_boundary(vector& point, double r, const vector > ranges) { bool boundary = 0; for (size_t j = 0; j < ranges.size(); ++j) { if (point[j] - r < ranges[j].first) { point[j] += ranges[j].second - ranges[j].first; boundary = true; } if (point[j] + r >= ranges[j].second) { point[j] -= ranges[j].second - ranges[j].first; boundary = true; } } return boundary; } template bool is_adjacent(typename graph_traits::vertex_descriptor v1, typename graph_traits::vertex_descriptor v2, Graph& g) { typename graph_traits::out_edge_iterator e, e_end; for (tie(e, e_end) = out_edges(v1, g); e != e_end; ++e) if (target(*e, g) == v2) return true; return false; } struct get_geometric { template void operator()(Graph& g, Pos upos, vector >& points, vector >& ranges, double r, bool periodic_boundary) const { int N = points.size(); typename Pos::checked_t pos = upos.get_checked(); vector box; tr1::unordered_multimap, typename graph_traits::vertex_descriptor, boost::hash > > boxes; for (int i = 0; i < N; ++i) { typename graph_traits::vertex_descriptor v = add_vertex(g); pos[v].resize(points[i].size()); copy(points[i].begin(), points[i].end(), pos[v].begin()); get_box(points[i], r, box); boxes.insert(make_pair(box, v)); if (periodic_boundary) { //insert the same vertex at the opposite box vector point = points[i]; if (is_boundary(point, r, ranges)) { get_box(point, r, box); boxes.insert(make_pair(box, v)); } } } #pragma omp parallel for default(shared) private(i, box) \ schedule(dynamic) for (int i = 0; i < N; ++i) { typename graph_traits::vertex_descriptor v = vertex(i, g); get_box(points[i], r, box); for (size_t k = 0; k < pow(3, box.size()); ++k) { for (size_t j = 0; j < box.size(); ++j) box[j] += (int(k / pow(3, j)) % 3) - 1; typeof(boxes.begin()) iter, end; for (tie(iter, end) = boxes.equal_range(box); iter != end; ++iter) { typename graph_traits::vertex_descriptor w = iter->second; double d = get_dist(pos[v], pos[w]); if (periodic_boundary) { if (is_adjacent(v, w, g)) continue; //get correct distance for boundary vector point = pos[w]; if (is_boundary(point, r, ranges)) d = min(d, get_dist(pos[v], point)); } if (w > v && d <= r) { #pragma omp critical add_edge(v, w, g); } } for (size_t j = 0; j < box.size(); ++j) box[j] -= (int(k / pow(3, j)) % 3) - 1; } } } }; } // namespace graph_tool #endif // GRAPH_GEOMETRIC_HH``````