Exemple #1
0
void TestGame::draw_triangle(Vertex v1, Vertex v2, Vertex v3,
    Texture * texture) {
    if (v1.coordinates.y > v2.coordinates.y)
        swap(v1, v2);
    if (v2.coordinates.y > v3.coordinates.y)
        swap(v2, v3);
    if (v1.coordinates.y > v2.coordinates.y)
        swap(v1, v2);

    if (lineside2d(v2.coordinates, v1.coordinates, v3.coordinates) > 0) {
        for (int y = max((int)v1.coordinates.y, 0);
        y <= min((int)v3.coordinates.y, ENGINE_HEIGHT - 1); y++) {
            if (y < v2.coordinates.y)
                scan_line(y, &v1, &v3, &v1, &v2, texture);
            else
                scan_line(y, &v1, &v3, &v2, &v3, texture);
        }
    }
    else {
        for (int y = max((int)v1.coordinates.y, 0);
        y <= min((int)v3.coordinates.y, ENGINE_HEIGHT - 1); y++) {
            if (y < v2.coordinates.y)
                scan_line(y, &v1, &v2, &v1, &v3, texture);
            else
                scan_line(y, &v2, &v3, &v1, &v3, texture);
        }
    }
}
Exemple #2
0
static void cpuinfo_intel(FILE *f)
{
  char *key, *val;
  int n_processor = 0;
  int n_sibling = 1;
  int n_core_proc = 1;

  rewind(f);

  while (scan_line(f, &key, &val)) {
    if (strcmp(key, "processor") == 0) {
      n_processor++;
    }
    else if (strcmp(key, "model name") == 0) {
      if (n_processor == 1) proc_name = strdup(val);
    }
    else if (strcmp(key, "cpu MHz") == 0) {
      if (n_processor == 1) {
        sscanf(val, "%lf", &proc_clock_freq);
        proc_clock_freq /= 1000.0;  /* MHz -> GHz */
      }
    }
    else if (strcmp(key, "siblings") == 0) {
      sscanf(val, "%d",  &n_sibling);
    }
    else if (strcmp(key, "cpu cores") == 0) {
      sscanf(val, "%d",  &n_core_proc);
    }
  }

  num_proc_node = n_processor / n_sibling;
  num_core_node = n_core_proc * num_proc_node;
}
Exemple #3
0
	void Parser::scan()
	{
        if (!scan_char())
            return;

		for(; !src->eof();)
		{
			if (current_char == L_SECTION)
			{
				scan_section();

				//we'll skip everything after R_SECTION
				// put your comments there or w/e
				//Also
				// let's skip all empty lines
				do
                    skip_until_after(NEWLINE);
                while (current_char == NEWLINE);
			}

			else
            {
                scan_line();

                do
                    skip_until_after(NEWLINE);
                while (current_char == NEWLINE);
            }
		}
	};
Exemple #4
0
void draw_polygons( struct matrix *polygons, screen s, color c, struct matrix* zbuffer ) {
  
  int i;  
  for( i=0; i < polygons->lastcol-2; i+=3 ) {

    if ( calculate_dot( polygons, i ) < 0) {
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 s, c, zbuffer);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 s, c, zbuffer);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 s, c, zbuffer);
      scan_line( polygons->m[0][i],  polygons->m[1][i],  polygons->m[2][i],
		 polygons->m[0][i+1],polygons->m[1][i+1],polygons->m[2][i+1],
		 polygons->m[0][i+2],polygons->m[1][i+2],polygons->m[2][i+2],
		 s, c, zbuffer);
    }
  }
}
Exemple #5
0
int main(void)
{
    float temp[12][31];
    float d_min[12];
    float d_max[12];
    float d_avg[12];
    int   d_count[12];
    int   d_year[12];
    int   d_month[12];
    char buff[1000];
    int n_months = 0;

    for (int j = 0; j < 12 && fgets(buff, 300, stdin) != NULL; j++)
    {
        int year, month;
        int ndays = scan_line(buff, temp[j], &month, &year);
        if (ndays < 0)
            break;
        float maxt = max(ndays, temp[j]);
        float mint = min(ndays, temp[j]);
        float avgt = avg(ndays, temp[j]);
        int count  = cnt(ndays, temp[j]);
        printf("%.4d-%.2d: days = %d, min = %6.2f, avg = %6.2f, max = %6.2f\n", year, month, count, mint, avgt, maxt);
        d_min[j] = mint;
        d_max[j] = maxt;
        d_avg[j] = avgt;
        d_count[j] = count;
        d_year[j] = year;
        d_month[j] = month;
        n_months = j + 1;
    }
    dump_data("Data all read", n_months, temp, d_min, d_max, d_avg, d_count, d_year, d_month);
    return 0;
}
Exemple #6
0
static void meminfo_mac(FILE *f)
{
  char *key, *val;

  while (scan_line(f, &key, &val)) {
    if (strcmp(key, "Memory") == 0) {
      sscanf(val, "%lf GB", &mem_node);
    }
  }
}
Exemple #7
0
static void scan_triangle(REAL tri[][2], int z, int *n, int vox[][3], int maxv)
{
	REAL *p[3], *t, lin[2];
	REAL x1, x2, y1, y2;
	int y, ye, i;

	/* sort p into ascending order of y */
	p[0] = tri[0]; p[1] = tri[1]; p[2] = tri[2];
	if (p[1][1]<p[0][1]) {t = p[1]; p[1] = p[0]; p[0] = t;}
	if (p[2][1]<p[1][1]) {t = p[2]; p[2] = p[1]; p[1] = t;}
	if (p[1][1]<p[0][1]) {t = p[1]; p[1] = p[0]; p[0] = t;}

	/* find lower lines cutting triangle where y is integer */
	for(y=ceil(p[0][1]), ye=floor(p[1][1]); y<=ye; y++)
	{
		x1 = p[0][0]; y1 = p[0][1];
		for(i=0; i<2; i++)
		{
			x2 = p[i+1][0]; y2 = p[i+1][1];
			if (y2-y1<=0)
				lin[i] = (x1+x2)/2.0;
			else
				lin[i] = (x1*(y2-y)+x2*(y-y1))/(y2-y1);
		}
		scan_line(lin,y,z, n,vox,maxv);
	}

	/* find upper lines cutting triangle where y is integer */
	for(y=ceil(p[1][1]), ye=floor(p[2][1]); y<=ye; y++)
	{
		x2 = p[2][0]; y2 = p[2][1];
		for(i=0; i<2; i++)
		{
			x1 = p[i][0]; y1 = p[i][1];
			if (y2-y1<=0)
				lin[i] = (x1+x2)/2.0;
			else
				lin[i] = (x1*(y2-y)+x2*(y-y1))/(y2-y1);
		}
		scan_line(lin,y,z, n,vox,maxv);
	}
}
Exemple #8
0
void	xml_scan(CLASS_DISPLAY *d, int ac, char **av)
{
  char	*s;
  BOOL  check;
  FD	xml;

  check = FALSE;
  display_init(d);
  if (ac != 2 || (xml = open(av[1], O_RDWR)) == -1)
    lerror(USAGE);
  if ((check = check_fd(get_next_line(xml), xml, INIT, &s)) == FALSE)
    lerror(USAGE);// || WRONG_FILE ?
  while ((check = check_fd(s, xml, END, &s)))
    scan_line(d, xml, s);
}
Exemple #9
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
         screen s
         color c  
Returns: 


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int i;
  
  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {
    
    if ( calculate_dot( polygons, i ) >= 0 ) {
      //      printf("drawing polygon %d\n", i/3);

      c.red = rand() % 255;
      c.blue = rand() % 255;
      c.green = rand() %255;

      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 s, c);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 s, c);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 s, c);
      //printf("wu tang clan aint nuthin to f**k with\n");
      scan_line(polygons->m, s, c, i);
      //printf("Hello2\n");
    }
  }
}
Exemple #10
0
int
set_num_fans(char *line)
{
	unsigned int input_val;

	if (id_header.num_fans == 0) {
		if (scan_line(line, &input_val) == ERROR)
			return (ERROR);
		id_header.num_fans = input_val;
		running_offset += NUM_FAN_SIZE +
			(id_header.num_fans * FAN_BLOCK_SIZE);
		id_header.fan_block = malloc(id_header.num_fans *
			FAN_BLOCK_SIZE);
		return (NO_ERROR);
	} else {
		printf("ERROR: Already initialized num_fans\n");
		return (ERROR);
	}
}
Exemple #11
0
void maprof_read_cpuinfo(const char *cpuinfo)
{
  FILE *f;
  char *key, *val;

  cpuinfo_first = 0;

#ifdef __APPLE__
  if (cpuinfo == NULL) {
    f = popen("system_profiler SPHardwareDataType", "r");
    if (f == NULL) {
      perror(NULL);
      cpuinfo_unknown();
      return;
    }
    cpuinfo_mac(f);
    pclose(f);
    return;
  }
#else
  if (cpuinfo == NULL) cpuinfo = "/proc/cpuinfo";
#endif

  if ((f = fopen(cpuinfo, "r")) == NULL) {
    perror(cpuinfo);
    cpuinfo_unknown();
    return;
  }

  if  (scan_line(f, &key, &val)) {
    if (strcmp(key, "processor") == 0) {
      cpuinfo_intel(f);
    } else if (strcmp(key, "cpu") == 0) {
      cpuinfo_sparc(f);
    } else {
      cpuinfo_unknown();
    }
  } else {
    cpuinfo_unknown();
  }

  fclose(f);
}
Exemple #12
0
image::image( char* img_data, unsigned size ){
	width = ((unsigned*)img_data)[0];
	height = ((unsigned*)img_data)[1];
	unsigned depth = ((unsigned*)img_data)[2];
	
	data = new color[ height * width ];
	
	unsigned char* img_contents = (unsigned char*)((unsigned*)img_data + 3);
	for( unsigned iy=0; iy<height; iy++ ){
		color* row = scan_line( iy );
		for( unsigned ix=0; ix<width; ++ix, ++row ){
			unsigned short c=127*255;
			c = *img_contents;
			img_contents++;
			c <<= 6;
			*row = color( c,c,c );
		}
	}
}
Exemple #13
0
int
update_fan_table(char *parameter, char *line)
{
	int i = 0, offset = 0;
	unsigned int input_val;
	int size;
	unsigned char *data_ptr;

	while (strcmp(fan_parameters[i].name, parameter)) {
		offset += fan_parameters[i++].size;
		if (fan_parameters[i].name == NULL) {
			fprintf(stderr,
				"%s is not a valid fan parameter\n", parameter);
			return (ERROR);
		}
	}
	size = fan_parameters[i].size;
	data_ptr = cur_fan_table->fan_data;

	if (strcmp(parameter, "fan-min-range") == 0) {
		offset = FAN_MIN_RANGE_SZ * current_fan_pair;
		current_fan_pair++;
		data_ptr = cur_fan_table->fan_ctl_data;
		if ((fan_min_range_scan(line, &input_val) == ERROR))
			return (ERROR);
	} else if ((scan_line(line, &input_val) == ERROR))
		return (ERROR);


	if (strcmp(parameter, "num-ctl-pairs") == 0) {
		num_fan_ctl_pairs = input_val;
		cur_fan_table->fan_ctl_data =
			(unsigned char *) malloc(FAN_MIN_RANGE_SZ *
			num_fan_ctl_pairs);
		cur_fan_table->fan_pair_size = num_fan_ctl_pairs *
			FAN_MIN_RANGE_SZ;
	}

	write_bytes(input_val, size, offset, data_ptr);

	return (NO_ERROR);
}
Exemple #14
0
int
update_env_table(char *parameter, char *line)
{
	int i = 0, offset = 0;
	unsigned int input_val;
	int size;
	unsigned char *data_ptr;

	while (strcmp(env_parameters[i].name, parameter)) {
		offset += env_parameters[i++].size;
		if (env_parameters[i].name == NULL) {
			fprintf(stderr,
			    "%s is not a valid env parameter\n", parameter);
			return (ERROR);
		}
	}
	size = env_parameters[i].size;
	data_ptr = cur_env_table->env_data;

	if (strcmp(parameter, "correction") == 0) {
		offset = COR_SIZE * current_correction;
		current_correction++;
		size = COR_SIZE;
		data_ptr = cur_env_table->correction_data;
		if ((correction_scan(line, &input_val) == ERROR))
			return (ERROR);
	} else if ((scan_line(line, &input_val) == ERROR))
		return (ERROR);


	if (strcmp(parameter, "num-corrections") == 0) {
		num_corrections = input_val;
		cur_env_table->correction_data =
		    (unsigned char *) malloc(COR_SIZE * num_corrections);
		cur_env_table->correction_size = num_corrections * COR_SIZE;
	}

	write_bytes(input_val, size, offset, data_ptr);

	return (NO_ERROR);
}
Exemple #15
0
int main()
{
    zmq::context_t context(1);

    zmq::socket_t socket_rec(context, ZMQ_PULL);
    socket_rec.connect("tcp://192.168.8.102:5557");    //localhost

    zmq::socket_t socket_sink(context, ZMQ_PUSH);
    socket_sink.connect("tcp://192.168.8.102:5558");    //localhost

    int line;
    std::string scanned_line;

    while (true)
    {
        line = std::stoi(s_recv(socket_rec));
        scanned_line = scan_line(line);
        std::cout << "received: " << line << std::endl;
        s_send(socket_sink, std::string("head3: ") + std::to_string(line) + " :: " + scanned_line);
    }
}
int main(int argc,char **argv){
    char line[80];    /*  line of text */
    int vowels = 0;   /*  vowel counter */
    int consonants = 0; /*  consonant counter */
    int digits = 0;     /*  digit counter */
    int whitespace = 0; /*  whitespace counter */
    int other = 0;      /*  remaining character counter */
    
    printf("Enter a line of text below:\n");
    scanf("%[^\n]", line);

    scan_line(line, &vowels, &consonants, &digits, &whitespace, &other);

    printf("\nNO. of vowels: %d", vowels);
    printf("\nNo. of consonants: %d", consonants);
    printf("\nNo. of digits: %d", digits);
    printf("\nNo. of whitespace characters: %d", whitespace);
    printf("\nNo. of other characters: %d", other);
    return 0;

}
Exemple #17
0
static void cpuinfo_mac(FILE *f)
{
  char *key, *val;
  int total_cores;

  while (scan_line(f, &key, &val)) {
    if (strcmp(key, "Processor Name") == 0) {
      proc_name = strdup(val);
    }
    else if (strcmp(key, "Processor Speed") == 0) {
      sscanf(val, "%lf GHz", &proc_clock_freq);
    }
    else if (strcmp(key, "Number of Processors") == 0) {
      sscanf(val, "%d", &num_proc_node);
    }
    else if (strcmp(key, "Total Number of Cores") == 0) {
      sscanf(val, "%d", &total_cores);
    }
  }
  num_core_node = total_cores / num_proc_node;
}
Exemple #18
0
void maprof_read_meminfo(const char *meminfo)
{
  FILE *f;
  char *key, *val;
  long int mem;

  meminfo_first = 0;

#ifdef __APPLE__
  if (meminfo == NULL) {
    f = popen("system_profiler SPHardwareDataType", "r");
    if (f == NULL) {
      perror(NULL);
      meminfo_unknown();
      return;
    }
    meminfo_mac(f);
    pclose(f);
    return;
  }
#else
  if (meminfo == NULL) meminfo = "/proc/meminfo";
#endif

  if ((f = fopen(meminfo, "r")) == NULL) {
    perror(meminfo);
    meminfo_unknown();
    return;
  }

  if (scan_line(f, &key, &val) &&
      strcmp(key, "MemTotal") == 0) {
    sscanf(val, "%ld kB", &mem);
    mem_node = mem * 1.0e-6;   /* kB -> GB */
  } else {
    meminfo_unknown();
  }

  fclose(f);
}
Exemple #19
0
static void cpuinfo_sparc(FILE *f)
{
  char *key, *val;
  long int freq;

  rewind(f);

  while (scan_line(f, &key, &val)) {
    if (strcmp(key, "cpu") == 0) {
      proc_name = strdup(val);
    }
    else if (strcmp(key, "ncpus probed") == 0) {
      sscanf(val, "%d", &num_core_node);
    }
    else if (strcmp(key, "Cpu0ClkTck") == 0) {
      sscanf(val, "%lx", &freq);
      proc_clock_freq = freq * 1.0e-9;   /* Hz -> GHz */
    }
  }

  num_proc_node = 1;  /* K and FX10 */
}
Exemple #20
0
int
parse_fan_id(char *line)
{
	unsigned int input_val, offset, i;

	offset = fan_id_num * FAN_BLOCK_SIZE;
	fan_id_num++;
	if (scan_line(line, &input_val) == ERROR)
		return (ERROR);
	for (i = 0; i < fan_id_num-1; i++) {
		if (!memcmp(&input_val, &id_header.fan_block[i*FAN_BLOCK_SIZE],
			FAN_ID_SIZE)) {
			printf("ERROR: Two fans with id = %x\n", input_val);
			return (ERROR);
		}
	}
	write_bytes(input_val, FAN_ID_SIZE, offset, id_header.fan_block);
	offset += FAN_ID_SIZE;
	write_bytes(running_offset, FAN_OFF_SIZE, offset, id_header.fan_block);

	return (NO_ERROR);
}
int glFlush()
{
	zBuffer_t zBuffer;
	scan_t scan[WINDOW_SIZE_Y];
	int i;

	zBuffer_init(&zBuffer);

	for(i = 0; i < list_polygon_count; i++)
	{
		scan_init((scan_t *) &scan, WINDOW_SIZE_Y);
		//draw_polygon(&list_polygon[i]);
		scan_line(&list_polygon[i], (scan_t *) &scan);
		scan_line_draw(&list_polygon[i], (scan_t *) &scan, WINDOW_SIZE_Y, &zBuffer);
	}

	zBuffer_render(&zBuffer);

	init();

	return 0;
}
Exemple #22
0
int
parse_id(char *line)
{
	unsigned int input_val, offset, i;

	offset = id_num * SENSOR_BLOCK_SIZE;
	id_num++;
	if (scan_line(line, &input_val) == ERROR)
		return (ERROR);

	for (i = 0; i < id_num-1; i++) {
	if (!memcmp(&input_val, &id_header.id_block[i*SENSOR_BLOCK_SIZE],
	    ID_SIZE)) {
			printf("ERROR: Two sensors with id = %x\n", input_val);
			return (ERROR);
		}
	}
	write_bytes(input_val, ID_SIZE, offset, id_header.id_block);
	offset += ID_SIZE;
	write_bytes(running_offset, OFFSET_SIZE, offset, id_header.id_block);

	return (NO_ERROR);
}
Exemple #23
0
void SrRasterizer::_new_set_tri2(SrTriangle* tri, std::vector<SrFragment*>& _triangles_fragments)
{
	//!!!性能issue
	//SrFragment* frg = new SrFragment();
	//用于判断左右三角
	float tc = (tri->v[1].position.y - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
	float xc = tri->v[0].position.x*(1 - tc) + tri->v[2].position.x*tc;

	//0->2
	if (tri->v[1].position.x >= xc)
	{
		//y中点在右边
		float x0 = tri->v[0].position.x;
		int y0 = ceil(tri->v[0].position.y - 0.5);
		float x1 = tri->v[1].position.x;
		int y1 = ceil(tri->v[1].position.y - 0.5);
		float x2 = tri->v[2].position.x;
		int y2 = ceil(tri->v[2].position.y - 0.5);

		tri->v[0].position.y = y0;
		tri->v[1].position.y = y1;
		tri->v[2].position.y = y2;

		//检测三角形是否退化为直线
		if (RBMath::is_nearly_equal(tri->v[0].position.y, tri->v[1].position.y) && RBMath::is_nearly_equal(tri->v[1].position.y, tri->v[2].position.y) ||
			RBMath::is_nearly_equal(tri->v[0].position.x, tri->v[1].position.x) && RBMath::is_nearly_equal(tri->v[1].position.x, tri->v[2].position.x))
			return;

		float dx_left_02 = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);

		float dx_right_12 = (tri->v[2].position.x - tri->v[1].position.x) / (tri->v[2].position.y - tri->v[1].position.y);
		float dx_right_01 = (tri->v[1].position.x - tri->v[0].position.x) / (tri->v[1].position.y - tri->v[0].position.y);

		VertexP3N3T2 ph;
		VertexP3N3T2 ph1;
		VertexP3N3T2 p11;
		VertexP3N3T2 p21;
		VertexP3N3T2 p22;

		float tempt = (tri->v[1].position.y - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
		ph.position = _lerp_position(tri->v[0].position, tri->v[2].position, tempt);
		ph.position.y = RBMath::round_f(ph.position.y);
		ph.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, tempt);
		ph.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		tempt = (tri->v[1].position.y - 1 - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
		ph1.position = _lerp_position(tri->v[0].position, tri->v[2].position, tempt);
		ph1.position.y = RBMath::round_f(ph1.position.y);
		ph1.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, tempt);
		ph1.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		tempt = (tri->v[1].position.y - 1 - tri->v[0].position.y) / (tri->v[1].position.y - tri->v[0].position.y);
		p11.position = _lerp_position(tri->v[0].position, tri->v[1].position, tempt);
		p11.position.y = RBMath::round_f(p11.position.y);
		p11.normal = _lerp_normal(tri->v[0].normal, tri->v[1].normal, tempt);
		p11.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[1].text_coord, tri->v[1].position.w, tempt);

		tempt = (tri->v[2].position.y - 1 - ph.position.y) / (y2 - ph.position.y);
		p21.position = _lerp_position(ph.position, tri->v[2].position, tempt);
		p21.position.y = RBMath::round_f(p21.position.y );
		p21.normal = _lerp_normal(ph.normal, tri->v[2].normal, tempt);
		p21.text_coord = _lerp_uv(ph.text_coord, ph.position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		p22.position = _lerp_position(tri->v[1].position, tri->v[2].position, tempt);
		p22.position.y = RBMath::round_f(p22.position.y );
		p22.normal = _lerp_normal(tri->v[1].normal, tri->v[2].normal, tempt);
		p22.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		VertexP3N3T2 vs;
		VertexP3N3T2 ve;

		float t = 0.f;
		int loop_y = 0;
		float dyl = ph1.position.y - y0;
		float dyr = p11.position.y - y0;
		float xs = x0;
		float xe = x0;
		for (loop_y = y0; loop_y <= ph1.position.y; ++loop_y)
		{
			if (RBMath::is_nearly_equal(dyl, 0.f))
			{
				t = 0;
			}
			else
			{
				t = (loop_y - y0) / dyl;
			}
			
			vs.position = _lerp_position(tri->v[0].position, ph1.position, t);
			vs.position.y = loop_y;
			vs.position.x = xs;
			vs.normal = _lerp_normal(tri->v[0].normal, ph1.normal, t);
			vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, ph1.text_coord, ph1.position.w, t);

			//t = (loop_y - y0) / dyr;
			//ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position = _lerp_position(tri->v[0].position, p11.position, t);
			ve.position.y = loop_y;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[0].normal, p11.normal, t);
			ve.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, p11.text_coord, p11.position.w, t);

			if(scan_line(vs, ve))
				return;

			xs += dx_left_02;
			xe += dx_right_01;
		}

		dyl = y2 - 1 - ph.position.y;
		dyr = y2 - 1 - y1;
		xs = ph.position.x;
		xe = x1;
		for (loop_y = ph.position.y; loop_y < y2; ++loop_y)
		{
			if (RBMath::is_nearly_equal(dyl, 0.f))
			{
				t = 0;
			}
			else
			{
				t = (loop_y - ph.position.y) / dyl;
			}
			
			vs.position = _lerp_position(ph.position, p21.position, t);
			vs.position.y = loop_y;
			vs.position.x = xs;
			vs.normal = _lerp_normal(ph.normal, p21.normal, t);
			vs.text_coord = _lerp_uv(ph.text_coord, ph.position.w, p21.text_coord, p21.position.w, t);

			//t = (loop_y - ph.position.y) / dyr;
			//ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position = _lerp_position(tri->v[1].position, p22.position, t);
			ve.position.y = loop_y;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[1].normal, p22.normal, t);
			ve.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, p22.text_coord, p22.position.w, t);

			if(scan_line(vs, ve))
				return;

			xs += dx_left_02;
			xe += dx_right_12;
		}

		//RBlog("line\n");
	}
	else
	{
		//y中点在左边
		float x0 = tri->v[0].position.x;
		int y0 = ceil(tri->v[0].position.y - 0.5);
		float x1 = tri->v[1].position.x;
		int y1 = ceil(tri->v[1].position.y - 0.5);
		float x2 = tri->v[2].position.x;
		int y2 = ceil(tri->v[2].position.y - 0.5);

		tri->v[0].position.y = y0;
		tri->v[1].position.y = y1;
		tri->v[2].position.y = y2;

		//检测三角形是否退化为直线
		if (RBMath::is_nearly_equal(tri->v[0].position.y, tri->v[1].position.y) && RBMath::is_nearly_equal(tri->v[1].position.y, tri->v[2].position.y) ||
			RBMath::is_nearly_equal(tri->v[0].position.x, tri->v[1].position.x) && RBMath::is_nearly_equal(tri->v[1].position.x, tri->v[2].position.x))
			return;

		float dx_right_02 = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);

		float dx_left_12 = (tri->v[2].position.x - tri->v[1].position.x) / (tri->v[2].position.y - tri->v[1].position.y);
		float dx_left_01 = (tri->v[1].position.x - tri->v[0].position.x) / (tri->v[1].position.y - tri->v[0].position.y);

		VertexP3N3T2 ph;
		VertexP3N3T2 ph1;
		VertexP3N3T2 p11;
		VertexP3N3T2 p21;
		VertexP3N3T2 p22;

		float tempt = (tri->v[1].position.y - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
		ph.position = _lerp_position(tri->v[0].position, tri->v[2].position, tempt);
		ph.position.y =  RBMath::round_f(ph.position.y );
		ph.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, tempt);
		ph.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		tempt = (tri->v[1].position.y - 1 - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
		ph1.position = _lerp_position(tri->v[0].position, tri->v[2].position, tempt);
		ph1.position.y = RBMath::round_f(ph1.position.y);
		ph1.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, tempt);
		ph1.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		tempt = (tri->v[1].position.y - 1 - tri->v[0].position.y) / (tri->v[1].position.y - tri->v[0].position.y);
		p11.position = _lerp_position(tri->v[0].position, tri->v[1].position, tempt);
		p11.position.y = RBMath::round_f(p11.position.y);
		p11.normal = _lerp_normal(tri->v[0].normal, tri->v[1].normal, tempt);
		p11.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[1].text_coord, tri->v[1].position.w, tempt);

		tempt = (tri->v[2].position.y - 1 - ph.position.y) / (y2 - ph.position.y);
		p21.position = _lerp_position(ph.position, tri->v[2].position, tempt);
		p21.position.y = RBMath::round_f(p21.position.y);
		p21.normal = _lerp_normal(ph.normal, tri->v[2].normal, tempt);
		p21.text_coord = _lerp_uv(ph.text_coord, ph.position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		p22.position = _lerp_position(tri->v[1].position, tri->v[2].position, tempt);
		p22.position.y = RBMath::round_f(p22.position.y);
		p22.normal = _lerp_normal(tri->v[1].normal, tri->v[2].normal, tempt);
		p22.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, tri->v[2].text_coord, tri->v[2].position.w, tempt);

		VertexP3N3T2 vs;
		VertexP3N3T2 ve;

		float t = 0.f;
		int loop_y = 0;
		float dyl = ph1.position.y - y0;
		float dyr = p11.position.y - y0;
		float xs = x0;
		float xe = x0;
		for (loop_y = y0; loop_y <= ph1.position.y; ++loop_y)
		{


			if (RBMath::is_nearly_equal(dyl, 0.f))
			{
				t = 0;
			}
			else
			{
				t = (loop_y - y0) / dyl;
			}
			
			vs.position = _lerp_position(tri->v[0].position, ph1.position, t);
			vs.position.y = loop_y;
			if (abs(xs - vs.position.x)>0.001)
			{
				xs += dx_right_02;
				xe += dx_left_01;
				continue;
			}
			vs.position.x = xs;
			vs.normal = _lerp_normal(tri->v[0].normal, ph1.normal, t);
			vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, ph1.text_coord, ph1.position.w, t);

			//t = (loop_y - y0) / dyr;
			//ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position = _lerp_position(tri->v[0].position, p11.position, t);
			ve.position.y = loop_y;

			ve.position.x = xe;

			ve.normal = _lerp_normal(tri->v[0].normal, p11.normal, t);
			ve.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, p11.text_coord, p11.position.w, t);

			if(scan_line(ve, vs))
				return;

			xs += dx_right_02;
			xe += dx_left_01;
		}

		dyl = y2 - 1 - ph.position.y;
		dyr = y2 - 1 - y1;
		xs = ph.position.x;
		xe = x1;
		for (loop_y = ph.position.y; loop_y < y2; ++loop_y)
		{
			if (RBMath::is_nearly_equal(dyl, 0.f))
			{
				t = 0;
			}
			else
			{
				t = (loop_y - ph.position.y) / dyl;
			}
			vs.position = _lerp_position(ph.position, p21.position, t);
			vs.position.y = loop_y;
			vs.position.x = xs;
			vs.normal = _lerp_normal(ph.normal, p21.normal, t);
			vs.text_coord = _lerp_uv(ph.text_coord, ph.position.w, p21.text_coord, p21.position.w, t);

			//t = (loop_y - ph.position.y) / dyr;
			//ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position = _lerp_position(tri->v[1].position, p22.position, t);
			ve.position.y = loop_y;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[1].normal, p22.normal, t);
			ve.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, p22.text_coord, p22.position.w, t);

			if (scan_line(ve, vs))
				return;

			xs += dx_right_02;
			xe += dx_left_12;
		}
		//RBlog("line\n");
	}
	
	//_triangles_fragments.push_back(frg);

}
Exemple #24
0
int read_input_file( char *fname, FILE *outfp, int *dim, RAT **ar, int *nel_ar, 
                     char *intkey1, int **intli1, char *intkey2, int **intli2,
                     char *RATkey1, RAT **RATli1 )
