File: tour1d.c

package info (click to toggle)
ggobi 2.1.10-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,080 kB
  • sloc: ansic: 53,256; xml: 28,411; sh: 11,700; makefile: 257; sed: 16
file content (1075 lines) | stat: -rw-r--r-- 30,548 bytes parent folder | download | duplicates (3)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
/* tour1d.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
*/

#include <gtk/gtk.h>
#ifdef USE_STRINGS_H
#include <strings.h>
#endif

#define __USE_ISOC99
#include <math.h>
#include <stdlib.h>
#include <unistd.h>

#include "vars.h"
#include "externs.h"

#include "tour_pp.h"
#include "tour1d_pp.h"

/* */

#define T1DON true
#define T1DOFF false

static void tour1d_speed_set_display(gfloat slidepos, displayd *dsp); 
void tour1d_write_video(ggobid *gg);

#ifdef TESTING_TOUR_STEP
void tour1d_step_cb(displayd *dsp, tour td, gint projdim, ggobid *gg,
  void *display)
{
  g_printerr ("tour_step\n");
}
#endif


void
display_tour1d_init_null (displayd *dsp, ggobid *gg)
{
  arrayd_init_null(&dsp->t1d.Fa);
  arrayd_init_null(&dsp->t1d.Fz);
  arrayd_init_null(&dsp->t1d.F);

  arrayd_init_null(&dsp->t1d.Ga);
  arrayd_init_null(&dsp->t1d.Gz);
  arrayd_init_null(&dsp->t1d.G);

  arrayd_init_null(&dsp->t1d.Va);
  arrayd_init_null(&dsp->t1d.Vz);

  arrayd_init_null(&dsp->t1d.tv);

  vectori_init_null(&dsp->t1d.subset_vars);
  vectorb_init_null(&dsp->t1d.subset_vars_p);
  vectori_init_null(&dsp->t1d.active_vars);
  vectorb_init_null(&dsp->t1d.active_vars_p);

  vectorf_init_null(&dsp->t1d.lambda);
  vectorf_init_null(&dsp->t1d.tau);
  vectorf_init_null(&dsp->t1d.tinc);

  /* manipulation controls */
  arrayd_init_null(&dsp->t1d_manbasis);
}

void
alloc_tour1d (displayd *dsp, ggobid *gg)
{
  GGobiData *d = dsp->d;
  gint nc = d->ncols;

  arrayd_alloc(&dsp->t1d.Fa, 1, nc);
  arrayd_alloc(&dsp->t1d.Fz, 1, nc);
  arrayd_alloc(&dsp->t1d.F, 1, nc);

  arrayd_alloc(&dsp->t1d.Ga, 1, nc);
  arrayd_alloc(&dsp->t1d.Gz, 1, nc);
  arrayd_alloc(&dsp->t1d.G, 1, nc);

  arrayd_alloc(&dsp->t1d.Va, 1, nc);
  arrayd_alloc(&dsp->t1d.Vz, 1, nc);

  arrayd_alloc(&dsp->t1d.tv, 1, nc);

  vectori_alloc(&dsp->t1d.subset_vars, nc);
  vectorb_alloc_zero(&dsp->t1d.subset_vars_p, nc);
  vectori_alloc(&dsp->t1d.active_vars, nc);
  vectorb_alloc_zero(&dsp->t1d.active_vars_p, nc);

  vectorf_alloc(&dsp->t1d.lambda, nc);
  vectorf_alloc(&dsp->t1d.tau, nc);
  vectorf_alloc(&dsp->t1d.tinc, nc);

  /* manipulation controls */
  arrayd_alloc(&dsp->t1d_manbasis, 2, nc);
}

/*-- eliminate the nc columns contained in *cols --*/
void
tour1d_realloc_down (gint nc, gint *cols, GGobiData *d, ggobid *gg)
{
  displayd *dsp;
  GList *l;
  for (l=gg->displays; l; l=l->next) {
    dsp = (displayd *) l->data;
    if (dsp->d == d) {
      arrayd_delete_cols (&dsp->t1d.Fa, nc, cols);
      arrayd_delete_cols (&dsp->t1d.Fz, nc, cols);
      arrayd_delete_cols (&dsp->t1d.F, nc, cols);
      arrayd_delete_cols (&dsp->t1d.Ga, nc, cols);
      arrayd_delete_cols (&dsp->t1d.Gz, nc, cols);
      arrayd_delete_cols (&dsp->t1d.G, nc, cols);
      arrayd_delete_cols (&dsp->t1d.Va, nc, cols);
      arrayd_delete_cols (&dsp->t1d.Vz, nc, cols);
      arrayd_delete_cols (&dsp->t1d.tv, nc, cols);

      vectori_delete_els (&dsp->t1d.subset_vars, nc, cols);
      vectorb_delete_els (&dsp->t1d.subset_vars_p, nc, cols);
      vectori_delete_els (&dsp->t1d.active_vars, nc, cols);
      vectorb_delete_els (&dsp->t1d.active_vars_p, nc, cols);

      vectorf_delete_els (&dsp->t1d.lambda, nc, cols);
      vectorf_delete_els (&dsp->t1d.tau, nc, cols);
      vectorf_delete_els (&dsp->t1d.tinc, nc, cols);

      arrayd_delete_cols (&dsp->t1d_manbasis, (gint) nc, cols);
    }
  }
}

void
free_tour1d(displayd *dsp)
{
  vectori_free(&dsp->t1d.subset_vars);
  vectorb_free(&dsp->t1d.subset_vars_p);
  vectori_free(&dsp->t1d.active_vars);
  vectorb_free(&dsp->t1d.active_vars_p);

  vectorf_free(&dsp->t1d.lambda);
  vectorf_free(&dsp->t1d.tau);
  vectorf_free(&dsp->t1d.tinc);

  arrayd_free(&dsp->t1d.Fa, 0, 0);
  arrayd_free(&dsp->t1d.Fz, 0, 0);
  arrayd_free(&dsp->t1d.F, 0, 0);

  arrayd_free(&dsp->t1d.Ga, 0, 0);
  arrayd_free(&dsp->t1d.Gz, 0, 0);
  arrayd_free(&dsp->t1d.F, 0, 0);

  arrayd_free(&dsp->t1d.Va, 0, 0);
  arrayd_free(&dsp->t1d.Vz, 0, 0);
  arrayd_free(&dsp->t1d.tv, 0, 0);

  arrayd_free(&dsp->t1d_manbasis, 0, 0);
}

void tour1d_snap(ggobid *gg)
{
  displayd *dsp = gg->current_display;
  splotd *sp = gg->current_splot;
  GGobiData *d = dsp->d;
  gint j;
  gdouble rnge;
  vartabled *vt;

  for (j=0; j<d->ncols; j++) {
    vt = vartable_element_get (j, d);
    rnge = vt->lim.max - vt->lim.min;
    fprintf(stdout,"%f %f \n", dsp->t1d.F.vals[0][j], 
      dsp->t1d.F.vals[0][j]/rnge*sp->scale.x);
  }
}

void tour1d_video(ggobid *gg)
{
  displayd *dsp = gg->current_display;
  if (dsp == NULL)
    return;

  dsp->t1d_video = !dsp->t1d_video;
}

void tour1d_write_video(ggobid *gg) 
{
  displayd *dsp = gg->current_display;
  splotd *sp = gg->current_splot;
  GGobiData *d = dsp->d;
  gint j;
  gdouble rnge;
  vartabled *vt;
  gfloat ppval;

  if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window))
    ppval = dsp->t1d.ppval;
  else
    ppval = 0.;
  for (j=0; j<d->ncols; j++) {
    vt = vartable_element_get (j, d);
    rnge = vt->lim.max - vt->lim.min;
    fprintf(stdout,"%f %f %f \n", dsp->t1d.F.vals[0][j], 
      dsp->t1d.F.vals[0][j]/rnge*sp->scale.x, ppval);
  }
}

void 
display_tour1d_init (displayd *dsp, ggobid *gg) 
{
  gint i, j;
  GGobiData *d = dsp->d;
  cpaneld *cpanel = &dsp->cpanel;
  gint nc = d->ncols;

  alloc_tour1d(dsp, gg);

    /* Initialize starting subset of active variables */
  if (nc < 8) {
    dsp->t1d.nsubset = dsp->t1d.nactive = nc;
    for (j=0; j<nc; j++) {
      dsp->t1d.subset_vars.els[j] = dsp->t1d.active_vars.els[j] = j;
      dsp->t1d.subset_vars_p.els[j] = dsp->t1d.active_vars_p.els[j] = true;
    }
  }
  else {
    dsp->t1d.nsubset = dsp->t1d.nactive = 3;
    for (j=0; j<3; j++) {
      dsp->t1d.subset_vars.els[j] = dsp->t1d.active_vars.els[j] = j;
      dsp->t1d.subset_vars_p.els[j] = dsp->t1d.active_vars_p.els[j] = true;
    }
    for (j=3; j<nc; j++) {
      dsp->t1d.subset_vars.els[j] = dsp->t1d.active_vars.els[j] = 0;
      dsp->t1d.subset_vars_p.els[j] = dsp->t1d.active_vars_p.els[j] = false;
    }

  }

  /* declare starting base as first p chosen variables */
  for (i=0; i<1; i++)
    for (j=0; j<nc; j++)
      dsp->t1d.Fa.vals[i][j] = dsp->t1d.Fz.vals[i][j] = 
        dsp->t1d.F.vals[i][j] = 
        dsp->t1d.Ga.vals[i][j] = dsp->t1d.Gz.vals[i][j] = 0.0;

  for (i=0; i<1; i++)
  {
    dsp->t1d.Fz.vals[i][dsp->t1d.active_vars.els[i]] =
      dsp->t1d.Fa.vals[i][dsp->t1d.active_vars.els[i]] = 
      dsp->t1d.F.vals[i][dsp->t1d.active_vars.els[i]] =
      dsp->t1d.Ga.vals[i][dsp->t1d.active_vars.els[i]] = 
      dsp->t1d.Gz.vals[i][dsp->t1d.active_vars.els[i]] = 1.0;
  }

  dsp->t1d.dist_az = 0.0;
  dsp->t1d.delta = cpanel->t1d.step*M_PI_2/10.0;
  dsp->t1d.tang = 0.0;

  dsp->t1d.idled = 0;
  dsp->t1d.get_new_target = true;

  dsp->t1d_video = false;

  /* manip */
  dsp->t1d_manip_var = 0;

  /* pp */
  dsp->t1d.target_selection_method = TOUR_RANDOM;
  dsp->t1d_ppda = NULL;
  dsp->t1d_axes = true;

  dsp->t1d_pp_op.temp_start = 1.0;
  dsp->t1d_pp_op.cooling = 0.99;

  tour1d_speed_set_display(sessionOptions->defaultTour1dSpeed, dsp);
}

void
tour1d_fade_vars (gboolean fade, ggobid *gg) 
{
  gg->tour1d.fade_vars = fade;
}

void
tour1d_all_vars (displayd *dsp) 
{
  ggobid *gg = dsp->ggobi;
  GGobiData *d = dsp->d;
  gint j;

  //gg->tour1d.all_vars = !gg->tour1d.all_vars;

  //if (gg->tour1d.all_vars)
  //{
    for (j=0; j<d->ncols; j++) {
      dsp->t1d.subset_vars.els[j] = j;
      dsp->t1d.active_vars.els[j] = j;
      dsp->t1d.subset_vars_p.els[j] = true;
      dsp->t1d.active_vars_p.els[j] = true;
    }
    dsp->t1d.nsubset = d->ncols;
    dsp->t1d.nactive = d->ncols;
    dsp->t1d.get_new_target = true;
    zero_tau(dsp->t1d.tau, 1);
    varcircles_visibility_set (dsp, gg);
    varpanel_refresh (dsp, gg);

    if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
      free_optimize0_p(&dsp->t1d_pp_op);
      alloc_optimize0_p(&dsp->t1d_pp_op, d->nrows_in_plot, dsp->t1d.nactive, 
        1);
      free_pp(&dsp->t1d_pp_param);
      alloc_pp(&dsp->t1d_pp_param, d->nrows_in_plot, dsp->t1d.nactive, 1);
      t1d_pp_reinit(dsp, gg);
    }  
  //}
}

static void 
tour1d_speed_set_display(gfloat slidepos, displayd *dsp) 
{
  cpaneld *cpanel = &dsp->cpanel;

  cpanel->t1d.slidepos = slidepos;
  speed_set(slidepos, &cpanel->t1d.step, &dsp->t1d.delta);

}

void tour1d_speed_set(gfloat slidepos, ggobid *gg) 
{
    tour1d_speed_set_display(slidepos, gg->current_display); 
}

#ifdef TESTING_TOUR_STEP
void tour1d_step_cb(displayd *dsp, tour td, gint projdim, ggobid *gg,
  void *display)
{
  g_printerr ("tour_step\n");
}
#endif

void tour1d_pause (cpaneld *cpanel, gboolean state, displayd *dsp, ggobid *gg)
{
  if (dsp == NULL)
    return;
  cpanel->t1d.paused = state;

  tour1d_func (!cpanel->t1d.paused, dsp, gg);

  if (cpanel->t1d.paused) {
    /*-- whenever motion stops, we need a FULL redraw --*/
    display_tailpipe (dsp, FULL, gg);
  }
}

/*-- add/remove jvar to/from the subset of variables that <may> be active --*/
static gboolean
tour1d_subset_var_set (gint jvar, GGobiData *d, displayd *dsp, ggobid *gg)
{
  gboolean in_subset = dsp->t1d.subset_vars_p.els[jvar];
  gint j, k;
  gboolean changed = false;

  /*
   * require 2 variables in the subset, though only 1 is
   *   required in active_vars
  */
  if (in_subset) {
    if (dsp->t1d.nsubset > MIN_NVARS_FOR_TOUR1D) {
      dsp->t1d.subset_vars_p.els[jvar] = false;
      dsp->t1d.nsubset -= 1;
      changed = true;
    }
  } else {
    dsp->t1d.subset_vars_p.els[jvar] = true;
    dsp->t1d.nsubset += 1;
    changed = true;
  }

  /*-- reset subset_vars based on subset_vars_p --*/
  if (changed) {
    dsp->t1d_manipvar_inc = false;
    for (j=0, k=0; j<d->ncols; j++) {
      if (dsp->t1d.subset_vars_p.els[j]) {
        dsp->t1d.subset_vars.els[k++] = j;
        if (j == dsp->t1d_manip_var)
          dsp->t1d_manipvar_inc = true;
      }
    }
    /*-- Manip var needs to be one of the active vars --*/
    if (!dsp->t1d_manipvar_inc) {
      dsp->t1d_manip_var = dsp->t1d.subset_vars.els[0];
    }
      
    zero_tau(dsp->t1d.tau, 1);
    dsp->t1d.get_new_target = true;
  }

  return changed;
}

void 
tour1d_active_var_set (gint jvar, GGobiData *d, displayd *dsp, ggobid *gg)
{
  gint j, k;
  gboolean in_subset = dsp->t1d.subset_vars_p.els[jvar];
  gboolean active = dsp->t1d.active_vars_p.els[jvar];

  /*
   * This covers the case where we've just removed a variable
   * from the subset and then called tour1d_active_var_set ..
   * but the variable is already inactive, so we don't need to
   * do anything.
  */
  if (!active && !in_subset)
/**/return;

  /* deselect var if t1d.nactive > 2 */
  if (active) {
    if (dsp->t1d.nactive > 1) {
      for (j=0; j<dsp->t1d.nactive; j++) {
        if (jvar == dsp->t1d.active_vars.els[j]) 
          break;
      }
      if (j<dsp->t1d.nactive-1) {
        for (k=j; k<dsp->t1d.nactive-1; k++){
          dsp->t1d.active_vars.els[k] = dsp->t1d.active_vars.els[k+1];
        }
      }
      dsp->t1d.nactive--;

      if (!gg->tour1d.fade_vars) /* set current position without sel var */
      {
        gt_basis(dsp->t1d.Fa, dsp->t1d.nactive, dsp->t1d.active_vars, 
          d->ncols, (gint) 1);
        arrayd_copy(&dsp->t1d.Fa, &dsp->t1d.F);
/*      copy_mat(dsp->t1d.F.vals, dsp->t1d.Fa.vals, d->ncols, 1);*/
      }
      dsp->t1d.active_vars_p.els[jvar] = false;
    }
  }
  else { /* not active, so add the variable */
    if (jvar > dsp->t1d.active_vars.els[dsp->t1d.nactive-1]) {
      dsp->t1d.active_vars.els[dsp->t1d.nactive] = jvar;
    }
    else if (jvar < dsp->t1d.active_vars.els[0]) {
      for (j=dsp->t1d.nactive; j>0; j--) {
          dsp->t1d.active_vars.els[j] = dsp->t1d.active_vars.els[j-1];
      }
      dsp->t1d.active_vars.els[0] = jvar;
    }
    else {
      gint jtmp = dsp->t1d.nactive;
      for (j=0; j<dsp->t1d.nactive-1; j++) {
        if (jvar > dsp->t1d.active_vars.els[j] && jvar < dsp->t1d.active_vars.els[j+1]) {
          jtmp = j+1;
          break;
        }
      }
      for (j=dsp->t1d.nactive-1;j>=jtmp; j--) 
          dsp->t1d.active_vars.els[j+1] = dsp->t1d.active_vars.els[j];
      dsp->t1d.active_vars.els[jtmp] = jvar;
    }
    dsp->t1d.nactive++;
    dsp->t1d.active_vars_p.els[jvar] = true;
  }

  if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
    free_optimize0_p(&dsp->t1d_pp_op);
    alloc_optimize0_p(&dsp->t1d_pp_op, d->nrows_in_plot, dsp->t1d.nactive, 
      1);
    free_pp(&dsp->t1d_pp_param);
    alloc_pp(&dsp->t1d_pp_param, d->nrows_in_plot, dsp->t1d.nactive, 1);
    t1d_pp_reinit(dsp, gg);
  }  
  dsp->t1d.get_new_target = true;

}

static void
tour1d_manip_var_set (gint j, gint btn, ggobid *gg)
{
  displayd *dsp = gg->current_display;

  if (btn == 1)
    dsp->t1d_manip_var = j;    
}

gboolean
tour1d_varsel (GtkWidget *w, gint jvar, gint toggle, gint mouse, GGobiData *d, ggobid *gg)
{
  displayd *dsp = gg->current_display;
  gboolean redraw = true;

  if (GTK_IS_TOGGLE_BUTTON(w) || GTK_IS_BUTTON(w)) {  /* toggle or 'label' */
    /*
     * add/remove jvar to/from the subset of variables that <may> be active
    */
    gboolean fade = gg->tour1d.fade_vars;

    redraw = tour1d_subset_var_set (jvar, d, dsp, gg);
    if (redraw) {
      varcircles_visibility_set (dsp, gg);

      /*-- Add/remove the variable to/from the active set, too.
           But: if we just removed it from the subset, but it was
           already inactive, there's no need to do anything --*/
      if (dsp->t1d.subset_vars_p.els[jvar] == false &&
          dsp->t1d.active_vars_p.els[jvar] == false)
        ;
      else {
        gg->tour1d.fade_vars = false;
        tour1d_active_var_set (jvar, d, dsp, gg);
        gg->tour1d.fade_vars = fade;

        if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
          free_optimize0_p(&dsp->t1d_pp_op);
          alloc_optimize0_p(&dsp->t1d_pp_op, d->nrows_in_plot,
            dsp->t1d.nactive, 1);
          free_pp(&dsp->t1d_pp_param);
          alloc_pp(&dsp->t1d_pp_param, d->nrows_in_plot, dsp->t1d.nactive, 1);
          t1d_pp_reinit(dsp, gg);
        }
      }
    }

  } else if (GTK_IS_DRAWING_AREA(w)) {

    /*-- any button --*/
    if (d->vcirc_ui.jcursor == GDK_HAND2) { /* This part sets the manip var */
      tour1d_manip_var_set (jvar, mouse, gg);
      varcircles_cursor_set_default (d);
    } else { /* This part sets active/not active var */
      tour1d_active_var_set (jvar, d, dsp, gg);

      if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
        free_optimize0_p(&dsp->t1d_pp_op);
        alloc_optimize0_p(&dsp->t1d_pp_op, d->nrows_in_plot, dsp->t1d.nactive, 
          1);
        free_pp(&dsp->t1d_pp_param);
        alloc_pp(&dsp->t1d_pp_param, d->nrows_in_plot, dsp->t1d.nactive, 1);
        t1d_pp_reinit(dsp, gg);
      }
    }
  }

  return redraw;
}

void
tour1d_projdata(splotd *sp, greal **world_data, GGobiData *d, ggobid *gg)
{
  gint i, j, m;
  displayd *dsp = (displayd *) sp->displayptr;
  gfloat min, max, mean;
  gfloat precis = PRECISION1;
  cpaneld *cpanel = &dsp->cpanel;
  gfloat *yy;

  if (sp == NULL)
    return;
  if (sp->p1d.spread_data.nels != d->nrows)
    vectorf_realloc (&sp->p1d.spread_data, d->nrows);

  yy = (gfloat *) g_malloc (d->nrows_in_plot * sizeof (gfloat));

  for (m=0; m < d->nrows_in_plot; m++) {
    i = d->rows_in_plot.els[m];
    yy[m] = sp->planar[i].x = 0;
    sp->planar[i].y = 0;
    for (j=0; j<d->ncols; j++)
    {
      yy[m] += (gfloat)(dsp->t1d.F.vals[0][j]*world_data[i][j]);
    }
  }

  do_ash1d (yy, d->nrows_in_plot,
            cpanel->t1d.nbins, cpanel->t1d.nASHes,
            sp->p1d.spread_data.els, &min, &max, &mean);

  if (sp->tour1d.initmax) {
    sp->tour1d.mincnt = 0.0;    /* let this be zero for consistency */
    sp->tour1d.maxcnt = max;    
    sp->tour1d.initmax = false;  
    sp->tour1d.minscreenx = yy[0];
    sp->tour1d.maxscreenx = yy[0];
  }
  else
    if (max > sp->tour1d.maxcnt) sp->tour1d.maxcnt = max;

  /*max = 2*mean;  * try letting the max for scaling depend on the mean */
  max = sp->tour1d.maxcnt;
  if (cpanel->t1d.vert) {
    for (m=0; m<d->nrows_in_plot; m++) {
      if (yy[m] < sp->tour1d.minscreenx) {
         sp->tour1d.minscreenx = yy[m];
      }
      else if (yy[m] > sp->tour1d.maxscreenx) {
         sp->tour1d.maxscreenx = yy[m];
      }
    }
    for (m=0; m<d->nrows_in_plot; m++) {
      i = d->rows_in_plot.els[m];
      sp->planar[i].x = (greal) (precis*(-1.0+2.0*
        sp->p1d.spread_data.els[m]/max));
        /*(sp->p1d_data.els[i]-min)/(max-min)));*/
      /*      sp->planar[i].y = yy[i];*/
      sp->planar[i].y = (greal) (precis*(-1.0+2.0*
        ((yy[m]-sp->tour1d.minscreenx)/
        (sp->tour1d.maxscreenx-sp->tour1d.minscreenx))));
    }
  }
  else {
    for (m=0; m<d->nrows_in_plot; m++) {
      if (yy[m] < sp->tour1d.minscreenx) {
         sp->tour1d.minscreenx = yy[m];
      }
      else if (yy[m] > sp->tour1d.maxscreenx) {
         sp->tour1d.maxscreenx = yy[m];
      }
    }
    for (m=0; m<d->nrows_in_plot; m++) {
      i = d->rows_in_plot.els[m];
      sp->planar[i].x = (greal) (precis*(-1.0+2.0*
        ((yy[m]-sp->tour1d.minscreenx)/
        (sp->tour1d.maxscreenx-sp->tour1d.minscreenx))));
      /*      sp->planar[i].x = yy[i];*/
      sp->planar[i].y = (greal) (precis*(-1.0+2.0*
        sp->p1d.spread_data.els[m]/max));
        /*(sp->p1d_data.els[i]-min)/(max-min)));*/
    }
  }

  g_free ((gpointer) yy);
}

void
tour1d_run(displayd *dsp, ggobid *gg)
{
  GGobiData *d = dsp->d;
  cpaneld *cpanel = &dsp->cpanel;
  /*  static gint count = 0;*/
  gboolean revert_random = false;
  gint pathprob = 0;
  gint i, nv;
  extern void t1d_ppdraw_think(displayd *, ggobid *);

  /* Controls interpolation steps */
  if (!dsp->t1d.get_new_target && 
      !reached_target(dsp->t1d.tang, dsp->t1d.dist_az, 
        dsp->t1d.target_selection_method,&dsp->t1d.ppval, &dsp->t1d.oppval)) {
    increment_tour(dsp->t1d.tinc, dsp->t1d.tau, dsp->t1d.dist_az, 
      dsp->t1d.delta, &dsp->t1d.tang, (gint) 1);
    tour_reproject(dsp->t1d.tinc, dsp->t1d.G, dsp->t1d.Ga, dsp->t1d.Gz,
      dsp->t1d.F, dsp->t1d.Va, d->ncols, (gint) 1);

    /* plot pp indx */
    if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
      /*    if (dsp->t1d_ppda != NULL) {*/

      dsp->t1d.oppval = dsp->t1d.ppval;
      revert_random = t1d_switch_index(cpanel->t1d.pp_indx, 
        0, dsp, gg);
      t1d_ppdraw(dsp->t1d.ppval, dsp, gg);
    }

  }
  else { /* we're at the target plane */
    if (dsp->t1d.get_new_target) { /* store the pp parameters */
      if (dsp->t1d.target_selection_method == TOUR_PP)
      {
	/*        dsp->t1d_pp_op.index_best = dsp->t1d.ppval;
        for (j=0; j<dsp->t1d.nactive; j++) 
          dsp->t1d_pp_op.proj_best.vals[0][j] = 
	  dsp->t1d.F.vals[0][dsp->t1d.active_vars.els[j]];*/
      }
    }
    else 
    { /* make sure the ending projection is the same as the target */
      if (dsp->t1d.target_selection_method == TOUR_RANDOM)
      {
        do_last_increment(dsp->t1d.tinc, dsp->t1d.tau, 
          dsp->t1d.dist_az, (gint) 1);
        tour_reproject(dsp->t1d.tinc, dsp->t1d.G, dsp->t1d.Ga, dsp->t1d.Gz,
          dsp->t1d.F, dsp->t1d.Va, d->ncols, (gint) 1);
      }
    }
    /* now cleanup: store the current basis into the starting basis */
    arrayd_copy(&dsp->t1d.F, &dsp->t1d.Fa);
    nv = 0;
    for (i=0; i<d->ncols; i++)
      if (fabs(dsp->t1d.Fa.vals[0][i]) > 0.01) {
        nv++;
      }
    if (nv == 1 && dsp->t1d.nactive == 1) /* only generate new dir if num of
                                           active/used variables is > 2 */
      dsp->t1d.get_new_target = true;
    else {
      if (dsp->t1d.target_selection_method == TOUR_RANDOM) {
        gt_basis(dsp->t1d.Fz, dsp->t1d.nactive, dsp->t1d.active_vars, 
          d->ncols, (gint) 1);
      }
      else if (dsp->t1d.target_selection_method == TOUR_PP) {
        /* pp guided tour  */
        /* get new target according to the selected pp index */
        for (i=0; i<d->ncols; i++)
          dsp->t1d.Fz.vals[0][i] = 0.0;
        dsp->t1d.Fz.vals[0][dsp->t1d.active_vars.els[0]]=1.0;

        dsp->t1d.oppval = -1.0;
        t1d_ppdraw_think(dsp, gg);
        gdk_flush ();
        revert_random = t1d_switch_index(cpanel->t1d.pp_indx, 
          dsp->t1d.target_selection_method, dsp, gg);

        if (!revert_random) {
          for (i=0; i<dsp->t1d.nactive; i++) {
            if (isfinite((gdouble)dsp->t1d_pp_op.proj_best.vals[0][i]) != 0)
              dsp->t1d.Fz.vals[0][dsp->t1d.active_vars.els[i]] = 
                dsp->t1d_pp_op.proj_best.vals[0][i];
          }
          dsp->t1d_pp_op.index_best = 0.0;
	  /*g_printerr ("tour_run:index_best %f temp %f \n", dsp->t1d_pp_op.index_best, dsp->t1d_pp_op.temp);
g_printerr ("proj: ");
for (i=0; i<dsp->t1d_pp_op.proj_best.ncols; i++) g_printerr ("%f ", dsp->t1d_pp_op.proj_best.vals[0][i]);
g_printerr ("\n");*/

          /* if the best projection is the same as the previous one, switch 
              to a random projection */
/*          if (!checkequiv(dsp->t1d.Fa.vals, dsp->t1d.Fz.vals, d->ncols, 1)) {
            g_printerr ("Using random projection\n");
            gt_basis(dsp->t1d.Fz, dsp->t1d.nactive, dsp->t1d.active_vars, 
              d->ncols, (gint) 1);
            for (j=0; j<dsp->t1d.nactive; j++)
              dsp->t1d_pp_op.proj_best.vals[0][j] = 
                dsp->t1d.Fz.vals[0][dsp->t1d.active_vars.els[j]];
            revert_random = t1d_switch_index(cpanel->t1d.pp_indx, 
              dsp->t1d.target_selection_method, dsp, gg);
          }*/
	  /*          t1d_ppdraw(dsp->t1d.ppval, dsp, gg);*/
  /*          count = 0;*/
          ggobi_sleep(0);
        }
        else /* Use random target */
        {
	  /*          gt_basis(dsp->t1d.Fz, dsp->t1d.nactive, dsp->t1d.active_vars, 
            d->ncols, (gint) 1);
	    g_printerr ("Using random projection 2\n");*/
        }
      }
      pathprob = tour_path(dsp->t1d.Fa, dsp->t1d.Fz, dsp->t1d.F, d->ncols, 
        (gint) 1, dsp->t1d.Ga, dsp->t1d.Gz, dsp->t1d.G, 
        dsp->t1d.lambda, dsp->t1d.tv, dsp->t1d.Va,
        dsp->t1d.Vz, dsp->t1d.tau, dsp->t1d.tinc, 
        &dsp->t1d.dist_az, &dsp->t1d.tang);
      if (pathprob == 0) 
        dsp->t1d.get_new_target = false;
      else if (pathprob == 1) { /* problems with Fa so need to force a jump */
        tour1d_scramble(gg);
        pathprob = tour_path(dsp->t1d.Fa, dsp->t1d.Fz, dsp->t1d.F, d->ncols, 
          (gint) 1, dsp->t1d.Ga,
          dsp->t1d.Gz, dsp->t1d.G, dsp->t1d.lambda, dsp->t1d.tv, dsp->t1d.Va,
          dsp->t1d.Vz,  dsp->t1d.tau, dsp->t1d.tinc, 
          &dsp->t1d.dist_az, &dsp->t1d.tang);
      }
      else if (pathprob == 2 || pathprob == 3) { /* problems with Fz,
                                      so will force a new choice of Fz */
        dsp->t1d.get_new_target = true;
      }
    }
  }
 /*  tour_reproject(dsp, 2);*/
#ifdef TESTING_TOUR_STEP
{
  GGobiDisplayClass *klass;
  klass = GGOBI_DISPLAY_GET_CLASS(dsp);
  g_signal_emit(G_OBJECT(dsp),
    klass->signals[TOUR_STEP_SIGNAL], 0, dsp->t1d,
    (gint) 1, gg);
}
#endif

  display_tailpipe (dsp, FULL, gg);

  varcircles_refresh (d, gg);
  if (dsp->t1d_video) tour1d_write_video(gg);
}

void
tour1d_do_step(displayd *dsp, ggobid *gg)
{
  tour1d_run(dsp, gg);
}

gint
tour1d_idle_func (displayd *dsp)
{
  ggobid *gg = GGobiFromDisplay (dsp);
  cpaneld *cpanel = &dsp->cpanel;
  gboolean doit = !cpanel->t1d.paused;

  if (doit) {
    tour1d_run (dsp, gg);
    gdk_flush ();
  }

  return (doit);
}

void tour1d_func (gboolean state, displayd *dsp, ggobid *gg)
{
  splotd *sp = (splotd *) g_list_nth_data (dsp->splots, 0);

  if (state) {
    if (dsp->t1d.idled == 0) {
      dsp->t1d.idled = g_idle_add_full (G_PRIORITY_LOW,
                                     (GtkFunction) tour1d_idle_func, dsp, NULL);
    }
    gg->tour1d.idled = 1;
  } else {
    if (dsp->t1d.idled) {
      g_source_remove (dsp->t1d.idled);
      dsp->t1d.idled = 0;
    }
    gg->tour1d.idled = 0;
  }

  splot_connect_expose_handler (dsp->t1d.idled, sp);
}

void tour1d_reinit(ggobid *gg)
{
  gint i, j;
  displayd *dsp = gg->current_display;
  GGobiData *d = dsp->d;
  splotd *sp = gg->current_splot;

  for (i=0; i<1; i++) {
    for (j=0; j<d->ncols; j++) {
      dsp->t1d.Fa.vals[i][j] = 0.;
      dsp->t1d.F.vals[i][j] = 0.;
    }
    dsp->t1d.Fa.vals[i][dsp->t1d.active_vars.els[i]] = 1.;
    dsp->t1d.F.vals[i][dsp->t1d.active_vars.els[i]] = 1.;
  }

  /* Reinits the vertical height for the ashes */
  sp->tour1d.initmax = true;

  dsp->t1d.get_new_target = true;

  display_tailpipe (dsp, FULL, gg);

  varcircles_refresh (d, gg);

  if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) 
    t1d_pp_reinit(dsp, gg);
}

void tour1d_scramble(ggobid *gg)
{
  int i, j;
  displayd *dsp = gg->current_display;
  GGobiData *d = dsp->d;
  gint nc = d->ncols;

  for (i=0; i<1; i++)
    for (j=0; j<nc; j++)
      dsp->t1d.Fa.vals[i][j] = dsp->t1d.Fz.vals[i][j] = 
        dsp->t1d.F.vals[i][j] = dsp->t1d.Ga.vals[i][j] = 
        dsp->t1d.Gz.vals[i][j] = 0.0;

  gt_basis(dsp->t1d.Fa, dsp->t1d.nactive, dsp->t1d.active_vars, 
    d->ncols, (gint) 1);
  arrayd_copy(&dsp->t1d.Fa, &dsp->t1d.F);
  /*  copy_mat(dsp->t1d.F.vals, dsp->t1d.Fa.vals, d->ncols, 1);*/

  dsp->t1d.get_new_target = true;

  display_tailpipe (dsp, FULL, gg);

  varcircles_refresh (d, gg);

  if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) 
    t1d_pp_reinit(dsp, gg);
}

void tour1d_vert(cpaneld *cpanel, gboolean state)
{
  cpanel->t1d.vert = state;
}

/* Variable manipulation */
void
tour1d_manip_init(gint p1, gint p2, splotd *sp) 
{
  displayd *dsp = (displayd *) sp->displayptr;
  GGobiData *d = dsp->d;
  cpaneld *cpanel = &dsp->cpanel;
  ggobid *gg = GGobiFromSPlot(sp);
  gint j;
  gint n1vars = dsp->t1d.nactive;
  gboolean dontdoit = false;

  dsp->t1d_phi = 0.;

  /* gets mouse position */
  if (cpanel->t1d.vert) 
    dsp->t1d_pos = dsp->t1d_pos_old = p2;
  else
    dsp->t1d_pos = dsp->t1d_pos_old = p1;

  /* initializes indicator for manip var being one of existing vars */
  dsp->t1d_manipvar_inc = false;

  /* need to turn off tour */
  if (!cpanel->t1d.paused)
    tour1d_func(T1DOFF, gg->current_display, gg);

  /* check if manip var is one of existing vars */
  /* n1vars, n2vars is the number of variables, excluding the
     manip var in hor and vert directions */
  for (j=0; j<dsp->t1d.nactive; j++)
    if (dsp->t1d.active_vars.els[j] == dsp->t1d_manip_var) {
      dsp->t1d_manipvar_inc = true;
      n1vars--;
    }

  /* make manip basis, from existing projection */
  /* 0 will be the remainder of the projection, and
     1 will be the indicator vector for the manip var */
  for (j=0; j<d->ncols; j++) {
    dsp->t1d_manbasis.vals[0][j] = dsp->t1d.F.vals[0][j];
    dsp->t1d_manbasis.vals[1][j] = 0.;
  }
  dsp->t1d_manbasis.vals[1][dsp->t1d_manip_var]=1.;

  if (n1vars > 0)
  {
    while (!gram_schmidt(dsp->t1d_manbasis.vals[0],  dsp->t1d_manbasis.vals[1],
      d->ncols))
    {
       gt_basis(dsp->t1d.tv, dsp->t1d.nactive, dsp->t1d.active_vars, 
        d->ncols, (gint) 1);
      for (j=0; j<d->ncols; j++) 
        dsp->t1d_manbasis.vals[1][j] = dsp->t1d.tv.vals[0][j];
    }
  }

  if (dontdoit)
    disconnect_motion_signal (sp);
}

void
tour1d_manip(gint p1, gint p2, splotd *sp, ggobid *gg) 
{
  displayd *dsp = (displayd *) sp->displayptr;
  GGobiData *d = dsp->d;
  cpaneld *cpanel = &dsp->cpanel;
  gfloat xcosphi=1., xsinphi=0.;
  gfloat distx, disty;
  gfloat denom = (float) MIN(sp->max.x, sp->max.y)/2.;
  gint actual_nxvars = dsp->t1d.nactive;
  gint j;
  gboolean offscreen = false;
  gboolean pp_problem = false;

  /* check if off the plot window */
  if (p1 > sp->max.x || p1 < 0 ||
      p2 > sp->max.y || p2 <0)
    offscreen = true;

  if (dsp->t1d_manipvar_inc)
    actual_nxvars = dsp->t1d.nactive-1;

  if (!offscreen) {
    dsp->t1d_pos_old = dsp->t1d_pos;
  
    dsp->t1d_pos = p1;

    if (actual_nxvars > 0)
    {
      if (cpanel->t1d.vert)
      {
        distx = 0.;
        disty = dsp->tc2_pos_old - dsp->tc2_pos;
      }
      else
      {
        distx = dsp->t1d_pos - dsp->t1d_pos_old;
        disty = 0.;
      }

      dsp->t1d_phi = dsp->t1d_phi + distx / denom;
  
      xcosphi = (gfloat) cos((gdouble) dsp->t1d_phi);
      xsinphi = (gfloat) sin((gdouble) dsp->t1d_phi);
      if (xcosphi > 1.0)
      {
        xcosphi = 1.0;
        xsinphi = 0.0;
      }
      else if (xcosphi < -1.0)
      {
        xcosphi = -1.0;
        xsinphi = 0.0;
      }
    }

    /* generate the projection basis */
    if (actual_nxvars > 0) 
    {
      for (j=0; j<d->ncols; j++)
        dsp->t1d.F.vals[0][j] = xcosphi * dsp->t1d_manbasis.vals[0][j] + 
         xsinphi * dsp->t1d_manbasis.vals[1][j];
    }
 
    /* plot pp index */
    /*    if (dsp->t1d_ppda != NULL) {*/
    if (dsp->t1d_window != NULL && GTK_WIDGET_VISIBLE (dsp->t1d_window)) {
      dsp->t1d.oppval = dsp->t1d.ppval;
      pp_problem = t1d_switch_index(cpanel->t1d.pp_indx, 
        0, dsp, gg);
      t1d_ppdraw(dsp->t1d.ppval, dsp, gg);
    }

    display_tailpipe (dsp, FULL, gg);
    varcircles_refresh (d, gg);
  }
  else {
    disconnect_motion_signal (sp);
    arrayd_copy(&dsp->t1d.F, &dsp->t1d.Fa);
    /*    copy_mat(dsp->t1d.Fa.vals, dsp->t1d.F.vals, d->ncols, 1);*/
    dsp->t1d.get_new_target = true;
    if (!cpanel->t1d.paused)
      tour1d_func(T1DON, gg->current_display, gg);
  }
}

void
tour1d_manip_end(splotd *sp) 
{
  displayd *dsp = (displayd *) sp->displayptr;
  cpaneld *cpanel = &dsp->cpanel;
  ggobid *gg = GGobiFromSPlot(sp);

  disconnect_motion_signal (sp);

  arrayd_copy(&dsp->t1d.F, &dsp->t1d.Fa);
  /*  copy_mat(dsp->t1d.Fa.vals, dsp->t1d.F.vals, d->ncols, 1);*/
  dsp->t1d.get_new_target = true;

  /* need to turn on tour? */
  if (!cpanel->t1d.paused) {
    tour1d_pause(cpanel, T1DOFF, dsp, gg);

    /*-- whenever motion stops, we need a FULL redraw --*/
    display_tailpipe (dsp, FULL, gg);
  }
}

#undef T1DON
#undef T1DOFF