File: texture.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 (362 lines) | stat: -rw-r--r-- 8,770 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
/* texture.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
*/

/*-- generates the data for a textured dotplot --*/

#include <stdlib.h>
#include <stdio.h>
/*#include <limits.h>
#include <float.h>*/
#include <math.h>
#include <gtk/gtk.h>

#include "ggobi.h"
#include "externs.h"

/*
 * The code in this file is taken from code written by Paul Tukey and
 * John Tukey, as described in their paper entitled
 * "Strips Displaying Empirical Distributions:  I.  Textured Dot
 * Strips."   I've converted it to C from ratfor and modfied it so
 * that it returns the texture axis in the order of the original
 * data.
*/

gint myrnd (gint);

/* 
 This variable is used as a temporary global value which
 is used to communicate with qsort and psort since we have no
 provision from textur to pass additional arguments.

 In a multi-threaded version, we would need to protect this.
*/
static ggobid *CurrentGGobi;
gint
psort (const void *arg1, const void *arg2)
{
  ggobid *gg = CurrentGGobi;

  gint val = 0;
  gint *x1 = (gint *) arg1;
  gint *x2 = (gint *) arg2;

  if (gg->p1d.gy[*x1] < gg->p1d.gy[*x2])
    val = -1;
  else if (gg->p1d.gy[*x1] > gg->p1d.gy[*x2])
    val = 1;

  return (val);
}

void
next5 (gint * xlast, gint * perm)
{
/*
 * Extend a 5-string by 5 more numbers:
 * Given a last perm of length 5, choose a next perm, subject to restrictions
 *
 * perms is the list of all 32 possible permutations of (0,1,2,3,4)
 *   with no runs up or down of length 3
 * Note: for every perm in the list, (4 - perm) is also in the list,
 *   symmetrically placed.
*/
  gint i, j, last[5];
  gint nperms = 32;
  static gint cumcnt[5] = { 4, 11, 19, 26, 31 };
  static gint perms[32][5] = {
    {0, 2, 1, 4, 3}, {0, 3, 1, 4, 2}, {0, 3, 2, 4, 1}, {0, 4, 1, 3, 2},
    {0, 4, 2, 3, 1}, {1, 0, 3, 2, 4}, {1, 0, 4, 2, 3}, {1, 2, 0, 4, 3},
    {1, 3, 0, 4, 2}, {1, 3, 2, 4, 0}, {1, 4, 0, 3, 2}, {1, 4, 2, 3, 0},
    {2, 0, 3, 1, 4}, {2, 0, 4, 1, 3}, {2, 1, 3, 0, 4}, {2, 1, 4, 0, 3},
    {2, 3, 0, 4, 1}, {2, 3, 1, 4, 0}, {2, 4, 0, 3, 1}, {2, 4, 1, 3, 0},
    {3, 0, 2, 1, 4}, {3, 0, 4, 1, 2}, {3, 1, 2, 0, 4}, {3, 1, 4, 0, 2},
    {3, 2, 4, 0, 1}, {3, 4, 0, 2, 1}, {3, 4, 1, 2, 0}, {4, 0, 2, 1, 3},
    {4, 0, 3, 1, 2}, {4, 1, 2, 0, 3}, {4, 1, 3, 0, 2}, {4, 2, 3, 0, 1}
  };

  for (i = 0; i < 5; i++)
    last[i] = xlast[i];

  if (last[0] == 0 && last[1] == 0) {
    /*
     * Initialize a new perm by choosing a perm at random
     */
    j = myrnd (nperms) - 1;
    for (i = 0; i < 5; i++)
      last[i] = perms[j][i];
  }
/*
 *  Randomly choose a permutation perm(1-5) from among those
 *  that do not start with the previous digit and that make a
 *  transition which reverses the previous direction.
*/

  if (last[3] < last[4]) {
    j = myrnd (cumcnt[last[4]]) - 1;
    for (i = 0; i < 5; i++)
      perm[i] = perms[j][i];
  }
  else {
    j = myrnd (cumcnt[3 - last[4]]) - 1;
    for (i = 0; i < 5; i++)
      perm[i] = 4 - perms[j][i];
  }
  return;
}


void
next25 (gint * tt, gint * bigt, gint * smallt)
{
/*
 *  Calculate the next 25 values of a 2nd-stage shift vector
 *   by interleaving five 5-strings
*/
  gint i, j, k;

  if (bigt[0] == 0 && bigt[1] == 0) {
    /*
     * Force initialization
     */
    bigt[20] = 0;
    bigt[21] = 0;
    for (i = 0; i < 25; i++)
      smallt[i] = 0;
  }

  /*
   * Get next 25 elements of bigt, 5 at a time
   */
  next5 (&bigt[20], bigt);
  for (j = 5; j < 21; j = j + 5)
    next5 (&bigt[j - 5], &bigt[j]);

  /*
   * Extend each of the smallt series by 5
   */
  for (j = 0; j < 21; j = j + 5)
    next5 (&smallt[j], &smallt[j]);

  /*
   * Interleave the smallt series according to bigt
   */
  for (i = 0; i < 5; i++)
    for (j = 0; j < 5; j++) {
      k = 5 * i + j;
      tt[k] = smallt[i + 5 * bigt[k]];
    }

  return;
}

