File: sp_plot_edges.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (494 lines) | stat: -rw-r--r-- 16,705 bytes parent folder | download | duplicates (4)
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
/* sp_plot_edges.c: routines for drawing edges */
/*
 * ggobi
 * Copyright (C) AT&T, Duncan Temple Lang, Dianne Cook 1999-2005
 *
 * ggobi is free software; you may use, redistribute, and/or modify it
 * under the terms of the Common Public License, which is distributed
 * with the source code and displayed on the ggobi web site, 
 * www.ggobi.org.  For more information, contact the authors:
 *
 *   Deborah F. Swayne   dfs@research.att.com
 *   Di Cook             dicook@iastate.edu
 *   Duncan Temple Lang  duncan@wald.ucdavis.edu
 *   Andreas Buja        andreas.buja@wharton.upenn.edu
*/

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "vars.h"
#include "externs.h"
#include "colorscheme.h"

/*------------------------------------------------------------------------*/
/*                   line drawing routines                                */
/*------------------------------------------------------------------------*/

gboolean
splot_plot_edge (gint m, GGobiData * d, GGobiData * e,
                 splotd * sp, displayd * display, ggobid * gg)
{
  gint a, b;
  gboolean draw_edge;
  endpointsd *endpoints;

  endpoints = resolveEdgePoints (e, d);
  if (!endpoints)
    return (false);
  draw_edge = edge_endpoints_get (m, &a, &b, d, endpoints, e);

  if (!draw_edge)
    return (false);

  /*-- determine whether edge m should be plotted --*/
  /*-- usually checking sampled is redundant because we're looping
       over rows_in_plot, but maybe we're not always --*/

  if (e->excluded.els[m] || !e->sampled.els[m])
    draw_edge = false;

  else if (!splot_plot_case (a, d, sp, display, gg) ||
           !splot_plot_case (b, d, sp, display, gg))
    draw_edge = false;

  /*-- can prevent drawing of missings for parcoords or scatmat plots --*/
  else if (ggobi_data_has_missings(e) && !e->missings_show_p) {
    if (GGOBI_IS_EXTENDED_SPLOT (sp)) {
      GGobiExtendedSPlotClass *klass;
      klass = GGOBI_EXTENDED_SPLOT_GET_CLASS (sp);
      if (klass->draw_edge_p) {
        draw_edge = klass->draw_edge_p (sp, m, d, e, gg);
      }
    }
  }
  return draw_edge;
}

gboolean
splot_hidden_edge (gint m, GGobiData * d, GGobiData * e,
                   splotd * sp, displayd * display, ggobid * gg)
{
  gint a, b;
  gboolean hiddenp = false;
  endpointsd *endpoints;

  endpoints = resolveEdgePoints (e, d);
  if (endpoints && edge_endpoints_get (m, &a, &b, d, endpoints, e))
    if (e->hidden_now.els[m] || d->hidden_now.els[a] || d->hidden_now.els[b])
      hiddenp = true;

  /*-- can prevent drawing of missings for parcoords or scatmat plots --*/
/*
  } else if (ggobi_data_has_missings(e) && !e->missings_show_p) {
    if (GGOBI_IS_EXTENDED_SPLOT(sp)) {
      GGobiExtendedSPlotClass *klass;
      klass = GGOBI_EXTENDED_SPLOT_CLASS(GTK_OBJECT(sp)->klass);
      if (klass->draw_edge_p) {
        draw_edge = klass->draw_edge_p(sp, m, d, e, gg);
      }
    }
  }
*/
  return hiddenp;
}

/*-- the current color and line type need to be drawn last --*/
void
splot_edges_draw (splotd * sp, gboolean draw_hidden, GdkDrawable * drawable,
                  ggobid * gg)
{
  gint i, j, m;
  gint k, n, p, pp;
  gint a, b;
  displayd *display = (displayd *) sp->displayptr;
  GGobiData *d = display->d;
  GGobiData *e = display->e;
  endpointsd *endpoints;
  gboolean edges_show_p, arrowheads_show_p;
  gint lwidth, ltype;
  GlyphType gtype;
  colorschemed *scheme = gg->activeColorScheme;

  if (e == (GGobiData *) NULL || e->edge.n == 0) {
     /**/ return;
  }

  if (d->idTable == NULL) {  /*-- d has no record ids --*/
     /**/ return;
  }

  edges_show_p = (display->options.edges_directed_show_p ||
                  display->options.edges_undirected_show_p);
  arrowheads_show_p = (display->options.edges_directed_show_p ||
                       display->options.edges_arrowheads_show_p);
  if (!gg->mono_p && (edges_show_p || arrowheads_show_p)) {

    gint symbols_used[NGLYPHSIZES][NEDGETYPES][MAXNCOLORS];
    gint nl = 0;
    gint ncolors = MIN (MAXNCOLORS, scheme->n);
    gint k_prev = -1, n_prev = -1, p_prev = -1;

    g_assert (e->color.nels == e->nrows);

    endpoints = resolveEdgePoints (e, d);
    if (!endpoints)
      return;

    for (k = 0; k < NGLYPHSIZES; k++)
      for (n = 0; n < NEDGETYPES; n++)
        for (p = 0; p < ncolors; p++)
          symbols_used[k][n][p] = 0;

    /*
     * Use e->color_now[] and e->glyph_now[] to work out which
     * line symbols should be drawn
     */
    for (i = 0; i < e->nrows_in_plot; i++) {
      m = e->rows_in_plot.els[i];
      /* If we're drawing hiddens and this is hidden and plottable ... */
      if (((draw_hidden && splot_hidden_edge (m, d, e, sp, display, gg)) ||
           /* Or if we're not drawing hiddens and this isn't hidden ... */
           (!draw_hidden && !e->hidden_now.els[m]))) {

        gtype = e->glyph_now.els[m].type;
        ltype = ltype_from_gtype (gtype);
        /*
           if (gtype == FC || gtype == FR)
           ltype = SOLID;
           else if (gtype == OC || gtype == OR)
           ltype = WIDE_DASH;
           else ltype = NARROW_DASH;
         */
        symbols_used[e->glyph_now.els[m].size][ltype][e->color_now.els[m]]++;
      }
    }


    if (draw_hidden)
      gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_hidden);

    for (k = 0; k < NGLYPHSIZES; k++) {
      for (n = 0; n < NEDGETYPES; n++) {
        for (pp = 0; pp < ncolors + 1; pp++) {
          /* 
           * This is a little trick to draw the edges using the
           * current color last.  I skip over them the first time
           * through, and pick them up at the end.  It shouldn't
           * cost me anything, I don't think.  Should I do
           * the same for glyphs?     -- dfs, 4/2004
           */
          if (pp < ncolors) {
            if (pp == gg->color_id)
              continue;
            else
              p = pp;
          }
          else
            p = gg->color_id;

          if (symbols_used[k][n][p]) {

            /*
             * Now work through through symbols_used[], plotting the edges
             * of each color and type in a group.
             */
            nl = 0;

            for (j = 0; j < e->edge.n; j++) {

              if (draw_hidden
                  && !splot_hidden_edge (j, d, e, sp, display, gg))
                continue;
              if (!draw_hidden
                  && splot_hidden_edge (j, d, e, sp, display, gg))
                continue;
              if (!splot_plot_edge (j, d, e, sp, display, gg))
                continue;

              edge_endpoints_get (j, &a, &b, d, endpoints, e);

              gtype = e->glyph_now.els[j].type;
              ltype = ltype_from_gtype (gtype);
              /*
                 if (gtype == FC || gtype == FR)
                 ltype = SOLID;
                 else if (gtype == OC || gtype == OR)
                 ltype = WIDE_DASH;
                 else ltype = NARROW_DASH;
               */

              if (e->color_now.els[j] == p &&
                  ltype == n && e->glyph_now.els[j].size == k) {
                if (edges_show_p) {
                  if (endpoints[j].jpartner == -1) {
                    sp->edges[nl].x1 = sp->screen[a].x;
                    sp->edges[nl].y1 = sp->screen[a].y;
                    sp->edges[nl].x2 = sp->screen[b].x;
                    sp->edges[nl].y2 = sp->screen[b].y;
                  }
                  else {
                    sp->edges[nl].x1 = sp->screen[a].x;
                    sp->edges[nl].y1 = sp->screen[a].y;
                    sp->edges[nl].x2 = sp->screen[a].x +
                      (sp->screen[b].x - sp->screen[a].x) / 2;
                    sp->edges[nl].y2 = sp->screen[a].y +
                      (sp->screen[b].y - sp->screen[a].y) / 2;
                  }
                }

                if (arrowheads_show_p) {
                  /*
                   * Add thick piece of the lines to suggest a
                   * directional arrow.  How thick should it be
                   * compared to the current line thickness?
                   */
                  if (endpoints[j].jpartner == -1) {  /* not bidirectional */
                    sp->arrowheads[nl].x1 =
                      (gint) (.2 * sp->screen[a].x + .8 * sp->screen[b].x);
                    sp->arrowheads[nl].y1 =
                      (gint) (.2 * sp->screen[a].y + .8 * sp->screen[b].y);
                    sp->arrowheads[nl].x2 = sp->screen[b].x;
                    sp->arrowheads[nl].y2 = sp->screen[b].y;
                  }
                  else {    /*-- draw the partner's arrowhead --*/
                    gint ja, jb, jp = endpoints[j].jpartner;
                    edge_endpoints_get (jp, &ja, &jb, d, endpoints, e);

                    sp->arrowheads[nl].x1 =
                      (gint) (.2 * sp->screen[ja].x + .8 * sp->screen[jb].x);
                    sp->arrowheads[nl].y1 =
                      (gint) (.2 * sp->screen[ja].y + .8 * sp->screen[jb].y);
                    sp->arrowheads[nl].x2 = sp->screen[jb].x;
                    sp->arrowheads[nl].y2 = sp->screen[jb].y;
                  }
                }
                nl++;
              }
            }  /*-- end of looping through edges --*/

            if (!draw_hidden) {
              if (p_prev == -1 || p_prev != p) {  /* color */
                gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb[p]);
              }
            }

            lwidth = lwidth_from_gsize (k);
            if (edges_show_p) {
              ltype = set_lattribute_from_ltype (n, gg);

              if (k_prev == -1 || k_prev != i || n_prev == -1 || n_prev != n) {
                gdk_gc_set_line_attributes (gg->plot_GC, lwidth,
                                            (gint) ltype, GDK_CAP_BUTT,
                                            GDK_JOIN_ROUND);
              }

              gdk_draw_segments (drawable, gg->plot_GC, sp->edges, nl);
            }

            if (arrowheads_show_p) {
              gdk_gc_set_line_attributes (gg->plot_GC,
                                          lwidth + 2, GDK_LINE_SOLID,
                                          GDK_CAP_ROUND, GDK_JOIN_ROUND);
              gdk_draw_segments (drawable, gg->plot_GC, sp->arrowheads, nl);
              gdk_gc_set_line_attributes (gg->plot_GC,
                                          0, GDK_LINE_SOLID, GDK_CAP_ROUND,
                                          GDK_JOIN_ROUND);
            }
          }

          /* 
           * I thought this should be one bracket earlier, but I'm
           * having some kind of intermittent problem with brushing
           * edges after I have added them using the GUI.  This may
           * fix the problem, which may have been a result of my
           * skipping over the current points --- though I don't see
           * why.  dfs 5/2004
           */
          k_prev = k;
          n_prev = n;
          p_prev = p;
        }
      }
    }
  }
  gdk_gc_set_line_attributes (gg->plot_GC,
                              0, GDK_LINE_SOLID, GDK_CAP_ROUND,
                              GDK_JOIN_ROUND);
}

