/** ** tori2CSV - convert .tori file to .CSV file for 'ant' ** ** CREATED: 2011.07.28 ABS copied from tori2script ** MODIFIED: 2011.07.29 ABS worked on ** MODIFIED: 2011.07.30 ABS worked on ** MODIFIED: 2011.07.31 ABS worked on ** MODIFIED: 2011.08.03 ABS bumped up PIN_OFFSET ** MODIFIED: 2011.11.05 ABS added scales for base, level 0 - 2 ** **/ #include "tori2CSV.h" main(argc, argv) int argc; char** argv; { /********************************************************************** This program assumes that a script is used to do two things: 1) strip comments (vertical bar = comment to end of line) 2) concatenate a 3- or 4-line header onto the output to create a well-formed CSV file The trickiest code in this program concerns id numbers. There are the 'input_id' and 'input_pid' (parent id), which are read from the input file, the variable 'n' which is a count, starting at zero, of tori scanned in (also variables 'n1' and 'n2' which are temporary forms of 'n'), and the 'output_id' and 'output_pid' which are usually the same as the corresponding input values plus constant MAGIC. So, given 'n' the 'input_id' is 'input_id[n]' given 'input_id' its 'n' is 'reverse_index[input_id]' given 'n' the 'n' of its parent is 'reverse_index[input_pid[n]]' given 'n' the 'output_id' is usually 'input_id[n] + MAGIC' **********************************************************************/ /* declare functions */ double atof(); void print_CSV_line(); /* declare local variables */ char input_line[255]; char suffix[8]; int i; int n, n1, n2; int num_scanned, num_printed; int nc, cc; int deepest_level; int num_base_tori; int num_level_one, num_level_two, num_level_three; int output_id, output_pid; double temp_scale; int done_setting_levels; /* Boolean */ int minmax_init_already_done; /* Boolean */ double r_boost; double x_min, x_max, x_range, y_min, y_max, y_range, z_min, z_max, z_range; int reverse_index[32767]; /* arrays indexed by 'n' */ int input_id[MAX_TORI]; int input_pid[MAX_TORI]; int level[MAX_TORI]; int num_children[MAX_TORI]; int t0[MAX_TORI], t1[MAX_TORI]; /* t0: inner tesselations; t1: outer tess */ double r0[MAX_TORI], r1[MAX_TORI]; /* r0: inner radius; r1: outer radius */ double red[MAX_TORI], grn[MAX_TORI], blu[MAX_TORI]; double ap[MAX_TORI], ang[MAX_TORI]; double x_val[MAX_TORI], y_val[MAX_TORI], z_val[MAX_TORI]; double x_net[MAX_TORI], y_net[MAX_TORI], z_net[MAX_TORI]; double roll[MAX_TORI], pitch[MAX_TORI], yaw[MAX_TORI]; /* initialize global variables (including parameters) */ defaultParms(); parseArgs(argc, argv); if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: NumTori = %d\n", argv[0], NumTori); fprintf(stderr, "%s: DEBUG: Scale = %f\n", argv[0], Scale); fprintf(stderr, "%s: DEBUG: PositionScale = %f\n", argv[0], PositionScale); fprintf(stderr, "%s: DEBUG: Base0Scale = %f\n", argv[0], Base0Scale); fprintf(stderr, "%s: DEBUG: Level0Scale = %f\n", argv[0], Level0Scale); fprintf(stderr, "%s: DEBUG: Level1Scale = %f\n", argv[0], Level1Scale); fprintf(stderr, "%s: DEBUG: Level2Scale = %f\n", argv[0], Level2Scale); } /* initialize local variables */ num_scanned = 0; num_printed = 0; num_base_tori = 0; num_level_one = 0; num_level_two = 0; num_level_three = 0; deepest_level = 0; minmax_init_already_done = FALSE; /* read */ /* * i = int; f = float * * +---------------------------------------- 0 - 1 * | +-------------------------------------- 0 - 1 * | | +------------------------------------ 0 - 1 * | | | +---------------------------------- -1 - 1 * | | | | +---------------------- *2Pi * | | | | | +------------ *2Pi * | | | | | | +------- *2Pi * | | | | | | | +- *2Pi * i i f f i i f f f f f f f f f f f * | | | | | | | | | | | | | | | | | * v v v v v v v v v v v v v v v v v * ID Parent r0 r1 t0 t1 R G B attachParam ang X Y Z roll pitch yaw * */ for (n = 0; n < NumTori; n++) { fscanf(stdin, "%d %d %lf %lf %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &input_id[n], &input_pid[n], &r0[n], &r1[n], &t0[n], &t1[n], &red[n], &grn[n], &blu[n], &ap[n], &ang[n], &x_val[n], &y_val[n], &z_val[n], &roll[n], &pitch[n], &yaw[n]); num_scanned++; reverse_index[input_id[n]] = n; level[n] = -1; num_children[n] = 0; if (Normalize != FALSE) { if (input_id[n] == input_pid[n]) { /* level 0 */ if (!minmax_init_already_done) { x_min = x_val[n]; x_max = x_val[n]; y_min = y_val[n]; y_max = y_val[n]; z_min = z_val[n]; z_max = z_val[n]; minmax_init_already_done = TRUE; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: !minmax_init_already_done; n = %d\n", argv[0], n); } } else { if (x_val[n] > x_max) x_max = x_val[n]; if (x_val[n] < x_min) x_min = x_val[n]; if (y_val[n] > y_max) y_max = y_val[n]; if (y_val[n] < y_min) y_min = y_val[n]; if (z_val[n] > z_max) z_max = z_val[n]; if (z_val[n] < z_min) z_min = z_val[n]; } } } if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: n = %d\n", argv[0], n); fprintf(stderr, "%s: DEBUG: input_id[n] = %d\n", argv[0], input_id[n]); fprintf(stderr, "%s: DEBUG: input_pid[n] = %d\n", argv[0], input_pid[n]); fprintf(stderr, "%s: DEBUG: r0[n] = %f\n", argv[0], r0[n]); fprintf(stderr, "%s: DEBUG: r1[n] = %f\n", argv[0], r1[n]); fprintf(stderr, "%s: DEBUG: t0[n] = %d\n", argv[0], t0[n]); fprintf(stderr, "%s: DEBUG: t1[n] = %d\n", argv[0], t1[n]); fprintf(stderr, "%s: DEBUG: red[n] = %f\n", argv[0], red[n]); fprintf(stderr, "%s: DEBUG: grn[n] = %f\n", argv[0], grn[n]); fprintf(stderr, "%s: DEBUG: blu[n] = %f\n", argv[0], blu[n]); fprintf(stderr, "%s: DEBUG: ap[n] = %f\n", argv[0], ap[n]); fprintf(stderr, "%s: DEBUG: ang[n] = %f\n", argv[0], ang[n]); fprintf(stderr, "%s: DEBUG: x_val[n] = %f\n", argv[0], x_val[n]); fprintf(stderr, "%s: DEBUG: y_val[n] = %f\n", argv[0], y_val[n]); fprintf(stderr, "%s: DEBUG: z_val[n] = %f\n", argv[0], z_val[n]); fprintf(stderr, "%s: DEBUG: roll[n] = %f\n", argv[0], roll[n]); fprintf(stderr, "%s: DEBUG: pitch[n] = %f\n", argv[0], pitch[n]); fprintf(stderr, "%s: DEBUG: yaw[n] = %f\n", argv[0], yaw[n]); } } if (Normalize != FALSE) { x_range = x_max - x_min; if (x_range == 0.0) x_range = 1.0; y_range = y_max - y_min; if (y_range == 0.0) y_range = 1.0; z_range = z_max - z_min; if (z_range == 0.0) z_range = 1.0; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: x_min = %f; x_max = %f; x_range = %f\n", argv[0], x_min, x_max, x_range); fprintf(stderr, "%s: DEBUG: y_min = %f; y_max = %f; y_range = %f\n", argv[0], y_min, y_max, y_range); fprintf(stderr, "%s: DEBUG: z_min = %f; z_max = %f; z_range = %f\n", argv[0], z_min, z_max, z_range); } } /* set levels */ done_setting_levels = FALSE; while(done_setting_levels == FALSE) { done_setting_levels = TRUE; for (n = 0; n < NumTori; n++) { if (input_id[n] == input_pid[n]) { level[n] = 0; num_base_tori++; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: level[%d] (id=%d) = 0!\n", argv[0], n, input_id[n]); } } else { if (level[reverse_index[input_pid[n]]] != -1) { level[n] = level[reverse_index[input_pid[n]]] + 1; if (level[n] > deepest_level) { deepest_level = level[n]; } if (level[n] == 1) { num_level_one++; } if (level[n] == 2) { num_level_two++; } if (level[n] == 3) { num_level_three++; } num_children[reverse_index[input_pid[n]]]++; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: level[%d] (input_id=%d) = %d\n", argv[0], n, input_id[n], level[n]); } } else { done_setting_levels = FALSE; /* I wish I still understood this control flow */ } } } } /* write */ for (n = 0; n < NumTori; n++) { if (level[n] == 0) { if (Normalize == FALSE) { x_net[n] = PositionScale*x_val[n]; y_net[n] = PositionScale*y_val[n]; z_net[n] = PositionScale*z_val[n]; } else { if (num_base_tori == 1) { /* KLUDGE */ x_net[0] = 0.0; y_net[0] = 0.0; z_net[0] = 0.0; } else { /* scale all positions to fit in box 10x10x2 */ x_net[n] = PositionScale*10.0 *((x_val[n] - x_min)/x_range - 0.5); y_net[n] = PositionScale*10.0 *((y_val[n] - y_min)/y_range - 0.5); z_net[n] = PositionScale*2.0 *((z_val[n] - z_min)/z_range - 0.5); } } num_printed++; /* pin */ output_id = input_id[n] + MAGIC + PIN_OFFSET; output_pid = 0; /* this is level 0 pin */ temp_scale = Base0Scale; print_CSV_line( output_id, /* int id */ output_pid, /* int pid */ 0, /* int level */ 0, /* int nc */ 1, /* int cc */ 0.0, /* float phi */ temp_scale, /* float xs */ temp_scale, /* float ys */ temp_scale, /* float zs */ x_net[n], /* float xt */ y_net[n], /* float yt */ z_net[n], /* float zt */ 6, /* int su */ ANTZ_GLYPH_PIN_SOLID, /* int sh */ (int)(255.0*red[n]), /* int r */ (int)(255.0*grn[n]), /* int g */ (int)(255.0*blu[n]) /* int b */ ); /* base torus */ num_printed++; nc = num_children[reverse_index[input_pid[n]]]; if (nc == 0) { cc = 0; } else { cc = nc - 1; } output_id = input_id[n] + MAGIC; output_pid = input_id[n] + MAGIC + PIN_OFFSET; temp_scale = Scale*Level0Scale; /* this is still level 0 -- torus */ print_CSV_line( output_id, /* int id */ output_pid, /* int pid */ 1, /* int level */ nc, /* int nc */ cc, /* int cc */ 0.0, /* float phi */ temp_scale, /* float xs */ temp_scale, /* float ys */ temp_scale, /* float zs */ x_net[n], /* float xt */ y_net[n], /* float yt */ z_net[n], /* float zt */ 5, /* int su */ ANTZ_GLYPH_TORUS_SOLID, /* int sh */ (int)(255.0*red[n]), /* int r */ (int)(255.0*grn[n]), /* int g */ (int)(255.0*blu[n]) /* int b */ ); for (n1 = 0; n1 < NumTori; n1++) { if (level[n1] == 1 && reverse_index[input_pid[n1]] == n) /* parent is n */ { /* level 1 child */ num_printed++; output_id = input_id[n1] + MAGIC; output_pid = input_pid[n1] + MAGIC; temp_scale = Level1Scale*Scale*r1[n1]; print_CSV_line( output_id, /* int id */ output_pid, /* int pid */ 2, /* int level */ 0, /* int nc */ 1, /* int cc */ ang[n1] - 0.25, /* float phi */ temp_scale, /* float xs */ temp_scale, /* float ys */ temp_scale, /* float zs */ 0.0, /* float xt */ 0.0, /* float yt */ 0.0, /* float zt */ 5, /* int su */ ANTZ_GLYPH_TORUS_SOLID, /* int sh */ (int)(255.0*red[n1]), /* int r */ (int)(255.0*grn[n1]), /* int g */ (int)(255.0*blu[n1]) /* int b */ ); for (n2 = 0; n2 < NumTori; n2++) { if (level[n2] == 2 && reverse_index[input_pid[n2]] == n1) /* parent is n1 */ { /* level 2 child */ num_printed++; output_id = input_id[n2] + MAGIC; output_pid = input_pid[n2] + MAGIC; temp_scale = Level2Scale*Scale*r1[n2]; print_CSV_line( output_id, /* int id */ output_pid, /* int pid */ 3, /* int level */ 0, /* int nc */ 1, /* int cc */ ang[n2] - 0.25, /* float phi */ temp_scale, /* float xs */ temp_scale, /* float ys */ temp_scale, /* float zs */ 0.0, /* float xt */ 0.0, /* float yt */ 0.0, /* float zt */ 5, /* int su */ ANTZ_GLYPH_TORUS_SOLID, /* int sh */ (int)(255.0*red[n2]), /* int r */ (int)(255.0*grn[n2]), /* int g */ (int)(255.0*blu[n2]) /* int b */ ); } /* end if level 2 & child */ } /* end for n2 */ } /* end if level 1 & child */ } } } if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: num_scanned = %d\n", argv[0], num_scanned); fprintf(stderr, "%s: DEBUG: num_printed = %d\n", argv[0], num_printed); fprintf(stderr, "%s: DEBUG: num_base_tori = %d\n", argv[0], num_base_tori); fprintf(stderr, "%s: DEBUG: num_level_one = %d\n", argv[0], num_level_one); fprintf(stderr, "%s: DEBUG: num_level_two = %d\n", argv[0], num_level_two); fprintf(stderr, "%s: DEBUG: num_level_three = %d\n", argv[0], num_level_three); fprintf(stderr, "%s: DEBUG: deepest_level = %d\n", argv[0], deepest_level); } } /* end main */ /* * defaultParms - initialize global variables */ defaultParms() { Debug = FALSE; Normalize = FALSE; NumTori = 1; Scale = 1.0; PositionScale = 1.0; Base0Scale = 1.0; Level0Scale = 1.0; Level1Scale = 1.0; Level2Scale = 1.0; Type = 2; } /* end defaultParms() */ /* * parseArgs - parse arguments into global variables */ parseArgs(argc, argv) int argc; char** argv; { int i; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D' : Debug = TRUE; break; case 'h' : help(argv); exit(0); break; case 'N' : Normalize = TRUE; break; case 'n' : NumTori = atoi(&argv[++i][0]); break; case 'p' : PositionScale = atof(&argv[++i][0]); break; case 's' : Scale = atof(&argv[++i][0]); break; case 't' : Type = atoi(&argv[++i][0]); break; case 'u' : usage(argv); exit(0); break; case 'b' : Base0Scale = atof(&argv[++i][0]); break; case '0' : Level0Scale = atof(&argv[++i][0]); break; case '1' : Level1Scale = atof(&argv[++i][0]); break; case '2' : Level2Scale = atof(&argv[++i][0]); break; default: fprintf(stderr, "%s: ERROR: illegal flag: %s\n", argv[0], argv[i]); usage(argv); exit(0); break; } /* end switch */ } /* end if */ } /* end for(i) */ } /* end parseArgs() */ void print_CSV_line( id, pid, level, nc, cc, phi, xs, ys, zs, xt, yt, zt, su, sh, r, g, b ) int id; int pid; int level; int nc; int cc; float phi; float xs; float ys; float zs; float xt; float yt; float zt; int su; int sh; int r; int g; int b; { /* id,type,data,selected,parent,branchLevel, (1-6) */ fprintf(stdout, "%d,5,%d,0,%d,%d,", id, id, pid, level); /* childArray,childIndex,childCount, (7-9) */ fprintf(stdout, "%d,%d,%d,", id, cc, nc); /* channel x,channel y,channel z, (10-12) */ fprintf(stdout, "0,0,0,"); /* channelIndex x,channelIndex y,channelIndex z, (13-15) */ fprintf(stdout, "0,0,0,"); /* averageType x,averageType y,averageType z, (16-18) */ fprintf(stdout, "0,0,0,"); /* sampleInterval x,sampleInterval y,sampleInterval z, (19-21) */ fprintf(stdout, "1,1,1,"); /* rotate x,rotate y,rotate z,rotate s, (22-25) */ fprintf(stdout, "%f,0,0,1,", phi*-360.0); /* scale x,scale y,scale z, (26-28) */ fprintf(stdout, "%f,%f,%f,", xs, ys, zs); /* translate x,translate y,translate z, (29-31) */ fprintf(stdout, "%f,%f,%f,", xt, yt, zt); /* origin x,origin y,origin z, (32-34) */ fprintf(stdout, "0,0,0,"); /* rotateRate x,rotateRate y,rotateRate z, (35-37) */ fprintf(stdout, "0,0,0,"); /* rotateRad x,rotateRad y,rotateRad z, (38-40) */ fprintf(stdout, "%f,0,0,", -2.0*3.1415926*phi); /* scaleRate x,scaleRate y,scaleRate z, (41-43) */ fprintf(stdout, "0,0,0,"); /* translateRate x,translateRate y,translateRate z, (44-46) */ fprintf(stdout, "0,0,0,"); /* translateVect x,translateVect y,translateVect z, (47-49) */ fprintf(stdout, "0,0,0,"); /* surfaceType,shaderType,lineWidth, (50-52) */ fprintf(stdout, "%d,%d,2,", su, sh); /* pointSize,circle,colorIndex, (53-55) */ fprintf(stdout, "0,0,0,"); /* color r,color g,color b,color a, (56-59) */ fprintf(stdout, "%d,%d,%d,255,", r, g, b); /* colorFade,hide,freeze,center,centerPerGrid, (60-64) */ fprintf(stdout, "0,0,0,0,0,"); /* autoZoom x,autoZoom y,autoZoom z,scroll, (65-68) */ fprintf(stdout, "0,0,0,0,"); /* triggerOnHi x,triggerOnHi y,triggerOnHi z, (69-71) */ fprintf(stdout, "0,0,0,"); /* triggerOnLow x,triggerOnLow y,triggerOnLow z, (72-74) */ fprintf(stdout, "0,0,0,"); /* triggerLevelHi x,triggerLevelHi y,triggerLevelHi z, (75-77) */ fprintf(stdout, "0,0,0,"); /* triggerLevelLow x,triggerLevelLow y,triggerLevelLow z, (78-80) */ fprintf(stdout, "0,0,0,"); /* triggerCommand,proximity x,proximity y,proximity z, (81-84) */ fprintf(stdout, "0,0,0,0,"); /* proximity radius x,proximity radius y,proximity radius z, (85-87) */ fprintf(stdout, "0,0,0,"); /* draw value x,draw value y,draw value z, (88-90) */ fprintf(stdout, "0,0,0,"); /* selection set x,selection set y,selection set z, (91-93) */ fprintf(stdout, "0,0,0,"); /* size (94) */ fprintf(stdout, "420\r\n"); }