File: graphviz.c

package info (click to toggle)
ggobi 2.1.11-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 13,208 kB
  • ctags: 6,166
  • sloc: ansic: 53,297; xml: 28,411; sh: 11,791; makefile: 264; sed: 16
file content (514 lines) | stat: -rw-r--r-- 14,912 bytes parent folder | download | duplicates (2)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <gtk/gtk.h>
#include "ggobi.h"
#include "externs.h"
#include "GGobiAPI.h"

#include <stdio.h>
#include <string.h>
#include <math.h>

#include "plugin.h"
/* glayout.h includes config.h, which defines HAVE_LIBGVC */
#include "glayout.h"

#if defined HAVE_LIBGVC

#include <gvc.h>

#define DOT_LAYOUT   0
#define NEATO_LAYOUT 1
#define FDP_LAYOUT   2 // currently disabled; crashes with memory
                       // errors when running snetwork.xml
#define TWOPI_LAYOUT 3 // currently disabled; redundant with radial,
                       // and not as good.
#define CIRCO_LAYOUT 4

#ifdef DEBUG
static void
test_edge_length (Agraph_t *graph, glayoutd *gl, ggobid *gg)
{
  gint i, a, b;
  GGobiData *d = gl->dsrc;
  GGobiData *e = gl->e;
  Agnode_t *head, *tail;
  Agedge_t *edge;
  gchar *name;
  endpointsd *endpoints = resolveEdgePoints(e, d);

  for (i=0; i<e->edge.n; i++) {
    edge_endpoints_get (i, &a, &b, d, endpoints, e);

    name = (gchar *) g_array_index (d->rowlab, gchar *, a);
    tail = agfindnode (graph, name);

    name = (gchar *) g_array_index (d->rowlab, gchar *, b);
    head = agfindnode (graph, name);

    if (head && tail) {
      edge = agfindedge (graph, tail, head);
      if (edge) {
        if (edge->u.dist > 1)
          g_printerr ("dist: %f\n", edge->u.dist);
      }
    }
  }
}
#endif

void neato_model_cb (GtkWidget *w, PluginInstance *inst)
{
  glayoutd *gl = glayoutFromInst (inst);
  gl->neato_model = gtk_combo_box_get_active(GTK_COMBO_BOX(w));
}
void neato_use_edge_length_cb (GtkToggleButton *button, PluginInstance *inst)
{
  glayoutd *gl = glayoutFromInst (inst);
  gl->neato_use_edge_length_p = button->active;
}

void neato_dim_cb (GtkAdjustment *adj, PluginInstance *inst)
{
  glayoutd *gl = glayoutFromInst (inst);
  gl->neato_dim = (gint) (adj->value);
}

static gint
neato_get_weight_var (Agraph_t *graph, GtkWidget *w, glayoutd *gl, ggobid *gg)
{
  gint weightvar = -1;
  GGobiData *e = gl->e;
  GGobiData *e_clist;
  GtkWidget *tree_view;

  /*-- find the variable which will define the edge lengths --*/
  /*-- first get the list of variables from the 'apply' button --*/
  tree_view = get_tree_view_from_object (G_OBJECT (w));
  if (!tree_view) {
    quick_message ("I can't identify a set of edges", false);
    return -1;
  }
  e_clist = g_object_get_data(G_OBJECT(tree_view), "datad");
  if (e_clist == NULL || e_clist != e) {
    quick_message ("This isn't the same set of edges you're using", false);
    return -1;
  }
  weightvar = get_one_selection_from_tree_view (tree_view, e);
  if (weightvar == -1) {
    quick_message ("Please specify a variable", false);
    return -1;
  }
  return weightvar;
}

static gboolean
neato_apply_edge_length (Agraph_t *graph, gint weightvar, glong *visible,
			 gint nvisible, GGobiData *d, GGobiData *e, ggobid *gg)
{
  gint i, a, b;
  //GGobiData *e_clist;
  Agnode_t *head, *tail;
  Agedge_t *edge;
  gchar *name;
  endpointsd *endpoints;

  /*
  GtkWidget *clist;
  clist = get_clist_from_object (GTK_OBJECT (w));
  if (!clist) {
    quick_message ("I can't identify a set of edges", false);
    return false;
  }
  // I don't think I have an equivalent of this test now.
  e_clist = g_object_get_data(G_OBJECT(clist), "datad");
  if (e_clist == NULL || e_clist != e) {
    quick_message ("This isn't the same set of edges you're using", false);
    return false;
  }
  */

  endpoints = resolveEdgePoints(e, d);
  if (endpoints) {

    // See splot_hidden_edge
    for (i=0; i<e->edge.n; i++) {
      edge_endpoints_get (i, &a, &b, d, endpoints, e);

      name = (gchar *) g_array_index (d->rowlab, gchar *, a);
      tail = agfindnode (graph, name);

      name = (gchar *) g_array_index (d->rowlab, gchar *, b);
      head = agfindnode (graph, name);

      if (head && tail) {
        edge = agfindedge (graph, tail, head);
        if (edge) {
          if (e->tform.vals[i][weightvar] < 1) {
            quick_message ("The minimum length is 1.0; perform a variable transformation before doing the layout.", false);
            return false;
          } else {
            agsafeset (edge, "len", 
              g_strdup_printf("%f", e->tform.vals[i][weightvar]), NULL);
          }
        }
      }
    }
  }
  return true;
}


