Example #1
0
int main(int argc, char *argv[])
{
  Shape* shape1  = new_shape_1d(100);
  Shape* shape2  = new_shape_2d(200, 200);
  Shape* shape3  = new_shape_3d(300, 200, 3);
  Shape* shape4  = new_shape_4d(200, 200, 3, 5000);
  dn_int data[5] = {1,2,3,4,5};
  Shape* shape5  = new_shape(5, data);
  dn_int data_new[4] = {2,3,4,5};
  Shape* shape6  = new_shape(5, data);
  reshape_to(shape6, 4, data_new);
  dn_int data_new_1[6] = {1,1,2,3,4,5};
  reshape_to(shape6,6,data_new_1);
  PRINT_LINE;
  print_shape(shape1);
  PRINT_LINE;
  print_shape(shape2);
  PRINT_LINE;
  print_shape(shape3);
  PRINT_LINE;
  print_shape(shape4);
  PRINT_LINE;
  print_shape(shape5);
  PRINT_LINE;
  print_shape(shape6);
  PRINT_LINE;
  del_shape(shape1);
  del_shape(shape2);
  del_shape(shape3);
  del_shape(shape4);
  del_shape(shape5);
  del_shape(shape6);
  return 0;
}
Example #2
0
void CropLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  const CropParameter& param = this->layer_param_.crop_param();
  int input_dim = bottom[0]->num_axes();
  const int start_axis = bottom[0]->CanonicalAxisIndex(param.axis());

  // Initialize offsets to 0 and the new shape to the current shape of the data.
  offsets = vector<int>(input_dim, 0);
  vector<int> new_shape(bottom[0]->shape());

  // Determine crop offsets and the new shape post-crop.
  for (int i = 0; i < input_dim; ++i) {
    int crop_offset = 0;
    int new_size = bottom[0]->shape(i);
    if (i >= start_axis) {
      new_size = bottom[1]->shape(i);
      if (param.offset_size() == 1) {
        // If only one offset is given, all crops have the same offset.
        crop_offset = param.offset(0);
      } else if (param.offset_size() > 1) {
        // For several offsets, the number of offsets must be equal to the
        // number of dimensions to crop, that is dimensions after the axis.
        crop_offset = param.offset(i - start_axis);
      }
      // Check that the crop and offset are within the dimension's bounds.
      CHECK_GE(bottom[0]->shape(i) - crop_offset, bottom[1]->shape(i))
          << "the crop for dimension " << i << " is out-of-bounds with "
          << "size " << bottom[1]->shape(i) << " and offset " << crop_offset;
    }
    new_shape[i] = new_size;
    offsets[i] = crop_offset;
  }
  top[0]->Reshape(new_shape);
}
Example #3
0
struct VertexArray * get_or_add_array(struct Shape * shape, unsigned int array_type)
{
  if (shape == NULL) shape = new_shape();
  if (shape->vertex_arrays == NULL) return NULL;
  unsigned int i = 0;
  for ( ; i < shape->num_vertex_arrays ; i++)
    if (shape->vertex_arrays[i].array_type == array_type)
      return &shape->vertex_arrays[i];
  
  shape->num_vertex_arrays++;
  shape->vertex_arrays = (struct VertexArray*)realloc(shape->vertex_arrays, shape->num_vertex_arrays*sizeof(struct VertexArray));
  if (shape->vertex_arrays == NULL) { fprintf(stderr, "malloc failed in get_or_add_array()\n"); exit(1); }
  
  struct VertexArray * va = &shape->vertex_arrays[shape->num_vertex_arrays-1];
  va->shape = shape;
  va->array_type = array_type;
  if (array_type == GL_COLOR_ARRAY)
    va->num_dimensions = 3;
  else
    va->num_dimensions = 2;
  va->vertexs = (float*)malloc(sizeof(float)*shape->num_vertexs*va->num_dimensions);
  if (va->vertexs == NULL) { fprintf(stderr, "malloc failed in get_or_add_array()\n"); exit(1); }
  
  return va;
}
Example #4
0
struct Shape * get_shape_from_bbox(struct BBox * bbox)
{
  struct Shape * shape = new_shape();
  struct VertexArray * va = get_or_add_array(shape, GL_VERTEX_ARRAY);
  shape->gl_type = GL_LINE_LOOP;
  va->num_dimensions = bbox->num_minmax;
  float v[5] = {0, 0, 0, 0, 0};
  
