提升几何联盟与自动分配 [英] Boost geometry union with auto-allocation

查看:166
本文介绍了提升几何联盟与自动分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream> 
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using namespace boost::geometry;

class CustomPoint{
public:
    double X;
    double Y;
};

using cpPtr = boost::shared_ptr<CustomPoint>;

namespace boost { namespace geometry { namespace traits {
    BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)

    template<> struct access<cpPtr, 0> {
        static inline double get(cpPtr const& p) { return p->X; }
        static inline void set(cpPtr& p, double const& value) { p->X = value; }
    };
    template<> struct access<cpPtr, 1> {
        static inline double get(cpPtr const& p) { return p->Y; }
        static inline void set(cpPtr& p, double const& value) { p->Y = value; }
    };
}}}

int main()
{
  std::vector<cpPtr> one,Two;
  //init polys
  std::vector< std::vector<cpPtr> > output;
  boost::geometry::union_(one,two,output)

}

您好我想一个boost :: shared_ptr的多边形。问题是,当我做工会裁剪算法没有分配内存。任何人都知道一个解决方案?

Hello i tried a boost::shared_ptr as polygon. The problem is when i do union clipping the algorithm didn't allocate memory. Anyone knows a solution for this?

推荐答案

首先让我走的时候说我的动机,这种尴尬点类型的选择困惑。

First let me take the time to say I'm puzzled by the motivation for this "awkward" point type choice.

在我看来,那


  • 如果你有非常几点,那么共享将不会出现是必不可少的

  • 如果您的的话它会出现的shared_ptr(2X指针开销和原子引用计数锁)的开销将prevent缩放。

  • if you have very few points, then the sharing would not appear to be essential
  • if you do then it would appear that the overhead of shared_ptr (2x pointer overhead and atomic reference count locking) would prevent scaling.

有关两全其美的(这点可以同时在多个集合)你有没有考虑直球,或者甚至升压介入式容器(这不会采取了所有权所包含的元素)?

For the best of both worlds (points that can be in multiple collections at once) have you considered straight pointers, or perhaps even Boost Intrusive containers (which will not take ownership over contained elements)?

所有的问题放在一边,这里做到这一点的一种方法:

All question aside, here's one way to do it:

您可以做一个简单的包装围绕一个shared_ptr,允许你以这种方式使用它:

You can do a simplistic wrapper around a shared_ptr that allows you to use it in this way:

template<typename T>
    struct shared_instancing : boost::shared_ptr<T> {
        using boost::shared_ptr<T>::shared_ptr;

        shared_instancing(boost::shared_ptr<T> sp = boost::make_shared<T>()) 
            : boost::shared_ptr<T>(sp)
        { }
    };

正如你所看到的,它会默认构造一个新的实例;看到它的 住在Coliru

As you can see, it will default construct to a new instance; See it Live On Coliru

#include <iostream> 
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::geometry;

struct CustomPoint{
    double X;
    double Y;
};

namespace {
    template<typename T>
        struct shared_instancing : boost::shared_ptr<T> {
            using boost::shared_ptr<T>::shared_ptr;

            shared_instancing(boost::shared_ptr<T> sp = boost::make_shared<T>()) 
                : boost::shared_ptr<T>(sp)
            { }
        };
}

using cpPtr = shared_instancing<CustomPoint>; // boost::shared_ptr<CustomPoint>;

namespace boost { namespace geometry { namespace traits {
    BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)

    template<> struct access<cpPtr, 0> {
        static inline double get(cpPtr const& p) { return p->X; }
        static inline void set(cpPtr& p, double const& value) { p->X = value; }
    };
    template<> struct access<cpPtr, 1> {
        static inline double get(cpPtr const& p) { return p->Y; }
        static inline void set(cpPtr& p, double const& value) { p->Y = value; }
    };
}}}

int main()
{
    typedef boost::geometry::model::polygon<cpPtr > polygon;

    polygon green, blue;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);

    boost::geometry::read_wkt(
        "POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);

    std::vector<polygon> output;
    boost::geometry::union_(green, blue, output);

    int i = 0;
    std::cout << "green || blue:" << std::endl;
    for(polygon const& p: output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }
}

这篇关于提升几何联盟与自动分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