我有两个多边形(一个矩形和一个曲线),如下图所示:
我正在使用boost::geometry::intersection
函数使用以下代码获取交叉点:
#include "boost\geometry\geometries\point_xy.hpp"
#include "boost\geometry\geometries\polygon.hpp"
typedef boost::geometry::model::d2::point_xy<float> point_xy;
typedef boost::geometry::model::polygon<point_xy> polygon;
void Gating::createEllipsePolygon(int i_popId, polygon & i_ellipse)
{
float *pointX = nullptr, *pointY = nullptr;
size_t size = 0;
switch (i_popId)
{
case Stromal:
pointX = &m_ellipseXG2[0];
pointY = &m_ellipseYG2[0];
size = m_ellipseXG2.size();
break;
case Tumor:
pointX = &m_ellipseXG1[0];
pointY = &m_ellipseYG1[0];
size = m_ellipseXG1.size();
break;
default:
break;
}
for (size_t i = 0; i < size; ++i)
{
bg::append(i_ellipse, bg::make<point_xy>(pointX[i], pointY[i]));
}
}
void Gating::createRectanglePolygon(int i_popId, polygon & i_rectangle)
{
Ranges *range = nullptr;
size_t size = 0;
switch (i_popId)
{
case Stromal:
range = &m_intermediateRangePop2;
bg::append(i_rectangle, bg::make<point_xy>(0., range->yLowerBound));
bg::append(i_rectangle, bg::make<point_xy>(0., range->yUpperBound));
bg::append(i_rectangle, bg::make<point_xy>(range->xUpperBound, range->yUpperBound));
bg::append(i_rectangle, bg::make<point_xy>(range->xUpperBound, range->yLowerBound));
bg::append(i_rectangle, bg::make<point_xy>(0., range->yLowerBound));
break;
case Tumor:
range = &m_intermediateRangePop1;
bg::append(i_rectangle, bg::make<point_xy>(range->xLowerBound, 0.));
bg::append(i_rectangle, bg::make<point_xy>(range->xLowerBound, range->yUpperBound));
bg::append(i_rectangle, bg::make<point_xy>(range->xUpperBound, range->yUpperBound));
bg::append(i_rectangle, bg::make<point_xy>(range->xUpperBound, 0.));
bg::append(i_rectangle, bg::make<point_xy>(range->xLowerBound, 0.));
break;
default:
break;
}
}
bool Gating::checkIntersectionBetweenEllipseAndRect(int i_popId)
{
std::vector<float> *polyPointX = nullptr, *polyPointY = nullptr;
bool intersection = false;
polygon ellipse, rectangle;
std::vector<polygon> output;
createEllipsePolygon(i_popId, ellipse);
createRectanglePolygon(i_popId, rectangle);
switch (i_popId)
{
case Stromal:
polyPointX = &m_polyPointXG2;
polyPointY = &m_polyPointYG2;
break;
case Tumor:
polyPointX = &m_polyPointXG1;
polyPointY = &m_polyPointYG1;
break;
default:
break;
}
if (bg::within(ellipse, rectangle))
{
for (auto it = bg::points_begin(ellipse); it != bg::points_end(ellipse); ++it)
{
polyPointX->push_back(it->x());
polyPointY->push_back(it->y());
}
}
else
{
intersection = bg::intersection(rectangle, ellipse, output);
for (polygon const& poly : output)
{
for (auto it = bg::points_begin(poly); it != bg::points_end(poly); ++it)
{
polyPointX->push_back(it->x());
polyPointY->push_back(it->y());
}
}
polyPointX->erase(polyPointX->begin(), polyPointX->begin() + 5);
polyPointY->erase(polyPointY->begin(), polyPointY->begin() + 5);
polyPointX->push_back((*polyPointX)[0]);
polyPointY->push_back((*polyPointY)[0]);
}
return intersection;
}
boost::geometry::intersection
的输出是两个多边形的vector
,如下图中的红色所示:
相反,我想得到这样的东西(绿线):
我是否以错误的方式使用boost::geometry::intersection
? boost::geometry::intersection
不是正确的使用方法吗?是否有其他功能可以得到我想要的东西?非常感谢任何帮助!