  long i;
  for (i = 0 ; i < bbox->num_minmax ; i++)
    v[i] = bbox->minmax[i].min;
  
  if (bbox->num_minmax == 2)
  {
    append_vertex(shape, v);
    v[0] = bbox->minmax[0].max;
    append_vertex(shape, v);
    v[0] = bbox->minmax[0].max;
    v[1] = bbox->minmax[1].max;
    append_vertex(shape, v);
    v[0] = bbox->minmax[0].min;
    v[1] = bbox->minmax[1].max;
    append_vertex(shape, v);
  }
  
  return shape;
}
Example #5
0
void drop_loop(void * tp_void_ptr){
	thread_params * tp_ptr = (thread_params *) tp_void_ptr;

	while(* tp_ptr->running > 0){

		if(check_move(*tp_ptr->s, *tp_ptr->b, 0, 1, 0)){
			tp_ptr->s->y++; 
		}else{

			if(add_to_board(*tp_ptr->s, tp_ptr->b) <= 0){
				* tp_ptr->running = -1;
				break;
			}

			int clears = check_lines(tp_ptr->b);
			if(clears > 0){
				mvprintw(40 + DEBUG_CNT++, 0, "Cleared lines %d", clears);
			}

			* tp_ptr->lines_cleared = * tp_ptr->lines_cleared + clears;

			new_shape(tp_ptr->s, tp_ptr->bucket);

			* tp_ptr->hold_cnt = 0;
		}

		//need method of changing this value as levels increase
		usleep(* tp_ptr->drop_speed);		//100ms
	}
}
Example #6
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;
}
Example #7
0
Ndarray<T> & dot(Ndarray<T> & a, Ndarray<T> & b, bool collect_operand) // only 2D-2D operation is supported.
{
  Shape new_shape(2);
  new_shape[0] = a.shape[0];
  new_shape[1] = b.shape[1];
  auto& ret = zeros<T>(new_shape);

  int I = new_shape[0];
  int J = new_shape[1];
  int K = a.shape[1];

  auto _a = a.buffer;
  auto _b = b.buffer;
  auto _ret = ret.buffer;

  for (int i = 0; i < I; i++) {
    for (int j = 0; j < J; j++) {
      int val = 0;
      for (int k = 0; k < K; k++) {
        val += _a[i * K + k] * _b[k * J + j];
      }
      _ret[i * J + j] = val;
    }
  }

  if (collect_operand) {
    delete &a;
    delete &b;
  }
  return ret;
}
Example #8
0
File: svg.c Project: Melab/gvmt
static Shape treeWalk(Node p) {
    int i, min_len, max_len;
    int sep, half_sep, offset;
    Shape shape;
    if (p->kids[1]) {
        Shape shape0 = treeWalk(p->kids[0]);
        Shape shape1 = treeWalk(p->kids[1]);
        Shape deeper;
        min_len = min(shape0->length, shape1->length);
        sep = 0;
        for (i = 0; i < min_len; i++) {
            sep = max(sep, shape0->items[i].right + shape1->items[i].left);
        }            
        half_sep = sep / 2;
        max_len = max(shape0->length, shape1->length);
        shape = new_shape(max_len + 1);
        for (i = 0; i < min_len; i++) {
            shape->items[i + 1].left = shape0->items[i].left + half_sep;
            shape->items[i + 1].right = shape1->items[i].right + half_sep;
        }
        if (shape0->length > shape1->length) {
            deeper = shape0;
            offset = -half_sep;
        } else {
            deeper = shape1;
            offset = half_sep;
        }
        for (i = min_len; i < max_len; i++) {
            shape->items[i + 1].left = deeper->items[i].left - offset;
            shape->items[i + 1].right = deeper->items[i].right + offset;
        }
        p->kids[0]->x.centre = -half_sep;
        p->kids[1]->x.centre = half_sep;
    } else if (p->kids[0]) {
        Shape kid_shape = treeWalk(p->kids[0]);
        shape = new_shape(kid_shape->length + 1);
        for (i = 0; i < kid_shape->length; i++) {
            shape->items[i + 1] = kid_shape->items[i];
        }
        p->kids[0]->x.centre = 0;
    } else {
        shape = new_shape(1);
    } 
    shape->items[0].left = X_SCALE;
    shape->items[0].right = X_SCALE;
    return shape;
}
Example #9
0
shape_t *copy_shape(const shape_t *s)
{
  shape_t *s2 = new_shape();
  for(unsigned i = 0; i < s->num_vertices; ++i) {
    add_vertex(s2, copy_vertex(s->vertices[i]));
  }
  return s2;
}
Example #10
0
void triangulate_visitor::visit( subd_mesh_model_t& model, shape_t& shape)
{
    if( !shape.attributes().point().has_attribute( g_P_name))
        throw core::runtime_error( core::string8_t( "No P attribute found in triangulate visitor"));

    shape_t new_shape( shape_t::create_subd_mesh());
    subd_mesh_model_t& new_model( shape_cast<subd_mesh_model_t>( new_shape));
    do_visit( model, shape, new_model, new_shape);
}
Example #11
0
gint timeout(gpointer data) {
  if (!(move_shape(0, 1, 0))) {
    detect_lines();
    if (!new_shape()) {
      return 0;
    }
  }
  g_timeout_add(500, timeout, NULL);
  return 0;
}
Example #12
0
void new_game() {
  set_grid_to_zero();
  srand(time(NULL));
  draw_tetramino();
  new_shape();
  fill_current_shape(current_shape.index + 1);
  update_score();
  gtk_widget_queue_draw(application.window);
  g_timeout_add(500, timeout, NULL);
}
Example #13
0
void to_revolution()
{
  init_geometry();
  shape_t *s = new_shape();
  for(unsigned i = 0; i < numpoints; ++i) {
    add_vertex(s, new_vertex(pt[i].x, WINDOW_HEIGHT-pt[i].y, 0));
  }
  new_revolution(s, DIVISION_NUMBER);
  flushOBJ(file_out);
  finalize_geometry();
}
Example #14
0
  void modify(Array& A) const {
    typedef typename Array::size_type size_type;
    std::vector<size_type> old_shape(A.num_dimensions());
    std::vector<size_type> new_shape(A.num_dimensions());

    std::copy(A.shape(),A.shape()+A.num_dimensions(),old_shape.begin());
    std::copy(old_shape.rbegin(),old_shape.rend(),new_shape.begin());

    A.reshape(new_shape);
    A.reshape(old_shape);
  }
