Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Geometry: How to create simple array of polygons and save tham as svg image?

I look at grate library called Boost Geometry I look at it but see no tutorials on working with anything at least a bit graphical. So I wonder if any one can help providing a simple tutorial on creating some N random poligons (random in color size and form) and saving tham as vector image like SVG?

like image 665
Rella Avatar asked Dec 22 '22 06:12

Rella


1 Answers

So... solved it: on google you can find this old code. It will not compile with latest boost 1.47.0. So you will try to fix it and you'd get here for example on some outdated docs... so long story short here is what you shall do to make it work:

Download 3 code files for boost/geometry/extensions/io/svg/, they are header only so no wories here.

and now you can compile fixed, updated for current boost code:

#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);
}

this will draw this:

enter image description here

(not image but vector svg file openable in google chrome=)) So this is how to create SVG file from vector in C++=)

like image 126
Rella Avatar answered Dec 24 '22 03:12

Rella