Example #1
0
void draw_shaded_edges(polygon p,
		       struct screen *s,
		       enum shading_t type,
		       struct constants *k,
		       color ambient,
		       struct light *l,
		       struct vector n[3],
		       struct vector *v) {
  struct vector centroid;
  struct vector cv;
  color c;
  edge e;

  if (type == FLAT) {
    calculate_centroid(&centroid, p);
    calculate_intensity(&cv, k, ambient, l, &centroid, n, v);
    c = vtoc(&cv);
    
    set_edge(e, p[0], p[1]);
    draw_line(e, s, c);

    set_edge(e, p[1], p[2]);
    draw_line(e, s, c);

    set_edge(e, p[2], p[0]);
    draw_line(e, s, c);
  }
  else {
    draw_shaded_edge(0, p, s, type, k, ambient, l, n, v);
    draw_shaded_edge(1, p, s, type, k, ambient, l, n, v);
    draw_shaded_edge(2, p, s, type, k, ambient, l, n, v);
  }
}
Example #2
0
int pure_c_kmeans( PGM_Matriz_Double *matriz, int k, int semente, int fim,
                   PGM_Matriz_Double *centroides, int *grupo, double *somatorio_distancias ) {
    int alterou = 1;
    int *n_elem_grupo = (int*) palloc0 ( sizeof(int) * k);

    init_group(centroides, matriz, grupo, n_elem_grupo, k, semente);
    while( alterou ){
        int i;
        alterou = 0;
        *somatorio_distancias = 0;

        for( i = 0; i < matriz->n_linhas; i++)
            alterou += change_group(centroides, matriz, grupo, i, somatorio_distancias);

        if(calculate_centroid(centroides, matriz, grupo, n_elem_grupo) < k){

            switch(fim){
                case 1:
                    break;

                case 2:
                    alterou += create_new_group(centroides, matriz, search_empty_group(n_elem_grupo, k));
                    break;

                case 3:
                    return 1;
            }
        }
    }
    pfree (n_elem_grupo);
    return 0;
}
Example #3
0
void init_group(PGM_Matriz_Double *centroides , PGM_Matriz_Double *matriz , int *grupo , int *n_elem_grupo, int k, int semente){
    switch ( semente ){
        //semente = 1 - para sortear k pontos para serem os centros dos grupos
        case 1: {
                  int i;
                  for( i = 0; i < k; i++ ) {
                    int linha;
                    do {
                      linha = rand() % matriz->n_linhas;
                    } while( grupo[linha]==1 );

                    grupo[linha] = 1;

                    PGM_COPIALINHA_MATRIZ( centroides, i, matriz, linha );

                  }
                }
                break;
        //semente = 2 - para sortear cada ponto em um grupo
        case 2: {
                  int i;

                  for( i = 0; i < matriz->n_linhas; i++ )
                    grupo[i] = rand() % k;

                  calculate_centroid( centroides, matriz, grupo, n_elem_grupo);
                }
                break;
    }
    memset(grupo, 0, sizeof(int)*matriz->n_linhas);
}
void indexed_force_tri_3D::load(std::ifstream& in, point_cloud* pPC)
{
	// read the label in and set it
	LABEL label = read_label(in);
	set_label(label);
	if (in.eof())
		return;

	// set the point cloud
	ppoint_cloud_instance = pPC;
	// read the point cloud indices for each vertex
	for (int t=0; t<3; t++)
		p[t] = read_int(in);
	// calculate the centroid
	calculate_centroid();
	// read the target index
	ds_index = read_int(in);
	// read the length of index list in
	int n_idx = read_int(in);
	for (int i=0; i<n_idx; i++)
	{
		grid_index gr_idx;
		gr_idx.i = read_int(in);
		gr_idx.j = read_int(in);
		gr_idx.cart_coord = read_vector(in);
		grid_indices.push_back(gr_idx);
	}
	// read the point and adjacency list
	for (int a=0; a<2; a++)
	{
		// get the size first
		int s = read_int(in);
		for (int i=0; i<s; i++)
			adjacency[a].push_back(read_label(in));
	}
}
Example #5
0
void horizontal_convert(polygon poly,
			struct screen *s,
			enum shading_t type,
			struct constants *k,
			color ambient,
			struct light *l,
			struct vector norm[3],
			struct vector *v) {
  const double
    y0 = poly[0]->y,
    y1 = poly[1]->y,
    y2 = poly[2]->y;

  int ib, im, it;
  int dy_bt, dy_bm, dy_mt;
  double dx;

  struct vector pb, pm, pt;
  struct vector dp_dy_bt, dp_dy_bm, dp_dy_mt;
  struct vector p0, p1;
  edge e = {&p0, &p1};
  struct vector p;
  double dz_dx;

  struct vector cb, cm, ct;
  struct vector dc_dy_bt, dc_dy_bm, dc_dy_mt;
  struct vector c0, c1;
  struct vector c;
  struct vector dc_dx;

  struct vector dn_dy_bt, dn_dy_bm, dn_dy_mt;
  struct vector n0, n1;
  struct vector n;
  struct vector dn_dx;

  if (y0 <= y1 && y1 <= y2) {
    ib = 0;
    im = 1;
    it = 2;
  }
  else if (y0 <= y2 && y2 <= y1) {
    ib = 0;
    im = 2;
    it = 1;
  }
  else if (y1 <= y0 && y0 <= y2) {
    ib = 1;
    im = 0;
    it = 2;
  }
  else if (y1 <= y2 && y2 <= y0) {
    ib = 1;
    im = 2;
    it = 0;
  }
  else if (y2 <= y0 && y0 <= y1) {
    ib = 2;
    im = 0;
    it = 1;
  }
  else {
    ib = 2;
    im = 1;
    it = 0;
  }

  pb = *poly[ib];
  pm = *poly[im];
  pt = *poly[it];

  pb.y = (int)pb.y; 
  pm.y = (int)pm.y; 
  pt.y = (int)pt.y; 

  dy_bt = pt.y - pb.y;
  dy_bm = pm.y - pb.y;
  dy_mt = pt.y - pm.y;

  p0 = pb;
  p1 = pb.y != pm.y? pb : pm;

  dp_dy_bt = pt;
  subtract_vectors(&dp_dy_bt, &pb);
  scalar_div(dy_bt, &dp_dy_bt);

  dp_dy_bm = pm;
  subtract_vectors(&dp_dy_bm, &pb);
  scalar_div(dy_bm, &dp_dy_bm);

  dp_dy_mt = pt;
  subtract_vectors(&dp_dy_mt, &pm);
  scalar_div(dy_mt, &dp_dy_mt);

  switch (type) {
  case FLAT:
    calculate_centroid(&p, poly);
    calculate_intensity(&c, k, ambient, l, &p, norm, v);

    for (p.y = pb.y; p.y <= pt.y; p.y++) {
      p0.y = p1.y = p.y;
      draw_line(e, s, vtoc(&c) );

      add_vectors(&p0, &dp_dy_bt);
      add_vectors(&p1, (p.y < pm.y? &dp_dy_bm : &dp_dy_mt) );
    }
    break;

  case GOROUD:
    calculate_intensity(&cb, k, ambient, l, poly[ib], norm + ib, v);
    calculate_intensity(&cm, k, ambient, l, poly[im], norm + im, v);
    calculate_intensity(&ct, k, ambient, l, poly[it], norm + it, v);

    c0 = cb;
    c1 = (pb.y != pm.y? cb : cm);

    dc_dy_bt = ct;
    subtract_vectors(&dc_dy_bt, &cb);
    scalar_div(dy_bt, &dc_dy_bt);

    dc_dy_bm = cm;
    subtract_vectors(&dc_dy_bm, &cb);
    scalar_div(dy_bm, &dc_dy_bm);

    dc_dy_mt = ct;
    subtract_vectors(&dc_dy_mt, &cm);
    scalar_div(dy_mt, &dc_dy_mt);

    for (p.y = pb.y; p.y <= pt.y; p.y++) {
      dx = p1.x - p0.x;

      dc_dx = c1;
      subtract_vectors(&dc_dx, &c0);
      scalar_div(dx, &dc_dx);

      dz_dx = (p1.z - p0.z) / dx;

      p.x = p0.x;
      p.z = p0.z;

      c = c0;
      while (1) {
	plot(s, vtoc(&c), &p);

	if (p0.x < p1.x) {
	  p.z += dz_dx;
	  add_vectors(&c, &dc_dx);
	  p.x++;
	  if (p.x > p1.x)
	    break;
	}
	else {
	  p.z -= dz_dx;
	  subtract_vectors(&c, &dc_dx);
	  p.x--;
	  if (p.x < p1.x)
	    break;
	}
      }

      add_vectors(&p0, &dp_dy_bt);
      add_vectors(&c0, &dc_dy_bt);

      if (p.y < pm.y) {
	add_vectors(&p1, &dp_dy_bm);
	add_vectors(&c1, &dc_dy_bm);
      }
      else {
	add_vectors(&p1, &dp_dy_mt);
	add_vectors(&c1, &dc_dy_mt);
      }
    }

    break;
      
  case PHONG:
    n0 = norm[ib];
    n1 = norm[pb.y != pm.y? ib : im];
    
    dn_dy_bt = norm[it];
    subtract_vectors(&dn_dy_bt, norm + ib);
    scalar_div(dy_bt, &dn_dy_bt);

    dn_dy_bm = norm[im];
    subtract_vectors(&dn_dy_bm, norm + ib);
    scalar_div(dy_bm, &dn_dy_bm);

    dn_dy_mt = norm[it];
    subtract_vectors(&dn_dy_mt, norm + im);
    scalar_div(dy_mt, &dn_dy_mt);

    for (p.y = pb.y; p.y <= pt.y; p.y++) {
      dx = p1.x - p0.x;

      dn_dx = n1;
      subtract_vectors(&dn_dx, &n0);
      scalar_div(dx, &dn_dx);

      dz_dx = (p1.z - p0.z) / dx;

      p = p0;
      n = n0;
      while (1) {
	calculate_intensity(&c, k, ambient, l, &p, &n, v);
	plot(s, vtoc(&c), &p);

	if (p0.x < p1.x) {
	  p.z += dz_dx;
	  add_vectors(&n, &dn_dx);
	  p.x++;
	  if (p.x > p1.x)
	    break;
	}
	else {
	  p.z -= dz_dx;
	  subtract_vectors(&n, &dn_dx);
	  p.x--;
	  if (p.x < p1.x)
	    break;
	}
      }

      add_vectors(&p0, &dp_dy_bt);
      add_vectors(&n0, &dn_dy_bt);
      
      if (p.y < pm.y) {
	add_vectors(&p1, &dp_dy_bm);
	add_vectors(&n1, &dn_dy_bm);
      }
      else {
	add_vectors(&p1, &dp_dy_mt);
	add_vectors(&n1, &dn_dy_mt);
      }
    }

    break;
  }
}
Example #6
0
void vertical_convert(polygon poly,
		      struct screen *s,
		      enum shading_t type,
		      struct constants *k,
		      color ambient,
		      struct light *l,
		      struct vector norm[3],
		      struct vector *v) {  
  const double
    x0 = poly[0]->x,
    x1 = poly[1]->x,
    x2 = poly[2]->x;

  int il, im, ir;
  int dx_lr, dx_lm, dx_mr;
  double dy;

  struct vector pl, pm, pr;
  struct vector dp_dx_lr, dp_dx_lm, dp_dx_mr;
  struct vector p0, p1;
  edge e = {&p0, &p1};
  struct vector p;
  double dz_dy;

  struct vector cl, cm, cr;
  struct vector dc_dx_lr, dc_dx_lm, dc_dx_mr;
  struct vector c0, c1;
  struct vector c;
  struct vector dc_dy;

  struct vector dn_dx_lr, dn_dx_lm, dn_dx_mr;
  struct vector n0, n1;
  struct vector n;
  struct vector dn_dy;

  if (x0 <= x1 && x1 <= x2) {
    il = 0;
    im = 1;
    ir = 2;
  }
  else if (x0 <= x2 && x2 <= x1) {
    il = 0;
    im = 2;
    ir = 1;
  }
  else if (x1 <= x0 && x0 <= x2) {
    il = 1;
    im = 0;
    ir = 2;
  }
  else if (x1 <= x2 && x2 <= x0) {
    il = 1;
    im = 2;
    ir = 0;
  }
  else if (x2 <= x0 && x0 <= x1) {
    il = 2;
    im = 0;
    ir = 1;
  }
  else {
    il = 2;
    im = 1;
    ir = 0;
  }

  pl = *poly[il];
  pm = *poly[im];
  pr = *poly[ir];

  pl.x = (int)pl.x; 
  pm.x = (int)pm.x; 
  pr.x = (int)pr.x; 

  dx_lr = pr.x - pl.x;
  dx_lm = pm.x - pl.x;
  dx_mr = pr.x - pm.x;

  p0 = pl;
  p1 = pl.x != pm.x? pl : pm;

  dp_dx_lr = pr;
  subtract_vectors(&dp_dx_lr, &pl);
  scalar_div(dx_lr, &dp_dx_lr);

  dp_dx_lm = pm;
  subtract_vectors(&dp_dx_lm, &pl);
  scalar_div(dx_lm, &dp_dx_lm);

  dp_dx_mr = pr;
  subtract_vectors(&dp_dx_mr, &pm);
  scalar_div(dx_mr, &dp_dx_mr);

  switch (type) {
  case FLAT:
    calculate_centroid(&p, poly);
    calculate_intensity(&c, k, ambient, l, &p, norm, v);

    for (p.x = pl.x; p.x <= pr.x; p.x++) {
      p0.x = p1.x = p.x;
      draw_line(e, s, vtoc(&c) );

      add_vectors(&p0, &dp_dx_lr);
      add_vectors(&p1, (p.x < pm.x? &dp_dx_lm : &dp_dx_mr) );
    }
    break;

  case GOROUD:
    calculate_intensity(&cl, k, ambient, l, poly[il], norm + il, v);
    calculate_intensity(&cm, k, ambient, l, poly[im], norm + im, v);
    calculate_intensity(&cr, k, ambient, l, poly[ir], norm + ir, v);

    c0 = cl;
    c1 = (pl.x != pm.x? cl : cm);

    dc_dx_lr = cr;
    subtract_vectors(&dc_dx_lr, &cl);
    scalar_div(dx_lr, &dc_dx_lr);

    dc_dx_lm = cm;
    subtract_vectors(&dc_dx_lm, &cl);
    scalar_div(dx_lm, &dc_dx_lm);

    dc_dx_mr = cr;
    subtract_vectors(&dc_dx_mr, &cm);
    scalar_div(dx_mr, &dc_dx_mr);

    for (p.x = pl.x; p.x <= pr.x; p.x++) {
      dy = p1.y - p0.y;

      dc_dy = c1;
      subtract_vectors(&dc_dy, &c0);
      scalar_div(dy, &dc_dy);

      dz_dy = (p1.z - p0.z) / dy;

      p.y = p0.y;
      p.z = p0.z;

      c = c0;
      while (1) {
	plot(s, vtoc(&c), &p);

	if (p0.y < p1.y) {
	  p.z += dz_dy;
	  add_vectors(&c, &dc_dy);
	  p.y++;
	  if (p.y > p1.y)
	    break;
	}
	else {
	  p.z -= dz_dy;
	  subtract_vectors(&c, &dc_dy);
	  p.y--;
	  if (p.y < p1.y)
	    break;
	}
      }

      add_vectors(&p0, &dp_dx_lr);
      add_vectors(&c0, &dc_dx_lr);

      if (p.x < pm.x) {
	add_vectors(&p1, &dp_dx_lm);
	add_vectors(&c1, &dc_dx_lm);
      }
      else {
	add_vectors(&p1, &dp_dx_mr);
	add_vectors(&c1, &dc_dx_mr);
      }
    }

    break;
      
  case PHONG:
    n0 = norm[il];
    n1 = norm[pl.x != pm.x? il : im];
    
    dn_dx_lr = norm[ir];
    subtract_vectors(&dn_dx_lr, norm + il);
    scalar_div(dx_lr, &dn_dx_lr);

    dn_dx_lm = norm[im];
    subtract_vectors(&dn_dx_lm, norm + il);
    scalar_div(dx_lm, &dn_dx_lm);

    dn_dx_mr = norm[ir];
    subtract_vectors(&dn_dx_mr, norm + im);
    scalar_div(dx_mr, &dn_dx_mr);

    for (p.x = pl.x; p.x <= pr.x; p.x++) {
      dy = p1.y - p0.y;

      dn_dy = n1;
      subtract_vectors(&dn_dy, &n0);
      scalar_div(dy, &dn_dy);

      dz_dy = (p1.z - p0.z) / dy;

      p = p0;
      n = n0;
      while (1) {
	calculate_intensity(&c, k, ambient, l, &p, &n, v);
	plot(s, vtoc(&c), &p);

	if (p0.y < p1.y) {
	  p.z += dz_dy;
	  add_vectors(&n, &dn_dy);
	  p.y++;
	  if (p.y > p1.y)
	    break;
	}
	else {
	  p.z -= dz_dy;
	  subtract_vectors(&n, &dn_dy);
	  p.y--;
	  if (p.y < p1.y)
	    break;
	}
      }

      add_vectors(&p0, &dp_dx_lr);
      add_vectors(&n0, &dn_dx_lr);
      
      if (p.x < pm.x) {
	add_vectors(&p1, &dp_dx_lm);
	add_vectors(&n1, &dn_dx_lm);
      }
      else {
	add_vectors(&p1, &dp_dx_mr);
	add_vectors(&n1, &dn_dx_mr);
      }
    }

    break;
  }
}