Ejemplo n.º 1
0
void free_all_shapes(struct Shape ** shapes, unsigned int num_shapes)
{
  unsigned int i = 0;
  for (i = 0 ; i < num_shapes ; i++)
    free_shape(shapes[i]);
  free(shapes);
}
Ejemplo n.º 2
0
VALUE method_new_shape(VALUE self)
{
  struct Shape * shape = new_shape();
  VALUE shape_rb = rb_shape_new(shape);
  free_shape(shape);
  return shape_rb;
}
Ejemplo n.º 3
0
VALUE method_read_one(VALUE self, VALUE fp)
{
  struct Shape * shape = NULL;
  shape = read_shape(GET_STDIO_FILE(fp));
  VALUE shape_rb = rb_shape_new(shape);
  free_shape(shape);
  return shape_rb;
}
Ejemplo n.º 4
0
void TF3D::prepare_char(wchar_t chr)
{
	if (!m_vbo[chr]) {
		font_prepare_chr(font, chr, 1);
		free_shape(&font->cshape[chr]);
		m_vbo[chr] = genBuffers(chr, font->cedges[chr]);
	}
}
Ejemplo n.º 5
0
void free_tree(tree_node_t *root)
{
	if (root->left != NULL)
		free_tree(root->left);
	if (root->right != NULL)
		free_tree(root->right);
	free_shape(root->curve);
	free(root);
}
Ejemplo n.º 6
0
VALUE method_read_each(VALUE self, VALUE fp)
{
  struct Shape * shape = NULL;
  while ((shape = read_shape(GET_STDIO_FILE(fp))))
  {
    rb_yield(rb_shape_new(shape));
    free_shape(shape);
  }
}
Ejemplo n.º 7
0
int main()
{
  init_geometry();
  shape_t *s = new_taurus(30, 15, 30, 30);
  flushOBJ(stdout);
  free_shape(s);
  finalize_geometry();
  return 0;
}
Ejemplo n.º 8
0
VALUE method_write_shape(VALUE self, VALUE fp, VALUE shape_rb)
{
  if (TYPE(fp) != T_FILE) rb_raise(rb_eArgError, "write_shape() TYPE(fp) = '%d' (in C)", TYPE(fp));
  if (TYPE(shape_rb) != T_HASH) rb_raise(rb_eArgError, "write_shape() expects a file pointer like STDIN and a shape");
  
  struct Shape * shape = shape_rb_to_shape(shape_rb);
  VALUE shape_again = rb_shape_new(shape);
  
  write_shape(GET_STDIO_FILE(fp), shape);
  free_shape(shape);
}
Ejemplo n.º 9
0
int main()
{
  init_geometry();
  shape_t *s = new_shape();
  add_vertex(s, new_vertex(0, 300, 0));
  add_vertex(s, new_vertex(20, 300, 0));
  add_vertex(s, new_vertex(20, 290, 0));
  add_vertex(s, new_vertex(300, 220, 0));
  add_vertex(s, new_vertex(295, 218, 0));
  add_vertex(s, new_vertex(60, 260, 0));
  add_vertex(s, new_vertex(5, 220, 0));
  add_vertex(s, new_vertex(5, 0, 0));
  add_vertex(s, new_vertex(0, 0, 0));
  shape_t *u = new_revolution(s, 60);
  flushOBJ(stdout);
  free_shape(s);
  free_shape(u);
  finalize_geometry();
  return 0;
}
Ejemplo n.º 10
0
Archivo: shape.c Proyecto: llbit/cttf
shape_t* load_shape(FILE* file)
{
	char	buf[64];
	shape_t* shape;

	assert(file != NULL);

	shape = new_shape();

	while (1 == fread(buf, 3, 1, file)) {
		buf[3] = '\0';
		if (!strcmp(buf, "v: ")) {
			float	x;
			float	y;
			/* this is a vector */
			if (2 != fscanf(file, "%f, %f\n", &x, &y)) {
				fprintf(stderr, "could not parse shape file\n");
				free_shape(&shape);
				return NULL;
			}
			shape_add_vec(shape, x, y);

		} else if (!strcmp(buf, "s: ")) {
			int	n;
			int	m;
			/* this is a segment */
			if (2 != fscanf(file, "%d, %d\n", &n, &m)) {
				fprintf(stderr, "could not parse shape file\n");
				free_shape(&shape);
				return NULL;
			}
			shape_add_seg(shape, n, m);
		} else {
			/* error! */
			fprintf(stderr, "unexpected character sequence in shape file: %3s\n", buf);
			free_shape(&shape);
			return NULL;
		}
	}
	return shape;
}
Ejemplo n.º 11
0
int main()
{
  init_geometry();
  shape_t *planet = new_sphere(50, 20, 20);
  shape_t *sp1 = new_sphere(10, 16, 16);
  shape_t *sp2 = new_sphere(10, 16, 16);
  shape_t *t1 = new_taurus(30, 4, 50, 10);
  shape_t *t2 = new_taurus(30, 4, 40, 10);
  shape_translate(sp1, 80, 70, 40);
  shape_translate(sp2, -90, 10, -60);
  shape_scale(t1, 5, 1.2, 5);
  shape_scale(t2, 3.5, 1.2, 3.5);
  shape_rotate(t1, -20, 0, 0, 1);
  shape_rotate(t2, 20, 0, 0, 1);
  flushOBJ(stdout);
  free_shape(planet);
  free_shape(t1);
  free_shape(t2);
  finalize_geometry();
  return 0;
}
Ejemplo n.º 12
0
void free_flp_desc(flp_desc_t *flp_desc)
{
	int i;
	for (i=0; i < flp_desc->n_units; i++) {
		/* wrapped L2 doesn't have a shape curve	*/
		if (flp_desc->units[i].shape)
			free_shape(flp_desc->units[i].shape);
		free(flp_desc->wire_density[i]);
	}
	free(flp_desc->units);
	free(flp_desc->wire_density);
	free(flp_desc);
}
Ejemplo n.º 13
0
int reduce_by_attribute(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  char name[21] = "";
  char value[200] = "";
  int invert_search = 0;
  int c;
  while ((c = getopt(argc, argv, "n:v:i")) != -1)
  switch (c)
  {
    case 'n':
      strncpy(name, optarg, 20);
      break;
    case 'v':
      strncpy(value, optarg, 20);
      break;
    case 'i':
      invert_search = 1;
      break;
    default:
      abort();
  }
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    if (shape->num_attributes == 0) continue;
    if (!invert_search)
    {
      if (strcmp(get_attribute(shape, name), value)!=0) continue;
    }
    else
    {
      if (strcmp(get_attribute(shape, name), value)==0) continue;
    }
    
    write_shape(pipe_out, shape);
    free_shape(shape);
  }
}
int add_color_from_source_interpolation(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  char filename[300] = "";
  int num_attributes = -1;
  int c;
  while ((c = getopt(argc, argv, "f:a:")) != -1)
  switch (c)
  {
    case 'f': strncpy(filename, optarg, 300); break;
    case 'a': num_attributes = atoi(optarg); break;
    default: abort();
  }
  
  if (filename[0] == 0) { fprintf(stderr, "%s: ERROR, -f [filename] required.\n", argv[0]); return EXIT_FAILURE; }
  
  FILE * fp = fopen(filename, "r");
  if (fp == NULL) { fprintf(stderr, "%s: ERROR, -f '%s' is not a file or does not exist.\n", argv[0], filename); return EXIT_FAILURE; }
  struct Shape * source_shape = read_shape(fp);
  fclose(fp);
  
  if (source_shape->num_vertex_arrays != 1) { fprintf(stderr, "%s: ERROR, source shape has %d vertex_arrays (should be only 1)\n", argv[0], source_shape->num_vertex_arrays); return EXIT_FAILURE; }
  if (source_shape->num_vertexs == 0) { fprintf(stderr, "%s: ERROR, source shape has no vertexs\n", argv[0]); return EXIT_FAILURE; }
  //if (source_shape->vertex_arrays[0].num_dimensions != 1) { fprintf(stderr, "%s: ERROR, source shape has %d dimensions, supports only 1\n", argv[0], source_shape->vertex_arrays[0].num_dimensions); return EXIT_FAILURE; }
  
  //write_shape(pipe_out, source_shape);
  struct BBox * source_bbox = get_bbox(source_shape, NULL);
  
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    if (shape->vertex_arrays[0].num_dimensions != source_shape->vertex_arrays[0].num_dimensions)
    {
      fprintf(stderr, "%s: ERROR, shape has %d dimensions, source shape has %d\n", argv[0], shape->vertex_arrays[0].num_dimensions, source_shape->vertex_arrays[0].num_dimensions);
      return EXIT_FAILURE;
    }
    
    struct VertexArray * cva = get_or_add_array(shape, GL_COLOR_ARRAY);
    set_num_dimensions(shape, 1, shape->vertex_arrays[0].num_dimensions);
    
    struct BBox * bbox = get_bbox(shape, NULL);
    
    int i;
    for (i = 0 ; i < shape->num_vertexs ; i++)
    {
      float j = i / (float)shape->num_vertexs * (float)source_shape->num_vertexs;
      
      float * v = get_vertex(shape, 0, i);
      float * cv = get_vertex(shape, 1, i);
      
      float * s1 = get_vertex(source_shape, 0, (int)floor(j));
      //float * s2 = ((int)ceil(j) == shape->num_vertexs) ? s1 : get_vertex(source_shape, 0, (int)ceil(j));
      
      // kbfu, should table s2 into account, would produce a better result
      
      int d;
      for (d = 0 ; d < shape->vertex_arrays[0].num_dimensions ; d++)
      {
        float * sv = get_vertex(source_shape, 0, (int)floor(j));
        cv[d] = (s1[d] - bbox->minmax[d].min) / (bbox->minmax[d].max - bbox->minmax[d].min) + bbox->minmax[d].min;
      }
      //float value = (s1[0] - bbox->minmax[0].min) / (bbox->minmax[0].max - bbox->minmax[0].min) + bbox->minmax[0].min;
      //fprintf(stderr, "s%d[0] = %f s%d[0] = %f\n", (int)floor(j), s1[0], (int)ceil(j), s2[0]);
      
      //cv[0] = value; // i / (float)shape->num_vertexs;
      //cv[1] = 0.0; // i / (float)shape->num_vertexs;
      //cv[2] = 1.0 - value; // i / (float)shape->num_vertexs;
      
      //if (i > 10) break;
    }
    
    // manipulate data here if you like
    write_shape(pipe_out, shape);
    free_shape(shape);
  }
  free_shape(source_shape);
  free_bbox(source_bbox);
}
Ejemplo n.º 15
0
int write_pdf(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  char * filename = argc > 1 ? argv[1] : "output.pdf";
  if (argc == 1)
  {
    fprintf(stderr, "no filename specified, using '%s'\n", filename);
  }
  
  float b[3][2] = {
    {FLT_MAX, -FLT_MAX},
    {FLT_MAX, -FLT_MAX},
    {FLT_MAX, -FLT_MAX}
  };
  
  HPDF_Doc pdf;
  
  pdf = HPDF_New(NULL, NULL); 
  if (!pdf)
  {
    fprintf(stderr, "ERROR: cannot create pdf object.\n");
    return 1;
  }
  
  HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
  HPDF_SetPageMode (pdf, HPDF_PAGE_MODE_USE_OUTLINE);
  
  int num_shapes = 0;
  struct Shape ** shapes = NULL;
  
  HPDF_Page page;
  page = HPDF_AddPage (pdf);
  
  HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_LETTER, HPDF_PAGE_PORTRAIT);
  
  long i = 0;
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    num_shapes++;
    shapes = (struct Shape **)realloc(shapes, sizeof(struct Shape*)*num_shapes);
    if (shapes == NULL) { fprintf(stderr, "realloc for shapes failed :(\n"); exit(0); }
    shapes[num_shapes-1] = shape;
    
    long j;
    for (j = 0 ; j < shape->num_vertexs ; j++)
    {
      float * v = get_vertex(shape, 0, j);
      if (v[0] < b[0][0]) b[0][0] = v[0]; if (v[0] > b[0][1]) b[0][1] = v[0];
      if (v[1] < b[1][0]) b[1][0] = v[1]; if (v[1] > b[1][1]) b[1][1] = v[1];
    }
  }
  
  float dx = b[0][1] - b[0][0];
  float dy = b[1][1] - b[1][0];
  float ratio = dy / dx;
  
  HPDF_Page_SetWidth(page, 2000);
  HPDF_Page_SetHeight(page, 2000.0 * ratio);
  
  for (i = 0 ; i < num_shapes ; i++)
  {
    shape = shapes[i];
    //if (shape->vertex_arrays[0].num_dimensions < 2) { fprintf(stderr, "\n"); continue; }
    
    long j;
    for (j = 0 ; j < shape->num_vertexs ; j++)
    {
      float * v = get_vertex(shape, 0, j);
      if (j == 0)
        HPDF_Page_MoveTo(page, (v[0] - b[0][0]) / dx * 2000.0, (v[1] - b[1][0]) / dy * (2000.0 * ratio));
      else
        HPDF_Page_LineTo(page, (float)(v[0] - b[0][0]) / dx * 2000.0, (float)(v[1] - b[1][0]) / dy * (2000.0 * ratio));
    }
    
    if (shape->gl_type == GL_LINE_LOOP)
    {
      float * v = get_vertex(shape, 0, 0);
      HPDF_Page_LineTo(page, (float)(v[0] - b[0][0]) / dx * 2000.0, (float)(v[1] - b[1][0]) / dy * (2000.0 * ratio));
    }
    
    HPDF_Page_Stroke(page);
    free_shape(shape);
  }
  free(shapes);
  
  HPDF_SaveToFile (pdf, filename);
  
  HPDF_Free(pdf);
}
Ejemplo n.º 16
0
int read_mysql(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  char * sql = (argc > 1) ? argv[1] : "SELECT lon AS x, lat AS y, id AS unique_set_id FROM civicsets.points where created_at > '2011-03-16 15:00:00' and source = 'gps_reporter' ORDER BY unique_set_id";
  
  //exec("tail -n1 read_mysql.sql.log");
  
  char sql_log_filename[100];
  sprintf(sql_log_filename, "%s.sql.log", argv[0]);
  FILE * sql_log = fopen(sql_log_filename, "a");
  if (sql_log != NULL)
  {
    fprintf(sql_log, "%s\n", sql);
    fclose(sql_log);
  }
  else
    fprintf(stderr, "failed to open sql log file '%s'\n", sql_log_filename);
  
  MYSQL mysql;
  
  if ((mysql_init(&mysql) == NULL)) { printf("mysql_init error\n"); return 0; }
  if (!mysql_real_connect(&mysql, "localhost", "root", "", "", 0, NULL, 0))
  {
    fprintf(stderr, "mysql_real_connect error (%s) (password for root should be blank)\n", mysql_error(&mysql));
    return 0;
  }
  
  if (mysql_query(&mysql, sql) == 0)
  {
	  MYSQL_RES * res = mysql_store_result(&mysql);
    MYSQL_ROW row;
    MYSQL_FIELD *field;
    
    int x_field_id = -1;
    int y_field_id = -1;
    int z_field_id = -1;
    int unique_set_field_id = -1;
    
    int r_field_id = -1;
    int g_field_id = -1;
    int b_field_id = -1;
    int a_field_id = -1;
    
    int i=0;
    for (i = 0 ; i < mysql_num_fields(res) ; i++)
    {
      field = mysql_fetch_field_direct(res, i);
      if (strcmp(field->name, "x") == 0) x_field_id = i;
      else if (strcmp(field->name, "y") == 0) y_field_id = i;
      else if (strcmp(field->name, "z") == 0) z_field_id = i;
      else if (strcmp(field->name, "r") == 0) r_field_id = i;
      else if (strcmp(field->name, "g") == 0) g_field_id = i;
      else if (strcmp(field->name, "b") == 0) b_field_id = i;
      else if (strcmp(field->name, "a") == 0) a_field_id = i;
      else if (strcmp(field->name, "unique_set_id") == 0) unique_set_field_id = i;
    }
    if (unique_set_field_id == -1)
    {
      for (i = 0 ; i < mysql_num_fields(res) ; i++)
      {
        field = mysql_fetch_field_direct(res, i);
        if (strcmp(field->name, "id") == 0) unique_set_field_id = i;
      }
    }
    
    if (x_field_id == -1 || y_field_id == -1)
    {
      fprintf(stderr, "at least one field named 'x' and one field named 'y' is required\n");
      return 0;
    }
    if (unique_set_field_id == -1)
    {
      fprintf(stderr, "at least one integer field named 'unique_set_id' is required\n");
      return 0;
    }
    
    struct Shape * shape = NULL;
    int prev_unique_set_id = -1;
    
    while ((row = mysql_fetch_row(res)))
    {
      if (row[unique_set_field_id] == NULL) continue;
      if (atol(row[unique_set_field_id]) != prev_unique_set_id)
      {
        if (shape != NULL)
        {
          if (shape->num_vertexs == 1) shape->gl_type = GL_POINTS;
          write_shape(pipe_out, shape);
          free_shape(shape);
          shape = NULL;
        }
        shape = new_shape();
        shape->gl_type = GL_LINE_STRIP;
        
        shape->unique_set_id = atol(row[unique_set_field_id]);
        if (z_field_id != -1) set_num_dimensions(shape, 0, 3); //shape->vertex_arrays[0].num_dimensions++;
        
        //get_or_add_array(shape, GL_VERTEX_ARRAY); // this is auto, but you, helps doc the code
        
        if (r_field_id != -1 &&
            g_field_id != -1 &&
            b_field_id != -1)
        {
          get_or_add_array(shape, GL_COLOR_ARRAY);
          if (a_field_id != -1) set_num_dimensions(shape, 1, 4);
        }
        
        for (i = 0 ; i < mysql_num_fields(res) ; i++)
        {
          field = mysql_fetch_field_direct(res, i);
          if (strcmp(field->name, "x") != 0 && strcmp(field->name, "y") != 0 && strcmp(field->name, "z") != 0 && 
              strcmp(field->name, "r") != 0 && strcmp(field->name, "g") != 0 && strcmp(field->name, "b") != 0 && strcmp(field->name, "a") != 0 && 
              strcmp(field->name, "id") != 0 && strcmp(field->name, "unique_set_id") != 0)
            set_attribute(shape, field->name, row[i]);
        }
      }
      
      float v[3] = { row[x_field_id]==NULL ? 0 : atof(row[x_field_id]), row[y_field_id]==NULL ? 0 : atof(row[y_field_id]), 0.0 };
      
      if (z_field_id != -1 && row[z_field_id])
        v[2] = atof(row[z_field_id]);
      
      if (r_field_id != -1 &&
        g_field_id != -1 &&
        b_field_id != -1)
      {
        float v2[4] = { atof(row[r_field_id]), atof(row[g_field_id]), atof(row[b_field_id]), 0 };
        if (a_field_id != -1) v2[3] = atof(row[a_field_id]);
        append_vertex2(shape, v, v2);
      }
      else
        append_vertex(shape, v);
      
      prev_unique_set_id = atol(row[unique_set_field_id]);
    }
    if (shape != NULL)
    {
      if (shape->num_vertexs == 1) shape->gl_type = GL_POINTS;
      write_shape(pipe_out, shape);
      free_shape(shape);
      shape = NULL;
    }
    mysql_free_result(res);
  }
  else
  {
    fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
  }
  mysql_close(&mysql);
}
Ejemplo n.º 17
0
int write_json(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  char file_name[300] = "";
  int add_whitespace = 0;
  int c;
  while ((c = getopt(argc, argv, "f:w")) != -1)
  switch (c)
  {
    case 'f': strncpy(file_name, optarg, 300); break;
    case 'w': add_whitespace = 1; break;
    default: abort();
  }
  
  if (file_name[0] == 0 && argc == 2 && argv[1] != NULL && argv[1][0] != '-')
    strncpy(file_name, argv[1], sizeof(file_name));
  
  //char gl_types_c[8][20] = {"GL_POINTS", "GL_LINES", "GL_LINE_LOOP", "GL_LINE_STRIP", "GL_TRIANGLES", "GL_TRIANGLE_STRIP", "GL_TRIANGLE_FAN"};
  
  FILE * fp = NULL;
  if (file_name[0] != 0)
  {
    fp = fopen(file_name, "w");
    if (fp == NULL)
      fprintf(stderr, "%s: ERROR: could not write to '%s'\n", argv[0], file_name);
  }
  
  if (fp == NULL) fp = pipe_out;
  
  int num_shapes = 0;
  fprintf(fp, "[");
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    if (num_shapes != 0) fprintf(fp, ", ");
    
    fprintf(fp, "{%s", (add_whitespace ? "\n" : ""));
    fprintf(fp, "%s\"unique_set_id\":\"%d\",%s", (add_whitespace ? "  " : ""), shape->unique_set_id, (add_whitespace ? "\n" : ""));
    if (shape->num_attributes > 0)
    {
      fprintf(fp, "%s\"attributes\":{%s", (add_whitespace ? "  " : ""), (add_whitespace ? "\n" : ""));
      int i;
      for (i = 0 ; i < shape->num_attributes ; i++)
      {
        struct Attribute * attribute = &shape->attributes[i];
        fprintf(fp, "%s\"%s\":\"%s\"%s%s", (add_whitespace ? "    " : ""), attribute->name, attribute->value, (i == shape->num_attributes-1)?"":",", (add_whitespace ? "\n" : ""));
      }
      fprintf(fp, "%s},%s", (add_whitespace ? "  " : ""), (add_whitespace ? "\n" : ""));
    }
    if (shape->num_vertex_arrays > 0 || shape->num_vertexs > 0)
    {
      fprintf(fp, "%s\"vertex_arrays\":[", (add_whitespace ? "  " : ""));
      int i;
      for (i = 0 ; i < shape->num_vertex_arrays ; i++)
      {
        struct VertexArray * va = &shape->vertex_arrays[i];
        fprintf(fp, "%s{%s", (i!=0?",":""), (add_whitespace ? "\n" : ""));
        fprintf(fp, "%s\"array_type\":\"%s\",%s", (add_whitespace ? "    " : ""), get_array_type_name(va->array_type), (add_whitespace ? "\n" : ""));
        fprintf(fp, "%s\"num_dimensions\":\"%d\",%s", (add_whitespace ? "    " : ""), va->num_dimensions, (add_whitespace ? "\n" : ""));
        fprintf(fp, "%s\"vertexs\":[", (add_whitespace ? "    " : ""));
        int j;
        for (j = 0 ; j < shape->num_vertexs ; j++)
        {
          float * v = get_vertex(shape, i, j);
          if (va->num_dimensions == 1) fprintf(fp, "%f%s", v[0], j==shape->num_vertexs-1?"":",");
          else if (va->num_dimensions == 2) fprintf(fp, "[%f,%f]%s", v[0], v[1], j==shape->num_vertexs-1?"":",");
          else if (va->num_dimensions == 3) fprintf(fp, "[%f,%f,%f]%s", v[0], v[1], v[2], j==shape->num_vertexs-1?"":",");
          else if (va->num_dimensions == 4) fprintf(fp, "[%f,%f,%f,%f]%s", v[0], v[1], v[2], v[3], j==shape->num_vertexs-1?"":",");
        }
        fprintf(fp, "]%s", (add_whitespace ? "\n" : ""));
        fprintf(fp, "%s}", (add_whitespace ? "  " : ""));
      }
      fprintf(fp, "],%s", (add_whitespace ? "\n" : ""));
    }
    fprintf(fp, "%s\"gl_type\":\"%s\"%s", (add_whitespace ? "  " : ""), get_gl_type_name(shape->gl_type), (add_whitespace ? "\n" : ""));
    free_shape(shape);
    fprintf(fp, "}");
    num_shapes++;
  }
  fprintf(fp, "]%s", (add_whitespace ? "\n" : ""));
  
  if (fp != pipe_out) fclose(fp);
}
Ejemplo n.º 18
0
int add_color_from_mysql(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  MYSQL mysql;
  
  if ((mysql_init(&mysql) == NULL)) { printf("mysql_init error\n"); return 0; }
  if (!mysql_real_connect(&mysql, "localhost", "root", "", "ADDRESS_POINT_WGS84", 0, NULL, 0))
  {
    fprintf(stderr, "mysql_real_connect error (%s) (password for root should be blank)\n", mysql_error(&mysql));
    return 0;
  }
  
  int min_cost = 0;
  int max_cost = 0;
  
  char sql[500] = "SELECT MIN(cost), MAX(cost) FROM results WHERE exec_id = 1096411009";
  if (mysql_query(&mysql, sql) == 0)
  {
    MYSQL_RES * res = mysql_store_result(&mysql);
    MYSQL_ROW row;
    row = mysql_fetch_row(res);
    min_cost = atoi(row[0]);
    max_cost = atoi(row[1]);
    mysql_free_result(res);
  }
  
  int count_bad = 0;
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    sprintf(sql, "SELECT cost FROM results WHERE dbf_id = %d AND exec_id = 1096411009 LIMIT 1", shape->unique_set_id);
    if (mysql_query(&mysql, sql) == 0)
    {
  	  MYSQL_RES * res = mysql_store_result(&mysql);
  	  MYSQL_ROW row = mysql_fetch_row(res);
  	  
      if (shape->num_vertex_arrays != 1 || shape->num_vertexs != 1) { fprintf(stderr, "shape->num_vertex_arrays != 1 || shape->num_vertexs != 1\n"); exit(1); }
      shape->num_vertex_arrays ++;
      shape->vertex_arrays = realloc(shape->vertex_arrays, sizeof(struct VertexArray)*shape->num_vertex_arrays);
      struct VertexArray *va = &shape->vertex_arrays[1];
      va->array_type = GL_COLOR_ARRAY;
      va->num_dimensions = 3;
      va->vertexs = (float*)malloc(sizeof(float)*va->num_dimensions*shape->num_vertexs);
      if (row == NULL || row[0] == NULL)
      {
        va->vertexs[0] = 1;
        va->vertexs[1] = 0;
        va->vertexs[2] = 0;
        count_bad ++;
      }
      else
      {
        va->vertexs[0] = atof(row[0]) / (float)(max_cost - min_cost);
        va->vertexs[1] = atof(row[0]) / (float)(max_cost - min_cost);
        va->vertexs[2] = atof(row[0]) / (float)(max_cost - min_cost);
      }
      mysql_free_result(res);
    }
    write_shape(pipe_out, shape);
    free_shape(shape);
  }
  fprintf(stderr, "count_bad = %d\n", count_bad);
}
Ejemplo n.º 19
0
this file isn't finished and doesn't do anything yet - this is so it won't compile.

    int read_gtfs(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
    {
    char * filename = NULL;
    int c;
    while ((c = getopt(argc, argv, "f:")) != -1)
    switch (c)
    {
    case 'f':
    filename = malloc(strlen(optarg)+1);
    strcpy(filename, optarg);
    break;
}

    FILE * fp = filename == NULL ? pipe_in : fopen(filename, "r");

    if (fp == NULL)
    {
    fprintf(pipe_err, "ERROR: Usage: %s -f [filename.csv]\n", argv[0]);
    return -1;
}

    char line[1000];
    struct Shape * shape = NULL;

    int prev_unique_set_id = -1;
    while (fgets(line, sizeof(line), fp))
    {
    char * x = strtok (line, " ,");
    if (x == NULL) continue;
    char * y = strtok (NULL, " ,");
    if (y == NULL) continue;
    char * unique_set_id = strtok (NULL, " ,\n");
    if (unique_set_id == NULL) continue;

    if (shape == NULL || prev_unique_set_id != atoi(unique_set_id))
    {
    if (shape != NULL)
    {
    write_shape(pipe_out, shape);
    free_shape(shape);
}
    shape = new_shape();
    shape->gl_type = GL_POINTS;
    shape->unique_set_id = atoi(unique_set_id);
}
    else
    {
    shape->gl_type = GL_LINE_STRIP;
}

    float v[3] = { atof(x), atof(y), 0.0 };

    append_vertex(shape, v);
    prev_unique_set_id = shape->unique_set_id;
}
    if (shape != NULL)
    {
    write_shape(pipe_out, shape);
    free_shape(shape);
}
    fprintf(stderr, "done\n");
}
int read_walk_distance_via_osm_to_bus_stop_from_iroquois(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  //char filename[300] = "";
  //int num_attributes = -1;
  //int c;
  //while ((c = getopt(argc, argv, "f:a:")) != -1)
  //switch (c)
  //{
  //  case 'f':
  //    strncpy(filename, optarg, 300);
  //    break;
  //  case 'a':
  //    num_attributes = atoi(optarg);
  //    break;
  //  default:
  //    abort();
  //}
  
  
  CURL *curl = NULL;
  CURLcode res;
  struct MemoryStruct chunk;
  
  char url[400];
  
  curl = curl_easy_init();
  
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in))) // address points
  {
    if (shape->num_vertexs > 1) { fprintf(stderr, "This is likely not the data this script was expecting. (was expecting addres points, single vertex shapes)\n"); break; }
    
    float * v = get_vertex(shape, 0, 0);
    
    chunk.memory = NULL;
    chunk.size = 0;
    
    sprintf(url, "http://localhost:3333/walk_distance_to_a_bus_stop?from=%f,%f", v[1], v[0]);
    
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
    res = curl_easy_perform(curl);
    
    if (chunk.size == 0)
    {
      set_attribute(shape, "route_error", "can not find a bus stop");
    }
    else
    {
      char * walk_distance = strtok(chunk.memory, ",");
      char * myttc_stop_id = strtok(NULL, ",");
      char * myttc_stop_name = strtok(NULL, ",");
      if (walk_distance != NULL) set_attribute(shape, "walk_distance", walk_distance);
      if (myttc_stop_id != NULL) set_attribute(shape, "myttc_stop_id", myttc_stop_id);
      if (myttc_stop_name != NULL) set_attribute(shape, "myttc_stop_name", myttc_stop_name);
    }
    
    free(chunk.memory);
    
    // manipulate data here if you like
    write_shape(pipe_out, shape);
    free_shape(shape);
  }
}