9

Boost にはジオメトリ用の優れたライブラリがあります。SVG 画像を描画することもできます。私のプロジェクトでそれを使用したいのですが、私にとっては本当に奇妙に機能します(下の画像を参照)。

したがって、2 次元空間で正方形の多角形として表される 3 つのピクセル ポイントがあります。

 1 1
 0 1

ここに画像の説明を入力 pic 1

それらからユニオンを取得し、それを単純化して、スケーリングすると次のような三角形が得られるようにします

1 1 1 1 1 1
1 1 1 1 1 1  
1 1 1 1 1 1
0 1 1 1 1 1 
0 0 1 1 1 1 
0 0 0 1 1 1

ここに画像の説明を入力 pic 2

しかし、これを取得します:

ここに画像の説明を入力

ここで、黄色の点線はユニオン、緑色は単純化です。

ソースコード:

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

#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/algorithms/envelope.hpp>

#include <boost/geometry/extensions/io/svg/svg_mapper.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()
{

    // create points (each point == square poligon)
    boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 0, 0 0, 0 1))", one);

    boost::geometry::read_wkt(
        "POLYGON((2 2, 2 1, 1 1, 1 2))", two);

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 2, 0 2, 0 1))", three);

    // create a container for joined points structure
    boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > output, simpl;

    // join points one by one (because one day we would have many=))
    boost::geometry::union_(one, two, output);
    boost::geometry::union_( output , three, output);

    // simplify joined structure
    boost::geometry::simplify(output, simpl, 0.5);

    // create an svg image
    create_svg("make_envelope.svg", simpl, output );
}

少なくともブースト 1.47.0 と、 boost/geometry/extensions/io/svg/からの 3 つのファイルが必要です。

では、形を整える意味が欲しいように単純化するにはどうすればよいpic 2ですか?

アップデート

新しいコードを作成し、正しく動作し、十分にテストしました:

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

//Boost
#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

#include <boost/foreach.hpp>

//and this is why we use Boost Geometry from Boost trunk 
#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

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-rule:nonzero;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");
}


void make_point(int x, int y,  boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > & ring)
{
    using namespace boost::assign;

    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x-1, y-1));
    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x, y-1));
    boost::geometry::append(  ring,      boost::geometry::model::d2::point_xy<double>(x, y));
    boost::geometry::append(  ring,      boost::geometry::model::d2::point_xy<double>(x-1, y));
    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x-1, y-1));
    boost::geometry::correct(ring);
}

void create_point(int x, int y, boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > & mp)
{
    boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > temp;
    boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > ring;
    make_point(x, y, ring);
    boost::geometry::union_(mp, ring, temp);
    boost::geometry::correct(temp);
    mp=temp;
}

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

    typedef boost::geometry::model::polygon
        <
        boost::geometry::model::d2::point_xy<double>
        > polygon;

    typedef boost::geometry::model::multi_polygon<polygon> mp;

    polygon ring;

    mp pol, simpl;
    polygon exring;

    create_point(1,1, pol);
    create_point(2, 1, pol);
    create_point(3, 1, pol);
    create_point(4,1, pol);
    create_point(5, 1, pol);

    create_point(1,2, pol);
    create_point(2, 2, pol);
    create_point(3, 2, pol);
    create_point(4,2, pol);
    create_point(5, 2, pol);

    create_point(2, 3, pol);
    create_point(3, 3, pol);
    create_point(5, 3, pol);

    create_point(3, 4, pol);

    create_point(5, 3, pol);

    create_point(5, 5, pol);

    //boost::geometry::dissolve(ring, pol); // Baad
    boost::geometry::simplify(pol, simpl, 0.5); // Good

    create_svg("make_envelope.svg",pol,  simpl );
}

そして、このコードはそのような画像を作成します:

ここに画像の説明を入力

3 ポイントの場合は、@J と同様の画像を返します。Calleja の答え:

ここに画像の説明を入力

4

1 に答える 1

5

コードにはいくつかの問題があると思います。

  • 定義しているポリゴンは次のとおりです。

1 1
1 0

あれは:

three two
one    -

したがって、期待される結果はpic2とは異なります。

  • ポリゴンを閉じて、時計回りに向ける必要があります。

終了点がなく、3番目のポリゴンが時計回りに方向付けられていません。正しい方法を見てください。この例では、定義するすべてのポリゴンに対してこれを呼び出す必要があります。

  • _unionを使用する場合、入力と出力に同じ引数を使用することはできません。

一時変数を使用する必要があります。

  boost::geometry::union_(one, two, outputTmp);    
  boost::geometry::union_( outputTmp, three, output);  
  • 期待される結果は、アルゴリズムの結果ではない可能性があります。

修正されたコードを実行すると、結果は次のようになります。

結果を単純化する

これは、ポリゴンの有効な簡略化である可能性があります。Ramer–Douglas–Peuckerアルゴリズムを参照してください。

これらの変更を実行した後、以下は結果のmain()です。

int main() 
{
  // create points (each point == square poligon)     
  boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;  
  boost::geometry::read_wkt(         "POLYGON((1 1, 1 0, 0 0, 0 1))", one);  
  boost::geometry::read_wkt(         "POLYGON((2 2, 2 1, 1 1, 1 2))", two); 
  boost::geometry::read_wkt(         "POLYGON((1 1, 1 2, 0 2, 0 1))", three);  
  boost::geometry::correct(one);
  boost::geometry::correct(two);
  boost::geometry::correct(three);

  // create a container for joined points structure  
  boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > outputTmp, output, simpl;      
  // join points one by one (because one day we would have many=))    
  boost::geometry::union_(one, two, outputTmp);    
  boost::geometry::union_( outputTmp, three, output);    
  // simplify joined structure  
  boost::geometry::simplify(output, simpl, 0.5);   
  // create an svg image   
  create_svg("make_envelope.svg", simpl, output ); 
}
于 2011-11-08T23:38:03.757 に答える