Debian Codegraph @

cmd/gvmap/cluster.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12 #include "../tools/openFile.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #define STANDALONE
16 #include <sparse/general.h>
17 #include <sparse/QuadTree.h>
18 #include <time.h>
19 #include <sparse/SparseMatrix.h>
20 #include <getopt.h>
21 #include <string.h>
22 #include "make_map.h"
23 #include <sfdpgen/spring_electrical.h>
24 #include <sfdpgen/post_process.h>
25 #include <neatogen/overlap.h>
26 #include <sparse/clustering.h>
27 #include <cgraph/ingraphs.h>
28 #include <sparse/DotIO.h>
29 #include <sparse/colorutil.h>
30 #include <util/unreachable.h>
31
32 typedef struct {
33 FILE* outfp;
34 char** infiles;
35 int maxcluster;
36 int clustering_method;
37 } opts_t;
38
39 static const char usestr[] =
40 " -C k - generate no more than k clusters (0)\n\
41 0 : no limit\n\
42 -c k - use clustering method k (0)\n\
43 0 : use modularity\n\
44 1 : use modularity quality\n\
45 -o <outfile> - output file (stdout)\n\
46 -v - verbose mode\n\
47 -? - print usage\n";
48
49 static void usage(char* cmd, int eval)
50 {
51 fprintf(stderr, "Usage: %s <options> graphfile\n", cmd);
52 fputs (usestr, stderr);
53 graphviz_exit(eval);
54 }
55
56 static void init(int argc, char *argv[], opts_t* opts) {
57 char* cmd = argv[0];
58 int c;
59 int v;
60
61 opts->maxcluster = 0;
62 opts->outfp = stdout;
63 Verbose = 0;
64
65 opts->clustering_method = CLUSTERING_MODULARITY;
66 while ((c = getopt(argc, argv, ":vC:c:o:?")) != -1) {
67 switch (c) {
68 case 'c':
69 if (sscanf(optarg, "%d", &v) == 0 || v < 0) {
70 usage(cmd,1);
71 }
72 else opts->clustering_method = v;
73 break;
74 case 'C':
75 if (sscanf(optarg, "%d", &v) == 0 || v < 0) {
76 usage(cmd,1);
77 }
78 else opts->maxcluster = v;
79 break;
80 case 'o':
81 opts->outfp = openFile(cmd, optarg, "w");
82 break;
83 case 'v':
84 Verbose = 1;
85 break;
86 case '?':
87 if (optopt == '\0' || optopt == '?')
88 usage(cmd, 0);
89 else {
90 fprintf(stderr, " option -%c unrecognized\n",
91 optopt);
92 usage(cmd, 1);
93 }
94 break;
95 default:
96 UNREACHABLE();
97 }
98 }
99
100 argv += optind;
101 argc -= optind;
102 if (argc)
103 opts->infiles = argv;
104 else
105 opts->infiles = NULL;
106 }
107
108 static void clusterGraph (Agraph_t* g, int maxcluster, int clustering_method){
109 initDotIO(g);
110 attached_clustering(g, maxcluster, clustering_method);
111 }
112
113 int main(int argc, char *argv[])
114 {
115 Agraph_t *g = 0, *prevg = 0;
116 ingraph_state ig;
117 opts_t opts;
118
119 init(argc, argv, &opts);
120
121 newIngraph(&ig, opts.infiles);
122
123 while ((g = nextGraph (&ig)) != 0) {
124 if (prevg) agclose (prevg);
125 clusterGraph (g, opts.maxcluster, opts.clustering_method);
126 agwrite(g, opts.outfp);
127 prevg = g;
128 }
129
130 graphviz_exit(0);
131 }