/** Finds the nav polygon in which the specified point resides in the nav mesh, if any. @param p The point whose nav polygon we want to find @param suggestedNavPoly A suggestion for the result (generally speaking, for a moving object this would be the last nav polygon we were in) @param polygons The collision polygons for the level @param tree The onion tree for the level @param navMesh The nav mesh in which to find the nav polygon @return The index of the nav polygon in which the specified point resides, if any, or -1 otherwise */ int NavMeshUtil::find_nav_polygon(const Vector3d& p, int suggestedNavPoly, const std::vector<CollisionPolygon_Ptr>& polygons, const OnionTree_CPtr& tree, const NavMesh_CPtr& navMesh) { // It's good to be paranoid and do a range check: we might no longer be on the same navigation mesh, for instance. if(suggestedNavPoly >= static_cast<int>(navMesh->polygons().size())) suggestedNavPoly = -1; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Step 1: If there's a suggested nav polygon, check it first. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int suggestedColPoly = -1; if(suggestedNavPoly != -1) { suggestedColPoly = navMesh->polygons()[suggestedNavPoly]->collision_poly_index(); if(point_in_polygon(p, *polygons[suggestedColPoly])) return suggestedNavPoly; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Step 2: Find the other potential collision polygons in which the point could lie. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int leafIndex = TreeUtil::find_leaf_index(p, tree); const std::vector<int>& polyIndices = tree->leaf(leafIndex)->polygon_indices(); std::vector<int> potentialColPolyIndices; for(std::vector<int>::const_iterator it=polyIndices.begin(), iend=polyIndices.end(); it!=iend; ++it) { // Note: We only add collision polygons which are also nav polygons to the list of potentials. if(*it != suggestedColPoly && navMesh->lookup_nav_poly_index(*it) != -1) potentialColPolyIndices.push_back(*it); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Step 3: Test each of the polygons to see whether the point's inside it, and return the index of the found nav polygon if so. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int potentialCount = static_cast<int>(potentialColPolyIndices.size()); for(int i=0; i<potentialCount; ++i) { int colPolyIndex = potentialColPolyIndices[i]; if(point_in_polygon(p, *polygons[colPolyIndex])) { int navPolyIndex = navMesh->lookup_nav_poly_index(colPolyIndex); #if 0 std::cout << "Now in polygon (" << colPolyIndex << ',' << navPolyIndex << ')' << std::endl; #endif return navPolyIndex; } } // If we get here, the point was not in a nav polygon. if(suggestedNavPoly != -1) { #if 0 std::cout << "Lost navmesh at " << p << std::endl; #endif } return -1; }
int collide_polygon_rectangle(int n, int *xs, int *ys, int rx, int ry, int w, int h) { assert(n > 0); int polygon_line[2][2]; int rectangle_lines[4][2][2] = { { {rx, ry}, {rx+w, ry} }, { {rx+w, ry}, {rx+w, ry+h} }, { {rx+w, ry+h}, {rx, ry+h} }, { {rx, ry+h}, {rx, ry} } }; int polygon_index, rectangle_index; int old_polygon_index = n - 1; for (polygon_index = 0; polygon_index < n; ++polygon_index) { polygon_line[0][0] = xs[old_polygon_index]; polygon_line[0][1] = ys[old_polygon_index]; polygon_line[1][0] = xs[polygon_index]; polygon_line[1][1] = ys[polygon_index]; for (rectangle_index = 0; rectangle_index < 4; ++rectangle_index) { if (lines_intersect(polygon_line, rectangle_lines[rectangle_index])) { return COLLISION; } } old_polygon_index = polygon_index; } if (point_in_rectangle(xs[0], ys[0], rx, ry, w, h)) { return COLLISION; } if (point_in_polygon(rx, ry, n, xs, ys)) { return COLLISION; } return NO_COLLISION; }
void cheak_inside_pos(Aircraft_t *f,int n_f,CONF_t conf){ int i,j; for(i=0;i<n_f;i++) for(j=0;j<conf.t_w;j++) if(f[i].pos[j][3]==0 &&point_in_polygon(f[i].pos[j], conf.bound, conf.Nbound)==1){ printf("SUSU\n"); } return; }
bool Circle::on_down(Vector2 p) { last_mouse = p; // check to see if mouse is on a corner; set drag_point if so for (size_t i = 0; i < num_points; ++i) { if (close_enough(p, pts[i])) { drag_point = i; return true; } } // check to see if mouse is inside if (point_in_polygon(p)) { drag_point = -2; return true; } return false; }
/* returns GLU_NO_ERROR if contours are disjoint */ static GLenum is_contour_contained_in(tess_contour * outer, tess_contour * inner) { GLenum relation_flag; /* set relation_flag to relation of containment of first inner vertex */ /* regarding outer contour */ if (point_in_polygon(outer, inner->vertices->x, inner->vertices->y)) relation_flag = GLU_INTERIOR; else relation_flag = GLU_EXTERIOR; if (relation_flag == GLU_INTERIOR) return GLU_INTERIOR; if (point_in_polygon(inner, outer->vertices->x, outer->vertices->y)) return GLU_EXTERIOR; return GLU_NO_ERROR; }
/* checks if every point of the individual lies within the polygon */ int valid_individual(individual* ind, opt_problem* problem){ int i; for(i=0;i<ind->size;i++){ if(!point_in_polygon(ind->points[i],problem)){ return 0; } } return 1; }
int polygon_overlap(Poly *p1, Poly *p2) { // loop over each pair of line segments, testing for intersection int i, j; for (i=0; i<p1->n-1; ++i) { for (j=0; j<p2->n-1; ++j) { if (lineSegmentsIntersect( p1->x[i], p1->y[i], p1->x[i+1], p1->y[i+1], p2->x[j], p2->y[j], p2->x[j+1], p2->y[j+1])) { return TRUE; } } } // test for containment: p2 in p1 int all_in=TRUE; for (i=0; i<p2->n; ++i) { if (!point_in_polygon(p1, p2->x[i], p2->y[i])) { all_in=FALSE; break; } } if (all_in) return TRUE; // test for containment: p1 in p2 all_in = TRUE; for (i=0; i<p1->n; ++i) { if (!point_in_polygon(p2, p1->x[i], p1->y[i])) { all_in=FALSE; break; } } if (all_in) return TRUE; // no overlap return FALSE; }
static OverlapInfo * overlap(double t, stateVector *st, BeamModeInfo *bmi, double look_angle, int zone, double clat, double clon, Poly *aoi) { Poly *viewable_region = get_viewable_region(st, bmi, look_angle, zone, clat, clon, NULL, NULL); if (!viewable_region) return NULL; // no overlap if (polygon_overlap(aoi, viewable_region)) { // need a good method to test for overlap amount! // for now we have this sort of kludgey method... which is // probably a bit slow: // generate 1000 points in the aoi -- pct is how many are also // in the viewable region srand(42); int i=0,pct=0,n=1000; double xmin, xmax, ymin, ymax; polygon_get_bbox(aoi, &xmin, &xmax, &ymin, &ymax); while (i<n) { double x = random_in_interval(xmin, xmax); double y = random_in_interval(ymin, ymax); if (point_in_polygon(aoi, x, y)) { ++i; if (point_in_polygon(viewable_region, x, y)) ++pct; } } return overlap_new(pct, n, viewable_region, zone, clat, clon, st, t); } else { // no overlap polygon_free(viewable_region); return NULL; } }
bool polygon_ctrl_impl::on_mouse_button_down(double x, double y) { unsigned i; bool ret = false; m_node = -1; m_edge = -1; inverse_transform_xy(&x, &y); for (i = 0; i < m_num_points; i++) { if(sqrt( (x-xn(i)) * (x-xn(i)) + (y-yn(i)) * (y-yn(i)) ) < m_point_radius) { m_dx = x - xn(i); m_dy = y - yn(i); m_node = int(i); ret = true; break; } } if(!ret) { for (i = 0; i < m_num_points; i++) { if(check_edge(i, x, y)) { m_dx = x; m_dy = y; m_edge = int(i); ret = true; break; } } } if(!ret) { if(point_in_polygon(x, y)) { m_dx = x; m_dy = y; m_node = int(m_num_points); ret = true; } } return ret; }
bool point_in_rectangle(int px, int py, int rx, int ry, int w, int h) { int rectangle_xs[4] = { rx, rx+w, rx+w, rx }; int rectangle_ys[4] = { ry, ry , ry+h, ry+h }; return point_in_polygon(px, py, 4, rectangle_xs, rectangle_ys); }
int rectangle_inside_polygon(int rx, int ry, int w, int h, int number_of_points, int *xs, int *ys) { return (point_in_polygon(rx, ry, number_of_points, xs, ys) && point_in_polygon(rx+w-1, ry, number_of_points, xs, ys) && point_in_polygon(rx, ry+h-1, number_of_points, xs, ys) && point_in_polygon(rx+w-1, ry+h-1, number_of_points, xs, ys)); }