/*****************************************************************/
/*
 * Read the dimension "dim",
 * a table "ar" of points (in case of a .poi file), 
 * or of inequalities (in case of a .ieq file),
 * a line "intli1" of integers occurring after keyword "intkey1",
 * a line "intli2" of integers occurring after keyword "intkey2",
 * a line "RATli1" of rationals occurring after keyword "RATkey1".
 * "ar6" is a point that is output in the VALID section in the .ieq file.
 * 
 * Change by M.S. 5.6.92:
 * If "outfp" exists (e.g. if is_set(Sort)),
 * copy the input file into outfp, 
 * except for the INEQUALITIES_SECTION, the CONV_SECTION, and the CONE_SECTION,
 * and END.
 */
{
    int i,j,ieqs,nonempty,arrows=0,arrowl=0,cone = 0,conv = 0,line;
    int *hip;
    char *in,*end = "END",
    equalities[22],
    convstr[13],conestr[13],key_eli[18],key_val[6],key_low[13],key_upp[13], 
    *comm = "COMMENT" ;
    char scanned_inline[MAXLINE];
    char in_line[MAXLINE];
    RAT val;

    line = 0;
    strcpy (equalities,"INEQUALITIES_SECTION");
    strcpy (convstr,"CONV_SECTION");
    strcpy (conestr,"CONE_SECTION");
    strcpy (key_eli,"ELIMINATION_ORDER");
    strcpy (key_val,"VALID");
    strcpy (key_upp,"UPPER_BOUNDS");
    strcpy (key_low,"LOWER_BOUNDS");
    fp = fopen(fname,"r");
    if (fp == 0)
        msg( "%s : no such file", fname, 0 );
    
    /* 
     * Read the first line into string "in_line". Leading blanks are skipped.
     * In_Line should start with "DIM=".
     */
    
    do 
    {
        nonempty = get_line(fp,fname,in_line,&line );
    } 
    while (!nonempty);
    
    scan_line(&val,3,0,line,fname,in_line,scanned_inline);
    if (strncmp(scanned_inline,"DIM=",4) == 0) 
    {
        in = scanned_inline+4;
        *dim = atoi(in);
        if (*dim == 0) 
            msg("%s, line %i : dimension error",fname,line);
        if (outfp) 
        {
            fprintf(outfp,"%s",in_line);

            /* 17.01.1994: include logging on file porta.log */
            porta_log( "%s",in_line);
        }
    }
    ieqs = (strcmp(fname+strlen(fname)-4,".ieq") == 0);
    if( ieqs ) 
    {
        equa = ineq = 0;
        convstr[0] = conestr[0] = '\n';
        arrowl = *dim+2;
        if (!strcmp(intkey1,"ELIMINATION_ORDER") || !strcmp(intkey2,"ELIMINATION_ORDER"))
            key_eli[0] =  '\n'; 
        if (!strcmp(intkey1,"LOWER_BOUNDS") || !strcmp(intkey2,"LOWER_BOUNDS"))
            key_low[0] =  '\n'; 
        if (!strcmp(intkey1,"UPPER_BOUNDS") || !strcmp(intkey2,"UPPER_BOUNDS"))
            key_upp[0] =  '\n'; 
        if (!strcmp(RATkey1,"VALID"))
            key_val[0] =  '\n'; 
    }
    else 
    {
        /* 
         * key_eli, key_low, key_upp, and key_val are set to 0,
         * if they already appear as input parameters to this subroutine.
         * In this way, if e.g. the keyword "VALID" is encountered twice in the 
         * input file, an error message is produced.
         * "convstr" and "conestr" are set to "\n",
         * so that the keywords "CONV_SECTION" and "CONE_SECTION" 
         * result in an error message, if they appear in the .ieq file.
         */
        cone = conv = 0;
        key_val[0] = key_upp[0] = key_low[0] = key_eli[0] = equalities[0] = '\n';
        arrowl = *dim+1;
    }
    
    /* 
     * ar6 is initialized to 0.
     * It is overwritten by a point in the CONV_SECTION,
     * or by a point in the VALID section.
     */
    ar6 = (RAT *) RATallo(ar6,0,*dim);
    for (j = 0; j < *dim; j++) 
    {
        ar6[j].num = 0;
        ar6[j].den.i = 1;
    }
    
    /* Read the next line into string "in_line" and scan it */
    nonempty = get_line(fp,fname,in_line,&line );
    scan_line(&val,3,0,line,fname,in_line,scanned_inline);
    do 
    {
        if (!nonempty) 
        {
            if(outfp)
            {
                fprintf(outfp,"%s",in_line);
                
                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
        }
        else if (strncmp(scanned_inline,comm,7) == 0) 
        {
            /*
             * Overread keyword COMMENT and all following lines,
             * until the next keyword (i.e. one of the arguments of nstrcmp() ) 
             * is encountered.
             */
            do 
            {
                if(outfp)
                { 
                    fprintf(outfp,"%s",in_line);
                    
                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
                nonempty = get_line(fp,fname,in_line,&line);
                scan_line(&val,3,0,line,fname,in_line,scanned_inline);
            } 
            while (nstrcmp(scanned_inline,conestr,equalities,end,
             intkey1,intkey2,RATkey1,convstr) || !nonempty);
        }
        else if (strcmp(scanned_inline,RATkey1) == 0) 
        {
            /*
             * If the keyword RATkey1 is encountered,
             * read the next line of "dim" rationals into array RATli1.
             * RATkey1 is "VALID" for "traf *.ieq" applications.
             */
            if(outfp)
            { 
                fprintf(outfp,"%s",in_line);
            
                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            /*
               When calling read_input_file, RATkey1 is always
               assigned to a const argument (string).
               Therefore it isn't allowed to assign new
               values to this argument. By the way, this
               statement causes a bus error on some architectures
               like the HP!
               18.01.1994, Andreas Loebel
               RATkey1[0] = '\n';
               *RATli1 = (RAT *) RATallo(*RATkey1,0,*dim);
               */
            *RATli1 = (RAT *) RATallo(CP 0,0,*dim);
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
                if(outfp)
                {
                    fprintf(outfp,"%s",in_line);

                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
            } 
            while (!nonempty);
            scan_line(*RATli1,1,*dim,line,fname,in_line,scanned_inline);
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
        }
        else if (strcmp(scanned_inline,key_val) == 0) 
        {
            /*
             * keyval[0] == "VALID", iff
             * Ratkey1 was not "VALID" at the start of function read_input_file()
             * Otherwise keyval[0] = '\n'
             * --> "VALID" is accepted as a keyword at most once.
             */
            if(outfp)
            { 
                fprintf(outfp,"%s",in_line);
                
                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            key_val[0] = '\n';
            /* 
             * Change by M.S.: "ar6" was already allocated earlier,
             * and it is never freed in this loop.
             ar6 = (RAT *) RATallo(ar6,0,*dim);
             */
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
                if(outfp)
                {
                    fprintf(outfp,"%s",in_line);

                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
            } 
            while (!nonempty);
            scan_line(ar6,1,*dim,line,fname,in_line,scanned_inline);
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
            /* change by M.S. 31.5.92: 
             * "ar6" is also used when reading the CONV_SECTION, so don't free it.
             ar6 = (RAT *) RATallo(ar6,*dim,0);
             */
        }
        else if (strcmp(scanned_inline,intkey1) == 0) 
        {
            if(outfp)
            { 
                fprintf(outfp,"%s",in_line);
            
                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            intkey1[0] = '\n';
            *intli1 = (int *) allo(*intli1,0,*dim*sizeof(int));
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
                if(outfp)
                { 
                    fprintf(outfp,"%s",in_line);
                
                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
            } 
            while (!nonempty);
            scan_line((RAT *)*intli1,2,*dim,line,fname,in_line,scanned_inline);
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
        }
        else if (strcmp(scanned_inline,intkey2) == 0) 
        {
            if(outfp)
            { 
                fprintf(outfp,"%s",in_line);

                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            intkey2[0] = '\n';
            *intli2 = (int *) allo(*intli2,0,*dim*sizeof(int));
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
                if(outfp)
                { 
                    fprintf(outfp,"%s",in_line);

                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
            } 
            while (!nonempty);
            scan_line((RAT *)*intli2,2,*dim,line,fname,in_line,scanned_inline);
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
        }
        else if (!strcmp(scanned_inline,key_eli) 
                 || !strcmp(scanned_inline,key_low) 
                 || !strcmp(scanned_inline,key_upp) ) 
        {
            /*
             * array "hip" is just a temporary array.
             */
            if(outfp)
            { 
                fprintf(outfp,"%s",in_line);
                
                /* 17.01.1994: include logging on file porta.log */
                porta_log( "%s",in_line);
            }
            if (!strcmp(scanned_inline,key_eli))   key_eli[0] = '\n';
            if (!strcmp(scanned_inline,key_low))   key_low[0] = '\n';
            if (!strcmp(scanned_inline,key_upp))   key_upp[0] = '\n';
            hip = (int *) allo(CP 0,0,*dim*sizeof(int));
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
                if(outfp)
                { 
                    fprintf(outfp,"%s",in_line);
                    
                    /* 17.01.1994: include logging on file porta.log */
                    porta_log( "%s",in_line);
                }
            } 
            while (!nonempty);
            scan_line((RAT *)hip,2,*dim,line,fname,in_line,scanned_inline);
            nonempty = get_line(fp,fname,in_line,&line);
            scan_line(&val,3,0,line,fname,in_line,scanned_inline);
            hip = (int *) allo(hip,*dim*sizeof(int),0);
        }
        else if (strcmp(scanned_inline, convstr) == 0) 
        {
            /* 
             * Read the CONV_SECTION into array "ar".
             * For all points j=1,...,conv in the CONV_SECTION, 
             * ar[j-1][dim] is set to 1.
             * "arrowl" was set to dim+1 earlier.
             */
            convstr[0] = '\n';
            *ar = (RAT *) RATallo(*ar,arrows*arrowl,
                                  (arrows+INCR_INSYS_ROW)*arrowl);
            arrows += INCR_INSYS_ROW;
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
            } 
            while (!nonempty);
            while (scan_line(*ar+(conv+cone)*arrowl,0,*dim,line,fname,
                             in_line,scanned_inline)) 
            {
                (*ar+(conv+cone)*arrowl+*dim)->num = 1;
                if (conv+cone+2 > arrows) 
                {
                    *ar = (RAT *) RATallo(*ar,arrows*arrowl,
                                    (arrows+INCR_INSYS_ROW)*arrowl);
                    arrows += INCR_INSYS_ROW;
                }
                do 
                {
                    nonempty = get_line(fp,fname,in_line,&line);
                } 
                while (!nonempty);
                conv++;
            }
            /* repeat the last point in array "ar6" */
            for (j = 0; j < *dim; j++)
                ar6[j] = (*ar+(conv+cone-1)*arrowl)[j];
        }
        else if ((strcmp(scanned_inline, conestr) == 0)) 
        {
            /* 
             * Read the CONE_SECTION
             * ar[j-1][dim] is initialized by RATallo to 0.
             */
            conestr[0] = '\n';
            *ar = (RAT *) RATallo(*ar,arrows*arrowl,
                                  (arrows+INCR_INSYS_ROW)*arrowl);
            arrows += INCR_INSYS_ROW;
            do 
            {
                nonempty = get_line(fp,fname,in_line,&line);
            } 
            while (!nonempty);
            while (scan_line(*ar+(conv+cone)*arrowl,0,*dim,line,fname,
                             in_line,scanned_inline)) 
            {
                if (conv+cone+2 > arrows) 
                {
                    *ar = (RAT *) RATallo(*ar,arrows*arrowl,
                                          (arrows+INCR_INSYS_ROW)*arrowl);
                    arrows += INCR_INSYS_ROW;
                }
                do 
                {
                    nonempty = get_line(fp,fname,in_line,&line);
                } 
                while (!nonempty);
                cone++;
            }
        }
        else if ( (strcmp(scanned_inline, equalities) == 0)) 
        {
            /*
             * Read the INEQUALITIES_SECTION
             * "arrowl" was set to dim+2 earlier.
             */
            equalities[0] = '\n';      
            *ar = (RAT *) RATallo(*ar,arrows*arrowl,
                                  (arrows+INCR_INSYS_ROW)*arrowl);
            arrows += INCR_INSYS_ROW;
            read_eqie(ar,*dim,&equa,&ineq,&arrows,&line,
                      fname,in_line,scanned_inline);
            nonempty = 1;
        }
        else if (strcmp(scanned_inline,end) == 0)
        {
            i =  (ieqs) ? equa+ineq : conv+cone;
            
            *nel_ar = (i+1)*arrowl;
            
            *ar = (RAT *) RATallo(*ar,arrows*arrowl,*nel_ar);
            
            fclose(fp);
            
            fprintf(prt,"input file %s o.k.\n",fname);
            fprintf(prt,  "dimension              : %4i \n",*dim);

            /* 17.01.1994: include logging on file porta.log */
            porta_log( "input file %s o.k.\n",fname);
            porta_log( "dimension              : %4i \n",*dim);

            if (ieqs) 
            {
                fprintf(prt,"number of equations    : %4i \n",equa);
                fprintf(prt,"number of inequalities : %4i \n\n",ineq);

                /* 17.01.1994: include logging on file porta.log */
                porta_log( "number of equations    : %4i \n",equa);
                porta_log( "number of inequalities : %4i \n\n",ineq);
            }
            else 
            {
                fprintf(prt,"number of cone-points  : %4i \n",cone);
                fprintf(prt,"number of conv-points  : %4i \n\n",conv);

                /* 17.01.1994: include logging on file porta.log */
                porta_log( "number of cone-points  : %4i \n",cone);
                porta_log( "number of conv-points  : %4i \n\n",conv);
            }
            
            return (i);
            
        }
        
        else
            msg("%s, line %i : invalid format",fname,line);
        
    } 
    while (1);
    
}
Exemple #25
0
/**
 * Reads the list of files from the host IDE runs on,
 * creates files, fills internal file table
 */
static int init_files() {
    trace("Files list initialization\n");
    int bufsize = PATH_MAX + 32;
    char buffer[bufsize];
    int success = false;
    file_elem* list = NULL;
    file_elem* tail = NULL;
    start_adding_file_data();
    while (1) {
        if( !fgets(buffer, bufsize, stdin)) {
            report_error("protocol error while reading file info: unexpected EOF\n");
            return false;
        }
        if (buffer[0] == '\n') {
            success = true;
            break;
        }
        trace("\tFile init: %s", buffer); // no trailing LF since it's in the buffer
        // remove trailing LF
        char* lf = strchr(buffer, '\n');
        if (lf) {
            *lf = 0;
        }
        if (strchr(buffer, '\r')) {
            report_error("protocol error: unexpected CR: %s\n", buffer);
            return false;
        }

        enum file_state state;
        int file_size;
        char *path;
        struct timeval file_time;
        file_time.tv_sec = file_time.tv_usec = 0;

        if (!scan_line(buffer, bufsize, &state, &file_size, &file_time, (const char**) &path)) {
            report_error("protocol error: %s\n", buffer);
            break;
        }
        trace("\t\tpath=%s size=%d state=%c (0x%x) time=%d.%d\n", path, file_size, (char) state, (char) state, file_time.tv_sec, file_time.tv_usec);

        if (state == -1) {
            report_error("protocol error: %s\n", buffer);
            break;
        } else if (state == DIRECTORY) { // directory
            create_dir(path);
        } else if (state == LINK) { // symbolic link
            char lnk_src[bufsize]; // it is followed by a line that contains the link source
            if( !fgets(lnk_src, sizeof lnk_src, stdin)) {
                report_error("protocol error while reading link info: unexpected EOF\n");
                return false;
            }
            char* lf = strchr(lnk_src, '\n');
            if (lf) {
                *lf = 0;
            }
            if (strchr(buffer, '\r')) {
                report_error("protocol error: unexpected CR: %s\n", buffer);
                return false;
            }
            create_lnk(path, lnk_src);
        } else { // plain file
            int touch = false;
            if (state == INITIAL) {
                touch = true;
            } else if (state == COPIED || state == TOUCHED) {
                touch = !file_exists(path, file_size);
            } else if (state == UNCONTROLLED || state == INEXISTENT) {
                // nothing
            } else {
                report_error("protocol error: %s\n", buffer);
            }

            enum file_state new_state = state;
            if (touch) {
                if (touch_file(path, file_size, &file_time)) {
                    new_state = TOUCHED;
                } else {
                    new_state = ERROR;
                }
            }

            if (*path == '/') {
                char real_path [PATH_MAX + 1];
                if (state == UNCONTROLLED || state == INEXISTENT) {
                    char *dir = path;
                    char *file = path;
                    // find trailing zero
                    while (*file) {
                        file++;
                    }
                    // we'll find '/' for sure - at least the starting one
                    while(*file != '/') {
                        file--;
                    }
                    if (file == path) { // the file resides in root directory
                        strcpy(real_path, path);
                    } else {
                        // NB: we modify the path! but we'll restore later
                        char *pfile_start = file; // save file start char
                        char file_start = *file;  // save file start char
                        *file = 0; // replace the '/' that separates file from dir by zero
                        file++; 
                        if (!realpath(dir, real_path)) {
                            report_unresolved_path(dir);
                            break;
                        }
                        char *p = real_path;
                        while (*p) {
                            p++;
                        }
                        *(p++) = '/';
                        strncpy(p, file, sizeof real_path - (p - real_path));
                        *pfile_start = file_start; // restore file start char
                    }
                } else {
                    if (!realpath(path, real_path)) {
                       report_unresolved_path(path);
                       break; 
                    }
                }
                trace("\t\tadding %s with state '%c' (0x%x) -> %s\n", path, (char) new_state, (char) new_state, real_path);
                add_file_data(real_path, new_state);
                // send real path to local controller
                tail = add_file_to_list(tail, path, new_state, real_path);
                //trace("\t\tadded to list %s with state '%c' (0x%x) -> %s\n", tail->filename, tail->state, tail->state, tail->real_path);
                if (list == NULL) {
                    list = tail;
                }
            } else {
                report_error("protocol error: %s is not absoulte\n", path);
                break;
            }
        }
    }
    stop_adding_file_data();
    trace("Files list initialization done\n");
    if (success) {
        // send info about touched files which were passed as copied files
        tail = list;
        while (tail != NULL) {
            fprintf(stdout, "*%c%s\n%s\n", tail->state, tail->filename, tail->real_path);
            fflush(stdout);
            tail = tail->next;
        }
        free_file_list(list);
        // empty line as indication of finished files list
        fprintf(stdout, "\n");
        fflush(stdout);
    }
    return success;
}
Exemple #26
0
void read_eqie( RAT **ar, int dim, int *equa, int *ineq, int *maxrows, int *line,
               char *fname, char in_line[], char *scanned_inline )
/*****************************************************************/
/*
 * Read the INEQUALITY_SECTION into the table "ar".
 * For each (in)equality, the corresponding line of "ar" contains, in this order:
 *    the "dim" coefficients of the variables x1--xn,
 *    the right-hand side,
 *    0 if it was an equation, 1 if it was an inequality.
 * All inequalities are stored as "<=" inequalities.
 * Lines as
 *   "x1 - 1 <= 3 + x2"
 * are interpreted as
 *   "x1 -x2 <= 4"
 */
{
    char *p,*in;
    int i,rs=0,index=0,j,sysrow, numberread,nonempty;
    RAT val;

    sysrow = dim+2; /* row length of array "ar" */
    do 
    {
        nonempty = get_line(fp,fname,in_line,line);
    } 
    while (!nonempty);
    for (i = 0; scan_line(&val,3,dim,*line,
                          fname,in_line,scanned_inline);
         i++) 
    {
        /* 
         * scan_line() reads an inequality into string "scanned_inline",
         * with a little formatting.
         * "val" is not used.
         */
        p = in = scanned_inline;
        rs = 0;     /* rs records the type of inequality,
                     * rs = 0 means: type not yet known. */
        
        /* scan string "scanned_inline" up to '\0' or '#' */
        while (*p != '\0' && *p != '#' ) 
        {
            
            val.den.i = 1;
            val.num = 1;
            if (*p == '-')
                val.num = -1;
        
            /* 
             * If: {+,-} has been read,
             *     or {<,=,>} followed by {x,0,1,..,9} has been read,
             *     or it is the beginning of the line. then ...
             */
            if ( *p == '-' || *p == '+'  || 
                (p == scanned_inline || 
                 (p != scanned_inline && (*(p-1)=='<' || *(p-1)=='=' || *(p-1)=='>')) 
                 && ((*p < 58 && *p > 47) || *p =='x') )     ) 
            {
                numberread = 0;
                if ( *p == '-' || *p == '+')    p++;
                in = p;
                while (*p > 47 && *p < 58)
                    p++;
                if (in != p) 
                {
                    /* in points to a number, p to the next non-number */
                    numberread = 1;
                    val.num *= atoi(in);
                    if (*p == '/') 
                    {
                        p++;
                        in = p;
                        while (*p > 47 && *p < 58) p++;
                        if (p == in)
                            msg("%s, line %i : invalid denominator",
                                fname,*line);
                        if ((val.den.i = atoi(in)) <= 0)
                            msg("%s, line %i : invalid denominator",
                                fname,*line);
                    }   
                } /* if (in != p) */

                if (*p == 'x') 
                {
                    p++;
                    in = p;
                    while (*p > 47 && *p < 58) p++;
                    if (p == in) 
                        msg("%s, line %i : invalid format",fname,*line);
                    index = atoi(in)-1;
                    if (index > dim-1 || index < 0)
                        msg("%s, line %i : only variable names x1,...,xdim allowed",
                            fname,*line);
                }
                /* 
                 * Change by M.S. 1.6.92:
                 * The following four lines of code are changed.
                 *
                 * In the case (*p in {+,-}), 
                 * something like "10 +", but not "x10 +" has been encountered.
                 * In the case (rs==0 && *p in {<,>,>} ),
                 * something like "10 <", but not "x10 <" has been encountered.
                 * In both cases, the number "10" is interpreted as part of
                 * the right-hand side.
                 * But it should be tested, 
                 * whether a number has actually been read.
                 * Otherwise
                 * "x1++x2 <= 20\n"  is interpreted as
                 * "x1+1+x2 <= 20\n".
                 */
                else if (numberread && (*p == '+' || *p == '-'  || 
                          ((rs == 0) && (*p =='<'||*p == '>'||*p == '=')))) 
                {
                    index = dim;
                }
                /* The following four lines mean:
                 * If "<=" or ">=" or "==" has already been read (i.e. rs > 0),
                 * and if p points to the end of the line,
                 * then interpret the number in val as the right hand side.
                 * to be stored in the dim-th position of the current row
                 * of table "ar".
                 */
                else if (numberread && rs && (*(p) == '\0' || *(p) == '#'))  
                {
                    index = dim;
                }
                else 
                    msg("%s, line %i : invalid format",fname,*line);
            } /* if ......... */

            else if ((p != scanned_inline) 
                     && (*p == '<' || *p == '>' || *p == '=')) 
            {
                /* 
                 * Record the type of inequality:
                 * rs = 1   if it is an equation ("=" or "==")
                 * rs = 2   if it is a "<=" inequality (may also be written "=<")
                 * rs = 3   if it is a ">=" inequality (may also be written "=>")
                 */
                if (++rs > 1) 
                    msg("%s, line %i : invalid format",fname,*line);
                if ((*p == '=') && (*(p+1) == '='))
                    p++;
                else if ((*p == '=') && (*(p+1) != '>') && (*(p+1) != '<'));
                else if ((*p == '=' && *(p+1) == '>') ||
                         (*p == '>' && *(p+1) == '=')) 
                {
                    rs++;rs++;
                    p++;
                }
                else if ((*p == '=' && *(p+1) == '<') ||
                         (*p == '<' && *(p+1) == '=')) 
                {
                    rs++; 
                    p++;
                }
                else
                    msg("%s, line %i : invalid format",fname,*line);
                p++;
                index = -1;
            } /* else if < = > */
            
            else 
                msg("%s, line %i : invalid format",fname,*line);
            
            if (index > -1)  
            { /* not "< > =" */ 
                if (index == dim) val.num *= -1;
                if (rs ) 
                    I_RAT_sub(*(*ar+i*sysrow+index),val,*ar+i*sysrow+index);
                else
                    I_RAT_add(*(*ar+i*sysrow+index),val,*ar+i*sysrow+index);
            }
            
        } /* while */
        
        /* transform ">=" into "<=" by multiplying the inequality with -1 */
        if (rs == 3 ) 
        {  /* >= */
            for (j = 0; j <= dim; j++)
                (*ar+i*sysrow+j)->num = -(*ar+i*sysrow+j)->num;
            rs -= 1;
        }
        (*ar+(i+1)*sysrow-1)->num  =  --rs; 
    
        /* 
         * Now rs = 0 if the line was an equation,
         * and rs = 1 if the line was an inequality.
         * The last line stored rs into the "dim+1"th position of ar[i].
         */
        (rs == 1) ? (*ineq)++ : (*equa)++;
        
        if ((*ineq)+(*equa)+2 > *maxrows) 
        {
            *ar = (RAT *) RATallo(*ar,(*maxrows)*(dim+2),(*maxrows+INCR_INSYS_ROW)*(dim+2));
            *maxrows += INCR_INSYS_ROW;
        }
        do 
        {
            nonempty = get_line(fp,fname,in_line,line);
        } 
        while (!nonempty);
    }  /* for i */
}
Exemple #27
0
void SrRasterizer::_set_top_tri(SrTriangle* tri, std::vector<SrFragment*>& _triangles_fragments)
{
	float dx_left = (tri->v[1].position.x - tri->v[0].position.x) / (tri->v[1].position.y - tri->v[0].position.y);
	float dx_right = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);
	//char a[64];
	//sprintf(a, "================(%f %f)\n", dx_left, dx_right);
	//RBlog(a);
	float x0 = tri->v[0].position.x;
	int y0 = ceil(tri->v[0].position.y - 0.5);
	//int x1 = ceil(tri->v[1].position.x - 0.5);
	//int y1 = ceil(tri->v[1].position.y - 0.5);
	//int x2 = ceil(tri->v[2].position.x - 0.5);
	int y2 = ceil(tri->v[2].position.y - 0.5);

	/*
	tri->v[0].position.x = x0;
	tri->v[0].position.y = y0;
	tri->v[1].position.x = x1;
	tri->v[1].position.y = y1;
	tri->v[2].position.x = x2;
	tri->v[2].position.y = y2;
	*/

	//检测三角形是否退化为直线
	if (RBMath::is_nearly_equal(tri->v[0].position.y, tri->v[1].position.y) && RBMath::is_nearly_equal(tri->v[1].position.y, tri->v[2].position.y) ||
		RBMath::is_nearly_equal(tri->v[0].position.x, tri->v[1].position.x) && RBMath::is_nearly_equal(tri->v[1].position.x, tri->v[2].position.x))
		return;

	//dx_left = ((float)(x1 - x0)) / float(y1 - y0);
	//dx_right = ((float)(x2 - x0)) / float(y2 - y0);

	SrFragment* frg = new SrFragment();

	int loop_y = 0;
	float xs = x0, xe = x0;
	float loop_x = 0;
	float t;
	float dy = y2 - y0+1;
	for (loop_y = 0; loop_y < y2-y0; ++loop_y)
	{

		char a[64];
		sprintf(a, "%f %f\n", xs, xe);
		//RBlog(a);
		t = loop_y / dy;


		VertexP3N3T2 vs;
		vs.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
		vs.position.y = loop_y + y0;
		vs.position.x = xs;
		vs.normal = _lerp_normal(tri->v[0].normal, tri->v[1].normal, t);
		vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[1].text_coord, tri->v[1].position.w, t);

		VertexP3N3T2 ve;
		ve.position = _lerp_position(tri->v[0].position, tri->v[2].position, t);
		ve.position.y = loop_y + y0;
		ve.position.x = xe;
		ve.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, t);
		ve.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, t);

		scan_line(vs, ve, frg);

		xs += dx_left;
		xe += dx_right;

	}
	//RBlog("tri top\n");
	_triangles_fragments.push_back(frg);
}
Exemple #28
0
void SrRasterizer::_set_bottom_tri(SrTriangle* tri, std::vector<SrFragment*>& _triangles_fragments)
{
	//issue
	float dx_left = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);
	float dx_right = (tri->v[2].position.x - tri->v[1].position.x) / (tri->v[2].position.y - tri->v[1].position.y);

	/*
	char a[64];
	sprintf(a, "================(%f %f)\n", dx_left, dx_right);
	RBlog(a);
	*/
	float x0 = tri->v[0].position.x;
	int y0 = ceil(tri->v[0].position.y - 0.5);
	float x1 = tri->v[1].position.x;
	int y1 = ceil(tri->v[1].position.y - 0.5);
	float x2 = tri->v[2].position.x;
	int y2 = ceil(tri->v[2].position.y - 0.5);

	/*
	tri->v[0].position.x = x0;
	tri->v[0].position.y = y0;
	tri->v[1].position.x = x1;
	tri->v[1].position.y = y1;
	tri->v[2].position.x = x2;
	tri->v[2].position.y = y2;
	*/

	//dx_left = ((float)(x2 - x0)) / (y2 - y0);
	//dx_right = ((float)(x2 - x1)) / (y2 - y1);

	SrFragment* frg = new SrFragment();

	int loop_y = 0;
	float xs = x0, xe = x1;
	float loop_x = 0;
	float t;
	float dy = y2 - y0 +1;
	for (loop_y = 0;loop_y<y2-y0;++loop_y)
	{
		t = loop_y / dy;
		char a[64];
		sprintf(a, "%f %f\n", xs, xe);
		//RBlog(a);

		VertexP3N3T2 vs;
		vs.position = _lerp_position(tri->v[0].position,tri->v[2].position,t);
		vs.position.y = loop_y + y0;
		vs.position.x = xs;
		vs.normal = _lerp_normal(tri->v[0].normal,tri->v[2].normal,t);
		vs.text_coord = _lerp_uv(tri->v[0].text_coord,tri->v[0].position.w,tri->v[2].text_coord,tri->v[2].position.w,t);

		VertexP3N3T2 ve;
		ve.position = _lerp_position(tri->v[1].position, tri->v[2].position, t);
		ve.position.y = loop_y + y0;
		ve.position.x = xe;
		ve.normal = _lerp_normal(tri->v[1].normal, tri->v[2].normal, t);
		ve.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, tri->v[2].text_coord, tri->v[2].position.w, t);

		scan_line(vs, ve, frg);

		xs += dx_left;
		xe += dx_right;
	}
	//RBlog("tri bottom\n");
	_triangles_fragments.push_back(frg);

}
Exemple #29
0
void SrRasterizer::_new_set_tri(SrTriangle* tri, std::vector<SrFragment*>& _triangles_fragments)
{
	//!!!性能issue
	SrFragment* frg = new SrFragment();
	/*
	//左右排序<
	if (tri->v[0].position.x > tri->v[1].position.x)
	{
	VertexP3N3T2 temp = tri->v[1];
	tri->v[1] = tri->v[0];
	tri->v[0] = temp;
	}
	*/
	//用于判断左右三角
	float tc = (tri->v[1].position.y - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
	float xc = tri->v[0].position.x*(1 - tc) + tri->v[2].position.x*tc;

	//0->2
	if (tri->v[1].position.x > xc)
	{
		//y中点在右边
		int i = 0;

		//y中点在右边
		float dx_left_02 = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);

		float dx_right_12 = (tri->v[2].position.x - tri->v[1].position.x) / (tri->v[2].position.y - tri->v[1].position.y);
		float dx_right_01 = (tri->v[1].position.x - tri->v[0].position.x) / (tri->v[1].position.y - tri->v[0].position.y);

		float x0 = tri->v[0].position.x;
		int y0 = ceil(tri->v[0].position.y - 0.5);
		float x1 = tri->v[1].position.x;
		int y1 = ceil(tri->v[1].position.y - 0.5);
		float x2 = tri->v[2].position.x;
		int y2 = ceil(tri->v[2].position.y - 0.5);

		VertexP3N3T2 vs;
		VertexP3N3T2 ve;

		float tt = (y0 - tri->v[0].position.y) / (tri->v[2].position.y - tri->v[0].position.y);
		RBVector4 temps = _lerp_position(tri->v[0].position, tri->v[2].position, tt);

		tt = (y0 - tri->v[0].position.y) / (tri->v[1].position.y - tri->v[0].position.y);
		RBVector4 temps2 = _lerp_position(tri->v[0].position, tri->v[1].position, tt);

		//float cur_right_dx = dx_right_01;
		int loop_y = 0;
		float dy = y2 - y0 + 1;
		float dy1 = y1 - y0 + 1;
		float t;
		float xs = x0, xe = x0;
		for (loop_y = 0; loop_y < y1 - y0; ++loop_y)
		{
			t = loop_y / dy;
			if (y0 >= y1)
			{
				return;
			}
			i++;

			//vs.position = _lerp_position(tri->v[0].position, tri->v[2].position, t);
			vs.position = _lerp_position(temps, tri->v[2].position, t);
			vs.position.y = loop_y + y0;
			vs.position.x = xs;
			vs.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, t);
			vs.text_coord = _lerp_uv(tri->v[0].text_coord, temps.w, tri->v[2].text_coord, tri->v[2].position.w, t);

			t = loop_y / dy1;
			//ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position = _lerp_position(temps2, tri->v[1].position, t);
			ve.position.y = loop_y + y0;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[0].normal, tri->v[1].normal, t);
			ve.text_coord = _lerp_uv(tri->v[0].text_coord, temps2.w, tri->v[1].text_coord, tri->v[1].position.w, t);

			scan_line(vs, ve, frg);

			xs += dx_left_02;
			xe += dx_right_01;
		}

		dy = y2 - y0 + 1;
		VertexP3N3T2 vnew;
		vs.position = _lerp_position(tri->v[0].position, tri->v[2].position, (loop_y) / dy);
		vs.position = _lerp_position(temps, tri->v[2].position, (loop_y) / dy);
		vs.position.y = y1;
		vs.position.x = xs;
		vs.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, (loop_y) / dy);
		vs.text_coord = _lerp_uv(tri->v[0].text_coord, temps.w, tri->v[2].text_coord, tri->v[2].position.w, (loop_y) / dy);
		vnew = vs;


		tt = (y1 - tri->v[1].position.y) / (tri->v[2].position.y - tri->v[1].position.y);
		temps2 = _lerp_position(tri->v[1].position, tri->v[2].position, tt);

		float dy_test = y2 - y0 + 1;
		float ttty = loop_y;

		dy = y2 - y1 + 1;
		xs = vnew.position.x;
		xe = x1;
		for (loop_y = 0; loop_y < y2 - y1; ++loop_y)
		{
			t = loop_y / (dy);
			if (y1 >= y2)
			{
				return;
			}
			/*
			vs.position = _lerp_position(vnew.position, tri->v[2].position, t);
			vs.position.y = loop_y + y1;
			vs.position.x = xs;
			vs.normal = _lerp_normal(vnew.normal, tri->v[2].normal, t);
			vs.text_coord = _lerp_uv(vnew.text_coord, vnew.position.w, tri->v[2].text_coord, tri->v[2].position.w, t);
			*/
			float tttt = ttty / dy_test;
			vs.position = _lerp_position(tri->v[0].position, tri->v[2].position, tttt);
			vs.position.y = loop_y + y1;
			vs.position.x = xs;
			vs.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, tttt);
			vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, tttt);

			ttty++;

			ve.position = _lerp_position(temps2, tri->v[2].position, t);
			ve.position.y = loop_y + y1;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[1].normal, tri->v[2].normal, t);
			ve.text_coord = _lerp_uv(tri->v[1].text_coord, temps2.w, tri->v[2].text_coord, tri->v[2].position.w, t);

			scan_line(vs, ve, frg);

			xs += dx_left_02;
			xe += dx_right_12;
			i++;
		}

		i = 0;

		
	}
	else 
	{
		//y中点在左边
		float dx_right_02 = (tri->v[2].position.x - tri->v[0].position.x) / (tri->v[2].position.y - tri->v[0].position.y);

		float dx_left_12 = (tri->v[2].position.x - tri->v[1].position.x) / (tri->v[2].position.y - tri->v[1].position.y);
		float dx_left_01 = (tri->v[1].position.x - tri->v[0].position.x) / (tri->v[1].position.y - tri->v[0].position.y);

		float x0 = tri->v[0].position.x;
		int y0 = ceil(tri->v[0].position.y - 0.5);
		float x1 = tri->v[1].position.x;
		int y1 = ceil(tri->v[1].position.y - 0.5);
		float x2 = tri->v[2].position.x;
		int y2 = ceil(tri->v[2].position.y - 0.5);

		VertexP3N3T2 vs;
		VertexP3N3T2 ve;

		//float cur_right_dx = dx_right_01;
		int loop_y = 0;
		float dy = y2 - y0 + 1;
		float dy1 = y1 - y0 + 1;
		float t;
		float xs = x0, xe = x0;
		for (loop_y = 0; loop_y < y1 - y0; ++loop_y)
		{
			t = loop_y / dy;
			if (y0 > y1)
			{
				return;
			}

			vs.position = _lerp_position(tri->v[0].position, tri->v[2].position, t);
			vs.position.y = loop_y + y0;
			vs.position.x = xs;
			vs.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, t);
			vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, t);

			t = loop_y / dy1;

			ve.position = _lerp_position(tri->v[0].position, tri->v[1].position, t);
			ve.position.y = loop_y + y0;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[0].normal, tri->v[1].normal, t);
			ve.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[1].text_coord, tri->v[1].position.w, t);

			scan_line(ve, vs, frg);

			xs += dx_right_02;
			xe += dx_left_01;
		}

		VertexP3N3T2 vnew;
		dy = y2 - y0 + 1;
		vs.position = _lerp_position(tri->v[0].position, tri->v[2].position, (loop_y) / dy);
		vs.position.y = y1;
		vs.position.x = xs;
		vs.normal = _lerp_normal(tri->v[0].normal, tri->v[2].normal, (loop_y ) / dy);
		vs.text_coord = _lerp_uv(tri->v[0].text_coord, tri->v[0].position.w, tri->v[2].text_coord, tri->v[2].position.w, (loop_y ) / dy);
		vnew = vs;

		dy = y2 - y1 + 1;
		xs = vnew.position.x;
		xe = x1;
		for (loop_y = 0; loop_y < y2 - y1; ++loop_y)
		{
			if (y1>y2)
			{
				return;
			}
			t = loop_y / dy;

			vs.position = _lerp_position(vnew.position, tri->v[2].position, t);
			vs.position.y = loop_y + y1;
			vs.position.x = xs;
			vs.normal = _lerp_normal(vnew.normal, tri->v[2].normal, t);
			vs.text_coord = _lerp_uv(vnew.text_coord, vnew.position.w, tri->v[2].text_coord, tri->v[2].position.w, t);

			ve.position = _lerp_position(tri->v[1].position, tri->v[2].position, t);
			ve.position.y = loop_y + y1;
			ve.position.x = xe;
			ve.normal = _lerp_normal(tri->v[1].normal, tri->v[2].normal, t);
			ve.text_coord = _lerp_uv(tri->v[1].text_coord, tri->v[1].position.w, tri->v[2].text_coord, tri->v[2].position.w, t);

			scan_line(ve, vs, frg);

			xs += dx_right_02;
			xe += dx_left_12;

		}
	}

	_triangles_fragments.push_back(frg);
}
Exemple #30
0
/**
 * Scan the line
 */
void scan_line() {
    switch (sensor) {
        case 0b11111110: // Most left
            PV = -7;
            robot_moves_forward();
            break;
        case 0b11111000:
        case 0b11111100:
            PV = -6;
            robot_moves_forward();
            break;
        case 0b11111101:
            PV = -5;
            robot_moves_forward();
            break;
        case 0b11110001:
        case 0b11111001:
            PV = -4;
            robot_moves_forward();
            break;
        case 0b11111011:
            PV = -3;
            robot_moves_forward();
            break;
        case 0b11100011:
        case 0b11110011:
            PV = -2;
            robot_moves_forward();
            break;
        case 0b11110111:
            PV = -1;
            robot_moves_forward();
            break;
        case 0b11100111: // Center
            PV = 0;
            robot_moves_forward();
            break;
        case 0b11101111:
            PV = 1;
            robot_moves_forward();
            break;
        case 0b11000111:
        case 0b11001111:
            PV = 2;
            robot_moves_forward();
            break;
        case 0b11011111:
            PV = 3;
            robot_moves_forward();
            break;
        case 0b10001111:
        case 0b10011111:
            PV = 4;
            robot_moves_forward();
            break;
        case 0b10111111:
            PV = 5;
            robot_moves_forward();
            break;
        case 0b00011111:
        case 0b00111111:
            PV = 6;
            robot_moves_forward();
            break;
        case 0b01111111: // Most right
            PV = 7;
            robot_moves_forward();
            break;
        case 0b11111111: // Loss
            if (PV < 0) {
                // PV = -8;
                lpwm = 150;
                rpwm = 185;
                robot_turns_left();
                goto exit;
            } else if (PV > 0) {
                // PV = 8;
                lpwm = 180;
                rpwm = 155;
                robot_turns_right();
                goto exit;
            }
    }

    error = SP - PV;
    P = (var_Kp * error) / 10;

    I = I + error;
    I = (I * var_Ki) / 10;

    rate = error - last_error;
    D    = (rate * var_Kd) / 10;

    last_error = error;

    MV = P + I + D;

    if (MV == 0) {
        lpwm = MAXPWM - diffPWM;
        rpwm = MAXPWM;
    } else if (MV > 0) { // Moves left
        rpwm = MAXPWM - ((intervalPWM - 20) * MV);
        lpwm = (MAXPWM - (intervalPWM * MV) - 15) - diffPWM;

        if (lpwm < MINPWM) lpwm = MINPWM;
        if (lpwm > MAXPWM) lpwm = MAXPWM;
        if (rpwm < MINPWM) rpwm = MINPWM;
        if (rpwm > MAXPWM) rpwm = MAXPWM;
    } else if (MV < 0) { // Moves right
        lpwm = MAXPWM + ( ( intervalPWM - 20 ) * MV);
        rpwm = MAXPWM + ( ( intervalPWM * MV ) - 15 );

        if (lpwm < MINPWM) lpwm = MINPWM;
        if (lpwm > MAXPWM) lpwm = MAXPWM;
        if (rpwm < MINPWM) rpwm = MINPWM;
        if (rpwm > MAXPWM) rpwm = MAXPWM;
    }

    exit:
}

void main(void)
{
    // sensor
    PORTA=0x00;
    DDRA=0x00;

    //switch & sKi & sKa
    PORTB=0x0F;
    DDRB=0x00;

    //lcd
    PORTC=0x00;
    DDRC=0x00;

    //motor
    PORTD=0x00;
    DDRD=0xFF;

    // Timer/Counter 0 initialization
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    // Timer/Counter 1 initialization
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x01;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // LCD module initialization
    lcd_init(16);

    /* define user character 0 */
    define_char(char0,0);

    // stop motor
    TCCR0=0x00;
    robot_stops();

    show_menu_in_lcd();

    TCCR0=0x05;
    #asm("sei")

    // read eeprom
    var_Kp  = Kp;
    var_Ki  = Ki;
    var_Kd  = Kd;
    MAXPWM  = (int)max_speed + 1;
    MINPWM  = min_speed;

    intervalPWM = (max_speed - min_speed) / 8;
    PV          = 0;
    error       = 0;
    last_error  = 0;

    robot_moves_forward();
    while (1) {
        scan_line();
    };
}