| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief merge and pack disjoint graphs |
| 4 |
*/ |
| 5 |
|
| 6 |
/************************************************************************* |
| 7 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 8 |
* All rights reserved. This program and the accompanying materials |
| 9 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 10 |
* which accompanies this distribution, and is available at |
| 11 |
* https://www.eclipse.org/legal/epl-v10.html |
| 12 |
* |
| 13 |
* Contributors: Details at https://graphviz.org |
| 14 |
*************************************************************************/ |
| 15 |
|
| 16 |
|
| 17 |
/* |
| 18 |
* Written by Emden R. Gansner |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <getopt.h> |
| 26 |
#include <algorithm> |
| 27 |
#include <cassert> |
| 28 |
#include <gvc/gvc.h> |
| 29 |
#include <cgraph/ingraphs.h> |
| 30 |
#include <common/render.h> |
| 31 |
#include <common/utils.h> |
| 32 |
#include <neatogen/neatoprocs.h> |
| 33 |
#include <iostream> |
| 34 |
#include <limits> |
| 35 |
#include <map> |
| 36 |
#include <optional> |
| 37 |
#include <pack/pack.h> |
| 38 |
#include <set> |
| 39 |
#include <stddef.h> |
| 40 |
#include <string> |
| 41 |
#include <utility> |
| 42 |
#include <util/alloc.h> |
| 43 |
#include <util/exit.h> |
| 44 |
#include <vector> |
| 45 |
#include "openFile.h" |
| 46 |
|
| 47 |
extern "C" { |
| 48 |
#ifdef GVDLL |
| 49 |
__declspec(dllimport) |
| 50 |
#endif |
| 51 |
extern gvplugin_library_t gvplugin_neato_layout_LTX_library; |
| 52 |
} |
| 53 |
|
| 54 |
lt_symlist_t lt_preloaded_symbols[] = { |
| 55 |
{ "gvplugin_neato_layout_LTX_library", 0 }, |
| 56 |
{ 0, 0 } |
| 57 |
}; |
| 58 |
|
| 59 |
/* gvpack: |
| 60 |
* Input consists of graphs in dot format. |
| 61 |
* The graphs should have pos, width and height set for nodes, |
| 62 |
* bb set for clusters, and, optionally, spline info for edges. |
| 63 |
* The graphs are packed geometrically and combined |
| 64 |
* into a single output graph, ready to be sent to neato -s -n2. |
| 65 |
* -m <i> specifies the margin, in points, about each graph. |
| 66 |
*/ |
| 67 |
|
| 68 |
typedef struct { |
| 69 |
char *name; |
| 70 |
char *value; |
| 71 |
} attr_t; |
| 72 |
|
| 73 |
static int verbose = 0; |
| 74 |
static char **myFiles = 0; |
| 75 |
static FILE *outfp; /* output; stdout by default */ |
| 76 |
static std::vector<attr_t> G_args; // Storage for -G arguments |
| 77 |
static bool doPack; /* Do packing if true */ |
| 78 |
static char* gname = const_cast<char*>("root"); |
| 79 |
|
| 80 |
#define NEWNODE(n) ((node_t*)ND_alg(n)) |
| 81 |
|
| 82 |
static const char useString[] = |
| 83 |
"Usage: gvpack [-gnuv?] [-m<margin>] {-array[_rc][n]] [-o<outf>] <files>\n\ |
| 84 |
-n - use node granularity\n\ |
| 85 |
-g - use graph granularity\n\ |
| 86 |
-array* - pack as array of graphs\n\ |
| 87 |
-G<n>=<v> - attach name/value attribute to output graph\n\ |
| 88 |
-m<n> - set margin to <n> points\n\ |
| 89 |
-s<gname> - use <gname> for name of root graph\n\ |
| 90 |
-o<outfile> - write output to <outfile>\n\ |
| 91 |
-u - no packing; just combine graphs\n\ |
| 92 |
-v - verbose\n\ |
| 93 |
-? - print usage\n\ |
| 94 |
If no files are specified, stdin is used\n"; |
| 95 |
|
| 96 |
static void usage(int v) |
| 97 |
{ |
| 98 |
std::cout << useString; |
| 99 |
graphviz_exit(v); |
| 100 |
} |
| 101 |
|
| 102 |
/* setNameValue: |
| 103 |
* If arg is a name-value pair, add it to the list |
| 104 |
* and return 0; otherwise, return 1. |
| 105 |
*/ |
| 106 |
static int setNameValue(char *arg) |
| 107 |
{ |
| 108 |
char *rhs = const_cast<char*>("true"); |
| 109 |
|
| 110 |
if (char *p = strchr(arg, '=')) { |
| 111 |
*p++ = '\0'; |
| 112 |
rhs = p; |
| 113 |
} |
| 114 |
G_args.push_back(attr_t{arg, rhs}); |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
/* setUInt: |
| 120 |
* If arg is an integer, value is stored in v |
| 121 |
* and function returns 0; otherwise, returns 1. |
| 122 |
*/ |
| 123 |
static int setUInt(unsigned int *v, char *arg) |
| 124 |
{ |
| 125 |
char *p; |
| 126 |
unsigned int i; |
| 127 |
|
| 128 |
i = (unsigned int) strtol(arg, &p, 10); |
| 129 |
if (p == arg) { |
| 130 |
std::cerr << "Error: bad value in flag -" << (arg - 1) << " - ignored\n"; |
| 131 |
return 1; |
| 132 |
} |
| 133 |
*v = i; |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static Agsym_t *agraphattr(Agraph_t *g, char *name, const char *value) { |
| 138 |
return agattr_text(g, AGRAPH, name, value); |
| 139 |
} |
| 140 |
|
| 141 |
static Agsym_t *agnodeattr(Agraph_t *g, char *name, const char *value) { |
| 142 |
return agattr_text(g, AGNODE, name, value); |
| 143 |
} |
| 144 |
|
| 145 |
static Agsym_t *agedgeattr(Agraph_t *g, char *name, const char *value) { |
| 146 |
return agattr_text(g, AGEDGE, name, value); |
| 147 |
} |
| 148 |
|
| 149 |
static void init(int argc, char *argv[], pack_info* pinfo) |
| 150 |
{ |
| 151 |
int c; |
| 152 |
|
| 153 |
agnodeattr(nullptr, const_cast<char*>("label"), NODENAME_ESC); |
| 154 |
pinfo->mode = l_clust; |
| 155 |
pinfo->margin = CL_OFFSET; |
| 156 |
pinfo->doSplines = true; // Use edges in packing |
| 157 |
pinfo->fixed = nullptr; |
| 158 |
pinfo->sz = 0; |
| 159 |
|
| 160 |
opterr = 0; |
| 161 |
while ((c = getopt(argc, argv, ":na:gvum:s:o:G:?")) != -1) { |
| 162 |
switch (c) { |
| 163 |
case 'a': { |
| 164 |
auto buf = std::string("a") + optarg + "\n"; |
| 165 |
parsePackModeInfo(buf.c_str(), pinfo->mode, pinfo); |
| 166 |
break; |
| 167 |
} |
| 168 |
case 'n': |
| 169 |
parsePackModeInfo ("node", pinfo->mode, pinfo); |
| 170 |
break; |
| 171 |
case 's': |
| 172 |
gname = optarg; |
| 173 |
break; |
| 174 |
case 'g': |
| 175 |
parsePackModeInfo ("graph", pinfo->mode, pinfo); |
| 176 |
break; |
| 177 |
case 'm': |
| 178 |
setUInt(&pinfo->margin, optarg); |
| 179 |
break; |
| 180 |
case 'o': |
| 181 |
if (outfp != nullptr) |
| 182 |
fclose(outfp); |
| 183 |
outfp = openFile("gvpack", optarg, "w"); |
| 184 |
break; |
| 185 |
case 'u': |
| 186 |
pinfo->mode = l_undef; |
| 187 |
break; |
| 188 |
case 'G': |
| 189 |
if (*optarg) |
| 190 |
setNameValue(optarg); |
| 191 |
else |
| 192 |
std::cerr << "gvpack: option -G missing argument - ignored\n"; |
| 193 |
break; |
| 194 |
case 'v': |
| 195 |
verbose = 1; |
| 196 |
Verbose = 1; |
| 197 |
break; |
| 198 |
case ':': |
| 199 |
std::cerr << "gvpack: option -" << (char)optopt |
| 200 |
<< " missing argument - ignored\n"; |
| 201 |
break; |
| 202 |
case '?': |
| 203 |
if (optopt == '\0' || optopt == '?') |
| 204 |
usage(0); |
| 205 |
else { |
| 206 |
std::cerr << "gvpack: option -" << (char)optopt << " unrecognized\n"; |
| 207 |
usage(1); |
| 208 |
} |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
argv += optind; |
| 213 |
argc -= optind; |
| 214 |
|
| 215 |
if (argc > 0) { |
| 216 |
myFiles = argv; |
| 217 |
} |
| 218 |
if (!outfp) |
| 219 |
outfp = stdout; /* stdout the default */ |
| 220 |
if (verbose) |
| 221 |
std::cerr << " margin " << pinfo->margin << '\n'; |
| 222 |
} |
| 223 |
|
| 224 |
/* init_node_edge: |
| 225 |
* initialize node and edge attributes |
| 226 |
*/ |
| 227 |
static void init_node_edge(Agraph_t * g) |
| 228 |
{ |
| 229 |
node_t *n; |
| 230 |
edge_t *e; |
| 231 |
int nG = agnnodes(g); |
| 232 |
attrsym_t *N_pos = agfindnodeattr(g, const_cast<char*>("pos")); |
| 233 |
attrsym_t *N_pin = agfindnodeattr(g, const_cast<char*>("pin")); |
| 234 |
|
| 235 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 236 |
neato_init_node(n); |
| 237 |
user_pos(N_pos, N_pin, n, nG); /* set user position if given */ |
| 238 |
} |
| 239 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 240 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) |
| 241 |
common_init_edge(e); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* init_graph: |
| 246 |
* Initialize attributes. We always do the minimum required by |
| 247 |
* libcommon. If fill is true, we use init_nop (neato -n) to |
| 248 |
* read in attributes relevant to the layout. |
| 249 |
*/ |
| 250 |
static void init_graph(Agraph_t *g, bool fill, GVC_t *gvc) { |
| 251 |
int d; |
| 252 |
node_t *n; |
| 253 |
edge_t *e; |
| 254 |
|
| 255 |
aginit (g, AGRAPH, "Agraphinfo_t", sizeof(Agraphinfo_t), true); |
| 256 |
aginit (g, AGNODE, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); |
| 257 |
aginit (g, AGEDGE, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); |
| 258 |
GD_gvc(g) = gvc; |
| 259 |
graph_init(g, false); |
| 260 |
d = late_int(g, agfindgraphattr(g, const_cast<char*>("dim")), 2, 2); |
| 261 |
if (d != 2) { |
| 262 |
std::cerr << "Error: graph " << agnameof(g) << " has dim = " << d |
| 263 |
<< " (!= 2)\n"; |
| 264 |
graphviz_exit(1); |
| 265 |
} |
| 266 |
Ndim = GD_ndim(g) = 2; |
| 267 |
init_node_edge(g); |
| 268 |
if (fill) { |
| 269 |
if (int ret = init_nop(g, 0)) { |
| 270 |
if (ret < 0) |
| 271 |
std::cerr << "Error loading layout info from graph " << agnameof(g) << '\n'; |
| 272 |
else if (ret > 0) |
| 273 |
std::cerr << "gvpack does not support backgrounds as found in graph " |
| 274 |
<< agnameof(g) << '\n'; |
| 275 |
graphviz_exit(1); |
| 276 |
} |
| 277 |
if (Concentrate) { /* check for edges without pos info */ |
| 278 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 279 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 280 |
if (ED_spl(e) == nullptr) ED_edge_type(e) = IGNORED; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/* cloneAttrs: |
| 288 |
* Copy all attributes from old object to new. Assume |
| 289 |
* attributes have been initialized. |
| 290 |
*/ |
| 291 |
static void cloneDfltAttrs(Agraph_t *old, Agraph_t *new_graph, int attr_kind) { |
| 292 |
for (Agsym_t *a = agnxtattr(old, attr_kind, 0); a; a = agnxtattr(old, attr_kind, a)) { |
| 293 |
if (aghtmlstr(a->defval)) { |
| 294 |
agattr_html(new_graph, attr_kind, a->name, a->defval); |
| 295 |
} else { |
| 296 |
agattr_text(new_graph, attr_kind, a->name, a->defval); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
static void cloneAttrs(void *old, void *new_graph) { |
| 301 |
int attr_kind = AGTYPE(old); |
| 302 |
char* s; |
| 303 |
Agraph_t *g = agroot(old); |
| 304 |
Agraph_t *ng = agroot(new_graph); |
| 305 |
|
| 306 |
for (Agsym_t *a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) { |
| 307 |
s = agxget (old, a); |
| 308 |
if (aghtmlstr(s)) { |
| 309 |
char *scopy = agstrdup_html(ng, s); |
| 310 |
agset(new_graph, a->name, scopy); |
| 311 |
agstrfree(ng, scopy, true); // drop the extra reference count we bumped for scopy |
| 312 |
} else { |
| 313 |
agset(new_graph, a->name, s); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/* cloneEdge: |
| 319 |
* Note that here, and in cloneNode and cloneCluster, |
| 320 |
* we do a shallow copy. We thus assume that attributes |
| 321 |
* are not disturbed. In particular, we assume they are |
| 322 |
* not deallocated. |
| 323 |
*/ |
| 324 |
static void cloneEdge(Agedge_t *old, Agedge_t *new_edge) { |
| 325 |
cloneAttrs(old, new_edge); |
| 326 |
ED_spl(new_edge) = ED_spl(old); |
| 327 |
ED_edge_type(new_edge) = ED_edge_type(old); |
| 328 |
ED_label(new_edge) = ED_label(old); |
| 329 |
ED_head_label(new_edge) = ED_head_label(old); |
| 330 |
ED_tail_label(new_edge) = ED_tail_label(old); |
| 331 |
ED_xlabel(new_edge) = ED_xlabel(old); |
| 332 |
} |
| 333 |
|
| 334 |
static void cloneNode(Agnode_t *old, Agnode_t *new_node) { |
| 335 |
cloneAttrs(old, new_node); |
| 336 |
ND_coord(new_node).x = POINTS(ND_pos(old)[0]); |
| 337 |
ND_coord(new_node).y = POINTS(ND_pos(old)[1]); |
| 338 |
ND_height(new_node) = ND_height(old); |
| 339 |
ND_ht(new_node) = ND_ht(old); |
| 340 |
ND_width(new_node) = ND_width(old); |
| 341 |
ND_lw(new_node) = ND_lw(old); |
| 342 |
ND_rw(new_node) = ND_rw(old); |
| 343 |
ND_shape(new_node) = ND_shape(old); |
| 344 |
ND_shape_info(new_node) = ND_shape_info(old); |
| 345 |
ND_xlabel(new_node) = ND_xlabel(old); |
| 346 |
} |
| 347 |
|
| 348 |
static void cloneCluster(Agraph_t *old, Agraph_t *new_cluster) { |
| 349 |
// string attributes were cloned as subgraphs |
| 350 |
GD_label(new_cluster) = GD_label(old); |
| 351 |
GD_bb(new_cluster) = GD_bb(old); |
| 352 |
} |
| 353 |
|
| 354 |
namespace { |
| 355 |
/// a value of a graph attribute, possibly set multiple times |
| 356 |
struct AttributeValue { |
| 357 |
std::string value; ///< text of the value |
| 358 |
size_t instances; ///< number of times this attribute was seen |
| 359 |
}; |
| 360 |
} // namespace |
| 361 |
|
| 362 |
/// attribute name → value collection of those we have seen |
| 363 |
using attr_map_t = std::map<std::string, AttributeValue>; |
| 364 |
|
| 365 |
/* fillDict: |
| 366 |
* Fill newdict with all the name-value attributes of |
| 367 |
* objp. If the attribute has already been defined and |
| 368 |
* has a different default, set default to "". |
| 369 |
*/ |
| 370 |
static void fillDict(attr_map_t &newdict, Agraph_t *g, int attr_kind) { |
| 371 |
|
| 372 |
for (Agsym_t *a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) { |
| 373 |
char *name = a->name; |
| 374 |
char *value = a->defval; |
| 375 |
auto it = newdict.find(name); |
| 376 |
if (it == newdict.end()) { |
| 377 |
newdict.insert({name, AttributeValue{value, 1}}); |
| 378 |
} else if (it->second.value == value) |
| 379 |
++it->second.instances; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* fillGraph: |
| 384 |
* Use all the name-value entries in the dictionary d to define |
| 385 |
* to define universal node/edge/graph attributes for g. |
| 386 |
* For a non-empty default value, the attribute must be defined and the |
| 387 |
* same in all graphs. |
| 388 |
*/ |
| 389 |
static void fillGraph(Agraph_t *g, const attr_map_t &d, |
| 390 |
Agsym_t *(*setf)(Agraph_t *, char *, const char *), |
| 391 |
size_t cnt) { |
| 392 |
for (const auto &kv : d) { |
| 393 |
const std::string &name = kv.first; |
| 394 |
const std::string &value = kv.second.value; |
| 395 |
const size_t &attr_cnt = kv.second.instances; |
| 396 |
if (cnt == attr_cnt) |
| 397 |
setf(g, const_cast<char *>(name.c_str()), value.c_str()); |
| 398 |
else |
| 399 |
setf(g, const_cast<char *>(name.c_str()), ""); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/* initAttrs: |
| 404 |
* Initialize the attributes of root as the union of the |
| 405 |
* attributes in the graphs gs. |
| 406 |
*/ |
| 407 |
static void initAttrs(Agraph_t *root, std::vector<Agraph_t*> &gs) { |
| 408 |
attr_map_t n_attrs; |
| 409 |
attr_map_t e_attrs; |
| 410 |
attr_map_t g_attrs; |
| 411 |
|
| 412 |
for (Agraph_t *g : gs) { |
| 413 |
fillDict(g_attrs, g, AGRAPH); |
| 414 |
fillDict(n_attrs, g, AGNODE); |
| 415 |
fillDict(e_attrs, g, AGEDGE); |
| 416 |
} |
| 417 |
|
| 418 |
fillGraph(root, g_attrs, agraphattr, gs.size()); |
| 419 |
fillGraph(root, n_attrs, agnodeattr, gs.size()); |
| 420 |
fillGraph(root, e_attrs, agedgeattr, gs.size()); |
| 421 |
} |
| 422 |
|
| 423 |
static void cloneGraphAttr(Agraph_t * g, Agraph_t * ng) |
| 424 |
{ |
| 425 |
cloneAttrs(g, ng); |
| 426 |
cloneDfltAttrs(g, ng, AGNODE); |
| 427 |
cloneDfltAttrs(g, ng, AGEDGE); |
| 428 |
} |
| 429 |
|
| 430 |
/// names that have already been used during generation |
| 431 |
using used_t = std::multiset<std::string>; |
| 432 |
|
| 433 |
/* xName: |
| 434 |
* Create a name for an object in the new graph using the |
| 435 |
* dictionary names and the old name. If the old name has not |
| 436 |
* been used, use it and add it to names. If it has been used, |
| 437 |
* create a new name using the old name and a number. |
| 438 |
* Note that returned string will immediately made into an agstring. |
| 439 |
*/ |
| 440 |
static std::string xName(used_t &names, char *oldname) { |
| 441 |
size_t previous_instances = names.count(oldname); |
| 442 |
names.insert(oldname); |
| 443 |
if (previous_instances > 0) { |
| 444 |
return std::string(oldname) + "_gv" + std::to_string(previous_instances); |
| 445 |
} |
| 446 |
return oldname; |
| 447 |
} |
| 448 |
|
| 449 |
#define MARK(e) (ED_alg(e) = e) |
| 450 |
#define MARKED(e) (ED_alg(e)) |
| 451 |
#define SETCLUST(g,h) (GD_alg(g) = h) |
| 452 |
#define GETCLUST(g) ((Agraph_t*)GD_alg(g)) |
| 453 |
|
| 454 |
/* cloneSubg: |
| 455 |
* Create a copy of g in ng, copying attributes, inserting nodes |
| 456 |
* and adding edges. |
| 457 |
*/ |
| 458 |
static void |
| 459 |
cloneSubg(Agraph_t *g, Agraph_t *ng, Agsym_t *G_bb, used_t &gnames) { |
| 460 |
node_t *n; |
| 461 |
node_t *nn; |
| 462 |
edge_t *e; |
| 463 |
edge_t *ne; |
| 464 |
node_t *nt; |
| 465 |
node_t *nh; |
| 466 |
Agraph_t *subg; |
| 467 |
Agraph_t *nsubg; |
| 468 |
|
| 469 |
cloneGraphAttr(g, ng); |
| 470 |
if (doPack) |
| 471 |
agxset(ng, G_bb, ""); /* Unset all subgraph bb */ |
| 472 |
|
| 473 |
/* clone subgraphs */ |
| 474 |
for (subg = agfstsubg (g); subg; subg = agnxtsubg (subg)) { |
| 475 |
nsubg = agsubg(ng, xName(gnames, agnameof(subg)).data(), 1); |
| 476 |
agbindrec (nsubg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); |
| 477 |
cloneSubg(subg, nsubg, G_bb, gnames); |
| 478 |
/* if subgraphs are clusters, point to the new |
| 479 |
* one so we can find it later. |
| 480 |
*/ |
| 481 |
if (is_a_cluster(subg)) |
| 482 |
SETCLUST(subg, nsubg); |
| 483 |
} |
| 484 |
|
| 485 |
/* add remaining nodes */ |
| 486 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 487 |
nn = NEWNODE(n); |
| 488 |
agsubnode(ng, nn, 1); |
| 489 |
} |
| 490 |
|
| 491 |
/* add remaining edges. libgraph doesn't provide a way to find |
| 492 |
* multiedges, so we add edges bottom up, marking edges when added. |
| 493 |
*/ |
| 494 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 495 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 496 |
if (MARKED(e)) |
| 497 |
continue; |
| 498 |
nt = NEWNODE(agtail(e)); |
| 499 |
nh = NEWNODE(aghead(e)); |
| 500 |
ne = agedge(ng, nt, nh, nullptr, 1); |
| 501 |
agbindrec (ne, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); |
| 502 |
cloneEdge(e, ne); |
| 503 |
MARK(e); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
/* cloneClusterTree: |
| 509 |
* Given old and new subgraphs which are corresponding |
| 510 |
* clusters, recursively create the subtree of clusters |
| 511 |
* under ng using the subtree of clusters under g. |
| 512 |
*/ |
| 513 |
static void cloneClusterTree(Agraph_t * g, Agraph_t * ng) |
| 514 |
{ |
| 515 |
int i; |
| 516 |
|
| 517 |
cloneCluster(g, ng); |
| 518 |
|
| 519 |
if (GD_n_cluster(g)) { |
| 520 |
GD_n_cluster(ng) = GD_n_cluster(g); |
| 521 |
void *const p = gv_calloc(1 + GD_n_cluster(g), sizeof(Agraph_t *)); |
| 522 |
GD_clust(ng) = new (p) Agraph_t *[1 + GD_n_cluster(g)]{}; |
| 523 |
for (i = 1; i <= GD_n_cluster(g); i++) { |
| 524 |
Agraph_t *c = GETCLUST(GD_clust(g)[i]); |
| 525 |
GD_clust(ng)[i] = c; |
| 526 |
cloneClusterTree(GD_clust(g)[i], c); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/* cloneGraph: |
| 532 |
* Create and return a new graph which is the logical union |
| 533 |
* of the graphs gs. |
| 534 |
*/ |
| 535 |
static Agraph_t *cloneGraph(std::vector<Agraph_t *> &gs, GVC_t *gvc, |
| 536 |
Agdesc_t kind) { |
| 537 |
Agraph_t *root; |
| 538 |
Agraph_t *subg; |
| 539 |
Agnode_t *n; |
| 540 |
Agnode_t *np; |
| 541 |
bool doWarn = true; |
| 542 |
|
| 543 |
if (verbose) |
| 544 |
std::cerr << "Creating clone graph\n"; |
| 545 |
root = agopen(gname, kind, &AgDefaultDisc); |
| 546 |
initAttrs(root, gs); |
| 547 |
Agsym_t *const G_bb = agfindgraphattr(root, const_cast<char*>("bb")); |
| 548 |
if (doPack) assert(G_bb); |
| 549 |
|
| 550 |
/* add command-line attributes */ |
| 551 |
for (attr_t &a : G_args) { |
| 552 |
if (Agsym_t *rv = agfindgraphattr(root, a.name)) |
| 553 |
agxset(root, rv, a.value); |
| 554 |
else |
| 555 |
agattr_text(root, AGRAPH, a.name, a.value); |
| 556 |
} |
| 557 |
|
| 558 |
/* do common initialization. This will handle root's label. */ |
| 559 |
init_graph(root, false, gvc); |
| 560 |
State = GVSPLINES; |
| 561 |
|
| 562 |
used_t gnames; // dict of used subgraph names |
| 563 |
used_t nnames; // dict of used node names |
| 564 |
for (size_t i = 0; i < gs.size(); i++) { |
| 565 |
Agraph_t *g = gs[i]; |
| 566 |
if (verbose) |
| 567 |
std::cerr << "Cloning graph " << agnameof(g) << '\n'; |
| 568 |
GD_n_cluster(root) += GD_n_cluster(g); |
| 569 |
GD_has_labels(root) |= GD_has_labels(g); |
| 570 |
|
| 571 |
/* Clone nodes, checking for node name conflicts */ |
| 572 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 573 |
if (doWarn && agfindnode(root, agnameof(n))) { |
| 574 |
std::cerr << "Warning: node " << agnameof(n) << " in graph[" << i << "] " |
| 575 |
<< agnameof(g) << " already defined\n" |
| 576 |
<< "Some nodes will be renamed.\n"; |
| 577 |
doWarn = false; |
| 578 |
} |
| 579 |
np = agnode(root, xName(nnames, agnameof(n)).data(), 1); |
| 580 |
agbindrec (np, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); |
| 581 |
ND_alg(n) = np; |
| 582 |
cloneNode(n, np); |
| 583 |
} |
| 584 |
|
| 585 |
/* wrap the clone of g in a subgraph of root */ |
| 586 |
subg = agsubg(root, xName(gnames, agnameof(g)).data(), 1); |
| 587 |
agbindrec (subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); |
| 588 |
cloneSubg(g, subg, G_bb, gnames); |
| 589 |
} |
| 590 |
|
| 591 |
/* set up cluster tree */ |
| 592 |
if (GD_n_cluster(root)) { |
| 593 |
int j, idx; |
| 594 |
void *const p = gv_calloc(1 + GD_n_cluster(root), sizeof(graph_t *)); |
| 595 |
GD_clust(root) = new (p) graph_t *[1 + GD_n_cluster(root)]{}; |
| 596 |
|
| 597 |
idx = 1; |
| 598 |
for (Agraph_t *g : gs) { |
| 599 |
for (j = 1; j <= GD_n_cluster(g); j++) { |
| 600 |
Agraph_t *c = GETCLUST(GD_clust(g)[j]); |
| 601 |
GD_clust(root)[idx++] = c; |
| 602 |
cloneClusterTree(GD_clust(g)[j], c); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
return root; |
| 608 |
} |
| 609 |
|
| 610 |
/* readGraphs: |
| 611 |
* Read input, parse the graphs, use init_nop (neato -n) to |
| 612 |
* read in all attributes need for layout. |
| 613 |
* Return the list of graphs. If cp != nullptr, set it to the number |
| 614 |
* of graphs read. |
| 615 |
* We keep track of the types of graphs read. They all must be |
| 616 |
* either directed or undirected. If all graphs are strict, the |
| 617 |
* combined graph will be strict; other, the combined graph will |
| 618 |
* be non-strict. |
| 619 |
* |
| 620 |
* @param kind [out] The type to use for the combined graph |
| 621 |
*/ |
| 622 |
static std::vector<Agraph_t *> readGraphs(GVC_t *gvc, |
| 623 |
std::optional<Agdesc_t> &kind) { |
| 624 |
std::vector<Agraph_t*> gs; |
| 625 |
ingraph_state ig; |
| 626 |
|
| 627 |
/* set various state values */ |
| 628 |
PSinputscale = POINTS_PER_INCH; |
| 629 |
Nop = 2; |
| 630 |
|
| 631 |
newIngraph(&ig, myFiles); |
| 632 |
while (Agraph_t *g = nextGraph(&ig)) { |
| 633 |
if (verbose) |
| 634 |
std::cerr << "Reading graph " << agnameof(g) << '\n'; |
| 635 |
if (agnnodes(g) == 0) { |
| 636 |
std::cerr << "Graph " << agnameof(g) << " is empty - ignoring\n"; |
| 637 |
continue; |
| 638 |
} |
| 639 |
if (!kind.has_value()) { |
| 640 |
kind = g->desc; |
| 641 |
} |
| 642 |
else if (kind->directed != g->desc.directed) { |
| 643 |
std::cerr << "Error: all graphs must be directed or undirected\n"; |
| 644 |
graphviz_exit(1); |
| 645 |
} else if (!agisstrict(g)) |
| 646 |
kind = g->desc; |
| 647 |
init_graph(g, doPack, gvc); |
| 648 |
gs.push_back(g); |
| 649 |
} |
| 650 |
|
| 651 |
return gs; |
| 652 |
} |
| 653 |
|
| 654 |
/* compBB: |
| 655 |
* Compute the bounding box containing the graphs. |
| 656 |
* We can just use the bounding boxes of the graphs. |
| 657 |
*/ |
| 658 |
static boxf compBB(std::vector<Agraph_t*> &gs) { |
| 659 |
boxf bb, bb2; |
| 660 |
|
| 661 |
bb = GD_bb(gs[0]); |
| 662 |
|
| 663 |
for (size_t i = 1; i < gs.size(); i++) { |
| 664 |
bb2 = GD_bb(gs[i]); |
| 665 |
bb.LL.x = std::min(bb.LL.x, bb2.LL.x); |
| 666 |
bb.LL.y = std::min(bb.LL.y, bb2.LL.y); |
| 667 |
bb.UR.x = std::max(bb.UR.x, bb2.UR.x); |
| 668 |
bb.UR.y = std::max(bb.UR.y, bb2.UR.y); |
| 669 |
} |
| 670 |
|
| 671 |
return bb; |
| 672 |
} |
| 673 |
|
| 674 |
#ifdef DEBUG |
| 675 |
void dump(Agraph_t * g) |
| 676 |
{ |
| 677 |
node_t *v; |
| 678 |
edge_t *e; |
| 679 |
|
| 680 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) { |
| 681 |
std::cerr << agnameof(v) << '\n'; |
| 682 |
for (e = agfstout(g, v); e; e = agnxtout(g, e)) { |
| 683 |
std::cerr << " " << agnameof(agtail(e)) << " -- " << agnameof(aghead(e)) |
| 684 |
<< '\n'; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
void dumps(Agraph_t * g) |
| 690 |
{ |
| 691 |
graph_t *subg; |
| 692 |
|
| 693 |
for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) { |
| 694 |
dump(subg); |
| 695 |
std::cerr << "====\n"; |
| 696 |
} |
| 697 |
} |
| 698 |
#endif |
| 699 |
|
| 700 |
int main(int argc, char *argv[]) |
| 701 |
{ |
| 702 |
Agraph_t *g; |
| 703 |
pack_info pinfo; |
| 704 |
GVC_t * gvc; |
| 705 |
|
| 706 |
init(argc, argv, &pinfo); |
| 707 |
|
| 708 |
doPack = (pinfo.mode != l_undef); |
| 709 |
|
| 710 |
lt_preloaded_symbols[0].address = &gvplugin_neato_layout_LTX_library; |
| 711 |
gvc = gvContextPlugins(lt_preloaded_symbols, DEMAND_LOADING); |
| 712 |
std::optional<Agdesc_t> kind; // type of graph |
| 713 |
std::vector<Agraph_t*> gs = readGraphs(gvc, kind); |
| 714 |
if (gs.empty()) |
| 715 |
graphviz_exit(0); |
| 716 |
|
| 717 |
/* pack graphs */ |
| 718 |
if (doPack) { |
| 719 |
if (packGraphs(gs.size(), gs.data(), 0, &pinfo)) { |
| 720 |
std::cerr << "gvpack: packing of graphs failed.\n"; |
| 721 |
graphviz_exit(1); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/* create union graph and copy attributes */ |
| 726 |
assert(kind.has_value()); |
| 727 |
g = cloneGraph(gs, gvc, *kind); |
| 728 |
|
| 729 |
/* compute new top-level bb and set */ |
| 730 |
if (doPack) { |
| 731 |
GD_bb(g) = compBB(gs); |
| 732 |
dotneato_postprocess(g); |
| 733 |
attach_attrs(g); |
| 734 |
} |
| 735 |
agwrite(g, outfp); |
| 736 |
graphviz_exit(0); |
| 737 |
} |