| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief adjusts directed graphs to improve layout aspect ratio, |
| 4 |
* API: cgraph.h, |
| 5 |
* implements @ref graphviz_unflatten, used in cmd/tools/unflatten.c |
| 6 |
* |
| 7 |
* @ingroup cgraph_app |
| 8 |
*/ |
| 9 |
/* |
| 10 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 11 |
* All rights reserved. This program and the accompanying materials |
| 12 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 13 |
* which accompanies this distribution, and is available at |
| 14 |
* https://www.eclipse.org/legal/epl-v10.html |
| 15 |
* |
| 16 |
* Authors: Stephen North, Emden Gansner |
| 17 |
* Contributors: Details at https://graphviz.org |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <cgraph/cghdr.h> |
| 23 |
#include <stdbool.h> |
| 24 |
#include <stddef.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <util/itos.h> |
| 27 |
|
| 28 |
static int myindegree(Agnode_t *n) { return agdegree(n->root, n, 1, 0); } |
| 29 |
|
| 30 |
// need outdegree without selfarcs |
| 31 |
static int myoutdegree(Agnode_t *n) { |
| 32 |
Agedge_t *e; |
| 33 |
int rv = 0; |
| 34 |
|
| 35 |
for (e = agfstout(n->root, n); e; e = agnxtout(n->root, e)) { |
| 36 |
if (agtail(e) != aghead(e)) |
| 37 |
rv++; |
| 38 |
} |
| 39 |
return rv; |
| 40 |
} |
| 41 |
|
| 42 |
static bool isleaf(Agnode_t *n) { return myindegree(n) + myoutdegree(n) == 1; } |
| 43 |
|
| 44 |
static bool ischainnode(Agnode_t *n) { |
| 45 |
return myindegree(n) == 1 && myoutdegree(n) == 1; |
| 46 |
} |
| 47 |
|
| 48 |
static void adjustlen(Agedge_t *e, Agsym_t *sym, int newlen) { |
| 49 |
agxset(e, sym, ITOS(newlen)); |
| 50 |
} |
| 51 |
|
| 52 |
static Agsym_t *bindedgeattr(Agraph_t *g, char *str) { |
| 53 |
return agattr_text(g, AGEDGE, str, ""); |
| 54 |
} |
| 55 |
|
| 56 |
void graphviz_unflatten(Agraph_t *g, const graphviz_unflatten_options_t *opts) { |
| 57 |
Agnode_t *n; |
| 58 |
Agedge_t *e; |
| 59 |
char *str; |
| 60 |
Agsym_t *m_ix, *s_ix; |
| 61 |
int cnt, d; |
| 62 |
int ChainSize = 0; |
| 63 |
Agnode_t *ChainNode = NULL; |
| 64 |
|
| 65 |
m_ix = bindedgeattr(g, "minlen"); |
| 66 |
s_ix = bindedgeattr(g, "style"); |
| 67 |
|
| 68 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 69 |
d = myindegree(n) + myoutdegree(n); |
| 70 |
if (d == 0) { |
| 71 |
if (opts->ChainLimit < 1) |
| 72 |
continue; |
| 73 |
if (ChainNode) { |
| 74 |
e = agedge(g, ChainNode, n, "", 1); |
| 75 |
agxset(e, s_ix, "invis"); |
| 76 |
ChainSize++; |
| 77 |
if (ChainSize < opts->ChainLimit) |
| 78 |
ChainNode = n; |
| 79 |
else { |
| 80 |
ChainNode = NULL; |
| 81 |
ChainSize = 0; |
| 82 |
} |
| 83 |
} else |
| 84 |
ChainNode = n; |
| 85 |
} else if (d > 1) { |
| 86 |
if (opts->MaxMinlen < 1) |
| 87 |
continue; |
| 88 |
cnt = 0; |
| 89 |
for (e = agfstin(g, n); e; e = agnxtin(g, e)) { |
| 90 |
if (isleaf(agtail(e))) { |
| 91 |
str = agxget(e, m_ix); |
| 92 |
if (str[0] == 0) { |
| 93 |
adjustlen(e, m_ix, cnt % opts->MaxMinlen + 1); |
| 94 |
cnt++; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
cnt = 0; |
| 100 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 101 |
if (isleaf(e->node) || (opts->Do_fans && ischainnode(e->node))) { |
| 102 |
str = agxget(e, m_ix); |
| 103 |
if (str[0] == 0) |
| 104 |
adjustlen(e, m_ix, cnt % opts->MaxMinlen + 1); |
| 105 |
cnt++; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |