//判定点是否在空间三角形上,不包括边界,三点共线无意义
int point_in_triangle2(const Point &p, const Plane &s)
{
    return point_in_triangle(p, s)
           && ((p - s.a) ^ (p - s.b)).norm() > eps
           && ((p - s.b) ^ (p - s.c)).norm() > eps
           && ((p - s.c) ^ (p - s.a)).norm() > eps;
}
// http://www.cse.yorku.ca/~aaw/Hang/quick_hull/Algorithm.html
void find_hull(const std::vector<Coordinate> &Sk, const Coordinate P, const Coordinate Q, std::vector<Coordinate> &hull_points) {
   if (Sk.size() == 0) return;

   std::vector<Coordinate> S0;
   std::vector<Coordinate> S1;
   std::vector<Coordinate> S2;

   float greatest_distance = 0.0f;
   Coordinate C;
   for (const auto &pt : Sk) {
      float distance = distance_from_line(P, Q, pt);
      if (distance > greatest_distance) {
         greatest_distance = distance;
         C = pt;
      }
   }

   hull_points.push_back(C);

   for (const auto &pt : Sk) {
      if (point_in_triangle(pt, P, C, Q)) 
         S0.push_back(pt);
      else if (0 < side_of_line(P, C, pt)) 
         S1.push_back(pt);
      else if (0 < side_of_line(C, Q, pt)) 
         S2.push_back(pt); 
   }

   find_hull(S1, P, C, hull_points);
   find_hull(S2, C, Q, hull_points);
}
int Circle::check_circle_is_clear_of_polygon( PongPingPolygon *polygon)
{

   Point v1, v2;

   double d;
   
   double a;
   
   int vn;
   
   if( polygon->attributes & IRREGULAR )
   {
      if( polygon->attributes & PONGPING_HIT_X_TIMES_POLYGON )
	  {
	     for(vn = 0; vn < polygon->number_of_sides; ++vn)
	     {
	        a = polygon->unrotated_points[vn].angle + polygon->angle;
	     
		    v1.x = polygon->cx + polygon->unrotated_points[vn].radius * cos(a);
	        v1.y = polygon->cy + polygon->unrotated_points[vn].radius * sin(a);
		 
			a = polygon->unrotated_points[(vn + 1) % polygon->number_of_sides].angle + polygon->angle;
		 
		    v2.x = polygon->cx + polygon->unrotated_points[(vn + 1) % polygon->number_of_sides].radius * cos(a);
	        v2.y = polygon->cy + polygon->unrotated_points[(vn + 1) % polygon->number_of_sides].radius * sin(a);
		 
            if( point_in_triangle(cc_x, cc_y, v1.x , v1.y, v2.x , v2.y, polygon->cx , polygon->cy ))   
	        {
	           /* centre of circle is in this triangle so the circle is in the polygon. */
	           logfile << "in this one.\n";
	           return 0;
	        }
	        else
	        {
	           //logfile << "not in this one.\n";
	        }
			
			d = sqrt( (v1.x - cc_x) * (v1.x - cc_x) + (v1.y - cc_y) * (v1.y - cc_y));
	        if( d <= radius )
	        {
			   logfile << "near vertex.\n";
	           return 0;
	        }
	    }
	  }
   }
   
   

   return 1;
}
Esempio n. 4
0
bool process_collision(Game_Level* level, Physics_Object* physics) {
    Collision_Face* face;

    glm::vec3 point_a; glm::vec3 point_b; glm::vec3 intersection_point;
    GLfloat t; GLfloat t_denominator; GLfloat t_numerator;

    //TODO: re-implement this with octree
    for(unsigned int i = 0; i < level->collision_model->face_count; i++) {
        //implemented using as reference:
        //http://www.peroxide.dk/download/tutorials/tut10/pxdtut10.html
        //ALSO: scratchapixel.com minimal ray tracer rendering tutorial

        face = &level->collision_model->faces[i];

        //STEP 1: Take 2 points. Test which side of the plane they're on.
        point_a = physics->position - (face->normal * physics->radii);
        point_b = physics->position + (face->normal * physics->radii);

        if(!same_side_of_plane(point_a, point_b, face->normal, face->distance)){

            //STEP 2: If they're on opposite sides then figure out where the
            //line between them intersects with the plane
            t_denominator =
                glm::dot(face->normal, point_b - point_a);
            t_numerator =
                glm::dot(face->center - point_b, face->normal);

            t = t_numerator/t_denominator;
            lerp(&intersection_point, physics->position, point_a, t);


            //STEP 3: We have the point of intersection with the plane, but
            //we need to know if it's actually within the boundaries of the
            //triangle
            if(!point_in_triangle(intersection_point, face->a.v, face->b.v,
                face->c.v)) {
                    continue;
            }

            //STEP 4: Now that we know we've collided we need to react.
            GLfloat rebound_distance_a = glm::distance(intersection_point, point_a);
            GLfloat rebound_distance_b = glm::distance(intersection_point, point_b);
            GLfloat rebound_distance = min(rebound_distance_a, rebound_distance_b);
            rebound_distance += 0.001f; //to make sure we're clear of the obstruction
            physics->position += (face->normal * rebound_distance);

            return true;
        }
    }

    return false;
}
Esempio n. 5
0
	int find_point_in_face(cv::Point& p, int index)
	{
		for(int i = 0; i < face_list.size(); i++)
		{
			cv::Point& p1 = g_point_list[index][face_list[i].x];
			cv::Point& p2 = g_point_list[index][face_list[i].y];
			cv::Point& p3 = g_point_list[index][face_list[i].z];
			//if(PointinTriangle(p1, p2, p3, p, 0.01))
			if(point_in_triangle(p1,p2,p3,p))
				return i;
		}
		return -1;
	}
Esempio n. 6
0
int main(int argc, char ** argv)
{
	if (stdout_is_piped()) // other wise you don't see the seg fault
		setup_segfault_handling(argv);
	
	assert_stdin_is_piped();
	assert_stdout_is_piped();
	//assert_stdin_or_out_is_piped();
	
	static char filename[1000] = "";
	static int debug = 0;
	
	int c;
	while (1)
	{
		static struct option long_options[] = {
			{"filename", required_argument, 0, 'f'},
			{"debug", no_argument, &debug, 1},
			{0, 0, 0, 0}
		};
		
		int option_index = 0;
		c = getopt_long(argc, argv, "d:f:", long_options, &option_index);
		if (c == -1) break;
		
		switch (c)
		{
			case 0: break;
			case 'f': strncpy(filename, optarg, sizeof(filename)); break;
			default: abort();
		}
	}
	
	if (filename[0] == 0)
	{
		fprintf(stderr, "Usage: cat points.b | ./join_geographic -f polygons.b | ...");
		return EXIT_FAILURE;
	}
	
	FILE * fp = fopen(filename, "r");
	if (fp == NULL)
	{
		fprintf(stderr, "filename '%s' is invalid.\n", filename);
		return EXIT_FAILURE;
	}
	
	struct Block * polygons = read_block(fp);
	fclose(fp);
	
	if (get_column_id_by_name(polygons, "x") == -1 ||
			get_column_id_by_name(polygons, "y") == -1 ||
			get_column_id_by_name(polygons, "shape_row_id") == -1 ||
			get_column_id_by_name(polygons, "shape_part_id") == -1) {
		fprintf(stderr, "No good, '%s' doesn't have the required fields\n", filename);
	}
	
	int polygon_shape_row_id_column_id = get_column_id_by_name(polygons, "shape_row_id");
	
	const char * shape_type = get_attribute_value_as_string(polygons, "shape_type");
	if (shape_type == NULL || strcmp(shape_type, "triangles") != 0) { fprintf(stderr, "Notice: %s expects '%s' to be 'triangles' (WILL END IN FAILURE)\n", argv[0], filename); }
	
	struct Block * block = NULL;
	while ((block = read_block(stdin)))
	{
		block = add_command(block, argc, argv);
		
		const char * shape_type = get_attribute_value_as_string(block, "shape_type");
		if (shape_type == NULL || strcmp(shape_type, "points") != 0) { fprintf(stderr, "Notice: %s expects piped blocks to be 'points', assuming points\n", argv[0]); }
		
		block = add_int32_column_and_blank(block, "number_of_polygon_hits");
		int number_of_polygon_hits_column_id = block->num_columns - 1;
		
		block = add_int32_column_and_blank(block, "first_hit_shape_row_id");
		int first_hit_shape_row_id_column_id = block->num_columns - 1;
		
		int i;
		for (i = 0 ; i < block->num_rows ; i++)
		{
			double x = get_x(block, i);
			double y = get_y(block, i);
			
			// foreach shape
			int shape_start_id = 0, shape_end_id;
			while ((shape_end_id = get_next_shape_start(polygons, shape_start_id)))
			{
				int polygon_shape_row_id = get_cell_as_int32(polygons, shape_start_id, polygon_shape_row_id_column_id);
				
				// foreach part of shape
				int part_start_id = shape_start_id, part_end_id;
				while ((part_end_id = get_next_part_start(polygons, part_start_id)))
				{
					//int shape_part_id = get_cell_as_int32(polygons, shape_start_id, shape_row_id_column_id);
					
					// this assumes the polygons are triangles.  if this isn't the case, this just won't work AT ALL
					int j;
					for (j = part_start_id ; j < part_end_id ; j += 3)
					{
						vec2d a = { get_x(polygons, j), get_y(polygons, j) };
						vec2d b = { get_x(polygons, j+1), get_y(polygons, j+1) };
						vec2d c = { get_x(polygons, j+2), get_y(polygons, j+2) };
						
						vec2d p = { x, y };
						
						if (point_in_triangle(a, b, c, p))
						{
							int32_t * number_of_polygon_hits = get_cell(block, i, number_of_polygon_hits_column_id);
							(*number_of_polygon_hits)++;
							int32_t * first_hit_shape_row_id = get_cell(block, i, first_hit_shape_row_id_column_id);
							(*first_hit_shape_row_id) = polygon_shape_row_id;
						}
					}
					
					//fprintf(stderr, " part %d to %d of shape %d to %d\n", part_start_id, part_end_id, shape_start_id, shape_end_id);
					if (part_end_id == shape_end_id) break; // last part of shape
					part_start_id = part_end_id;
				}
				
				if (shape_end_id == polygons->num_rows) break; // last shape
				shape_start_id = shape_end_id;
			}
		}
		
		write_block(stdout, block);
		free_block(block);
	}
}
Esempio n. 7
0
int prct2tex(double sand_input, double clay_input, double silt_input)
{
    G_debug(1,"%5.3f||%5.3f||%5.3f",sand_input,clay_input,silt_input);
    
    /* set up index for soil texture classes */
    int index = 20;

    /* set up mark index for inside/outside polygon check */
    double mark[POLYGON_DIMENSION] = { 0.0 };
    /*G_message("in prct2tex()"); */
    /*setup the 3Dvectors and initialize them */
    /* index 0 */
    struct vector cls_clay[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 1 */
    struct vector cls_sandy_clay[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 2 */
    struct vector cls_silty_clay[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 3 */
    struct vector cls_sandy_clay_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 4 */
    struct vector cls_clay_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 5 */
    struct vector cls_silty_clay_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 6 */
    struct vector cls_sand[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 7 */
    struct vector cls_loamy_sand[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 8 */
    struct vector cls_sandy_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 9 */
    struct vector cls_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 10 */
    struct vector cls_silt_loam[POLYGON_DIMENSION] = { { 0.0 } };
    /* index 11 */
    struct vector cls_silt[POLYGON_DIMENSION] = { { 0.0 } };

    if ((sand_input + clay_input + silt_input) <= 10.0) {
	sand_input = sand_input * 100.0;
	clay_input = clay_input * 100.0;
	silt_input = silt_input * 100.0;
    }
    /*G_message("%5.3f||%5.3f||%5.3f|",sand_input,clay_input,silt_input); */

    /*Feed the polygons for index 0 */
    cls_clay[0].sand = 0.0;
    cls_clay[0].clay = 100.0;
    cls_clay[0].silt = 0.0;
    cls_clay[1].sand = 0.0;
    cls_clay[1].clay = 60.0;
    cls_clay[1].silt = 40.0;
    cls_clay[2].sand = 20.0;
    cls_clay[2].clay = 40.0;
    cls_clay[2].silt = 40.0;
    cls_clay[3].sand = 50.0;
    cls_clay[3].clay = 40.0;
    cls_clay[3].silt = 10.0;
    cls_clay[4].sand = 50.0;
    cls_clay[4].clay = 50.0;
    cls_clay[4].silt = 0.0;
    /* Check for index 0 */
    /* G_message("in prct2tex(): check for index 0"); */
    mark[0] =
	point_in_triangle(sand_input, clay_input, silt_input,
			  cls_clay[0].sand, cls_clay[0].clay,
			  cls_clay[0].silt, cls_clay[1].sand,
			  cls_clay[1].clay, cls_clay[1].silt,
			  cls_clay[2].sand, cls_clay[2].clay,
			  cls_clay[2].silt);
    mark[1] =
	point_in_triangle(sand_input, clay_input, silt_input,
			  cls_clay[0].sand, cls_clay[0].clay,
			  cls_clay[0].silt, cls_clay[2].sand,
			  cls_clay[2].clay, cls_clay[2].silt,
			  cls_clay[3].sand, cls_clay[3].clay,
			  cls_clay[3].silt);
    mark[2] =
	point_in_triangle(sand_input, clay_input, silt_input,
			  cls_clay[0].sand, cls_clay[0].clay,
			  cls_clay[0].silt, cls_clay[3].sand,
			  cls_clay[3].clay, cls_clay[3].silt,
			  cls_clay[4].sand, cls_clay[4].clay,
			  cls_clay[4].silt);
    /* G_message("Clay: mark[0]=%f",mark[0]); */
    /* G_message("Clay: mark[1]=%f",mark[1]); */
    /* G_message("Clay: mark[2]=%f",mark[2]); */
    if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
	index = 0;
	/* G_message("Clay: index labelled as 0"); */
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 1 */
	cls_sandy_clay[0].sand = 50.0;
	cls_sandy_clay[0].clay = 50.0;
	cls_sandy_clay[0].silt = 0.0;
	cls_sandy_clay[1].sand = 50.0;
	cls_sandy_clay[1].clay = 35.0;
	cls_sandy_clay[1].silt = 15.0;
	cls_sandy_clay[2].sand = 65.0;
	cls_sandy_clay[2].clay = 35.0;
	cls_sandy_clay[2].silt = 0.0;
	/* Check for index 1 */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_clay[0].sand, cls_sandy_clay[0].clay,
			      cls_sandy_clay[0].silt, cls_sandy_clay[1].sand,
			      cls_sandy_clay[1].clay, cls_sandy_clay[1].silt,
			      cls_sandy_clay[2].sand, cls_sandy_clay[2].clay,
			      cls_sandy_clay[2].silt);

	/* G_message("Sandy Clay: mark[0]=%f",mark[0]); */
	if (mark[0] == 1) {
	    index = 1;
	    /* G_message("Sandy Clay: index labelled as 1"); */
	}
    }
    if (index == 20) {		/* if index not found then continue */
	/*Feed the polygons for index 2 */
	cls_silty_clay[0].sand = 0.0;
	cls_silty_clay[0].clay = 60.0;
	cls_silty_clay[0].silt = 40.0;
	cls_silty_clay[1].sand = 0.0;
	cls_silty_clay[1].clay = 40.0;
	cls_silty_clay[1].silt = 60.0;
	cls_silty_clay[2].sand = 20.0;
	cls_silty_clay[2].clay = 40.0;
	cls_silty_clay[2].silt = 40.0;
	/* Check for index 2 */
	/* G_message("sand=%5.3f||clay=%5.3f||silt=%5.3f",sand_input,
				clay_input,silt_input); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silty_clay[0].sand, cls_silty_clay[0].clay,
			      cls_silty_clay[0].silt, cls_silty_clay[1].sand,
			      cls_silty_clay[1].clay, cls_silty_clay[1].silt,
			      cls_silty_clay[2].sand, cls_silty_clay[2].clay,
			      cls_silty_clay[2].silt);

	/* G_message("Silty Clay: mark[0]=%f",mark[0]); */
	if (mark[0] == 1) {
	    index = 2;
	    /* G_message("Silty Clay: index labelled as 2"); */
	}
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 3 */
	cls_sandy_clay_loam[0].sand = 65.0;
	cls_sandy_clay_loam[0].clay = 35.0;
	cls_sandy_clay_loam[0].silt = 0.0;
	cls_sandy_clay_loam[1].sand = 50.0;
	cls_sandy_clay_loam[1].clay = 35.0;
	cls_sandy_clay_loam[1].silt = 15.0;
	cls_sandy_clay_loam[2].sand = 50.0;
	cls_sandy_clay_loam[2].clay = 30.0;
	cls_sandy_clay_loam[2].silt = 20.0;
	cls_sandy_clay_loam[3].sand = 55.0;
	cls_sandy_clay_loam[3].clay = 25.0;
	cls_sandy_clay_loam[3].silt = 20.0;
	cls_sandy_clay_loam[4].sand = 75.0;
	cls_sandy_clay_loam[4].clay = 25.0;
	cls_sandy_clay_loam[4].silt = 0.0;
	/* Check for index 0 */
	/* G_message("in prct2tex(): check for index 3"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_clay_loam[0].sand,
			      cls_sandy_clay_loam[0].clay,
			      cls_sandy_clay_loam[0].silt,
			      cls_sandy_clay_loam[1].sand,
			      cls_sandy_clay_loam[1].clay,
			      cls_sandy_clay_loam[1].silt,
			      cls_sandy_clay_loam[2].sand,
			      cls_sandy_clay_loam[2].clay,
			      cls_sandy_clay_loam[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_clay_loam[0].sand,
			      cls_sandy_clay_loam[0].clay,
			      cls_sandy_clay_loam[0].silt,
			      cls_sandy_clay_loam[2].sand,
			      cls_sandy_clay_loam[2].clay,
			      cls_sandy_clay_loam[2].silt,
			      cls_sandy_clay_loam[3].sand,
			      cls_sandy_clay_loam[3].clay,
			      cls_sandy_clay_loam[3].silt);
	mark[2] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_clay_loam[0].sand,
			      cls_sandy_clay_loam[0].clay,
			      cls_sandy_clay_loam[0].silt,
			      cls_sandy_clay_loam[3].sand,
			      cls_sandy_clay_loam[3].clay,
			      cls_sandy_clay_loam[3].silt,
			      cls_sandy_clay_loam[4].sand,
			      cls_sandy_clay_loam[4].clay,
			      cls_sandy_clay_loam[4].silt);
	/* G_message("Sandy Clay Loam: mark[0]=%f",mark[0]); */
	/* G_message("Sandy Clay Loam: mark[1]=%f",mark[1]); */
	/* G_message("Sandy Clay Loam: mark[2]=%f",mark[2]); */
	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
	    index = 3;
	    /* G_message("Sandy Clay Loam: index labelled as 3"); */
	}
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 4 */
	cls_clay_loam[0].sand = 20.0;
	cls_clay_loam[0].clay = 40.0;
	cls_clay_loam[0].silt = 40.0;
	cls_clay_loam[1].sand = 20.0;
	cls_clay_loam[1].clay = 30.0;
	cls_clay_loam[1].silt = 50.0;
	cls_clay_loam[2].sand = 50.0;
	cls_clay_loam[2].clay = 30.0;
	cls_clay_loam[2].silt = 20.0;
	cls_clay_loam[3].sand = 10.0;
	cls_clay_loam[3].clay = 50.0;
	cls_clay_loam[3].silt = 40.0;
	/* Check for index 4 */
	/* G_message("in prct2tex(): check for index 4"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_clay_loam[0].sand, cls_clay_loam[0].clay,
			      cls_clay_loam[0].silt, cls_clay_loam[1].sand,
			      cls_clay_loam[1].clay, cls_clay_loam[1].silt,
			      cls_clay_loam[2].sand, cls_clay_loam[2].clay,
			      cls_clay_loam[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_clay_loam[0].sand, cls_clay_loam[0].clay,
			      cls_clay_loam[0].silt, cls_clay_loam[2].sand,
			      cls_clay_loam[2].clay, cls_clay_loam[2].silt,
			      cls_clay_loam[3].sand, cls_clay_loam[3].clay,
			      cls_clay_loam[3].silt);
	/* G_message("Clay Loam: mark[0]=%f",mark[0]); */
	/* G_message("Clay Loam: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1) {
	    index = 4;
	    /* G_message("Clay Loam: index labelled as 4"); */
	}
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 5 */
	cls_silty_clay_loam[0].sand = 0.0;
	cls_silty_clay_loam[0].clay = 40.0;
	cls_silty_clay_loam[0].silt = 60.0;
	cls_silty_clay_loam[1].sand = 0.0;
	cls_silty_clay_loam[1].clay = 30.0;
	cls_silty_clay_loam[1].silt = 70.0;
	cls_silty_clay_loam[2].sand = 20.0;
	cls_silty_clay_loam[2].clay = 30.0;
	cls_silty_clay_loam[2].silt = 50.0;
	cls_silty_clay_loam[3].sand = 20.0;
	cls_silty_clay_loam[3].clay = 40.0;
	cls_silty_clay_loam[3].silt = 40.0;
	/* Check for index 5 */
	/* G_message("in prct2tex(): check for index 5"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silty_clay_loam[0].sand,
			      cls_silty_clay_loam[0].clay,
			      cls_silty_clay_loam[0].silt,
			      cls_silty_clay_loam[1].sand,
			      cls_silty_clay_loam[1].clay,
			      cls_silty_clay_loam[1].silt,
			      cls_silty_clay_loam[2].sand,
			      cls_silty_clay_loam[2].clay,
			      cls_silty_clay_loam[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silty_clay_loam[0].sand,
			      cls_silty_clay_loam[0].clay,
			      cls_silty_clay_loam[0].silt,
			      cls_silty_clay_loam[2].sand,
			      cls_silty_clay_loam[2].clay,
			      cls_silty_clay_loam[2].silt,
			      cls_silty_clay_loam[3].sand,
			      cls_silty_clay_loam[3].clay,
			      cls_silty_clay_loam[3].silt);
	/* G_message("Silty Clay Loam: mark[0]=%f",mark[0]); */
	/* G_message("Silty Clay Loam: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1) {
	    index = 5;
	    /* G_message("Silty Clay Loam: index labelled as 5"); */
	}
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 6 */
	cls_sand[0].sand = 85.0;
	cls_sand[0].clay = 15.0;
	cls_sand[0].silt = 0.0;
	cls_sand[1].sand = 85.0;
	cls_sand[1].clay = 0.0;
	cls_sand[1].silt = 15.0;
	cls_sand[2].sand = 100.0;
	cls_sand[2].clay = 0.0;
	cls_sand[2].silt = 0.0;
	/* Check for index 6 */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sand[0].sand, cls_sand[0].clay,
			      cls_sand[0].silt, cls_sand[1].sand,
			      cls_sand[1].clay, cls_sand[1].silt,
			      cls_sand[2].sand, cls_sand[2].clay,
			      cls_sand[2].silt);
	/* G_message("Sand: mark[0]=%f",mark[0]); */
	if (mark[0] == 1) {
	    index = 6;
	    /* G_message("Sand: index labelled as 6"); */
	}
    }
    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 7 */
	cls_loamy_sand[0].sand = 80.0;
	cls_loamy_sand[0].clay = 20.0;
	cls_loamy_sand[0].silt = 0.0;
	cls_loamy_sand[1].sand = 70.0;
	cls_loamy_sand[1].clay = 0.0;
	cls_loamy_sand[1].silt = 30.0;
	cls_loamy_sand[2].sand = 85.0;
	cls_loamy_sand[2].clay = 0.0;
	cls_loamy_sand[2].silt = 15.0;
	cls_loamy_sand[3].sand = 85.0;
	cls_loamy_sand[3].clay = 15.0;
	cls_loamy_sand[3].silt = 0.0;
	/* Check for index 7 */
	/* G_message("in prct2tex(): check for index 7"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_loamy_sand[0].sand, cls_loamy_sand[0].clay,
			      cls_loamy_sand[0].silt, cls_loamy_sand[1].sand,
			      cls_loamy_sand[1].clay, cls_loamy_sand[1].silt,
			      cls_loamy_sand[2].sand, cls_loamy_sand[2].clay,
			      cls_loamy_sand[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_loamy_sand[0].sand, cls_loamy_sand[0].clay,
			      cls_loamy_sand[0].silt, cls_loamy_sand[2].sand,
			      cls_loamy_sand[2].clay, cls_loamy_sand[2].silt,
			      cls_loamy_sand[3].sand, cls_loamy_sand[3].clay,
			      cls_loamy_sand[3].silt);
	/* G_message("Loamy Sand: mark[0]=%f",mark[0]); */
	/* G_message("Loamy Sand: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1) {
	    index = 7;
	    /* G_message("Loamy Sand: index labelled as 7"); */
	}
    }

    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 8 */
	cls_sandy_loam[0].sand = 75.0;
	cls_sandy_loam[0].clay = 25.0;
	cls_sandy_loam[0].silt = 0.0;
	cls_sandy_loam[1].sand = 55.0;
	cls_sandy_loam[1].clay = 25.0;
	cls_sandy_loam[1].silt = 20.0;
	cls_sandy_loam[2].sand = 55.0;
	cls_sandy_loam[2].clay = 10.0;
	cls_sandy_loam[2].silt = 35.0;
	cls_sandy_loam[3].sand = 40.0;
	cls_sandy_loam[3].clay = 10.0;
	cls_sandy_loam[3].silt = 50.0;
	cls_sandy_loam[4].sand = 50.0;
	cls_sandy_loam[4].clay = 0.0;
	cls_sandy_loam[4].silt = 50.0;
	cls_sandy_loam[5].sand = 70.0;
	cls_sandy_loam[5].clay = 0.0;
	cls_sandy_loam[5].silt = 30.0;
	cls_sandy_loam[6].sand = 80.0;
	cls_sandy_loam[6].clay = 20.0;
	cls_sandy_loam[6].silt = 0.0;
	/* Check for index 8 */
	/* G_message("in prct2tex(): check for index 8"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
			      cls_sandy_loam[2].silt, cls_sandy_loam[3].sand,
			      cls_sandy_loam[3].clay, cls_sandy_loam[3].silt,
			      cls_sandy_loam[4].sand, cls_sandy_loam[4].clay,
			      cls_sandy_loam[4].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
			      cls_sandy_loam[2].silt, cls_sandy_loam[4].sand,
			      cls_sandy_loam[4].clay, cls_sandy_loam[4].silt,
			      cls_sandy_loam[5].sand, cls_sandy_loam[5].clay,
			      cls_sandy_loam[5].silt);
	mark[2] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
			      cls_sandy_loam[2].silt, cls_sandy_loam[5].sand,
			      cls_sandy_loam[5].clay, cls_sandy_loam[5].silt,
			      cls_sandy_loam[6].sand, cls_sandy_loam[6].clay,
			      cls_sandy_loam[6].silt);
	mark[3] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
			      cls_sandy_loam[2].silt, cls_sandy_loam[6].sand,
			      cls_sandy_loam[6].clay, cls_sandy_loam[6].silt,
			      cls_sandy_loam[0].sand, cls_sandy_loam[0].clay,
			      cls_sandy_loam[0].silt);
	mark[4] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_sandy_loam[2].sand, cls_sandy_loam[2].clay,
			      cls_sandy_loam[2].silt, cls_sandy_loam[0].sand,
			      cls_sandy_loam[0].clay, cls_sandy_loam[0].silt,
			      cls_sandy_loam[1].sand, cls_sandy_loam[1].clay,
			      cls_sandy_loam[1].silt);
	/* G_message("Sandy Loam: mark[0]=%f",mark[0]); */
	/* G_message("Sandy Loam: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1 || mark[3] == 1 ||
	    mark[4] == 1) {
	    index = 8;
	    /* G_message("Sandy Loam: index labelled as 8"); */
	}
    }

    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 9 */
	cls_loam[0].sand = 50.0;
	cls_loam[0].clay = 30.0;
	cls_loam[0].silt = 20.0;
	cls_loam[1].sand = 20.0;
	cls_loam[1].clay = 30.0;
	cls_loam[1].silt = 50.0;
	cls_loam[2].sand = 40.0;
	cls_loam[2].clay = 10.0;
	cls_loam[2].silt = 50.0;
	cls_loam[3].sand = 55.0;
	cls_loam[3].clay = 10.0;
	cls_loam[3].silt = 35.0;
	cls_loam[4].sand = 55.0;
	cls_loam[4].clay = 25.0;
	cls_loam[4].silt = 15.0;
	/* Check for index 9 */
	/* G_message("in prct2tex(): check for index 9"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_loam[0].sand, cls_loam[0].clay,
			      cls_loam[0].silt, cls_loam[1].sand,
			      cls_loam[1].clay, cls_loam[1].silt,
			      cls_loam[2].sand, cls_loam[2].clay,
			      cls_loam[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_loam[0].sand, cls_loam[0].clay,
			      cls_loam[0].silt, cls_loam[2].sand,
			      cls_loam[2].clay, cls_loam[2].silt,
			      cls_loam[3].sand, cls_loam[3].clay,
			      cls_loam[3].silt);
	mark[2] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_loam[0].sand, cls_loam[0].clay,
			      cls_loam[0].silt, cls_loam[3].sand,
			      cls_loam[3].clay, cls_loam[3].silt,
			      cls_loam[4].sand, cls_loam[4].clay,
			      cls_loam[4].silt);
	/* G_message("Sandy Loam: mark[0]=%f",mark[0]); */
	/* G_message("Sandy Loam: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1) {
	    index = 9;
	    /* G_message("Loam: index labelled as 9"); */
	}
    }

    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 10 */
	cls_silt_loam[0].sand = 20.0;
	cls_silt_loam[0].clay = 30.0;
	cls_silt_loam[0].silt = 50.0;
	cls_silt_loam[1].sand = 0.0;
	cls_silt_loam[1].clay = 30.0;
	cls_silt_loam[1].silt = 70.0;
	cls_silt_loam[2].sand = 0.0;
	cls_silt_loam[2].clay = 10.0;
	cls_silt_loam[2].silt = 90.0;
	cls_silt_loam[3].sand = 15.0;
	cls_silt_loam[3].clay = 10.0;
	cls_silt_loam[3].silt = 75.0;
	cls_silt_loam[4].sand = 25.0;
	cls_silt_loam[4].clay = 0.0;
	cls_silt_loam[4].silt = 75.0;
	cls_silt_loam[5].sand = 50.0;
	cls_silt_loam[5].clay = 0.0;
	cls_silt_loam[5].silt = 50.0;
	/* Check for index 10 */
	/* G_message("in prct2tex(): check for index 10"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
			      cls_silt_loam[3].silt, cls_silt_loam[4].sand,
			      cls_silt_loam[4].clay, cls_silt_loam[4].silt,
			      cls_silt_loam[5].sand, cls_silt_loam[5].clay,
			      cls_silt_loam[5].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
			      cls_silt_loam[3].silt, cls_silt_loam[5].sand,
			      cls_silt_loam[5].clay, cls_silt_loam[5].silt,
			      cls_silt_loam[0].sand, cls_silt_loam[0].clay,
			      cls_silt_loam[0].silt);
	mark[2] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
			      cls_silt_loam[3].silt, cls_silt_loam[0].sand,
			      cls_silt_loam[0].clay, cls_silt_loam[0].silt,
			      cls_silt_loam[1].sand, cls_silt_loam[1].clay,
			      cls_silt_loam[1].silt);
	mark[3] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt_loam[3].sand, cls_silt_loam[3].clay,
			      cls_silt_loam[3].silt, cls_silt_loam[1].sand,
			      cls_silt_loam[1].clay, cls_silt_loam[1].silt,
			      cls_silt_loam[2].sand, cls_silt_loam[2].clay,
			      cls_silt_loam[2].silt);
	/* G_message("Silt Loam: mark[0]=%f",mark[0]); */
	/* G_message("Silt Loam: mark[1]=%f",mark[1]); */
	/* G_message("Silt Loam: mark[2]=%f",mark[2]); */
	/* G_message("Silt Loam: mark[3]=%f",mark[3]); */
	if (mark[0] == 1 || mark[1] == 1 || mark[2] == 1 || mark[3] == 1) {
	    index = 10;
	    /* G_message("Silt Loam: index labelled as 10"); */
	}
    }

    if (index == 20) {	/* if index not found then continue */
	/*Feed the polygons for index 11 */
	cls_silt[0].sand = 15.0;
	cls_silt[0].clay = 10.0;
	cls_silt[0].silt = 75.0;
	cls_silt[1].sand = 0.0;
	cls_silt[1].clay = 10.0;
	cls_silt[1].silt = 90.0;
	cls_silt[2].sand = 0.0;
	cls_silt[2].clay = 0.0;
	cls_silt[2].silt = 100.0;
	cls_silt[3].sand = 25.0;
	cls_silt[3].clay = 0.0;
	cls_silt[3].silt = 75.0;
	/* Check for index 11 */
	/* G_message("in prct2tex(): check for index 11"); */
	mark[0] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt[0].sand, cls_silt[0].clay,
			      cls_silt[0].silt, cls_silt[1].sand,
			      cls_silt[1].clay, cls_silt[1].silt,
			      cls_silt[2].sand, cls_silt[2].clay,
			      cls_silt[2].silt);
	mark[1] =
	    point_in_triangle(sand_input, clay_input, silt_input,
			      cls_silt[0].sand, cls_silt[0].clay,
			      cls_silt[0].silt, cls_silt[2].sand,
			      cls_silt[2].clay, cls_silt[2].silt,
			      cls_silt[3].sand, cls_silt[3].clay,
			      cls_silt[3].silt);
	/* G_message("Silt: mark[0]=%f",mark[0]); */
	/* G_message("Silt: mark[1]=%f",mark[1]); */
	if (mark[0] == 1 || mark[1] == 1) {
	    index = 11;
	    /* G_message("Silt: index labelled as 11"); */
	}
    }
    if(index==0){
       G_debug(1,"clay");
       } else if (index==1){
       G_debug(1,"sandy clay");
       } else if (index==2){
       G_debug(1,"silty clay");
       } else if (index==3){
       G_debug(1,"sandy clay loam");
       } else if (index==4){
       G_debug(1,"clay loam");
       } else if (index==5){
       G_debug(1,"silty clay loam");
       } else if (index==6){
       G_debug(1,"sand");
       } else if (index==7){
       G_debug(1,"loamy sand");
       } else if (index==8){
       G_debug(1,"sandy loam");
       } else if (index==9){
       G_debug(1,"loam");
       } else if (index==10){
       G_message("silt loam");
       } else if (index==11){
       G_debug(1,"silt");
       } else {
       G_debug(1,"Unable to allocate class");
       }
    return index;
}
Esempio n. 8
0
int main(int argc, char **argv)
{
  FILE *fp;
  int x_1,y_1,x_2,y_2,x_3,y_3;
  char a;
  int i,j;
  int outside;
  int ButtonPressed = 0;
  int temp_x, temp_y;
  pixel_count = 0;
  for(i=0;i<302;i++)
  {              
    for(j=0;j<302;j++)  
    {          
      cost[i][j] = 9999; 
    } 
  }

  if( (display_ptr = XOpenDisplay(display_name)) == NULL )
    { printf("Could not open display. \n"); exit(-1);}
  printf("Connected to X server  %s\n", XDisplayName(display_name) );
  screen_num = DefaultScreen( display_ptr );
  screen_ptr = DefaultScreenOfDisplay( display_ptr );
  color_map  = XDefaultColormap( display_ptr, screen_num );
  display_width  = DisplayWidth( display_ptr, screen_num );
  display_height = DisplayHeight( display_ptr, screen_num );

  printf("Width %d, Height %d, Screen Number %d\n", 
           display_width, display_height, screen_num);
  border_width = 10;
  win_x = 0; win_y = 0;
  win_width = display_width;
  win_height = display_height; 
  
  win= XCreateSimpleWindow( display_ptr, RootWindow( display_ptr, screen_num),
                            win_x, win_y, win_width, win_height, border_width,
                            BlackPixel(display_ptr, screen_num),
                            WhitePixel(display_ptr, screen_num) );
  size_hints = XAllocSizeHints();
  wm_hints = XAllocWMHints();
  class_hints = XAllocClassHint();
  if( size_hints == NULL || wm_hints == NULL || class_hints == NULL )
    { printf("Error allocating memory for hints. \n"); exit(-1);}

  size_hints -> flags = PPosition | PSize | PMinSize  ;
  size_hints -> min_width = 60;
  size_hints -> min_height = 60;

  XStringListToTextProperty( &win_name_string,1,&win_name);
  XStringListToTextProperty( &icon_name_string,1,&icon_name);
  
  wm_hints -> flags = StateHint | InputHint ;
  wm_hints -> initial_state = NormalState;
  wm_hints -> input = False;

  class_hints -> res_name = "x_use_example";
  class_hints -> res_class = "examples";

  XSetWMProperties( display_ptr, win, &win_name, &icon_name, argv, argc,
                    size_hints, wm_hints, class_hints );

  XSelectInput( display_ptr, win, 
            ExposureMask | StructureNotifyMask | ButtonPressMask );
  
  XMapWindow( display_ptr, win );

  XFlush(display_ptr);
  gc_red = XCreateGC( display_ptr, win, valuemask, &gc_red_values);
  XSetLineAttributes( display_ptr, gc_red, 3, LineSolid, CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "red", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color red\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_red, tmp_color1.pixel );
  gc_black = XCreateGC( display_ptr, win, valuemask, &gc_black_values);
  XSetLineAttributes(display_ptr, gc_black, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "black", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color black\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_black, tmp_color1.pixel );
  gc_blue = XCreateGC( display_ptr, win, valuemask, &gc_blue_values);
  XSetLineAttributes(display_ptr, gc_blue, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "blue", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get blue yellow\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_blue, tmp_color1.pixel );
  gc_white = XCreateGC( display_ptr, win, valuemask, &gc_white_values);
  XSetLineAttributes(display_ptr, gc_white, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "white", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color white\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_white, tmp_color1.pixel );

  if ( argc != 2 ) 
  {
      printf( "Usage: %s filename.extension \n", argv[0] );
      exit(-1);
  }
  fp = fopen(argv[1], "r");
  if (fp == NULL) 
  {
    fprintf(stderr, "Can't open file %s!\n", argv[1]);
    printf( "Usage: %s filename.extension or Check whether file is present or not\n", argv[0] );
    exit(-1);
  } 
  triangle_count = 0;
  while (fscanf(fp, "%c %c%d%c%d%c %c%d%c%d%c %c%d%c%d%c%c", &a,&a,&x_1,&a,&y_1,&a,&a,&x_2,&a,&y_2,&a,&a,&x_3,&a,&y_3,&a,&a) != EOF) 
  {
    if(get_x_min(x_1,x_2,x_3) >= 0 && get_y_min(y_1,y_2,y_3) >= 0 && get_x_max(x_1,x_2,x_3) <= display_width - 300 && get_y_max(y_1,y_2,y_3) <= display_height - 200)
    {
      t[triangle_count].x1 = x_1;
      t[triangle_count].x2 = x_2;
      t[triangle_count].x3 = x_3;
      t[triangle_count].y1 = y_1;
      t[triangle_count].y2 = y_2;
      t[triangle_count].y3 = y_3;
      triangle_count++;
    }   
  }
  
  x_min = t[0].x1;
  x_max = x_min;
  y_min = t[0].y1;
  y_max = y_min;
  for(i=0;i<triangle_count;i++)
  {
    if(x_min > t[i].x1)
      x_min = t[i].x1;
    if(x_min > t[i].x2)
      x_min = t[i].x2;
    if(x_min > t[i].x2)
      x_min = t[i].x3;
    if(y_min > t[i].y1)
      y_min = t[i].y1;
    if(y_min > t[i].y2)
      y_min = t[i].y2;
    if(y_min > t[i].y3)
      y_min = t[i].y3;
    if(x_max < t[i].x1)
      x_max = t[i].x1;
    if(x_max < t[i].x2)
      x_max = t[i].x2;
    if(x_max < t[i].x3)
      x_max = t[i].x3;
    if(y_max < t[i].y1)
      y_max = t[i].y1;
    if(y_max < t[i].y2)
      y_max = t[i].y2;
    if(y_max < t[i].y3)
      y_max = t[i].y3;        
  }
   
  x_center = win_width / 2;
  y_center = win_height / 2;
  x_gap = x_max - x_min;
  y_gap = y_max - y_min;
  x_start = x_center - x_gap/2 - 50;
  y_start = y_center - y_gap/2 - 50;
  x_end = x_start + x_gap + 100;
  y_end = y_start + y_gap + 100;
  rec_width = x_end - x_start;
  rec_height = y_end - y_start;

  for(i=0;i<triangle_count;i++)
  {
    t[i].x1 = t[i].x1 + x_start + 50 - x_min;
    t[i].x2 = t[i].x2 + x_start + 50 - x_min;
    t[i].x3 = t[i].x3 + x_start + 50 - x_min;
    t[i].y1 = t[i].y1 + y_start + 50 - y_min;
    t[i].y2 = t[i].y2 + y_start + 50 - y_min;
    t[i].y3 = t[i].y3 + y_start + 50 - y_min;
  }
  for(i=0;i<triangle_count;i++)
  {
    t[i].point_1_inside_triangle = 0;
    t[i].point_2_inside_triangle = 0;
    t[i].point_3_inside_triangle = 0;       
  }
  for(i=0;i<triangle_count;i++)
  {
    for(j=0;j<triangle_count;j++)
    {
      if(j!=i)
      {
        if(point_in_triangle(t[i].x1,t[i].y1,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_1_inside_triangle = 1;
        }
        if(point_in_triangle(t[i].x2,t[i].y2,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_2_inside_triangle = 1;
        }
        if(point_in_triangle(t[i].x3,t[i].y3,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_3_inside_triangle = 1;
        }
      }
    }
  }
  
  while(1)
  { 
    XNextEvent( display_ptr, &report );
    switch( report.type )
	  {
	    case Expose:
	      for(i=0;i<triangle_count;i++)
        {
          XDrawLine(display_ptr, win, gc_black, t[i].x1, t[i].y1, t[i].x2, t[i].y2);
          XDrawLine(display_ptr, win, gc_black, t[i].x2, t[i].y2, t[i].x3, t[i].y3);
          XDrawLine(display_ptr, win, gc_black, t[i].x3, t[i].y3, t[i].x1, t[i].y1);
        }
        XDrawRectangle(display_ptr, win, gc_black, x_start, y_start, rec_width, rec_height ); 
        break;
        
      case ConfigureNotify:
          win_width = report.xconfigure.width;
          win_height = report.xconfigure.height;
          break;
        
      case ButtonPress:
      {  
        int x, y;
  	    x = report.xbutton.x;
        y = report.xbutton.y;
        if (report.xbutton.button == Button1 )
	      {
          outside = 0;
          if(x <= x_start || y <= y_start || x >= x_end || y >= y_end)
          {
            outside = 1;
          } 
          for(i=0;i<triangle_count;i++)
          {
            if(point_in_triangle(x,y,t[i].x1,t[i].y1,t[i].x2,t[i].y2,t[i].x3,t[i].y3)==1)
            {
              outside = 1;
              break;
            }
          }
          if(outside == 0)
          {
            ButtonPressed++;
            if(ButtonPressed == 1)
            { 
              temp_x = x;
              temp_y = y;
              pixel_count = 0;
              XFillArc( display_ptr, win, gc_red, x - win_height/150, y - win_height/150, win_height/100, win_height/100, 0, 360*64);
              pixel[pixel_count].name = pixel_count;
              pixel[pixel_count].x = x; 
              pixel[pixel_count].y = y;
              pixel_count++;
            }
            else if(ButtonPressed == 2)
            { 
              if(temp_x == x && temp_y == y)
              {
                ButtonPressed = 1;
                break;
              }
              XFillArc( display_ptr, win, gc_red, x - win_height/150, y - win_height/150, win_height/100, win_height/100, 0, 360*64);
              for(i=0;i<triangle_count;i++)
              {
                if(t[i].point_1_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x1; 
                  pixel[pixel_count].y = t[i].y1;
                  pixel_count++;
                }
                if(t[i].point_2_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x2; 
                  pixel[pixel_count].y = t[i].y2;
                  pixel_count++;
                }
                if(t[i].point_3_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x3; 
                  pixel[pixel_count].y = t[i].y3;
                  pixel_count++;
                }
              }
              pixel[pixel_count].name = pixel_count;
              pixel[pixel_count].x = x; 
              pixel[pixel_count].y = y;
              pixel_count++;
              for(i=0;i<pixel_count-1;i++)
              {         
                for(j=i+1;j<pixel_count;j++)
                {
                  if(can_see(pixel[i].x,pixel[i].y,pixel[j].x,pixel[j].y)==1)
                  {
                    cost[pixel[i].name][pixel[j].name] = cost[pixel[j].name][pixel[i].name] = eculidian_dist(pixel[i].x,pixel[i].y,pixel[j].x,pixel[j].y);
                  }
                }
              }
              dijsktra(pixel[0].name, pixel[pixel_count-1].name);
            }
            else
            {
              clear_cost();
              expose();
              ButtonPressed = 0;          
            }
          }
        }
        else
        {
          XFlush(display_ptr);
          XCloseDisplay(display_ptr);
          exit(0);
        }
      }
      break;
      default:
        break;
    }
  }
  exit(0);
}
Esempio n. 9
0
bool triangulate_simple_polygon_naive( const std::vector<double> &coords, const int *facet, std::vector<int> &tris ){
	// get the number of facet vertices
	int num_verts = facet[0];
	
	// store some vectors of next and previous vertices, as well
	// as pointers to the xyz coordinates, original vertex id and
	// a convexity flag indicating which vertices can be clipped.
	std::vector<int>			vert( num_verts );		// stores the input vertex id 
	std::vector<int>			next( num_verts );		// stores the index of the next vertex in the contour
	std::vector<int>			prev( num_verts );		// stores the index of the previous vertex in the contour
	std::vector<const double*>	xyz( num_verts );		// stores pointers to the xyz coordinates of each vertex
	std::vector<double>			convexity( num_verts ); // stores the convexity status of each vertex, >0 == convex
	double normal[3];
	
	// compute the facet normal
	triangulate_estimate_facet_normal( coords, facet, normal );
	
	// set up the linked list pointers, vertex indices and coordinates
	for( int i=0; i<num_verts; i++ ){
		vert[i] = facet[i+1];
		xyz[i] = &coords[vert[i]*3];
		prev[i] = (i-1+num_verts)%num_verts;
		next[i] = (i+1)%num_verts;
	}
	
	// compute the convexity of each vertex
	for( int i=0; i<num_verts; i++ ){
		convexity[i] = compute_convexity( normal, xyz[prev[i]], xyz[i], xyz[next[i]] );
	}
	
	// Main loop.  A stopping criteria has been aded so that the 
	// algorithm does not stall on bad inputs where no progress can be made.
	// For simple polygons in the worst case, the algorithm will remove
	// one vertex for every full traversal of the polygon.  This gives an
	// upper bound of (num_verts^2)/2 that the main loop should execute,
	// although in practice it will typically do much better. A bound of
	// num_verts^2 will consequently be more than sufficient for the 
	// algorithm to complete on valid inputs, failure to finish in that
	// many iterations indicates either improper input, or input that
	// cannot be tesselated due to precision of the non-exact tests.
	bool okay;
	int test, tmp_next, tmp_prev, max_tries=num_verts*num_verts, tries=0, curr = 0;
	while( num_verts > 2 && tries++ < max_tries ){
		
		// if the current vertex is (strictly) convex
		if( convexity[curr] > TRIANGULATE_EPSILON ){
			
			// then check the other vertices in the polygon to see if 
			// they fall within the triangle that would be clipped. 
			// All vertices except those that are in the clipped triangle
			// are checked.  This section could (should) be replaced
			// with spatial partitioning searches for very large inputs
			okay = true;
			test = next[curr];
			while( okay && test != curr ){
				// This conditional is necessary, since we may have bridges connecting two contours to form
				// a simple polygon. This test prevents incorrectly failing when a vertex that will be on 
				// the clipped triangle appears later in the polydon due to one of these bridges
				if( vert[test] != vert[prev[curr]] && vert[test] != vert[curr] && vert[test] != vert[next[curr]] ){
					okay &= !point_in_triangle( normal, xyz[prev[curr]], xyz[curr], xyz[next[curr]], xyz[test] );
				}
				test = next[test];
			}
			
			// no vertices fell within the clipped triangle, so 
			// the triangle can be added to the output and the 
			// current vertex removed from the contour
			if( okay ){
				// decrement the number of vertices
				num_verts--;
				
				// store the next and previous vertices for convenience, it
				// avoids unnecessary nesting like next[prev[curr]] that happens
				// otherwise to avoid using vertex curr which gets removed.
				tmp_prev = prev[curr];
				tmp_next = next[curr];
				
				// add the triangle to the output
				tris.push_back( 3 );
				tris.push_back( vert[tmp_prev] );
				tris.push_back( vert[curr] );
				tris.push_back( vert[tmp_next] );
				
				// update the linked list indices
				next[tmp_prev] = tmp_next;
				prev[tmp_next] = tmp_prev;
				
				// update the convexity status of the next
				// and previous vertices, since it may
				// have changed after clipping
				convexity[tmp_prev] = compute_convexity( normal, xyz[prev[tmp_prev]], xyz[tmp_prev], xyz[next[tmp_prev]] );
				convexity[tmp_next] = compute_convexity( normal, xyz[prev[tmp_next]], xyz[tmp_next], xyz[next[tmp_next]] );
			}
		}
		
		// advance one point around the boundary, we do this regardless of whether
		// the vertex was clipped. It is always valid since we never invalidate
		// the current vertex, just clip it out of the loop (i.e. next[curr] will
		// point to a valid vertex in the contour, even though curr will never 
		// be reached again).
		curr = next[curr];
	}
	
	//std::cout << "took " << tries << " out of a maximum of " << max_tries << std::endl;
		
	// should always end up with num_verts-2 triangles when
	// tesselating a simple polygon. Failure to do so indicates
	// non-simple input, or input that cannot be resolved with 
	// the current TRIANGULATE_EPSILON setting
	return num_verts == 2;
}
Esempio n. 10
0
/**
 @brief Triangulates a simple polygon using an ear-clipping algorithm and heuristic to (hopefully) reduce the time-complexity.  Always tries to clip the ear with minimal area first, under the assumption that this will have the least probability of enclosing an unrelated polygon vertex.  Worst-case complexity of O(N^3 log(N)), with a best-case complexity of O(N^2 log(N)), which the algorithm appears to achieve often in practice. Note that this explicitly checks every vertex in the polygon for inclusion in a clipped ear, spatial partitioning should reduce the the complexity to O(N log(N)).
 @param[in] coords input array of polygon vertices, stored [x, y, z, x, y, z, ...]
 @param[in] facet input pointer to facet contour vertex indices, stored [nverts, v_0, v_1, ... v_(n_verts-1}]
 @param[out] tris output vector of triangles, stored as [ 3, v0, v1, v2, 3, v0, v1, v2, ... ]
 @return true if the triangulation succeeded, false otherwise
*/
bool triangulate_simple_polygon_set( const std::vector<double> &coords, const int *facet, std::vector<int> &tris ){
	// get the number of facet vertices
	int num_verts = facet[0];
	
	// store some vectors of next and previous vertices, as well
	// as pointers to the xyz coordinates, original vertex id and
	// a convexity flag indicating which vertices can be clipped.
	std::vector<int>			vert( num_verts );		// stores the input vertex id 
	std::vector<int>			next( num_verts );		// stores the index of the next vertex in the contour
	std::vector<int>			prev( num_verts );		// stores the index of the previous vertex in the contour
	std::vector<const double*>	xyz( num_verts );		// stores pointers to the xyz coordinates of each vertex
	std::vector<double>			convexity( num_verts ); // stores the convexity status of each vertex, >0 == convex
	double normal[3];
	
	triangulate_compare comp( convexity );
	std::set< int, triangulate_compare > best_vert( comp );
	std::set< int, triangulate_compare >::iterator iter;
	
	// compute the facet normal
	triangulate_estimate_facet_normal( coords, facet, normal );
	
	// set up the linked list pointers, vertex indices and coordinates
	for( int i=0; i<num_verts; i++ ){
		vert[i] = facet[i+1];
		xyz[i] = &coords[vert[i]*3];
		prev[i] = (i-1+num_verts)%num_verts;
		next[i] = (i+1)%num_verts;
	}
	
	// compute the convexity of each vertex
	for( int i=0; i<num_verts; i++ ){
		convexity[i] = compute_convexity( normal, xyz[prev[i]], xyz[i], xyz[next[i]] );
		if( convexity[i] > TRIANGULATE_EPSILON ){
			best_vert.insert( i );
		}
	}
	
	// Main loop.  A stopping criteria has been aded so that the 
	// algorithm does not stall on bad inputs where no progress can be made.
	// For simple polygons in the worst case, the algorithm will remove
	// one vertex for every full traversal of the polygon.  This gives an
	// upper bound of (num_verts^2)/2 that the main loop should execute,
	// although in practice it will typically do much better. A bound of
	// num_verts^2 will consequently be more than sufficient for the 
	// algorithm to complete on valid inputs, failure to finish in that
	// many iterations indicates either improper input, or input that
	// cannot be tesselated due to precision of the non-exact tests.
	bool okay;
	int test, curr, tmp_next, tmp_prev, init_verts=num_verts, tries=0, max_tries=num_verts*num_verts;
	for( int i=0; i<init_verts-2; i++ ){
		
		// Grab the first vertex from the sorted list of vertices. This should
		// be the 'best' vertex to clip according to the criteria in the 
		// triangulate_compare class, which could be the smallest area triangle,
		// or a randomized metric.  We have to iterate over these in order
		// to make sure that we actually find one that can be clipped, since the
		// first might produce a triangle that encloses another vertex
		for( iter=best_vert.begin(); iter!=best_vert.end(); iter++ ){
			curr = *iter;
			tries++;
			
			okay = true;
			test = next[curr];
			while( okay && test != curr ){
				// This conditional is necessary, since we may have bridges connecting two contours to form
				// a simple polygon. This test prevents incorrectly failing when a vertex that will be on 
				// the clipped triangle appears later in the polydon due to one of these bridges
				if( vert[test] != vert[prev[curr]] && vert[test] != vert[curr] && vert[test] != vert[next[curr]] ){
					okay &= !point_in_triangle( normal, xyz[prev[curr]], xyz[curr], xyz[next[curr]], xyz[test] );
				}
				test = next[test];
			}
			if( okay ){
				if( okay ){
					// decrement the number of vertices
					num_verts--;
					
					// remove the clipped vertex from consideration
					best_vert.erase( curr );
					
					// store the next and previous vertices for convenience, it
					// avoids unnecessary nesting like next[prev[curr]] that happens
					// otherwise to avoid using vertex curr which gets removed.
					tmp_prev = prev[curr];
					tmp_next = next[curr];
					
					// add the triangle to the output
					tris.push_back( 3 );
					tris.push_back( vert[tmp_prev] );
					tris.push_back( vert[curr] );
					tris.push_back( vert[tmp_next] );
					
					// update the linked list indices
					next[tmp_prev] = tmp_next;
					prev[tmp_next] = tmp_prev;
					
					// update the convexity status of the next
					// and previous vertices, since it may
					// have changed after clipping
					best_vert.erase( tmp_prev );
					best_vert.erase( tmp_next );
					convexity[tmp_prev] = compute_convexity( normal, xyz[prev[tmp_prev]], xyz[tmp_prev], xyz[next[tmp_prev]] );
					convexity[tmp_next] = compute_convexity( normal, xyz[prev[tmp_next]], xyz[tmp_next], xyz[next[tmp_next]] );
					if( convexity[tmp_prev] > TRIANGULATE_EPSILON ) best_vert.insert( tmp_prev );
					if( convexity[tmp_next] > TRIANGULATE_EPSILON ) best_vert.insert( tmp_next );
					break;
				}				
			}
		}		
	}
	
	//std::cout << "took " << tries << " out of a maximum of " << max_tries << std::endl;
	
	// should always end up with num_verts-2 triangles when
	// tesselating a simple polygon. Failure to do so indicates
	// non-simple input, or input that cannot be resolved with 
	// the current TRIANGULATE_EPSILON setting
	return num_verts == 2;
}