File: jitter.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 (140 lines) | stat: -rw-r--r-- 3,295 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
/* jitter.c */
/*
 * 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
*/

/*
 * Contains jittering routines for tform; see missing.c for the
 * jittering routines for missing data.
*/

#include <stdlib.h>
#include <math.h>

#include <gtk/gtk.h>
#include "vars.h"
#include "externs.h"

gfloat
jitter_randval (gint type)
{
/*
 * generate a random value.
*/
  gdouble drand;
  static gdouble dsave;
  static gboolean isave = false;

  if (type == UNIFORM) {
    drand = randvalue ();
    /*
     * Center and scale to [-1, 1]
     */
    drand = (drand - .5) * 2;

  }
  else if (type == NORMAL) {

    gboolean check = true;
    gdouble d, dfac;

    if (isave) {
      isave = false;
      /* prepare to return the previously saved value */
      drand = dsave;
    }
    else {
      isave = true;
      while (check) {

        rnorm2 (&drand, &dsave);
        d = drand * drand + dsave * dsave;

        if (d < 1.0) {
          check = false;
          dfac = sqrt (-2. * log (d) / d);
          drand = drand * dfac;
          dsave = dsave * dfac;
        }
      }                         /* end while */
    }                           /* end else */

    /*
     * Already centered; scale to approximately [-1, 1]
     */
    drand = (drand / 3.0);
  }
  return ((gfloat) drand);
}

void
rejitter (gint * selected_cols, gint nselected_cols, GGobiData * d,
          ggobid * gg)
{
  gint i, j, k, m;
  greal frand, fworld, fjit;
  greal precis = (gfloat) PRECISION1;
  vartabled *vt;

  g_assert (d->jitdata.nrows == d->nrows);
  g_assert (d->jitdata.ncols == d->ncols);

  for (j = 0; j < nselected_cols; j++) {
    k = selected_cols[j];
    vt = vartable_element_get (k, d);

    for (i = 0; i < d->nrows_in_plot; i++) {
      m = d->rows_in_plot.els[i];
      /*-- jitter_one_value (m, k); --*/

      frand = (greal) jitter_randval (d->jitter.type) * precis;

      /*
       * The world.vals used here is already jittered:
       * subtract out the previous jittered value ...
       */
      if (d->jitter.convex) {
        fworld = d->world.vals[m][k] - d->jitdata.vals[m][k];
        fjit = (greal) vt->jitter_factor * (frand - fworld);
      }
      else
        fjit = vt->jitter_factor * frand;

      d->jitdata.vals[m][k] = fjit;
    }
  }
  tform_to_world (d, gg);
  displays_tailpipe (FULL, gg);
}


void
jitter_value_set (gfloat value, GGobiData * d, ggobid * gg)
{
  GtkWidget *tree_view =
    get_tree_view_from_object (G_OBJECT (gg->jitter_ui.window));
  gint *vars;                   // = (gint *) g_malloc (d->ncols * sizeof(gint));
  gint nvars;
  gint j;
  vartabled *vt;

  vars = get_selections_from_tree_view (tree_view, &nvars);

  for (j = 0; j < nvars; j++) {
    vt = vartable_element_get (vars[j], d);
    vt->jitter_factor = value;
  }

  g_free ((gpointer) vars);
}