Пример #1
0
void add_connection_link(CrItem *group, struct point p1, struct point p2,
						struct point p3, struct point p4, gboolean has_end_point)
{
	CrItem *link1, *link2;
	CrItem *upper_line;
	guint color;

/*	printf("Add connection link \n");

	printf("[%d %d]\n",p1.x, p1.y);
	printf("[%d %d]\n",p2.x, p2.y);
	printf("[%d %d]\n",p3.x, p3.y);
	printf("[%d %d]\n",p4.x, p4.y);
*/
	/* Draw connection link 1 */
	if(has_end_point){
		link1 = cr_vector_new(group, p1.x , p1.y,
					p3.x - p1.x, p3.y - p1.y,
					"outline_color_rgba", LINK_CONNECTION_COLOR,
					"end_scaleable", FALSE,
					"line_scaleable", FALSE,
					"line_width", 1.5,
					NULL);
	}

	/* Draw connection link 2 */
	link2 = cr_vector_new(group, p2.x , p2.y,
				p4.x - p2.x, p4.y - p4.y,
				"outline_color_rgba", LINK_CONNECTION_COLOR,
				"end_scaleable", FALSE,
				"line_scaleable", FALSE,
				"line_width", 1.5,
				NULL);

	/* Draw line up timeline */
	upper_line = cr_vector_new(group, p3.x , p3.y,
				p4.x - p3.x, p4.y - p3.y,
				"outline_color_rgba", LINK_CONNECTION_UPPER_COLOR,
				"end_scaleable", FALSE,
				"line_scaleable", FALSE,
				"line_width", 1.5,
				NULL);

	/* Draw circles */
	add_circle(group, p3);
	add_circle(group, p4);

}
Пример #2
0
/*======== void generate_sphere() ==========
  Inputs:   struct matrix * points
	double cx
	double cy
	double r
	double step  
  Returns: 

  Generates all the points along the surface of a 
  sphere with center (cx, cy) and radius r

  Adds these points to the matrix parameter

  03/22/12 11:30:26
  jdyrlandweaver
  ====================*/
void generate_sphere( struct matrix * points, 
											double cx, double cy, double r, 
											double step ) {
  double i;
  struct matrix * temp;
	/* from notes */
	/*  1     0        0     0   rcos(theta)   x */
	/* 	0 cos(phi) -sin(phi) 0 * rsin(theta) = y */
	/* 	0 sin(phi)  cos(phi) 0       1         z */
	/* 	0     0        0     1       0           */

	
	for (i = 0; i < M_PI / 2; i += step) {
		add_circle(points, cx, cy, r, step);

		temp = make_translate(-1*cx, 0, 0);

		//rotate
		matrix_mult(make_rotX(i), temp);
		matrix_mult(temp, points);

		temp = make_translate(cx, 0, 0);
		matrix_mult(temp, points);
	}
}  
Пример #3
0
void add_comunication_link(CrItem *group, struct point p1, struct point p2, int comunication_type)
{
	CrItem *link;

	unsigned long long int color;

	if(comunication_type == EVENT_CONNECTION)
		color = LINK_CONNECTION_COLOR;
	else if(comunication_type == EVENT_INPUT)
		color = LINK_RIGHT_COLOR;
	else if(comunication_type == EVENT_OUTPUT)
		color = LINK_LEFT_COLOR;

	link = cr_vector_new(group, p1.x , p2.y,
					p2.x - p1.x, p2.y - p1.y,
					"outline_color_rgba", color,
					"fill_color_rgba", color,
					"end_scaleable", FALSE,
					"line_scaleable", FALSE,
					"line_width", 1.5,
					NULL);

	/*add_circle(link, p1);*/
	if(comunication_type == EVENT_CONNECTION)
		add_circle(group, p2);
	else
		cr_arrow_new(link, 0, NULL);
}
Пример #4
0
/*======== void generate_torus() ==========
  Inputs:   struct matrix * points
	double cx
	double cy
	double r
	double step  
  Returns: 

  Generates all the points along the surface of a 
  tarus with center (cx, cy) and radii r1 and r2

  Adds these points to the matrix parameter

  jdyrlandweaver
  ====================*/
