Urvang Joshi | a22aca5 | 2020-03-04 12:12:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2006 Pedro Felzenszwalb |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #ifndef SEGMENT_GRAPH |
| 20 | #define SEGMENT_GRAPH |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cmath> |
| 24 | #include "third_party/segment/disjoint-set.h" |
| 25 | |
| 26 | // threshold function |
| 27 | #define THRESHOLD(size, c) (c / size) |
| 28 | |
| 29 | typedef struct { |
| 30 | float w; |
| 31 | int a, b; |
| 32 | } edge; |
| 33 | |
| 34 | bool operator<(const edge &a, const edge &b) { return a.w < b.w; } |
| 35 | |
| 36 | /* |
| 37 | * Segment a graph |
| 38 | * |
| 39 | * Returns a disjoint-set forest representing the segmentation. |
| 40 | * |
| 41 | * num_vertices: number of vertices in graph. |
| 42 | * num_edges: number of edges in graph |
| 43 | * edges: array of edges. |
| 44 | * c: constant for treshold function. |
| 45 | */ |
| 46 | universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c) { |
| 47 | // sort edges by weight |
| 48 | std::sort(edges, edges + num_edges); |
| 49 | |
| 50 | // make a disjoint-set forest |
| 51 | universe *u = new universe(num_vertices); |
| 52 | |
| 53 | // init thresholds |
| 54 | float *threshold = new float[num_vertices]; |
| 55 | for (int i = 0; i < num_vertices; i++) threshold[i] = THRESHOLD(1, c); |
| 56 | |
| 57 | // for each edge, in non-decreasing weight order... |
| 58 | for (int i = 0; i < num_edges; i++) { |
| 59 | edge *pedge = &edges[i]; |
| 60 | |
| 61 | // components conected by this edge |
| 62 | int a = u->find(pedge->a); |
| 63 | int b = u->find(pedge->b); |
| 64 | if (a != b) { |
| 65 | if ((pedge->w <= threshold[a]) && (pedge->w <= threshold[b])) { |
| 66 | u->join(a, b); |
| 67 | a = u->find(a); |
| 68 | threshold[a] = pedge->w + THRESHOLD(u->size(a), c); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // free up |
Urvang Joshi | d89b340 | 2020-05-18 15:14:32 -0700 | [diff] [blame] | 74 | delete[] threshold; |
Urvang Joshi | a22aca5 | 2020-03-04 12:12:56 -0800 | [diff] [blame] | 75 | return u; |
| 76 | } |
| 77 | |
| 78 | #endif |