/* This program is used to transform Deneb IGRIP or TGRIP device, workcell and recording file to VRML2 Usage: workcell -option ... -d device translation : workcell -d input_igrip output_vrml -w workcell translation : workcell -w input_igrip output_vrml -p workcell recording translation: workcell -s input_igrip output_vrml recording_file timecycle To compile, cc -o workcell workcell.c -lm Qiming Wang Visualization and Virtual Reality Group Information Technology Lab NIST */ #include #include #include #define NUMBER_COLOR 32 #define MAXDEV 30 #define MAXPART 150 #define MAXPART_DEV 50 #define MAXKIN 10 #define MAXJOINT 10 #define MAXLEN 500 #define Q_LENGTH 300 #define MAXTIME 2000 #define MAXNAME 80 #define BUFSIZE 200 typedef struct { char *name; char *out_name; char *path_name; int global_id; int device_id; char attach[20]; float matrix[4][4]; int kin_num; int *kin; int joint_num; int *joint; } Part; /* only one attach in recording*/ typedef struct { char *devicename; int global_id; int part_number; float matrix[4][4]; int dof; int parts[MAXPART_DEV]; int attach; int attach_done; float *joint_init; int *joint_type; float *joint_lower; float *joint_upper; int anim_attach; int anim_attach_done; int attach_time[MAXTIME]; float attach_mat[4][4]; } Device; Device devices[MAXDEV]; Part parts[MAXPART]; typedef struct { char *name; int old_c; float new_c[3]; } table; int mode; int part_global; int n_device; char s[MAXLEN], buffer[BUFSIZE]; float time[MAXTIME], timecycle; int time_step; char **device_name; FILE *tmp_file; void read_workcell_file(); void scan_record(); void output_vrml(); void output_anim(); void device_anim(); void animate(); void unmatrix(); void insert(); int q_length(); void limit(); void route(); void part2vrml2(); void t_color(); void get_parts_directory(); void transfer(); typedef struct { int dof; char *device_name; float value[MAXTIME]; } Animation; Animation **animation; typedef struct { char *device_name; float translation[MAXTIME][3]; float rotation[MAXTIME][4]; } Transform; Transform *transform; table table_data[] = { {"white", 4, {1., 1., 1.}}, {"grey", 5, {0.5, 0.5, 0.5}}, {"black", 6, {0.2,0.2,0.2}}, {"red", 7, {1., 0., 0.}}, {"green", 8, {0.,1.,0.}}, {"blue", 9, {0., 0., 1.}}, {"yellow", 10, {1., 1., 0.}}, {"cyan", 11, {0., 1., 1.}}, {"purple", 12, {0.53, 0.12, 0.47}}, {"orange", 13, {1., 0.5, 0.}}, {"magenta", 14, {1., 0., 1.}}, {"cherry", 15, {0.74, 0.56, 0.56}}, {"light blue", 16, {0., 0., 0.75}}, {"brown", 17, {0.65, 0.16, 0.16}}, {"lime", 18, {0.20, 0.8, 0.2}}, {"tan", 19, {0.85, 0.58, 0.44}}, {"all black", 20, {0.,0.,0}}, {"grey-75", 21, {0.75,0.75,0.75}}, {"slate grey", 22, {0.18, 0.31, 0.31}}, {"teal", 23, {0.2, 0.2, 0.8}}, {"dodger blue", 24, {0.26, 0.43, 0.26}}, {"cobalt", 25, {0.14, 0.14, 0.56}}, {"dark plum", 26, {0.92, 0.68, 0.92}}, {"brass", 27, {0.71, 0.65, 0.25}}, {"color25", 28, {0.85, 0.58,0.44}}, {"color26", 29, {0.20,0.20, 0.8}}, {"color27", 30, {1.,1.,1.}}, {"color28", 31, {0.20,0.20,0.8}}, {"color29", 32, {0.74, 0.56, 0.56}}, {"color30", 33, {0.92, 0.68, 0.92}}, {"color31", 34, {1.,1.,1.}}, {"color32", 35, {0.,0.,0.}}, }; char path[MAXNAME]; int n_dir; char *dirs[MAXNAME]; char *vrml_files[MAXNAME]; int n_vrml_files=0; /* * process a command line argument */ void scan_args(char *argv[]) { char c; c = argv[1][1]; switch (c) { case 'd' : printf("Translating device %s to VRML2 %s\n\n",argv[2],argv[3]); mode = 0; break; case 'w' : printf("Translating workcell %s to VRML2 %s\n\n", argv[2],argv[3]); mode = 1; break; case 'p' : printf("Translating workcell %s with simulation recording\n\n",argv[2]); mode = 3; break; default: printf("Unknown option. \n\t-d for device\t-w for workcell\t-r for resource\n\t-p for playing recording\n"); exit(); } } main( int argc, char **argv) { FILE *file, *out_file, *record_file; printf("\n\n*******************************\n"); printf("Deneb IGRIP to VRML2\n"); printf("By Qiming Wang, VVRG, ITL, NIST\n"); printf("********************************\n\n"); scan_args(argv); get_parts_directory(); file = fopen(argv[2], "r"); out_file = fopen(argv[3], "w"); if (mode == 3) { record_file = fopen(argv[4],"r"); timecycle = atof(argv[5]); } tmp_file = fopen("tmp.wrl", "w"); read_workcell_file(file); /* read recording file */ if (mode==3) scan_record(record_file); /* output vrml workcell file*/ output_vrml(out_file, n_device); /* output recording */ if (mode == 3) output_anim(out_file); /* end of the translation */ printf("\nTranslation is finished\n"); } /* read IGRIP workcell file */ void read_workcell_file(FILE *file) { int version, prog, max_digit, max_analg, dof,dynamic, signat, n_home, n_uframe, n_utool, n_config, dummy; char device_name[MAXNAME], kin_type[MAXNAME], partname[MAXNAME], new_name[MAXNAME], attach[MAXNAME], s1[10], s2[10], program_name[MAXNAME]; int i,j,k, i_dummy, nn,ii, left_right, end_flag, kk; int part_device, kin_num, joint_num, kin[MAXKIN], joint[MAXJOINT]; float d_mat[4][4], p_mat[4][4], f_dummy; if (mode == 1 || mode == 3) { for (i=0; i<4; i++) fgets(s, MAXLEN, file); /* comments */ fscanf(file, "%d\n", &nn); if (nn>0) for (i=0; i 0 && time[time_step-1]>0 && tmp_time==0.0) { break; } if (time_step > 0 && time[time_step-1]==tmp_time) dummy=0; if (dummy) time[time_step]=tmp_time; for (i=0; i 1) { /* get touch information */ l=0; for (j=1; j 0) { for (i=0; i0) kk = q_length(queue)-2; label: insert(queue, kk, 999); kk++; left_right=1; end_flag=0; for (k=0; k tmp.dir"); file = fopen("tmp.dir", "r"); while (fscanf(file,"%s\n", buffer) > 0) { if (strncmp(buffer, dir, strlen(dir)) == 0) { fclose(file); system("rm tmp.dir"); return 1; } } system("rm tmp.dir"); return 0; } void transfer(char *filename, char *newname, char *device_name) { int i, loc,j; char command[BUFSIZE], *vrmlpart; /* if the geometry file name is not default, do following : search the dirs, to see which directory has this file */ search_dir(filename); vrmlpart=(char *)malloc((strlen(device_name)+strlen(filename)+4) *sizeof(char)); strcpy(vrmlpart, ""); strcpy(buffer, "d_"); strcat(buffer, device_name); if (if_device_exist(buffer)==0) { sprintf(command, "mkdir d_%s\n", device_name); system(command); } /* strcat(vrmlpart, "/");*/ strcat(vrmlpart, "d_"); strcat(vrmlpart, device_name); strcat(vrmlpart, "/"); loc = 0; for (i=0; i0) exist = search_vrml_file(vrmlpart); if (exist == 1) return; else { strcpy(vrml_files[n_vrml_files], vrmlpart); n_vrml_files++; } file = fopen(denebpart, "r"); out_file = fopen(vrmlpart, "w"); fprintf(out_file, "#VRML V2.0 utf8\n\n"); fprintf(out_file, "Transform {\n"); /* fprintf(out_file, " scale 0.001 0.001 0.001\n"); fprintf(out_file, " rotation 1 0 0 -1.57\n");*/ fprintf(out_file, " children [\n"); /* read version and number of coordinate system */ fscanf(file, "%d\n", &version); fscanf(file, "%d\n", &n_coorsys); for( i=0; i 8) { /* curves */ fscanf(file, "%d\n", &n_curve); if (n_curve != 0) { printf("number of curve = %d, information lost\n", n_curve); for (i=0; i 0\n"); return;*/ } } /* surface */ fscanf(file, "%d\n", &n_surface); if (n_surface != 0) { printf("number of surface = %d, information lost\n", n_surface); for (i=0; i 0 \n"); return;*/ } } } fscanf(file, "%d\n", &n_obj); for (i=0; i 0) printf("text information lost\n"); if (version >= 11) { fscanf(file, "%d\n", &n_texture); for (j=0; j 0) printf("texture information lost\n"); } /* now to output object */ fprintf(out_file, " Transform {\n"); fprintf(out_file, " children [\n"); fprintf(out_file, " Shape {\n"); /* get color from table */ t_color(color_index, color); fprintf(out_file, " appearance Appearance {\n"); fprintf(out_file, " material Material { \n"); fprintf(out_file, " diffuseColor %f %f %f\n", color[0],color[1],color[2]); fprintf(out_file, " }\n"); fprintf(out_file, " }\n"); if (n_poly > 0 ) { fprintf(out_file, " geometry IndexedFaceSet {\n"); fprintf(out_file, " coord Coordinate {\n"); fprintf(out_file, " point [\n"); for (j=0; j 0) { fprintf(out_file, " geometry IndexedLineSet {\n"); fprintf(out_file, " coord Coordinate {\n"); fprintf(out_file, " point [\n"); for (j=0; j0 && mat[0][2]<0 && mat[1][2]<0) tran[5]=-tran[5]; if (mat[0][1]<0 && mat[0][2]>0 && mat[1][2]<0) tran[4]=-tran[4]; if (mat[0][1]<0 && mat[0][2]<0 && mat[1][2]>0) tran[3]=-tran[3]; return; } tran[6] = acos(c); tran[3] = (mat[1][2]-mat[2][1])/(2*sin(tran[6])); tran[4] = (mat[2][0]-mat[0][2])/(2*sin(tran[6])); tran[5] = (mat[0][1]-mat[1][0])/(2*sin(tran[6])); return; } void insert(int q[Q_LENGTH], int k, int n) { int i; for (i=Q_LENGTH-1; i>k; i--) { if (q[i] == -1) continue; else q[i+1] = q[i]; } q[k+1]=n; } int q_length(int q[Q_LENGTH]) { int i; for (i=0; i