source: proiecte/pmake3d/segment/segment-graph.h @ 77

Last change on this file since 77 was 77, checked in by (none), 14 years ago

Added parallelized code

File size: 2.6 KB
Line 
1/*
2Copyright (C) 2006 Pedro Felzenszwalb
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, 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 "disjoint-set.h"
25
26// threshold function
27#define THRESHOLD(size, c) (c/size)
28
29
30#include <sys/time.h>
31#include <unistd.h>
32
33
34struct timeval start, end;
35long mtime, seconds, useconds; 
36
37
38typedef struct {
39  float w;
40  int a, b;
41} edge;
42
43bool operator<(const edge &a, const edge &b) {
44  return a.w < b.w;
45}
46
47/*
48 * Segment a graph
49 *
50 * Returns a disjoint-set forest representing the segmentation.
51 *
52 * num_vertices: number of vertices in graph.
53 * num_edges: number of edges in graph
54 * edges: array of edges.
55 * c: constant for treshold function.
56 */
57universe *segment_graph(int num_vertices, int num_edges, edge *edges, 
58                        float c) { 
59  // sort edges by weight
60  //std::sort(edges, edges + num_edges);
61
62        gettimeofday(&end, NULL);
63
64    seconds  = end.tv_sec  - start.tv_sec;
65    useconds = end.tv_usec - start.tv_usec;
66
67    mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
68
69    printf("Elapsed time: %ld milliseconds\n", mtime);
70
71
72  exit(0);
73  // make a disjoint-set forest
74  universe *u = universe_construct(num_vertices); //new universe(num_vertices);
75
76  // init thresholds
77  float *threshold = new float[num_vertices];
78  for (int i = 0; i < num_vertices; i++)
79    threshold[i] = THRESHOLD(1,c);
80
81  // for each edge, in non-decreasing weight order...
82  for (int i = 0; i < num_edges; i++) {
83    edge *pedge = &edges[i];
84   
85    // components conected by this edge
86    int a = universe_find(u,pedge->a);//u->find(pedge->a);
87    int b = universe_find(u,pedge->b);//u->find(pedge->b);
88    if (a != b) {
89      if ((pedge->w <= threshold[a]) &&
90          (pedge->w <= threshold[b])) {
91        universe_join(u,a,b);//u->join(a, b);
92        a = universe_find(u,a);//u->find(a);
93        //threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
94        threshold[a] = pedge->w + THRESHOLD(universe_size(u,a), c);
95      }
96    }
97  }
98
99  // free up
100  delete threshold;
101  return u;
102}
103
104#endif
Note: See TracBrowser for help on using the repository browser.