Example #15
0
static void do_move_down()
{
	if (shape_landed()) {
		// current piece has hit the bottom; now out of play
		burn_shape();
		new_shape();
	}
	cur_y --;

	timer_generation ++; // reschedule auto-drop timer
	glutTimerFunc(game_time_unit, glut_timer_callback, timer_generation);
}
Example #16
0
static void new_game()
{
	game_over = 0;
	game_score = 0;
	game_update_title();
	timer_generation ++;
	game_time_unit = MSEC_PER_SEC;
	game_clear_all_rows();
	glutTimerFunc(game_time_unit, glut_timer_callback, timer_generation);
	glutPostRedisplay();
	new_shape();
}
Example #17
0
Ndarray<T> & get(Ndarray<T> & a, std::vector<int> axis_idx, bool collect_original)
{
  Shape new_shape(1);
  new_shape[0] = 1;
  auto& ret = empty<T>(new_shape);

  auto idx = std::inner_product(a.shape_cumprod.begin() + 1,
    a.shape_cumprod.end() + 1, axis_idx.begin(), 0);

  *(ret.buffer) = *((a.buffer) + idx);

  return ret;
}
Example #18
0
Ndarray<T> & get(Ndarray<T> & a, std::function<bool(T)> condition, bool collect_original)
{
  auto& ret = array<T,T>(a);
  int cnt = 0;
  for (int i = 0; i < a.shape_prod; i++) {
    auto x = *((a.buffer) + i);
    if (condition(x)) {
      *((ret.buffer) + cnt) = x;
      cnt++;
    }
  }
  Shape new_shape(1);
  new_shape[0] = cnt;
  adjust_shape(ret);
  return ret;
}
Example #19
0
Ndarray<T> & flatten(Ndarray<T> & a, bool collect_operand)
{
  if (collect_operand) {
    a.shape.resize(1);
    a.shape[0] = a.shape_prod;
    adjust_shape(a);
    return a;
  }
  else {
    auto& ret = array<T,T>(a);
    Shape new_shape(1);
    new_shape[0] = a.shape_prod;
    ret.shape = new_shape;
    adjust_shape(ret);
    return ret;
  }
}
Example #20
0
void hold(shape * s, int * h_s, bucket * b){

	//hold empty
	if(* h_s == -1){
		* h_s = s->type;
		int x = s->x;
		int y = s->y;

		new_shape(s, b); 
		s->x = x;
		s->y = y;
	//swap
	}else{
		int type = s->type;
		s->type = * h_s;
		* h_s = type;
	}

}
Example #21
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;
}
Example #22
0
Ndarray<T> & concatenate(Ndarray<T> & a, Ndarray<T> & b, unsigned int axis, bool collect_operand)
{
  Shape new_shape(a.shape);
  new_shape[axis] += b.shape[axis];
  auto& ret = empty<T>(new_shape);

  if (axis == 0) {
    std::memcpy(ret.buffer, a.buffer, a.shape_prod * sizeof(T));
    std::memcpy(((byte*)ret.buffer) + ret.shape_prod * sizeof(T), b.buffer, a.shape_prod * sizeof(T));
  }
  else {
    //TODO after implementation of copy operator
  }

  if (collect_operand) {
    delete &a;
    delete &b;
  }
  return ret;
}
Example #23
0
File: shape.c Project: 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;
}
Example #24
0
struct Shape * shape_rb_to_shape(VALUE shape_rb)
{
  if (TYPE(shape_rb) != T_HASH) rb_raise(rb_eArgError, "rb_shape_to_shape() expects a Hash");
  