void dot_neato_layout_cb (GtkWidget *button, PluginInstance *inst)
{
  ggobid *gg = inst->gg;
  glayoutd *gl = glayoutFromInst (inst);
  GGobiData *d = gl->dsrc;
  GGobiData *e = gl->e;
  Agnode_t *node, *head, *tail;
  Agedge_t *edge;
  Agedge_t **edgev;
  gint *intv;
  gchar *name;
  gint kind = AGRAPH;
  gint i, k;
  gint a, b;
#ifdef DEBUG
  FILE* f;
#endif
  Agraph_t *graph;
  gdouble **pos;
  gint layout_type = DOT_LAYOUT;
  static GVC_t *gvc; 
/*-- to add the new datad --*/
  gint m, nvisible, nc;
  InputDescription *desc = NULL;
  GGobiData *dnew;
  gdouble *values;
  gchar **rownames, **colnames, **rowids;
  glong *visible;
  displayd *dspnew;
  gint dim, weightvar = -1, nedges;
  endpointsd *endpoints = resolveEdgePoints(e, d);
  gchar       modelchar = 'd';

  DisplayOptions *opts = NULL;

  if (e == NULL) {
    g_printerr ("Trouble:  no edge set is specified\n");
    return;
  }

  visible = (glong *) g_malloc (d->nrows_in_plot * sizeof (glong));
  nvisible = visible_set (visible, d);

  if (strcmp (gtk_widget_get_name (button), "neato") == 0) {
    layout_type = NEATO_LAYOUT;
    if (gl->neato_use_edge_length_p)
      weightvar = neato_get_weight_var (graph, button, gl, gg);
  } else if (strcmp (gtk_widget_get_name (button), "twopi") == 0) {
    layout_type = TWOPI_LAYOUT;
  } else if (strcmp (gtk_widget_get_name (button), "fdp") == 0) {
    layout_type = FDP_LAYOUT;
  } else if (strcmp (gtk_widget_get_name (button), "circo") == 0) {
    layout_type = CIRCO_LAYOUT;
  }

#ifndef HAVE_CGRAPH
  aginit();
#endif
  if (gvc == NULL)
    gvc = gvContext();

  /*-- create a new empty graph --*/
#ifdef HAVE_CGRAPH
  graph = agopen("graph", Agdirected, NULL);
#else
  graph = agopen("graph", kind);
#endif
  
  /*-- create new nodes, add to graph --*/
  for (i=0; i<nvisible; i++) {
    m = visible[i];
    name = (gchar *) g_array_index (d->rowlab, gchar *, m);
#ifdef HAVE_CGRAPH
    agnode(graph, name, 1);
#else
    agnode(graph, name);
#endif    
  }

  intv = (gint *) g_malloc (e->edge.n * sizeof(gint));
  edgev = (Agedge_t **) g_malloc (e->edge.n * sizeof (Agedge_t *));
  nedges = 0;
  /*-- create new edges, add to graph --*/
  for (i=0; i<e->edge.n; i++) {
    if (e->excluded.els[i])
      continue;

/* still need a test for edge visibility */
    edge_endpoints_get (i, &a, &b, d, endpoints, e);

    name = (gchar *) g_array_index (d->rowlab, gchar *, a);
    tail = agfindnode (graph, name);

    name = (gchar *) g_array_index (d->rowlab, gchar *, b);
    head = agfindnode (graph, name);

    /*-- if head and tail are both in the visible subset --*/
    if (head && tail) {
#ifdef HAVE_CGRAPH
      edge = agedge(graph, tail, head, "", 1);
#else
      edge = agedge(graph, tail, head);
#endif
      intv[nedges] = i;
      edgev[nedges] = edge;
      nedges++;
    }
  }

  pos = (gdouble **) g_malloc0 (nvisible * sizeof (gdouble *));
  if (layout_type == DOT_LAYOUT || layout_type == TWOPI_LAYOUT || layout_type == FDP_LAYOUT || layout_type == CIRCO_LAYOUT)
    dim = 2;
  else dim = gl->neato_dim;
  for (i=0; i<nvisible; i++)
    pos[i] = (gdouble *) g_malloc0 (dim * sizeof (gdouble));

  if (layout_type == DOT_LAYOUT) {
    gvLayout(gvc, graph, "dot");

    /*-- add variables generated by the layout algorithm --*/
    for (i=0; i<nvisible; i++) {  /*-- nrows = nnodes --*/
      m = visible[i];
      name = (gchar *) g_array_index (d->rowlab, gchar *, m);
      node = agfindnode (graph, name);
#ifdef HAVE_CGRAPH
      pos[i][0] = ND_coord(node).x;
      pos[i][1] = ND_coord(node).y;
#else
      pos[i][0] = node->u.coord.x;
      pos[i][1] = node->u.coord.y;
#endif
    } 

  } else if (layout_type == TWOPI_LAYOUT) {
    Agnode_t *ctr = 0;

    /* Get the rowlab associated with the center node, if defined */
    if (gl->centerNodeIndex >= 0) {
      ctr = agfindnode(graph,
        (gchar *) g_array_index (d->rowlab, gchar *, 
				 (gl->centerNodeIndex)));
    }
    
    if (ctr)
      agsafeset (graph, "root",
#ifdef HAVE_CGRAPH
                 agnameof(ctr),
#else
                 ctr->name,
#endif
                 NULL);
    gvLayout (gvc, graph, "twopi");

    /*-- add variables generated by the layout algorithm --*/
    for (i=0; i<nvisible; i++) {  /*-- nrows = nnodes --*/
      m = visible[i];
      name = (gchar *) g_array_index (d->rowlab, gchar *, m);
      node = agfindnode (graph, name);
      for (k=0; k<dim; k++)
        pos[i][k] = (gdouble) ND_pos(node)[k]; /* node->u.pos[k];*/
    }
   
  } else if (layout_type == FDP_LAYOUT) {  /* could use fdp_dim */
    gvLayout (gvc, graph, "fdp");

    /*-- add variables generated by the layout algorithm --*/
    for (i=0; i<nvisible; i++) {  /*-- nrows = nnodes --*/
      m = visible[i];
      name = (gchar *) g_array_index (d->rowlab, gchar *, m);
      node = agfindnode (graph, name);
      for (k=0; k<dim; k++)
        pos[i][k] = (gdouble) ND_pos(node)[k]; /* node->u.pos[k];*/
    }

  } else if (layout_type == NEATO_LAYOUT) {
    attrsym_t*  sym;
    gchar *model;

    if (gl->neato_dim > 2) {
        char buf[20];
#ifdef HAVE_CGRAPH
        sym = agattrsym(graph,"dim");
        if (!sym)
          sym = agattr(graph, AGRAPH, "dim", "");
        sprintf (buf, "%d", gl->neato_dim);
        agxset(graph,  sym, buf);
#else
        sym = agfindattr(graph,"dim");
        if (!sym)
          sym = agraphattr(graph,"dim","");
        sprintf (buf, "%d", gl->neato_dim);
        agxset(graph,  sym->index, buf);
#endif
    }
    /* rather than doing the previous, you can take neato_init_graph
     * apart as you originally did. We should probably put in some
     * mechanism for setting Ndim but obviously wouldn't be in 1.16.
     * -- Emden
     */
    /* There is also a subset model */
    if (gl->neato_model == neato_circuit_resistance) {
      model = "circuit";
      modelchar = 'c';
    } else if (gl->neato_model == neato_shortest_path) {
      model = "shortpath";
      modelchar = 's';
    } else {
      model = "subset";
      modelchar = 'b';
    }
    agsafeset (graph, "model", model, "");

    /* In place of MODE_MAJOR, you could use MODE_KK which is the old neato */
    if (gl->neato_use_edge_length_p && weightvar >= 0) {
      neato_apply_edge_length(graph, weightvar, visible, nvisible, d, e, gg);
    }
    gvLayout (gvc, graph, "neato");

    /*-- add variables generated by the layout algorithm --*/
    for (i=0; i<nvisible; i++) {  /*-- nrows = nnodes --*/
      m = visible[i];
      name = (gchar *) g_array_index (d->rowlab, gchar *, m);
      node = agfindnode (graph, name);
      for (k=0; k<gl->neato_dim; k++)
        pos[i][k] = (gdouble) ND_pos(node)[k]; /* node->u.pos[k];*/
      /*-- may want to set u.width, u.height to very small values --*/
      /*-- I see no effect, though ... dfs --*/
#ifdef HAVE_CGRAPH
      ND_width(node) = ND_height(node) = .001;
#else
      node->u.width = node->u.height = .001;
#endif
    }
#ifdef DEBUG
    f = fopen ("test.out", "w");
    agwrite (graph, f);
    fclose(f);
#endif

  } else if (layout_type == CIRCO_LAYOUT) {

    gvLayout (gvc, graph, "circo");

    /*-- add variables generated by the layout algorithm --*/
    for (i=0; i<nvisible; i++) {  /*-- nrows = nnodes --*/
      m = visible[i];
      name = (gchar *) g_array_index (d->rowlab, gchar *, m);
      node = agfindnode (graph, name);
      for (k=0; k<dim; k++)
        pos[i][k] = (gdouble) ND_pos(node)[k]; /* node->u.pos[k];*/
    }
  }

  gvFreeLayout (gvc, graph);
/*
 * create a new datad with the new variables.  include only
 * those nodes that are visible.  these needs some more testing ...
 * and this code could be more efficient -- writing to one set
 * of arrays, then copying to a matrix is probably unnecessary.
*/
  nc = dim;

  rowids = (gchar **) g_malloc (nvisible * sizeof(gchar *));
  for (m=0; m<nvisible; m++) {
    i = visible[m];
    rowids[m] = g_strdup (d->rowIds[i]);
  }

  values = (gdouble *) g_malloc (nvisible * nc * sizeof(gdouble));
  rownames = (gchar **) g_malloc (nvisible * sizeof(gchar *));
  for (i=0; i<nvisible; i++) {
    rownames[i] = (gchar *) g_array_index (d->rowlab, gchar *, visible[i]);
    for (k=0; k<dim; k++)
      values[i + k*nvisible] = (gdouble) pos[i][k];
  }

  colnames = (gchar **) g_malloc (nc * sizeof(gchar *));
  for (k=0; k<dim; k++)
    colnames[k] = g_strdup_printf ("Pos%d", k);

  /*
   * In case there is no initial scatterplot because the datasets
   * have no variables, we don't want creating a datad to trigger
   * the initialization of this plot.   This takes care of it.
  */
  GGOBI_getSessionOptions()->info->createInitialScatterPlot = false;
  /*-- --*/

  dnew = ggobi_data_new (nvisible, nc);
  switch (layout_type) {
    case DOT_LAYOUT:
      dnew->name =  g_strdup ("dot");
      dnew->nickname = g_strdup ("dot");
      break;
    case NEATO_LAYOUT:
      dnew->name = g_strdup_printf ("neato %dd%c", gl->neato_dim, modelchar);
      dnew->nickname = g_strdup_printf ("nto%dd%c", gl->neato_dim, modelchar);
      break;
    case TWOPI_LAYOUT:
      dnew->name =  g_strdup ("twopi");
      dnew->nickname = g_strdup ("twopi");
      break;
    case FDP_LAYOUT:
      dnew->name =  g_strdup ("fdp");
      dnew->nickname = g_strdup ("fdp");
      break;
    case CIRCO_LAYOUT:
      dnew->name =  g_strdup ("circo");
      dnew->nickname = g_strdup ("circo");
      break;
  }

  GGOBI(setData) (values, rownames, colnames, nvisible, nc, dnew, false,
    gg, rowids, false, desc);

  /*-- copy the color and glyph vectors from d to dnew --*/
  for (i=0; i<nvisible; i++) {
    dnew->color.els[i] = dnew->color_now.els[i] = dnew->color_prev.els[i] =
      d->color.els[visible[i]];
    dnew->glyph.els[i].type = dnew->glyph_now.els[i].type =
      dnew->glyph_prev.els[i].type = d->glyph.els[visible[i]].type;
    dnew->glyph.els[i].size = dnew->glyph_now.els[i].size =
      dnew->glyph_prev.els[i].size = d->glyph.els[visible[i]].size;
  }

/*
 * open a new scatterplot with the new data, and display edges
 * as they're displayed in the current datad.
*/ 

  /* dfs -- in imitation of code in display_ui.c, maybe not needed */
  splot_set_current (gg->current_splot, off, gg);   
  /* */

  opts = GGOBI(getDefaultDisplayOptions)();
  opts->axes_show_p = false;
  opts->edges_undirected_show_p = true;
  dspnew = GGOBI(newScatterplot) (0, 1, true, dnew, gg);
  opts->axes_show_p = true;  /*-- restore it --*/
  opts->edges_undirected_show_p = false;

  display_add(dspnew, gg);
  varpanel_refresh(dspnew, gg);

  setDisplayEdge (dspnew, e);

  for (i=0; i<nvisible; i++) {
    g_free (pos[i]);
  }
  g_free (pos);
  g_free (visible);
  agclose (graph);
}

#endif