void generate_torus( struct matrix * points, 
										 double cx, double cy, double r1, double r2, 
										 double step ) {
  double i;
  struct matrix * temp;
  
  for (i = 0; i < M_PI; i += step) {
    add_circle(points, cx, cy, r2 - r1, step);
    
    temp = make_translate(-1*cx + r1, 0, 0);
    matrix_mult(make_rotZ(i), temp);
    matrix_mult(temp, points);

    temp = make_translate(cx - r1, 0, 0);
    matrix_mult(temp, points);
  }
}
Пример #5
0
void GCSWrapper::point_segment_coincidence(int id1, int id2)	// id1 = point, id2 = line
{
	SaPoint* p = (SaPoint*)get_shape(id1);
	SaLine* l1 = (SaLine*)get_shape(id2);

	double l1_lgh;
	double l1_midx;
	double l1_midy;
	calculate_line_length(l1, l1_lgh);
	calculate_line_midpoint(l1, l1_midx, l1_midy);

    //int p = add_point(5.0, 5.0);
	//int c = add_circle(10.0, 10.0, 3.0);
	int c = add_circle(l1_midx, l1_midy, l1_lgh/2);
	SaCircle* circle = (SaCircle*)get_shape(c);


	coincident_line_circle(id2, c);	
	//solve();
	gcs_sys.addConstraintPointOnLine(circle->get_gcs_circle().center, l1->get_gcs_line(), 1);
	//solve();

	collinear_point_line(id1, id2);
	//solve();
	
	double l2_x1, l2_y1, l2_x2, l2_y2;
	calculate_rotate_line(l1, l2_x1, l2_y1, l2_x2, l2_y2);
	int l2 = add_segment(l2_x1, l2_y1, l2_x2, l2_y2);

	perpendicular(id2, l2);
	//solve();
	
	coincident_line_circle(l2, c);
	//solve();

	//collinear_point_line(p, id2);
	collinear_point_line(id1, l2);
	//solve();
	
	 //circle->get_gcs_circle().center;	
}
Пример #6
0
int process_circle_general(Drawing d, int fill, PROCESSOR_RESULT result) {
	double x, y, r, t;
	if (stack_size(d->stack) < 4) {
		return throw_error("Impossivel realizar operação. Motivo: há menos elementos na pilha do que o necessário (stack underflow).\n", result);

	}

	// printf("[DEBUG] Showing stack before creating circle.\n");
	// process_show(d->stack);
	
	t = stack_pop(d->stack);
	r = stack_pop(d->stack);
	y = stack_pop(d->stack);
	x = stack_pop(d->stack);

	if (t < 0 || r < 0) {
		return throw_error("Impossivel realizar operação. Motivo: ao criar um circulo, a grossura e o raio não podem ser negativos.\n", result);
	}

	return add_circle(d, x, y, r, t, fill) ? PROCESSOR_SUCCESS : result;
}
Пример #7
0
/*======== 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");
}
Пример #8
0
/*======== 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");
}
Пример #9
0
/*======== 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");
}
Пример #10
0
/*======== 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");
}
Пример #11
0
/*======== 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);  
  }
}
Пример #12
0
/*======== 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;
        }
    }
}
Пример #13
0
void DebugLine::add_sphere(const Vector3& center, const f32 radius, const Color4& color, u32 segments)
{
	add_circle(center, radius, VECTOR3_XAXIS, color, segments);
	add_circle(center, radius, VECTOR3_YAXIS, color, segments);
	add_circle(center, radius, VECTOR3_ZAXIS, color, segments);
}
Пример #14
0
/*======== 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); 
  }
}
Пример #15
0
Файл: parser.c Проект: stuydw/3d
/*======== 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;
    }
    }
    }
Пример #16
0
/*======== 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);
  
}
Пример #17
0
/*======== 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");
}
Пример #18
0
/*======== 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);
    }
  }
}
Пример #19
0
/*======== 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
CollisionManager::add_point(int object_id, float x, float y)
{
    add_circle(object_id, x, y, .5f);
}
Пример #21
0
/*======== 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");
    }
  }
}
Пример #22
0
/*======== 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);
		}
  }
}