  struct Shape * shape = new_shape();
  VALUE v;
  
  v = rb_hash_aref(shape_rb, ENCODED_STR_NEW2("unique_set_id", "ASCII-8BIT"));
  if (TYPE(v) != T_FIXNUM) rb_raise(rb_eArgError, "unique_set_id is not provided or is not a Fixnum");
  shape->unique_set_id = NUM2INT(v);
  
  v = rb_hash_aref(shape_rb, ENCODED_STR_NEW2("version", "ASCII-8BIT"));
  if (TYPE(v) != T_FIXNUM) rb_raise(rb_eArgError, "version is not provided or is not a Fixnum");
  shape->version = NUM2INT(v);
  
  v = rb_hash_aref(shape_rb, ENCODED_STR_NEW2("gl_type", "ASCII-8BIT"));
  if (TYPE(v) != T_FIXNUM) rb_raise(rb_eArgError, "gl_type is not provided or is not a Fixnum");
  shape->gl_type = NUM2INT(v);
  
  VALUE arrays = rb_hash_aref(shape_rb, ENCODED_STR_NEW2("vertex_arrays", "ASCII-8BIT"));
  if (TYPE(arrays) != T_HASH) rb_raise(rb_eArgError, "write_shape() expects a hash with a 'vertex_arrays' key containing a hash (was %d)", TYPE(arrays));
  rb_hash_foreach(arrays, foreach_array, (unsigned long)shape);
  
  VALUE attributes = rb_hash_aref(shape_rb, ENCODED_STR_NEW2("attributes", "ASCII-8BIT"));
  if (TYPE(attributes) != T_ARRAY) rb_raise(rb_eArgError, "write_shape() expects a hash with a 'attributes' key containing an array (was %d)", TYPE(attributes));
  int i;
  for (i = 0 ; i < RARRAY_LEN(attributes) ; i++)
  {
    VALUE v = rb_ary_entry(attributes, i);
    if (TYPE(v) != T_ARRAY) continue;
    
    char * name = RSTRING_PTR(rb_ary_entry(v, 0));
    char * value = RSTRING_PTR(rb_ary_entry(v, 1));
    set_attribute(shape, name, value);
  }
  
