void Graphic31::Bspline_curve_to ( Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2 ) { Coord p1x = (x + x + x1) / 3; Coord p1y = (y + y + y1) / 3; Coord p2x = (x + x + x2) / 3; Coord p2y = (y + y + y2) / 3; Coord p3x = (x1 + x1 + x) / 3; Coord p3y = (y1 + y1 + y) / 3; add_curve((p1x + p2x) / 2, (p1y + p2y) / 2, p3x, p3y, p1x, p1y); }
Ellipse31::Ellipse31 ( const Brush* brush, const Color* stroke, const Color* fill, Coord x, Coord y, Coord rx, Coord ry, Transformer* t ) : Graphic31(brush, stroke, fill, nil, true, true, 25, t) { float px0 = p0 * rx, py0 = p0 * ry; float px1 = p1 * rx, py1 = p1 * ry; float px2 = p2 * rx, py2 = p2 * ry; float px3 = p3 * rx, py3 = p3 * ry; float px4 = p4 * rx, py4 = p4 * ry; add_point(x + rx, y); add_curve(x + px2, y + py2, x + px0, y + py4, x + px1, y + py3); add_curve(x, y + ry, x + px3, y + py1, x + px4, y + py0); add_curve(x - px2, y + py2, x - px4, y + py0, x - px3, y + py1); add_curve(x - rx, y, x - px1, y + py3, x - px0, y + py4); add_curve(x - px2, y - py2, x - px0, y - py4, x - px1, y - py3); add_curve(x, y - ry, x - px3, y - py1, x - px4, y - py0); add_curve(x + px2, y - py2, x + px4, y - py0, x + px3, y - py1); add_curve(x + rx, y, x + px1, y - py3, x + px0, y - py4); }
Circle::Circle ( const Brush* brush, const Color* stroke, const Color* fill, Coord x, Coord y, Coord r, Transformer* t ) : Graphic(brush, stroke, fill, nil, true, true, 25, t) { float px0 = p0 * r, py0 = p0 * r; float px1 = p1 * r, py1 = p1 * r; float px2 = p2 * r, py2 = p2 * r; float px3 = p3 * r, py3 = p3 * r; float px4 = p4 * r, py4 = p4 * r; add_point(x + r, y); add_curve(x + px2, y + py2, x + px0, y + py4, x + px1, y + py3); add_curve(x, y + r, x + px3, y + py1, x + px4, y + py0); add_curve(x - px2, y + py2, x - px4, y + py0, x - px3, y + py1); add_curve(x - r, y, x - px1, y + py3, x - px0, y + py4); add_curve(x - px2, y - py2, x - px0, y - py4, x - px1, y - py3); add_curve(x, y - r, x - px3, y - py1, x - px4, y - py0); add_curve(x + px2, y - py2, x + px4, y - py0, x + px3, y - py1); add_curve(x + r, y, x + px1, y - py3, x + px0, y - py4); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) sphere: add a sphere to the edge matrix - takes 3 arguemnts (cx, cy, r) torus: add a torus to the edge matrix - takes 4 arguemnts (cx, cy, r1, r2) box: add a rectangular prism to the edge matrix - takes 6 arguemnts (x, y, z, width, height, depth) clear: clear the currnt edge matrix - takes 0 arguments ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; struct stack* shakestack; //relative coordinate system stack int draw_mode=-1; //equals 0 if draw_edges, equals 1 if draw_polygons shakestack = new_stack(); g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); draw_mode = 0; } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { //printf("BEZIER\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); draw_mode = 1; } else if (strncmp(line, "sphere", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 10); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 1; } else if (strncmp(line, "torus", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 10); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 1; } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); //matrix_mult(transform, pm); matrix_mult(shakestack->data[ shakestack->top ], pm); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { clear_screen(s); if( draw_mode==0 ) { draw_lines(pm, s, g); } else { draw_polygons(pm, s, g); } display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); if( draw_mode==0 ) { draw_lines(pm, s, g); } else { draw_polygons(pm, s, g); } save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } //==================STACK COMMANDS================================= else if( strncmp(line, "push", strlen(line)) == 0 ) { push(shakestack); print_stack(shakestack); } else if( strncmp(line, "pop", strlen(line)) == 0 ) { pop(shakestack); print_stack(shakestack); } else if ( line[0] == '#' ) { printf("Invalid command\n"); } //adding color coommand================== else if( strncmp(line, "color", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%i %i %i", &x, &y, &z); g.red = x; g.green = y; g.blue = z; } else { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
Curve *rna_Main_curves_new(Main *UNUSED(bmain), const char *name, int type) { Curve *cu= add_curve(name, type); id_us_min(&cu->id); return cu; }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) h: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) b: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f = fopen(filename, "r"); if (f == NULL) { printf("%s: %s\n", filename, strerror(errno)); } color c; c.red = 255; c.green = 255; c.blue = 255; char buffer[100]; while (fgets(buffer, 100, f)) { /*printf("%c\n", buffer[0]);*/ switch (buffer[0]) { case 'l': { int x0, y0, z0, x1, y1, z1; fscanf(f, "%d %d %d %d %d %d\n", &x0, &y0, &z0, &x1, &y1, &z1); add_edge(pm, x0, y0, z0, x1, y1, z1); break; } case 'c': { int cx, cy, r; fscanf(f, "%d %d %d\n", &cx, &cy, &r); add_circle(pm, cx, cy, r, .001); break; } case 'h': { double x0, y0, x1, y1, x2, y2, x3, y3; fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, HERMITE_MODE); break; } case 'b': { double x0, y0, x1, y1, x2, y2, x3, y3; fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, BEZIER_MODE); break; } case 'i': transform = new_matrix(4, 4); ident(transform); break; case 's': { double sx, sy, sz; fscanf(f, "%lf %lf %lf\n", &sx, &sy, &sz); struct matrix *scale = make_scale(sx, sy, sz); matrix_mult(scale, transform); break; } case 't': { double tx, ty, tz; fscanf(f, "%lf %lf %lf\n", &tx, &ty, &tz); struct matrix *translate = make_translate(tx, ty, tz); matrix_mult(translate, transform); break; } break; case 'x': { double theta; fscanf(f, "%lf\n", &theta); struct matrix *rotX = make_rotX(rad(theta)); matrix_mult(rotX, transform); break; } break; case 'y': { double theta; fscanf(f, "%lf\n", &theta); struct matrix *rotY = make_rotY(rad(theta)); matrix_mult(rotY, transform); break; } break; case 'z': { double theta; fscanf(f, "%lf\n", &theta); struct matrix *rotZ = make_rotZ(rad(theta)); matrix_mult(rotZ, transform); break; } break; case 'a': matrix_mult(transform, pm); break; case 'v': clear_screen(s); draw_lines(pm, s, c); display( s ); break; case 'g': { clear_screen(s); draw_lines(pm, s, c); char filename_buffer[100]; fgets(filename_buffer, 100, f); save_extension(s, filename_buffer); break; } case 'q': return; } } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; char next[256]; char *next_arg; color c; c.red=255;c.blue=255;c.green=25; double args[52]; double DEFAULT_STEP=0.01; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; if (strcmp(line,"apply")==0){ matrix_mult(transform,pm); } else if (strcmp(line,"display")==0){ draw_lines(pm,s,c); display(s); } else if (strcmp(line,"quit")==0){ exit(0); } else if (strcmp(line,"ident")==0){ ident(transform); } else{ fgets(next,255,f); next[strlen(next)-1]='\0'; next_arg=next; int i = 0; while (next_arg != 0){ args[i]=strtod(strsep(&next_arg," "),NULL); i++; } if (strcmp(line,"save")==0){ fgets(next,255,f); save_extension(s,next); } } if (strcmp(line,"line")==0){ add_edge(pm,args[0],args[1],args[2],args[3],args[4],args[5]); } if (strcmp(line,"circle")==0){ add_circle(pm,args[0],args[1],args[2],DEFAULT_STEP); } if (strcmp(line,"hermite")==0){ add_curve(pm,args[0],args[1],args[4],args[5],args[2]-args[0],args[3]-args[1],args[6]-args[4],args[7]-args[5],DEFAULT_STEP,HERMITE_MODE); } if (strcmp(line,"bezier")==0){ add_curve(pm,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],DEFAULT_STEP,BEZIER_MODE); } if (strcmp(line,"scale")==0){ matrix_mult(make_scale(args[0],args[1],args[2]),transform); } if (strcmp(line,"translate")==0){ matrix_mult(make_translate(args[0],args[1],args[2]),transform); } if (strcmp(line,"xrotate")==0){ matrix_mult(make_rotX(M_PI*args[0]/180.0),transform); } if (strcmp(line,"yrotate")==0){ matrix_mult(make_rotY(M_PI*args[0]/180.0),transform); } if (strcmp(line,"zrotate")==0){ matrix_mult(make_rotZ(M_PI*args[0]/180.0),transform); } printf(":%s:\n",line); } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) sphere: add a sphere to the edge matrix - takes 3 arguemnts (cx, cy, r) torus: add a torus to the edge matrix - takes 4 arguemnts (cx, cy, r1, r2) box: add a rectangular prism to the edge matrix - takes 6 arguemnts (x, y, z, width, height, depth) clear: clear the currnt edge matrix - takes 0 arguments ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; struct stack * STACK = new_stack(); g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if (strncmp(line, "sphere", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 10); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if (strncmp(line, "torus", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 10); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "print", strlen(line)) == 0) { print_matrix( STACK->data[ STACK->top ] ); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; //clear_screen(s); //draw_polygons(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if ( strncmp(line, "push", strlen(line)) == 0 ) { push( STACK ); //seg fault printf("Pushed\n"); } else if ( strncmp(line, "pop", strlen(line)) == 0 ) { pop( STACK ); printf("Popped\n"); } else if ( line[0] != '#' ) { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { color c; c.red=MAX_COLOR; c.blue=0; c.green=0; double step=10000; FILE *f; char line[256]; double x0,y0,z0,x1,y1,z1; double cx,cy,radius; double x2,y2,x3,y3; double sx,sy,sz; double tx,ty,tz; double theta; if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; printf(":%s:\n",line); if (strcmp(line,"line") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf %lf %lf %lf",&x0,&y0,&z0,&x1,&y1,&z1); printf("%f %f %f %f %f %f\n",x0,y0,z0,x1,y1,z1); add_edge(pm,x0,y0,z0,x1,y1,z1); } if (strcmp(line,"circle") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf",&cx,&cy,&radius); add_circle(pm,cx,cy,radius,step); } if (strcmp(line,"hermite") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3); add_curve(pm,x0,y0,x1,y1,x2,y2,x3,y3,step,2); } if (strcmp(line,"bezier") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3); add_curve(pm,x0,y0,x1,y1,x2,y2,x3,y3,step,1); } if (strcmp(line,"ident") == 0) ident(transform); if (strcmp(line,"scale") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf",&sx,&sy,&sz); struct matrix *scale = make_scale(sx,sy,sz); matrix_mult(scale,transform); free_matrix(scale); } if (strcmp(line,"translate") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf %lf %lf",&tx,&ty,&tz); struct matrix *translate = make_translate(tx,ty,tz); matrix_mult(translate,transform); free_matrix(translate); } if (strcmp(line,"xrotate") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf",&theta); struct matrix *rotX = make_rotX((theta/180.)*M_PI); matrix_mult(rotX,transform); free_matrix(rotX); } if (strcmp(line,"yrotate") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf",&theta); struct matrix *rotY = make_rotY((theta/180.)*M_PI); matrix_mult(rotY,transform); free_matrix(rotY); } if (strcmp(line,"zrotate") == 0){ fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); sscanf(line,"%lf",&theta); struct matrix *rotZ = make_rotZ((theta/180.)*M_PI); matrix_mult(rotZ,transform); free_matrix(rotZ); } if (strcmp(line,"apply") == 0) matrix_mult(transform,pm); if (strcmp(line,"display") == 0){ clear_screen(s); draw_lines(pm,s,c); display(s); } if (strcmp(line,"save") == 0){ clear_screen(s); fgets(line,255, f); line[strlen(line)-1]='\0'; printf(":%s:\n",line); draw_lines(pm,s,c); save_extension(s,line); } if (strcmp(line,"quit") == 0) exit(42); } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) h: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) b: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) 03/08/12 16:22:10 jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; double step = .025; color g; g.red = 255; g.green = 80; g.blue = 80; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); char c; double x, y, z, x0, y0, x1, y1, z1, x2, y2, x3, y3, cx, cy, r; c = line[0]; switch (c) { case 'l': // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 's': //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 't': //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 'x': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); break; case 'y': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); break; case 'z': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); break; case 'c': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &cx, &cy, &r); add_circle(pm, cx, cy, r, step); break; case 'h': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 0); break; case 'b': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 1); break; case 'i': ident(transform); break; case 'a': //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); break; case 'v': clear_screen(s); draw_lines(pm, s, g); display(s); break; case 'g': fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_lines(pm, s, g); save_extension(s, line); break; case 'q': return; default: printf("Invalid command\n"); break; } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; clear_screen(s); double arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; color c; if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; printf(":%s:\n",line); if (strcmp(line, "quit") == 0){ exit(0); } else if (strcmp(line, "line") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6); add_edge(pm, arg1, arg2, arg3, arg4, arg5, arg6); } else if (strcmp(line, "circle") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3); add_circle(pm, arg1, arg2, arg3, 0.01); } else if (strcmp(line, "hermite") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8); add_curve(pm, arg1, arg2, arg5, arg6, arg3, arg4, arg7, arg8, 0.01, HERMITE_MODE); } else if (strcmp(line, "bezier") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8); add_curve(pm, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, 0.01, BEZIER_MODE); } else if (strcmp(line, "ident") == 0){ ident(transform); } else if (strcmp(line, "scale") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3); matrix_mult(make_scale(arg1, arg2, arg3), transform); } else if (strcmp(line, "translate") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3); matrix_mult(make_translate(arg1, arg2, arg3), transform); } else if (strcmp(line, "xrotate") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf", &arg1); matrix_mult(make_rotX(arg1 * M_PI / 180), transform); } else if (strcmp(line, "yrotate") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf", &arg1); matrix_mult(make_rotY(arg1 * M_PI / 180), transform); } else if (strcmp(line, "zrotate") == 0){ fgets(line, 255, f); line[strlen(line)]='\0'; sscanf(line, "%lf", &arg1); matrix_mult(make_rotZ(arg1 * M_PI / 180), transform); } else if (strcmp(line, "apply") == 0){ matrix_mult(transform, pm); } else if (strcmp(line, "display") == 0){ clear_screen(s); draw_lines(pm, s, c); display(s); } else if (strcmp(line, "save") == 0){ clear_screen(s); draw_lines(pm, s, c); fgets(line, 255, f); line[strlen(line)]='\0'; save_extension(s, line); } else { printf("Parse error\n"); } } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; printf(":%s:\n",line); /* line: add a line to the edge matrix - */ /* takes 6 arguments (x0, y0, z0, x1, y1, z1) */ if (strcmp(line, "line") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; printf("LINE: %s\n", line); char * points[6]; int i; points[0] = strtok(line, " "); for(i = 0; i < 5; i++) points[i+1] = strtok(NULL, " "); add_edge(pm, atof(points[0]), atof(points[1]), atof(points[2]), atof(points[3]), atof(points[4]), atof(points[5])); } /* circle: add a circle to the edge matrix - */ /* takes 3 arguments (cx, cy, r) */ else if (strcmp(line, "circle") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; char * points[3]; int i; points[0] = strtok(line, " "); for(i = 0; i < 2; i++) points[i + 1] = strtok(NULL, " "); add_circle(pm, atof(points[0]), atof(points[1]), atof(points[2]), 1000); } /* hermite: add a hermite curve to the edge matrix - */ /* takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) */ else if (strcmp(line, "hermite") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; char * points[8]; int i; points[0] = strtok(line, " "); for(i = 0; i < 7; i++) points[i + 1] = strtok(NULL, " "); printf("Hermite Curve: %s,%s,%s,%s,%s,%s,%s,%s\n", points[0],points[1],points[2],points[3],points[4],points[5],points[6],points[7]); add_curve(pm, atof(points[0]), atof(points[1]), atof(points[2]), atof(points[3]), atof(points[4]), atof(points[5]), atof(points[6]), atof(points[7]), 100, HERMITE_MODE); } /* bezier: add a bezier curve to the edge matrix - */ /* takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) */ else if (strcmp(line, "bezier") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; char * points[8]; int i; points[0] = strtok(line, " "); for(i = 0; i < 7; i++) points[i + 1] = strtok(NULL, " "); add_curve(pm, atof(points[0]), atof(points[1]), atof(points[2]), atof(points[3]), atof(points[4]), atof(points[5]), atof(points[6]), atof(points[7]), 1000, BEZIER_MODE); } /* ident: set the transform matrix to the identity matrix */ else if (strcmp(line, "ident") == 0) { ident(transform); } /* translate: create a translation matrix, */ /* then multiply the transform matrix by the translation matrix - */ /* takes 3 arguments (tx, ty, tz) */ else if (strcmp(line, "translate") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; char * sX, * sY, * sZ; sX = strtok(line, " "); sY = strtok(NULL, " "); sZ = strtok(NULL, " "); transform = make_translate(atof(sX), atof(sY), atof(sZ)); } /* scale: create a scale matrix, */ /* then multiply the transform matrix by the scale matrix - */ /* takes 3 arguments (sx, sy, sz) */ else if (strcmp(line, "scale") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; char * sX, * sY, * sZ; sX = strtok(line, " "); sY = strtok(NULL, " "); sZ = strtok(NULL, " "); transform = make_scale(atof(sX), atof(sY), atof(sZ)); } /* xrotate: create an x-axis rotation matrix, */ /* then multiply the transform matrix by the rotation matrix - */ /* takes 1 argument (theta) */ else if (strcmp(line, "xrotate") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; matrix_mult(make_rotX(M_PI * atof(line) / 180), transform); } /* yrotate: create an y-axis rotation matrix, */ /* then multiply the transform matrix by the rotation matrix - */ /* takes 1 argument (theta) */ else if (strcmp(line, "yrotate") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; matrix_mult(make_rotY(M_PI * atof(line) / 180), transform); } /* zrotate: create an z-axis rotation matrix, */ /* then multiply the transform matrix by the rotation matrix - */ /* takes 1 argument (theta) */ else if (strcmp(line, "zrotate") == 0) { fgets(line, 255, f); line[strlen(line) - 1] = '\0'; matrix_mult(make_rotZ(M_PI * atof(line) / 180), transform); } /* apply: apply the current transformation matrix to the edge matrix */ else if (strcmp(line, "apply") == 0) { matrix_mult(transform, pm); } /* display: draw the lines of the edge matrix to the screen */ /* display the screen */ else if (strcmp(line, "display") == 0) { color c; c.red = 0; c.green = 250; c.blue = 100; draw_lines(pm, s, c); display(s); clear_screen(s); } /* save: draw the lines of the edge matrix to the screen */ /* save the screen to a file - */ /* takes 1 argument (file name) */ else if (strcmp(line, "save") == 0) { save_extension(s, "curves.png"); } /* quit: end parsing */ else if (strcmp(line, "quit") == 0) { exit(0); } } }
void animated_param_t::add_curves( int num) { for( int i = 0; i < num; ++i) add_curve(); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) sphere: add a sphere to the edge matrix - takes 3 arguemnts (cx, cy, r) torus: add a torus to the edge matrix - takes 4 arguemnts (cx, cy, r1, r2) box: add a rectangular prism to the edge matrix - takes 6 arguemnts (x, y, z, width, height, depth) clear: clear the currnt edge matrix - takes 0 arguments ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { //printf("BEZIER\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); } else if (strncmp(line, "sphere", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 1); //printf( "%lf %lf %lf\n", x, y, z); } else if (strncmp(line, "torus", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 1); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { clear_screen(s); draw_polygons(pm, s, g); //printf("THE ISSUE IS HERE\n"); //display(s); //printf("ISSUE AVERTED\n"); // // //so i have been having repeated issue with imagemagik and thus display doesnt work, so i test by saving as a png save_extension(s, "parser.png"); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_polygons(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if ( line[0] != '#' ) { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; clear_screen(s); color c; c.red = 0; c.green = 0; c.blue = 0; if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { if(strstr(line,"\n")) line[strlen(line)-1]='\0'; if(!strcmp(line, "quit")){ printf("quitting\n"); fclose(f); return; } else if (!strcmp(line, "display")) { draw_lines(pm,s,c); display(s); } else if (!strcmp(line, "save")) { draw_lines(pm,s,c); fgets(line, 256, f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; printf("saving\n"); save_extension(s,line); } else if (!strcmp(line, "apply")){ printf("applying\n"); matrix_mult(transform,pm); } else if (!strcmp(line, "line")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,z0,x1,y1,z1; sscanf(tmp,"%lf %lf %lf %lf %lf %lf", &x0, &y0, &z0, &x1, &y1, &z1); add_edge(pm,x0,y0,z0,x1,y1,z1); } else if (!strcmp(line, "circle")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double cx,cy,r; sscanf(tmp,"%lf %lf %lf",&cx,&cy,&r); add_circle(pm,cx,cy,r,420); printf("drawing a circle\n"); } else if (!strcmp(line, "hermite")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,x1,y1,x2,y2,x3,y3; sscanf(tmp,"%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1-x0,y1-y0, x2, y2, x3-x2, y3-y2,100, HERMITE_MODE); printf("making a hermite curve\n"); } else if (!strcmp(line, "bezier")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,x1,y1,x2,y2,x3,y3; sscanf(tmp,"%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3,100, BEZIER_MODE); printf("making a bezier curve\n"); } else if (!strcmp(line, "ident")){ ident(transform); } else if (!strcmp(line, "scale")){ fgets(line,255,f); if(strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double sx,sy,sz; sscanf(tmp,"%lf %lf %lf",&sx,&sy,&sz); matrix_mult(make_scale(sx,sy,sz),transform); } else if (!strcmp(line, "translate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double tx,ty,tz; sscanf(tmp,"%lf %lf %lf",&tx,&ty,&tz); matrix_mult(make_translate(tx,ty,tz),transform); } else if (!strcmp(line,"xrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta*=180/M_PI; matrix_mult(make_rotX(theta),transform); } else if (!strcmp(line,"yrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta*=180/M_PI; matrix_mult(make_rotY(theta),transform); } else if (!strcmp(line,"zrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta *=180/M_PI; matrix_mult(make_rotZ(theta),transform); } } }
// CONSTRUCTOR DPC_standard_plot::DPC_standard_plot( DPC_datastream_supplier *DS_Supplier, std::vector <DPM_run *> *RunListp, DPM_relation *Relation, DPM_time_constraints *ParentTimeConstraints ) throw (std::invalid_argument) : DPC_plot (Relation) { DPM_time_constraints *my_time_constraints; DPM_time_constraints total_time_constraints; int n_runs, n_curves; int cix, runix; if ( !DS_Supplier ) { throw std::invalid_argument("DS_Supplier is NULL."); } if ( !RunListp ) { throw std::invalid_argument("RunListp is NULL."); } if ( !Relation ) { throw std::invalid_argument("Relation is NULL."); } if ( !ParentTimeConstraints ) { throw std::invalid_argument("ParentTimeConstraints is NULL."); } // Combine the time constraints levied by the plot specification // and those levied by the parent-page. The combined time constraints // will be levied on each of the subordinate curves. my_time_constraints = Relation->getTimeConstraints(); total_time_constraints = *my_time_constraints + *ParentTimeConstraints; n_runs = (int)RunListp->size(); n_curves = Relation->NumberOfCurves(); // for each of the RUNs for (runix = 0 ; runix < n_runs ; runix++) { // for each of the curves for (cix = 0 ; cix < n_curves ; cix ++ ) { DPC_curve *curve; DPM_curve *curve_spec = Relation->getCurve(cix); try { // Make a curve. curve = new DPC_std_curve( curve_spec, (*RunListp)[runix], DS_Supplier, &total_time_constraints ); } catch (const std::logic_error& error) { curve = NULL; std::cerr << error.what() << std::endl; } // Put that new curve object into the curve list. if ( curve) { if ( add_curve( curve) < 0 ) { std::cerr << "ERROR: Rejecting curve." << std::endl; delete curve; } } } } // VALIDATION // 1. We must have added at least 1 valid curve to the list. // 2. If we have more than one curve, all of the ACTUAL units must match. if ( getNumCurves() <= 0) { throw std::invalid_argument("ERROR: DPC_plot does not contain any valid curves."); } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguments (x0, y0, z0, x1, y1, z1) b: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) h: add a bezier to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) m: add a sphere to the edge matrix - takes 3 arguments (cx, cy, r) d: add a torus to the edge matrix - takes 4 arguments (cx, cy, r1, r2) p: add a rectangular prism to the edge matrix - takes 6 arguments (x, y, z, width, height, depth) w: clear the current edge matrix - takes 0 arguments i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) 03/08/12 16:22:10 jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 0; g.green = 255; g.blue = 255; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); char c; double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4; c = line[0]; switch (c) { case 'l': // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'p': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'm': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'd': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'c': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); break; case 'b': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 'h': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 's': //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 't': //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 'x': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); break; case 'y': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); break; case 'z': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); break; case 'i': ident(transform); break; case 'a': //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); break; case 'v': /* clear_screen(s); draw_lines(pm, s, g); display(s); break; */ // Second version for triangles: clear_screen(s); draw_polygons(pm, s, g); display(s); break; case 'w': pm->lastcol = 0; break; case 'g': fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_polygons(pm, s, g); save_extension(s, line); break; case 'q': return; case '#': break; default: printf("Invalid command\n"); break; } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { char temp; float a,b,c,d,e,f,g,h; int i,j; char * ImgName; FILE * file = fopen(filename, "r"); while(fscanf(file,"%c", &temp) ==1){ //add torus if(temp == 'd'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f %f",&a,&b,&c,&d) == 4) add_torus(pm,a,b,c,d,0.01); else printf("somethings wrong that need to be fix in d"); } //add sphere if(temp == 'm'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f", &a,&b,&c) == 3) add_sphere(pm,a,b,c,0.01); else printf("somethings wrong that need to be fix in m"); } //add box point if(temp == 'p'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) ==6) add_box(pm,a,b,c,d,e,f); else printf("somethings wrong that need to be fix in p\n"); } //add edge if(temp == 'l'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) == 6) add_edge(pm,a,b,c,d,e,f); else printf("SomeThings wrong that need to be fix in l\n"); } //add circle if(temp == 'c'){ fscanf(file,"%c", &temp); if(fscanf(file,"%f %f %f",&a,&b,&c) == 3) add_circle(pm,a,b,c,M_PI/20); else printf("fix in c\n"); } //add hermite if(temp == 'h'){ fscanf(file,"%c", &temp); if(fscanf(file,"%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8) add_curve(pm,a,b,c,d,e,f,g,h,0.001,0); else printf("fix in h\n"); } //add bezier if(temp == 'b'){ fscanf(file,"%c", &temp); if(fscanf(file, "%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8) add_curve(pm,a,b,c,d,e,f,g,h,0.001,1); else printf("fix in b\n"); } //ident transform else if(temp == 'i'){ ident(transform); } //scaling else if(temp == 's'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f", &a,&b,&c) == 3){ struct matrix * scalar = make_scale(a,b,c); matrix_mult(scalar,transform); } else printf("Somethings wrong that need to be fix in s\n"); } //Translating else if (temp == 't'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f %f %f", &d,&e,&f) == 3){ struct matrix * tranlator = make_translate(d,e,f); matrix_mult(tranlator, transform); } else printf("somethings wrong that need to be fix in t\n"); } //Rotating with x else if(temp == 'x'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f",&a) ==1){ struct matrix * rotX = make_rotX(a); matrix_mult(rotX,transform); } else{ printf("something wrong that need to be fix in x\n"); } } //Rotating with Y else if(temp == 'y'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f",&b) ==1){ struct matrix * rotY = make_rotY(b); matrix_mult(rotY,transform); } else{ printf("something wrong that need to be fix in y\n"); } } //Rotating with Z else if(temp == 'z'){ fscanf(file,"%c",&temp); if(fscanf(file,"%f",&c) ==1){ struct matrix * rotZ = make_rotZ(a); matrix_mult(rotZ,transform); } else{ printf("something wrong that need to be fix in z\n"); } } //Applying Transformation else if(temp == 'a'){ matrix_mult(transform,pm); } else if(temp == 'v'){ color c; c.red = 122; c.green = 218; c.blue = 225; clear_screen(s); for( i=0; i<XRES; i++){ for ( j=0; j<YRES; j++){ //c.red = random() % (MAX_COLOR + 1); //c.green = random() % (MAX_COLOR + 1); //c.blue = random() % (MAX_COLOR + 1); plot( s, c, i, j); } } c.green = 40; draw_lines(pm,s,c); display(s); } else if(temp == 'g'){ color c; c.red = 122; c.green = 218; c.blue = 225; clear_screen(s); for( i=0; i<XRES; i++){ for ( j=0; j<YRES; j++){ //c.red = random() % (MAX_COLOR + 1); //c.green = random() % (MAX_COLOR + 1); //c.blue = random() % (MAX_COLOR + 1); plot( s, c, i, j); } } c.green = 40; draw_lines(pm,s,c); ImgName = (char *)malloc(256); fscanf(file,"%c",&temp); if(fscanf(file,"%s",ImgName) ==1){ ImgName = strsep(&ImgName, "."); save_ppm(s,ImgName); ImgName = strncat(ImgName,".png",4); save_extension(s,ImgName); free(ImgName); } else printf("Something wrong that need to be fix in g\n"); } else if (temp == 'q'){ return; } } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 255; g.green = 0; g.blue = 255; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; double width, height, depth, radius, radius1, radius2; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { //printf("BEZIER\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "box", strlen(line)) == 0 ) { //printf("BOX\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &width, &height, &depth); add_box(pm, x, y, z, width, height, depth); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "sphere", strlen(line)) == 0 ) { //printf("SPHERE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &radius); add_sphere(pm, x, y, radius, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "torus", strlen(line)) == 0 ) { //printf("TORUS\n");ds fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &radius1, &radius2 ); add_torus(pm, x, y, radius1, radius2, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { clear_screen(s); draw_lines(pm, s, g); display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_lines(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if (strncmp(line, "#", strlen(1)) == 0){ } else { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) h: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) b: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s, color c) { FILE * fp = fopen(filename, "r"); char * lines = (char *)malloc(1); float x0, y0, z0, x1, y1, z1, x2, y2, x3, y3; float step; float cx, cy, r; float x, y, z; float theta; while(fscanf(fp, "%c", lines)) { printf("lines gets: %c \n", *lines); switch (*lines){ case 'c': { fscanf(fp, "%f %f %f %f", &cx, &cy, &r, &step); add_circle(pm, cx, cy, r, step); break; } case 'b': { fscanf(fp, "%f %f %f %f %f %f %f %f %f", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3, &step); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 0); break; } case 'h': { fscanf(fp, "%f %f %f %f %f %f %f %f %f", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3, &step); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 1); break; } case 'l': { fscanf(fp, "%f %f %f %f %f %f", &x0, &y0, &z0, &x1, &y1, &z1); add_edge(pm, x0, y0, z0, x1, y1, z1); break; } case 'i': { ident(transform); break; } case 's': { fscanf(fp, "%f %f %f", &x, &y, &z); struct matrix * scale = make_scale(x, y, z); matrix_mult(scale, transform); free(scale); break; } case 't': { fscanf(fp, "%f %f %f", &x, &y, &z); struct matrix * trans = make_translate(x, y, z); matrix_mult(trans, transform); free(trans); break; } case 'x': { fscanf(fp, "%f", &theta); struct matrix * transx = make_rotX(theta); matrix_mult(transx, transform); free(transx); break; } case 'y': { fscanf(fp, "%f", &theta); struct matrix * transy = make_rotY(theta); matrix_mult(transy, transform); free(transy); break; } case 'z': { fscanf(fp, "%f", &theta); struct matrix * transz = make_rotZ(theta); matrix_mult(transz, transform); free(transz); break; } case 'a': { matrix_mult(transform, pm); break; } case 'v': { clear_screen(s); draw_lines(pm, s, c); display(s); break; } case 'g': { char * file = (char *)malloc(256); fscanf(fp, "%s", file); clear_screen(s); draw_lines(pm, s, c); save_extension(s, file); free(file); break; } case 'q': { exit(0); } default: { exit(1); } } fscanf(fp, "%c", lines); } fclose(fp); free(lines); }