问题
I want to calculate the intersection point between a line:
l := direction * x + origin for x e R or x e [0,R+)
and a default Boost polygon. In the documentation I only found the possibility to get the intersection with a line Segment (fixed start and end point)
At the moment I am using boost geometry and for intersection : http://www.boost.org/doc/libs/1_57_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection.html
Did I miss anything? Or do you know some boost function which I can use to solve my Problem.
I tried a workaround with:
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::segment<Point> Segment;
Segment AB( Point{1,1*std::numeric_limits<double>::lowest()},Point{0,1*std::numeric_limits<double>::max()});
boost::geometry::model::polygon<Point> poly;
poly.outer().push_back(Point{0,0});
poly.outer().push_back(Point{10,0});
poly.outer().push_back(Point{10,10});
poly.outer().push_back(Point{0,10});
poly.outer().push_back(Point{0,0});
std::vector<Segment> result;
boost::geometry::intersection(AB,poly,result);
I am using boost 1.56 and get the error that this is not implemented yet. Do you know where I can found a list of which intersection are implemented? Or do know have some new idea?
回答1:
Boost.Geometry doesn't have an intinite Line or Ray concept. So indeed you need to use a Segment or Linestring for this. Using Segment indeed looks as the best way but it just may be not supported by intersection() now. You can create a ticket with a feature request if you need it badly. For now you could use Linestring instead of a Segment to define a line. To store a result you could either use MultiLinestring or a vector of Points. In the second case you'll get the intersection points so AFAIU exactly what you need:
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::linestring<Point> Linestring;
typedef boost::geometry::model::polygon<Point> Polygon;
Linestring ls;
Polygon poly;
std::vector<Point> result;
boost::geometry::intersection(ls,poly,result);
If the above didn't compile you should use more recent version of Boost.
You probably shouldn't create a Linestring containing double's lowest/max. It's because you'd get massive numeric errors. The closer the points of a segment the better. You could for instance manually calculate points of the segment being an intersection of a line and Polgon's bounding box or bounding sphere, etc.
来源:https://stackoverflow.com/questions/27906510/intersection-line-poly-boost-geometry