  return shape;
}
Example #25
0
Ndarray<T> & get(Ndarray<T> & a, std::vector<slice> slices, bool collect_original)
{
  Shape new_shape(a.shape.size());
  std::transform(slices.begin(), slices.end(), new_shape.begin(), [](auto& s) {return s.get_size();});
  auto& ret = empty<T>(new_shape);

  int cnt_end = std::accumulate(slices.begin(), slices.end(), 1,
    [](int val, auto& s) {return val * s.get_size();});
  std::vector<int> axis_idx(slices.size(), 0);

  int cnt = 0;
  
  while (true) {
    if (cnt == cnt_end) break;

    auto s_it = slices.rbegin();

    for (auto it = axis_idx.rbegin(); it != axis_idx.rend(); ++it, ++s_it)
    {
      if ((*s_it).get_size() == (*it)) {
        *it = 0;
        (*(it + 1))++;
      }
      else break;
    }

    auto idx = std::inner_product(a.shape_cumprod.begin() + 1,
      a.shape_cumprod.end() + 1, axis_idx.begin(), 0);

    *((ret.buffer) + cnt) = *((a.buffer) + idx);

    cnt++;
    (*(axis_idx.rbegin()))++;
  }
    
  return ret;
}
Example #26
0
int main()
{
 int exit = 0;

 init();

 BITMAP *cursor_bmp = load_bitmap("cursor.bmp", NULL);

 VECTOR temp_shape[100];
 LINE_LOOP loop[1000];
 int n = 0, sn = 0, timeout = 100, i, j;

 while(!exit)
  {
   if(keypressed())
    {
     if(key[KEY_ESC]) { exit = 1; }
     if(key[KEY_C] && n > 2 && sn < 1000)
      {
       loop[sn].point = (VECTOR *)malloc(n * sizeof(VECTOR));
       for(i = 0; i < n; i++)
        loop[sn].point[i] = temp_shape[i];
       loop[sn].n = n;
       loop[sn].closed = 1;
       sn++;
       n = 0;
      }

     if(key[KEY_V] && n > 1 && sn < 1000)
      {
       loop[sn].point = (VECTOR *)malloc(n * sizeof(VECTOR));
       for(i = 0; i < n; i++)
        loop[sn].point[i] = temp_shape[i];
       loop[sn].n = n;
       loop[sn].closed = 0;
       sn++;
       n = 0;
      }
    }

   VECTOR cursor;
   cursor.x = (mouse_x / 5) * 5;
   cursor.y = (mouse_y / 5) * 5;
   timeout--;

   if(mouse_b == 1 && timeout < 1 && n < 100)
    {
     int error_flag = 0;
     timeout = 100;

     if(n > 0)
      if(sq_dist(cursor, temp_shape[n - 1]) < 25)
       error_flag = 1;

     if(n > 1)
      if(fabs(VECTOR_SLOPE(VECTOR_DIFF(temp_shape[n - 1], temp_shape[n - 2])) -
              VECTOR_SLOPE(VECTOR_DIFF(temp_shape[n - 1], cursor))) < 0.2 ||
         (temp_shape[n - 1].x == temp_shape[n - 2].x && temp_shape[n - 2].x == cursor.x))
       error_flag = 1;

     if(!error_flag)
      {
       temp_shape[n] = cursor;
       n++;
      }
    }

   clear_to_color(buffer, makecol(128, 128, 128));

   if(sn > 0)
    for(j = 0; j < sn; j++)
     vector_loop(buffer, loop[j].point, loop[j].n, loop[j].closed, 0);
    vector_loop(buffer, temp_shape, n, -2, 0);

   if(n > 1)
    {
     vector_loop(buffer, temp_shape, n, 0, 0);
     vector_line(buffer, temp_shape[n - 1], temp_shape[0], makecol(128, 0, 0));
    }

   if(n > 0)
    {
     vector_line(buffer, temp_shape[n - 1], cursor, makecol(0, 128, 128));
     vector_line(buffer, cursor, temp_shape[0], makecol(0, 128, 128));
    }

   draw_sprite(buffer, cursor_bmp, cursor.x - 4, cursor.y - 5);
   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  }

 clear_keybuf();
 clear_to_color(buffer, makecol(128, 128, 128));

 float wall_width = 20.0;

 LINE_LOOP *final_shape;
 final_shape = (LINE_LOOP *)malloc(sn * sizeof(LINE_LOOP));

 if(sn > 0)
  for(j = 0; j < sn; j++)
   {
    if(loop[j].n > 2 && !loop[j].closed)
     {
      VECTOR norm;
      int pn = loop[j].n;
      final_shape[j] = new_shape(pn * 2, 0);

      for(i = 0; i < pn; i++)
       final_shape[j].point[i] = loop[j].point[i];

      norm = NORMALIZED_NORMAL(loop[j].point[0], loop[j].point[1]);
      final_shape[j].point[pn] = VECTOR_SUM(loop[j].point[0], USCALE_VECTOR(norm, wall_width));
      norm = NORMALIZED_NORMAL(loop[j].point[pn - 2], loop[j].point[pn - 1]);
      final_shape[j].point[pn * 2 - 1] = VECTOR_SUM(loop[j].point[pn - 1], USCALE_VECTOR(norm, wall_width));

      for(i = 1; i < pn - 1; i++)
       final_shape[j].point[pn + i] = make_wall(loop[j].point[i - 1], loop[j].point[i], loop[j].point[i + 1], wall_width);
     }

    if(loop[j].n > 2 && loop[j].closed)
     {
      int pn = loop[j].n;
      final_shape[j] = new_shape(pn, 1);
      for(i = 0; i < pn; i++)
       final_shape[j].point[i] = loop[j].point[i];
     }

    if(loop[j].n == 2)
     {
      final_shape[j] = new_shape(4, 0);
      VECTOR norm = USCALE_VECTOR(NORMALIZED_NORMAL(loop[j].point[0], loop[j].point[1]), wall_width);
      final_shape[j].point[0] = loop[j].point[0];
      final_shape[j].point[1] = loop[j].point[1];
      final_shape[j].point[2] = VECTOR_SUM(loop[j].point[0], norm);
      final_shape[j].point[3] = VECTOR_SUM(loop[j].point[1], norm);
     }
   }

 draw_map_sketch(buffer, final_shape, sn);
 save_map("map.txt", final_shape, sn);

 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
 readkey();

 free_map(final_shape, sn);
 for(i = 0; i < sn; i++)
  free(loop[i].point);
 destroy_bitmap(cursor_bmp);
 destroy_bitmap(buffer);
 return 0;
}
Example #27
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");
}
Example #28
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);
}
Example #29
0
	geom::shape_ref geom::circle::translate(const vec2 & displacement) const {
		shape_ref new_shape(new circle(*this, displacement));
		return new_shape;
	}
