1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
/* FIX: handle cluster labels */
/*
* Written by Emden R. Gansner
*/
#include <assert.h>
#include <common/geomprocs.h>
#include <common/render.h>
#include <common/utils.h>
#include <float.h>
#include <limits.h>
#include <osage/osage.h>
#include <neatogen/neatoprocs.h>
#include <pack/pack.h>
#include <stdbool.h>
#include <stddef.h>
#include <util/alloc.h>
#include <util/list.h>
#include <util/startswith.h>
#define DFLT_SZ 18
#define PARENT(n) ((Agraph_t*)ND_alg(n))
static void
indent (int i)
{
for (; i > 0; i--)
fputs (" ", stderr);
}
typedef LIST(Agraph_t *) clist_t;
static void cluster_init_graph(graph_t * g)
{
Agnode_t *n;
Agedge_t *e;
setEdgeType (g, EDGETYPE_LINE);
Ndim = GD_ndim(g)=2; /* The algorithm only makes sense in 2D */
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
neato_init_node (n);
}
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //edge custom data
common_init_edge(e);
}
}
}
/* layout:
*/
static void
layout (Agraph_t* g, int depth)
{
int nvs = 0; /* no. of nodes in subclusters */
pack_info pinfo;
Agsym_t* cattr = NULL;
Agsym_t* vattr = NULL;
Agraph_t* root = g->root;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "layout %s\n", agnameof(g));
}
/* Lay out subclusters */
for (int i = 1; i <= GD_n_cluster(g); i++) {
Agraph_t *const subg = GD_clust(g)[i];
layout (subg, depth+1);
nvs += agnnodes (subg);
}
const int nv = agnnodes(g);
const int total = nv - nvs + GD_n_cluster(g);
if (total == 0 && GD_label(g) == NULL) {
GD_bb(g) = (boxf){.UR = {.x = DFLT_SZ, .y = DFLT_SZ}};
return;
}
const pack_mode pmode = getPackInfo(g, l_array, DFLT_MARGIN, &pinfo);
if (pmode < l_graph) pinfo.mode = l_graph;
/* add user sort values if necessary */
if (pinfo.mode == l_array && (pinfo.flags & PK_USER_VALS)) {
cattr = agattr_text(root, AGRAPH, "sortv", 0);
vattr = agattr_text(root, AGNODE, "sortv", 0);
if (cattr || vattr)
pinfo.vals = gv_calloc(total+1, sizeof(packval_t));
else
agwarningf("Graph %s has array packing with user values but no \"sortv\" attributes are defined.",
agnameof(g));
}
boxf *gs = gv_calloc(total+1, sizeof(boxf));
void **children = gv_calloc(total+1, sizeof(void*));
int j = 0;
for (int i = 1; i <= GD_n_cluster(g); i++) {
Agraph_t *const subg = GD_clust(g)[i];
gs[j] = GD_bb(subg);
if (pinfo.vals && cattr) {
pinfo.vals[j] = (packval_t)late_int(subg, cattr, 0, 0);
}
children[j++] = subg;
}
if (nv-nvs > 0) {
for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (ND_alg(n)) continue;
ND_alg(n) = g;
const boxf bb = {.UR = {.x = ND_xsize(n), .y = ND_ysize(n)}};
gs[j] = bb;
if (pinfo.vals && vattr) {
pinfo.vals[j] = (packval_t)late_int(n, vattr, 0, 0);
}
children[j++] = n;
}
}
/* pack rectangles */
assert(total >= 0);
pointf *const pts = putRects((size_t)total, gs, &pinfo);
free (pinfo.vals);
boxf rootbb = {.LL = {DBL_MAX, DBL_MAX}, .UR = {-DBL_MAX, -DBL_MAX}};
/* reposition children relative to GD_bb(g) */
for (j = 0; j < total; j++) {
const pointf p = pts[j];
boxf bb = gs[j];
bb.LL = add_pointf(bb.LL, p);
bb.UR = add_pointf(bb.UR, p);
EXPANDBB(&rootbb, bb);
if (j < GD_n_cluster(g)) {
Agraph_t *const subg = children[j];
GD_bb(subg) = bb;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
}
}
else {
Agnode_t *const n = children[j];
if (n) {
ND_coord(n) = mid_pointf (bb.LL, bb.UR);
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
}
}
}
}
if (GD_label(g)) {
double d;
pointf pt = GD_label(g)->dimen;
if (total == 0) {
rootbb = (boxf){.UR = pt};
}
d = pt.x - (rootbb.UR.x - rootbb.LL.x);
if (d > 0) { /* height of label is added below */
d /= 2;
rootbb.LL.x -= d;
rootbb.UR.x += d;
}
}
const double margin = depth > 0 ? pinfo.margin / 2.0 : 0;
rootbb.LL.x -= margin;
rootbb.UR.x += margin;
rootbb.LL.y -= margin + GD_border(g)[BOTTOM_IX].y;
rootbb.UR.y += margin + GD_border(g)[TOP_IX].y;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f %f %f\n", agnameof(g), rootbb.LL.x, rootbb.LL.y, rootbb.UR.x, rootbb.UR.y);
}
/* Translate so that rootbb.LL is origin.
* This makes the repositioning simpler; we just translate the clusters and nodes in g by
* the final bb.ll of g.
*/
for (j = 0; j < total; j++) {
if (j < GD_n_cluster(g)) {
Agraph_t *const subg = children[j];
boxf bb = GD_bb(subg);
bb.LL = sub_pointf(bb.LL, rootbb.LL);
bb.UR = sub_pointf(bb.UR, rootbb.LL);
GD_bb(subg) = bb;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
}
}
else {
Agnode_t *const n = children[j];
if (n) {
ND_coord(n) = sub_pointf (ND_coord(n), rootbb.LL);
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
}
}
}
}
rootbb = (boxf){.UR = sub_pointf(rootbb.UR, rootbb.LL)};
GD_bb(g) = rootbb;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f %f %f\n", agnameof(g), rootbb.LL.x, rootbb.LL.y, rootbb.UR.x, rootbb.UR.y);
}
free (gs);
free (children);
free (pts);
}
static void
reposition (Agraph_t* g, int depth)
{
boxf bb = GD_bb(g);
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "reposition %s\n", agnameof(g));
}
/* translate nodes in g but not in a subcluster */
if (depth) {
for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (PARENT(n) != g)
continue;
ND_coord(n).x += bb.LL.x;
ND_coord(n).y += bb.LL.y;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
}
}
}
/* translate top-level clusters and recurse */
for (int i = 1; i <= GD_n_cluster(g); i++) {
Agraph_t *const subg = GD_clust(g)[i];
if (depth) {
boxf sbb = GD_bb(subg);
sbb.LL.x += bb.LL.x;
sbb.LL.y += bb.LL.y;
sbb.UR.x += bb.LL.x;
sbb.UR.y += bb.LL.y;
if (Verbose > 1) {
indent (depth);
fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), sbb.LL.x, sbb.LL.y, sbb.UR.x, sbb.UR.y);
}
GD_bb(subg) = sbb;
}
reposition (subg, depth+1);
}
}
static void
mkClusters (Agraph_t* g, clist_t* pclist, Agraph_t* parent)
{
clist_t list = {0};
clist_t* clist;
if (pclist == NULL) {
// [0] is empty. The clusters are in [1..cnt].
LIST_APPEND(&list, NULL);
clist = &list;
}
else
clist = pclist;
for (graph_t *subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
if (is_a_cluster(subg)) {
agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
do_graph_label (subg);
LIST_APPEND(clist, subg);
mkClusters(subg, NULL, subg);
}
else {
mkClusters(subg, clist, parent);
}
}
if (pclist == NULL) {
assert(LIST_SIZE(&list) - 1 <= INT_MAX);
GD_n_cluster(g) = (int)(LIST_SIZE(&list) - 1);
if (LIST_SIZE(&list) > 1) {
LIST_SHRINK_TO_FIT(&list);
LIST_DETACH(&list, &GD_clust(g), NULL);
} else {
LIST_FREE(&list);
}
}
}
void osage_layout(Agraph_t *g)
{
cluster_init_graph(g);
mkClusters(g, NULL, g);
layout(g, 0);
reposition (g, 0);
if (GD_drawing(g)->ratio_kind) {
Agnode_t* n;
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
ND_pos(n)[0] = PS2INCH(ND_coord(n).x);
ND_pos(n)[1] = PS2INCH(ND_coord(n).y);
}
spline_edges0(g, true);
}
else {
int et = EDGE_TYPE (g);
if (et != EDGETYPE_NONE) spline_edges1(g, et);
}
dotneato_postprocess(g);
}
static void cleanup_graphs (Agraph_t *g)
{
for (int i = 1; i <= GD_n_cluster(g); i++) {
graph_t *const subg = GD_clust(g)[i];
free_label(GD_label(subg));
cleanup_graphs (subg);
}
free (GD_clust(g));
}
void osage_cleanup(Agraph_t *g)
{
for (node_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (edge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
gv_cleanup_edge(e);
}
gv_cleanup_node(n);
}
cleanup_graphs(g);
}
/**
* @dir lib/osage
* @brief clustered layout engine, API osage/osage.h
* @ingroup engines
*
* [Osage layout user manual](https://graphviz.org/docs/layouts/osage/)
*
* Other @ref engines
*/
|