Exemplo n.º 1
0
/*!
  Find number of times ray intersects polygon defined by iterators
*/
int Line2::noIntersections(const Polygon2& poly) const
{
  int n = 0;
  Vector2 v, v_prev;
  Point2  p, p_prev;
  ConstPointIterator2 begin = poly.begin();
  ConstPointIterator2 end = poly.end();   
  ConstPointIterator2 it;
  for (it = begin+1; it != end; ++it) {
    if (intersect(Segment2(*(it-1), *it)))
      ++n;
  }
  if (intersect(Segment2(*(end-1), *begin)))
    ++n;
  return n;
}
Exemplo n.º 2
0
bool GraphImp2::shortestPath(Trapezoid2* source, Trapezoid2* target, Polygon2& path) const
{
  assert(iGraph != 0);
  assert(source != 0 && target != 0);
  Graph& g = *iGraph;

  // Find source and target vertex in graph
  Vertex s = vertex(source->tag(), g);
  Vertex t = vertex(target->tag(), g);
  
  Vertices p(num_vertices(g));  // Predecessor map
  Reals    d(num_vertices(g));  // Distance map
  
  // A* will find ALL shortest paths like Dijkstra so we need to throw exception when goal has been found
  try {
    astar_search(
      g, 
      s,
      DistanceHeuristic(iGraph, t),
      weight_map(get(&EdgeProperty::weight, g)).
      predecessor_map(make_iterator_property_map(p.begin(), get(vertex_index, g))).
      distance_map(make_iterator_property_map(d.begin(), get(vertex_index, g))).
      visitor(GoalVisitor(t))
    );    
  }
  catch (FoundGoalException e) {
    Vertex t = vertex(target->tag(), g);
    
    // Store coordinates of shortest path
    while (t != p[t]) {
      path.push_back(g[t]);
      t = p[t];
    }
    path.push_back(g[t]);    
    reverse(path.begin(), path.end());
    
    return true;
  }
  
  return false;
}
Exemplo n.º 3
0
/*!
  Find closest intersection point on polygon
*/
bool Line2::intersection(const Polygon2& poly, Vector2& result) const
{
  assert(false); // TODO: Think this is buggy. Why substact origin() from v and why no test on end to begin part
  
  bool found_point = false;
  Vector2 v, v_prev;
  Point2  p, p_prev;
  ConstPointIterator2 begin = poly.begin();
  ConstPointIterator2 end = poly.end();  
  ConstPointIterator2 it;
  for (it = begin+1; it != end; ++it) {
    if (!intersection(Segment2(*(it-1), *it), p)) continue;
    v = p - origin();
    if (v.squaredLength() >= v_prev.squaredLength()) continue;
    v_prev = v;
    p_prev = p;
    found_point = true;
  }
  result = p_prev;
  return found_point;
}