Example #30
0
double twenty_lines(){

	clear();

	int board_x = 15;
	int board_y = 1;
	int board_width = 10;
	int board_height = 20;

	//board boarders
	int i;
	for(i = 0; i < board_height; i++){
		mvprintw(i + board_y, board_x, "|");
		mvprintw(i + board_y, (board_width * 2) + (board_x) + 1, "|", i);
	}

	mvprintw(board_y + board_height, board_x, "+");	
	for(i = 1; i < (board_width * 2) + 1; i++)
		mvprintw(board_y + board_height, i + board_x, "-");	
	mvprintw(board_y + board_height, board_x + (board_width * 2) + 1, "+");	

	//bucket
	init_bucket(&bu);

	//shape
	new_shape(&s, &bu);

	//board
	b = init_board(board_width, board_height);

	int running = 1;
	int lines = 0;

	//drop loop params
	thread_params tp;
	tp.b = &b;
	tp.s = &s;
	tp.bucket = &bu;
	tp.hold_cnt = &hold_cnt;
	tp.drop_speed = &drop_speed;
	tp.running = &running; 
	tp.lines_cleared = &lines;

	pthread_t pth;
    pthread_create(&pth, NULL, drop_loop, (void * ) &tp);

    struct timeval tval_before, tval_after, tval_result;
    gettimeofday(&tval_before, NULL);

   	char tmbuf[64];

	// game loop
	while(running > 0 && lines < 10){

		timeout(100);

		draw_board(b, board_x, board_y);
		draw_hold(h_s, hold_cnt, 1, 1);
		draw_shape(s, board_x, board_y);
		draw_bucket(&bu, 45, 1);

		mvprintw(10, 2, "Lines: %d", lines);

		gettimeofday(&tval_after, NULL);
		timersub(&tval_after, &tval_before, &tval_result);
		strftime(tmbuf, sizeof tmbuf, "%M:%S", localtime(&tval_result.tv_sec));
		mvprintw(14, 2, "        ");
		mvprintw(14, 2, "%s.%02d", tmbuf, tval_result.tv_usec/10000);

		int ch = getch();
		if(ch != ERR){

			//hold - temporarily cycles trough
			if(ch == 'x'){
				s.type++;
				if(s.type == 7)
					s.type = 0;

			//left
			}else if(ch == KEY_LEFT && check_move(s, b, -1, 0, 0)){
				s.x--;

			//right
			}else if(ch == KEY_RIGHT && check_move(s, b, 1, 0, 0)){
				s.x++;

			//rotate
			}else if(ch == KEY_UP && check_move(s, b, 0, 0, 1)){
				s.rotation++;
				if(s.rotation == 4)
					s.rotation = 0;

			//hard drop
			}else if(ch == ' '){
				while(true){
					if(!check_move(s, b, 0, 1, 0))
						break;
					s.y++;
				}
			//hold
			}else if(ch == 'z' && hold_cnt < 1){
				hold(&s, &h_s, &bu);
				hold_cnt++;
			}
		}

		refresh();

		if(DEBUG_CNT == 20)
			DEBUG_CNT = 0;
	}

	//stop drop loop
	running = -1;

	//delay for drop loop to stop
	usleep(1000000);

	clear();

	return 1;
}