void
textur (gfloat * yy, gfloat * shft, gint ny, gint option, gfloat del,
        gint stages, ggobid * gg)
{
/*
 * Calculate a texturing shft vector based on data yy
 * Note: data vector yy is returned sorted
 *
 * Return shft resorted into the original order of yy.
 * Use the default values for these arguments, as follow:
 * option=1, del=1.0, stages=3
 *
 * A bit of work is needed if we want to use option=2.
*/
  gfloat lohnge, hihnge, delta;
  gfloat srnge, slo, shi;
  gint nny, window, mid, k, h, kk, hh;
  gint tmp5x5[25];
  gint tlarge[25], tsmall[25];
  gint i, ii;
  gint *indx;
  gfloat *xx;


/*
 * Force initialization on first calls to next5.
*/
  for (i = 0; i < 2; i++)
    tlarge[i] = tsmall[i] = 0;

  indx = (gint *) g_malloc (ny * sizeof (gint));
/*
 * gy is needed solely for the psort routine:  psort is used by
 * qsort to put an index vector in the order that yy will assume.
*/
  gg->p1d.gy = (gfloat *) g_malloc (ny * sizeof (gfloat));
  xx = (gfloat *) g_malloc (ny * sizeof (gfloat));

  for (i = 0; i < ny; i++) {
    indx[i] = i;
    gg->p1d.gy[i] = yy[i];
  }

  CurrentGGobi = gg;

  qsort ((void *) indx, (gsize) ny, sizeof (gint), psort);
  qsort ((void *) yy, (gsize) ny, sizeof (gfloat), fcompare);
  CurrentGGobi = NULL;
/*
 * Bug here:  this is screwy if ny < 4.
*/
  lohnge = yy[ny / 4 - 1];
  hihnge = yy[ny - ny / 4 - 1];
  delta = del * .03 * (hihnge - lohnge);

/*
 *  Do the first two stages of shift, based on 5-strings
*/
  nny = ny;
/*
 *  if( option == 2 )
 *    nny = MIN(nny, 50);
*/

  for (i = 0; i < nny; i++) {
    ii = (i % 25);
    if (ii == 0)
      next25 (tsmall, tlarge, tmp5x5);

    if (stages >= 2)
      shft[i] = (gfloat) (20 * tlarge[ii] + 4 * tsmall[ii]) + 2;
    else
      shft[i] = (gfloat) (20 * tlarge[ii]) + 2;

    /*
     *  Note: we use the same tlarge 5-string both for gross shift
     *  and to interleave the 2nd-stage 5-strings.
     */
  }

  if (stages <= 1) {
    g_free ((gpointer) indx);
    g_free ((gpointer) gg->p1d.gy);
    g_free ((gpointer) xx);
    return;
  }

/*
 * Optionally, add a tiny bit of uniform jitter on the smallest scale
*/
  if (option == 1) {
    for (i = 0; i < ny; i++) {
      shft[i] = shft[i] + ((gfloat) randvalue ()) * 4 - 2;
    }
  }

/*
 * Optionally, repeat first block of 50, shifting the first 25 by a bit
 *  else if (option == 2)
 *  {
 *    for (i=0; i<25 && i<ny; i++)
 *      shft[i] = shft[i] - 2;
 *    for(i=50; i<ny; i++)
 *      shft[i] = shft[i-50];
 *  }
*/

/*
 * Now look at y values, and stretch bunched-up sections out to the edges
*/
  window = 5;
  mid = (window + 1) / 2;
  h = mid;
  while (h - mid + window < ny) {
    /*for(k = h; k-mid+window < ny; k = k+window) *//* bug */
    for (k = h; k - mid + window < ny && k + window < ny; k = k + window)
      if (yy[k + window] > yy[h] + 10. * delta)
        break;
    if (k - mid + window >= ny)
      break;
    hh = h - mid;
    kk = k - h + window;
    slo = 5;
    shi = 0;
    for (i = hh; i < hh + kk; i++) {
      if (shft[i] < slo)
        slo = shft[i];
      if (shft[i] > shi)
        shi = shft[i];
    }
    srnge = shi - slo;
    for (i = hh; i < kk; i++)
      shft[i] = 100 * (shft[i] - slo) / srnge;
    h = k + window;
  }


/*
 * Again looking at y values, pull points back to center thread,
 * or to the 30% and 70% positions, in sparse regions
*/
  for (i = 1; i < ny - 1; i++) {
    if ((yy[i] - yy[i - 1] > delta) && (yy[i + 1] - yy[i] > delta)) {
      shft[i] = 50;
    }
  }

  for (i = 1; i < ny - 2; i++) {
    if ((yy[i] - yy[i - 1] > delta) &&
        (yy[i + 2] - yy[i + 1] > delta) && (yy[i + 1] - yy[i] < delta)) {
      shft[i] = 30;
      shft[i + 1] = 70;
    }
  }


  if (yy[1] - yy[0] > delta)
    shft[0] = 50;
  if (yy[ny - 1] - yy[ny - 2] > delta)
    shft[ny - 1] = 50;

  if ((yy[2] - yy[1] > delta) && (yy[1] - yy[0] < delta)) {
    shft[0] = 30;
    shft[1] = 70;
  }
  if ((yy[ny - 1] - yy[ny - 2] < delta) && (yy[ny - 2] - yy[ny - 3] > delta)) {
    shft[ny - 2] = 30;
    shft[ny - 1] = 70;
  }

  for (i = 0; i < ny; i++)
    xx[indx[i]] = shft[i];

  for (i = 0; i < ny; i++)
    shft[i] = xx[i];

  g_free ((gpointer) indx);
  g_free ((gpointer) gg->p1d.gy);
  g_free ((gpointer) xx);

  return;
}

gint
myrnd (gint n)
{
/*
 * Select a random integer between 1 and n
*/
  gint nn, myrndval;
  gfloat rrand;
  nn = MAX (n, 1);

  rrand = (gfloat) randvalue ();
  myrndval = MIN (nn, (gint) (rrand * (gfloat) nn) + 1);
  return (myrndval);
}