Boost.Geometry:如何创建简单的多边形数组并将其保存为svg图像?

时间:2011-11-07 16:30:26

标签: c++ svg vector boost boost-geometry

我看一下名为Boost Geometry的图书库  我看一下它,但是看不到任何关于使用任何东西的教程,至少有点图形化。所以我想知道是否有人可以帮助提供一个简单的教程来创建一些N随机poligons(颜色大小和形式随机)并将tham保存为像SVG这样的矢量图像?

2 个答案:

答案 0 :(得分:11)

所以...解决了它:在谷歌上你可以找到this旧代码。它不会使用最新的1.47.0升级编译。所以你会尝试修复它,你会得到here例如一些过时的文档...这么长的故事就是你要做的就是让它工作:

下载boost/geometry/extensions/io/svg/的3个代码文件,它们只是标题,所以这里没有任何内容。

现在您可以编译修复,更新当前的提升代码:

#include <iostream>
#include <fstream>
#include <boost/assign.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/io/svg/write_svg.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/envelope.hpp>

#include <boost/geometry/io/svg/write_svg.hpp>


template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
    typedef typename boost::geometry::point_type<Geometry1>::type point_type;
    std::ofstream svg(filename.c_str());

    boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
    mapper.add(a);
    mapper.add(b);

    mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
    mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}

int main()
{
    using namespace boost::assign;


    boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring;
    ring +=
        boost::geometry::model::d2::point_xy<double>(4.0, -0.5), boost::geometry::model::d2::point_xy<double>(3.5, 1.0),
        boost::geometry::model::d2::point_xy<double>(2.0, 1.5), boost::geometry::model::d2::point_xy<double>(3.5, 2.0),
        boost::geometry::model::d2::point_xy<double>(4.0, 3.5), boost::geometry::model::d2::point_xy<double>(4.5, 2.0),
        boost::geometry::model::d2::point_xy<double>(6.0, 1.5), boost::geometry::model::d2::point_xy<double>(4.5, 1.0),
        boost::geometry::model::d2::point_xy<double>(4.0, -0.5);


     boost::geometry::model::box<boost::geometry::model::d2::point_xy<double> > box;
     boost::geometry::envelope(ring, box); 
    std::cout
        << "make_envelope:"
        << boost::geometry::dsv(box)
        << std::endl;

    create_svg("make_envelope.svg", ring, box);
}

这将得出这个:

enter image description here

(不是图像,但是矢量svg文件可以在google chrome中打开=))所以这就是如何在C ++中从vector创建SVG文件=)

答案 1 :(得分:0)

对于 boost >= 1.54 的版本,我们现在从 boost::geometry::io::svg 下的库获得官方支持。

#include <string>
#include <fstream>

#include <boost/geometry.hpp>


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

using Point2D = bgm::d2::point_xy<double>;
using Polygon2D = bgm::polygon<Point2D>;


template <typename Geometry>
void create_svg(std::string const& file_path, Geometry const& geometry, std::string const& style) {
    using PointType = typename bg::point_type<Geometry>::type;
    std::ofstream svg_file(file_path.c_str());
    bg::svg_mapper<PointType> mapper(svg_file, 400, 400);
    mapper.add(geometry);
    mapper.map(geometry, style);
}

Polygon2D create_polygon(Point2D const& p1, Point2D const& p2, Point2D const& p3, Point2D const& p4) {
    // NOTE: For closed poly first point equal to the last
    return {{p1, p2, p3, p4, p1}};
}

int main() {
    auto const& polygon = create_polygon({0., 0.}, {0., 4.}, {7., 4.}, {7., 0.});
    std::string style{"fill-rule:nonzero;fill-opacity:0.5;fill:yellow;stroke:black;stroke-width:2;"};
    create_svg("image.svg", polygon, style);
    return 0;
}

这给了我们

enter image description here

完整的游戏代码可用 here