void
splot_add_edge_highlight_cue (splotd * sp, GdkDrawable * drawable, gint k,
                              gboolean nearest, ggobid * gg)
{
  displayd *dsp = (displayd *) sp->displayptr;
  GGobiData *d = dsp->d;
  GGobiData *e = dsp->e;
  gint a, b;
  endpointsd *endpoints;
  colorschemed *scheme = gg->activeColorScheme;
  gboolean draw_edge = (dsp->options.edges_undirected_show_p ||
                        dsp->options.edges_directed_show_p);

  endpoints = resolveEdgePoints (e, d);
  if (!endpoints)
    return;
  draw_edge = draw_edge && edge_endpoints_get (k, &a, &b, d, endpoints, e);

/*
 * How to distinguish between sticky and nearest edges?
*/
  /*-- draw a thickened line only for nearest --*/
  if (nearest && draw_edge) {
    gdk_gc_set_line_attributes (gg->plot_GC,
                                3, GDK_LINE_SOLID, GDK_CAP_ROUND,
                                GDK_JOIN_ROUND);
    gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb[e->color_now.els[k]]);

    if (endpoints[k].jpartner == -1) {
      gdk_draw_line (drawable, gg->plot_GC,
                     sp->screen[a].x, sp->screen[a].y,
                     sp->screen[b].x, sp->screen[b].y);
    }
    else {                      /* thicken only half the line */
      gdk_draw_line (drawable, gg->plot_GC,
                     sp->screen[a].x, sp->screen[a].y,
                     sp->screen[a].x + (sp->screen[b].x -
                                        sp->screen[a].x) / 2,
                     sp->screen[a].y + (sp->screen[b].y -
                                        sp->screen[a].y) / 2);
    }

    gdk_gc_set_line_attributes (gg->plot_GC,
                                0, GDK_LINE_SOLID, GDK_CAP_ROUND,
                                GDK_JOIN_ROUND);
    gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_accent);
  }
}

