gpt4 book ai didi

c++ - 使用 boost 将几何体切割成碎片

转载 作者:行者123 更新时间:2023-11-30 04:59:04 34 4
gpt4 key购买 nike

boost::geometry 中是否有任何内置工具可以像下图那样切割几何体?我的想法是找到一个交叉点几何图形,并从两个来源中减去它们。但感觉这不是最佳解决方案,因为有超过 2 个相交的矩形共享相同的区域。

cut result

enter image description here

因此,将输入数据转换为代码中的同构测试用例:

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/io/io.hpp>
#include <iostream>
#include <fstream>

namespace bg = boost::geometry;
namespace bgm = bg::model;

using point = bgm::d2::point_xy<int>;
using poly = bgm::polygon<point>;
using mpoly = bgm::multi_polygon<poly>;

int main()
{
poly a, b, c;
bg::read_wkt("POLYGON((0 0 0 6 6 6 6 0 0 0))", a);
bg::read_wkt("POLYGON((4 -1 4 4 5 4 5 -1 4 -1))", b);
bg::read_wkt("POLYGON((3 -3 3 3 9 3 9 -3 3 -3))", c);

std::cout << bg::wkt(a) << "\n";
std::cout << bg::wkt(b) << "\n";
std::cout << bg::wkt(c) << "\n";

{
std::ofstream svg("output.svg");
boost::geometry::svg_mapper<point> mapper(svg, 400, 400);
mapper.add(a);
mapper.add(b);
mapper.add(c);

mapper.map(a, "fill-opacity:0.2;fill:rgb(0,0,153);stroke:rgb(0,0,200);stroke-width:2");
mapper.map(b, "fill-opacity:0.2;fill:rgb(153,0,0);stroke:rgb(200,0,0);stroke-width:2");
mapper.map(c, "fill-opacity:0.2;fill:rgb(0,153,0);stroke:rgb(0,200,0);stroke-width:2");
}
}

它反射(reflect)了以下 SVG:

enter image description here

最佳答案

考虑以下代码:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry.hpp>

#include <boost/foreach.hpp>
#include <boost/foreach.hpp>


namespace bg = boost::geometry;
namespace bgm = bg::model;

typedef bgm::d2::point_xy<int> point;
typedef bgm::polygon<point> poly;
typedef bgm::multi_polygon<poly> mpoly;

int main()
{
poly a, b, c;
bg::read_wkt("POLYGON((0 0 0 6 6 6 6 0 0 0))", a);
bg::read_wkt("POLYGON((4 -1 4 4 5 4 5 -1 4 -1))", b);
bg::read_wkt("POLYGON((3 -3 3 3 9 3 9 -3 3 -3))", c);

std::vector<poly> polies;
polies.push_back(a);
polies.push_back(b);
polies.push_back(c);

std::vector<poly> res;
for (size_t i = 0; i < polies.size(); ++i)
{
for (size_t j = i; j < polies.size(); ++j)
{
boost::geometry::model::multi_polygon<poly> output;
boost::geometry::union_(polies[i], polies[j], output);

for (auto it = output.begin(); it != output.end(); ++it)
{
res.push_back(*it);
}
}
}

for (size_t i = 0; i < polies.size(); ++i)
{
for (size_t j = i; j < polies.size(); ++j)
{
boost::geometry::model::multi_polygon<poly> multi;
boost::geometry::sym_difference(polies[i], polies[j], multi);

for (auto it = multi.begin(); it != multi.end(); ++it)
{
res.push_back(*it);
}
}
}


{
std::ofstream svg("output2.svg");
boost::geometry::svg_mapper<point> mapper(svg, 400, 400);
size_t i = 0;
BOOST_FOREACH(poly& p, res)
{
std::stringstream ss;
ss << ++i * 10;
std::stringstream ss2;
ss2 << 255 - i * 10;
mapper.add(p);
mapper.map(p, "fill-opacity:0.2;fill:rgb("+ ss.str() + "," + ss2.str() +",153);stroke:rgb(0,0,200);stroke-width:2");
}
}

return 0;
}

产生以下输出: enter image description here为此,您必须遍历所有组合并计算初始多边形的并集和 sym_differences。

抱歉,颜色没有你的好看。

这有帮助吗?

关于c++ - 使用 boost 将几何体切割成碎片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51421516/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com