【发布时间】:2018-07-22 15:45:35
【问题描述】:
我正在尝试弄清楚为什么我的下面的代码在 boost 1.61 中按预期工作,但在 boost 1.67 中却没有。
在boost 1.61 中,输入多边形正确组合并显示轮廓多边形。
在boost 1.67 中,没有代码更改,轮廓多边形错误且不完整。好像缺点一样。
添加了输出图像以帮助解释。还有一个#define BOOST_1_6_1 我需要添加,因为boost 1.61 似乎不会自动添加该头文件。
如果您想查看输入多边形的外观,请取消注释 drawAllPolygons()。
有人可以帮忙吗?
#include <iostream>
#include <boost\geometry.hpp>
//#define BOOST_1_6_1
#ifdef BOOST_1_6_1
#include <boost/geometry/geometries/point_xy.hpp>
#endif
typedef boost::geometry::model::d2::point_xy<int> intPt;
typedef boost::geometry::model::polygon<intPt> polygon;
const int GRID_WIDTH = 220;
const int GRID_HEIGHT = 60;
bool canvas[GRID_WIDTH][GRID_HEIGHT];
std::vector<polygon> output;
std::vector<polygon> input;
void initCanvas()
{
for (unsigned int y = 0; y < GRID_HEIGHT; ++y)
{
for (unsigned int x = 0; x < GRID_WIDTH; ++x)
{
canvas[x][y] = false;
}
}
}
void drawGrid()
{
for (unsigned int y = 0; y < GRID_HEIGHT; ++y)
{
for (unsigned int x = 0; x < GRID_WIDTH; ++x)
{
if (canvas[x][y])
{
std::cout << "x";
}
else
{
std::cout << ".";
}
}
std::cout << std::endl;
}
}
polygon setupPolygon(const int startX, const int startY, const int width, const int height)
{
polygon polygon1;
int endX = startX + width;
int endY = startY + height;
for (int x = startX; x <= endX; ++x)
{
intPt pt(x, startY);
polygon1.outer().push_back(pt);
}
for (int y = startY; y <= endY; ++y)
{
intPt pt(endX, y);
polygon1.outer().push_back(pt);
}
for (int x = endX; x >= startX; --x)
{
intPt pt(x, endY);
polygon1.outer().push_back(pt);
}
for (int y = endY; y >= startY; --y)
{
intPt pt(startX, y);
polygon1.outer().push_back(pt);
}
return polygon1;
}
std::vector<polygon> combine(std::vector<polygon> input)
{
bool loop = true;
while (loop)
{
unsigned int before = input.size();
bool exit = false;
for (unsigned int i = 0; i < input.size() && !exit; ++i)
{
for (unsigned int j = i + 1; j < input.size() && !exit; ++j)
{
std::vector<polygon> output;
boost::geometry::correct(input[i]);
boost::geometry::correct(input[j]);
boost::geometry::union_(input[i], input[j], output);
if (i < j)
{
input.erase(input.begin() + j);
input.erase(input.begin() + i);
}
else
{
input.erase(input.begin() + i);
input.erase(input.begin() + j);
}
input.insert(input.begin(), output.begin(), output.end());
exit = true;
}
}
if (before == input.size())
{
loop = false;
}
}
return input;
}
void drawAllPolygons()
{
for (unsigned int i = 0; i < input.size(); ++i)
{
auto outer = input[i].outer();
for (unsigned int i = 0; i < outer.size(); ++i)
{
int x = outer[i].get<0>();
int y = outer[i].get<1>();
canvas[x][y] = true;
}
}
}
void drawCombinedPolygons()
{
for (unsigned int j = 0; j < output.size(); ++j)
{
auto outer = output[j].outer();
for (unsigned int i = 0; i < outer.size(); ++i)
{
int x = outer[i].get<0>();
int y = outer[i].get<1>();
canvas[x][y] = true;
}
}
}
int main()
{
initCanvas();
input.push_back(setupPolygon(40, 10, 50, 15));
input.push_back(setupPolygon(40, 20, 50, 15));
input.push_back(setupPolygon(50, 10, 50, 15));
input.push_back(setupPolygon(60, 30, 50, 15));
output = combine(input);
//drawAllPolygons();
drawCombinedPolygons();
drawGrid();
std::cin.get();
return 0;
}
【问题讨论】:
-
请创建一个MCVE 的问题。将问题隔离在更小的代码中,然后提出来寻求帮助。
-
@badola - 我有。看我的帖子。有人只需要下载 boost 1.61 和 1.67 并按原样运行我的代码即可重现问题。我认为使用 union_ 的方式在这些版本之间发生了变化。但我不确定它是如何改变的。
-
您是否尝试过中间版本的 boost 以查看您是否可以隔离出现差异的版本(当然假设库在这些版本之间发生了变化)?
标签: c++ boost geometry union boost-geometry