void
splot_add_edge_label (splotd * sp, GdkDrawable * drawable, gint k,
                      gboolean nearest, ggobid * gg)
{
  gchar *lbl;
  displayd *dsp = (displayd *) sp->displayptr;
  GGobiData *d = dsp->d;
  GGobiData *e = dsp->e;
  gint xp, yp;
  gint a, b;
  endpointsd *endpoints;
  PangoLayout *layout = gtk_widget_create_pango_layout (sp->da, NULL);
  PangoRectangle rect;

  gboolean draw_edge = (dsp->options.edges_undirected_show_p ||
                        dsp->options.edges_directed_show_p);

  endpoints = resolveEdgePoints (e, d);
  if (!endpoints)
    return;
  draw_edge = draw_edge && edge_endpoints_get (k, &a, &b, d, endpoints, e);

  /*-- If the edge is bidirectional, use both labels --*/

  if (draw_edge) {

    /*-- add the label last so it will be in front of other markings --*/

    /* for edge labels, this is not the appropriate cpanel ... */
    lbl = identify_label_fetch (k, &dsp->cpanel, e, gg);
    layout_text (layout, lbl, &rect);

    if (sp->screen[a].x > sp->screen[b].x) {
      gint itmp = b;
      b = a;
      a = itmp;
    }
    xp = (sp->screen[b].x - sp->screen[a].x) / 2 + sp->screen[a].x;
    if (sp->screen[a].y > sp->screen[b].y) {
      gint itmp = b;
      b = a;
      a = itmp;
    }
    yp =
      (sp->screen[b].y - sp->screen[a].y) / 2 + sp->screen[a].y - rect.height;

    if (nearest) {
      underline_text (layout);
      gdk_draw_layout (drawable, gg->plot_GC,
                       (sp->max.x - rect.width) / 2, 5, layout);
    }
    gdk_draw_layout (drawable, gg->plot_GC, xp, yp, layout);
  }
}

void
splot_add_identify_edge_cues (splotd * sp, GdkDrawable * drawable, gint k,
                              gboolean nearest, ggobid * gg)
{
  displayd *dsp = (displayd *) sp->displayptr;
  GGobiData *e = dsp->e;
  gboolean useDefault = false;

  if (k >= e->edge.n)
    return;

  if (e->hidden_now.els[k])
    return;

  if (GGOBI_IS_EXTENDED_SPLOT (sp)) {
    GGobiExtendedSPlotClass *klass;
    klass = GGOBI_EXTENDED_SPLOT_GET_CLASS (sp);
    if (klass->add_identify_edge_cues)
      klass->add_identify_edge_cues (k, sp, drawable, nearest, gg);
    else
      useDefault = true;
  }

  if (useDefault) {
    splot_add_edge_highlight_cue (sp, drawable, k, nearest, gg);
    splot_add_edge_label (sp, drawable, k, nearest, gg);
  }
}

void
splot_add_edgeedit_cues (splotd * sp, GdkDrawable * drawable,
                         gint k, gboolean nearest, ggobid * gg)
{
  displayd *display = sp->displayptr;
  cpaneld *cpanel = &display->cpanel;
  colorschemed *scheme = gg->activeColorScheme;
  gint lwidth;
  gint size = gg->glyph_id.size;

  /*-- just rely on the cursor for adding points; no other markup --*/

  if (cpanel->ee_mode == ADDING_EDGES) {
    if (k != -1)
      splot_add_diamond_cue (k, sp, drawable, gg);

    if (gg->edgeedit.a != -1)
      splot_add_diamond_cue (gg->edgeedit.a, sp, drawable, gg);

    if (gg->buttondown && gg->edgeedit.a != -1 &&
        k != -1 && k != gg->edgeedit.a) {

      lwidth = lwidth_from_gsize (size);  // Not sure gsize is relevant.
      gdk_gc_set_line_attributes (gg->plot_GC, lwidth,
                                  GDK_LINE_SOLID, GDK_CAP_BUTT,
                                  GDK_JOIN_ROUND);

      /* This isn't really the color I want to use, but I don't know
         how to get at the color of the endpoints here. */
      gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb[gg->color_id]);
      gdk_draw_line (drawable, gg->plot_GC,
                     sp->screen[gg->edgeedit.a].x,
                     sp->screen[gg->edgeedit.a].y, sp->screen[k].x,
                     sp->screen[k].y);
    }
/*  not ready to support deleting
  else if (cpanel->ee_deleting_p)
*/
  }
}