【问题标题】:Boost geometry union with auto-allocation通过自动分配增强几何联合
【发布时间】:2014-09-29 12:37:01
【问题描述】:
#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 作为多边形。问题是当我进行联合裁剪时,算法没有分配内存。有谁知道这个的解决方案吗?

【问题讨论】:

    标签: c++ boost boost-geometry


    【解决方案1】:

    首先让我花点时间说一下我对这种“尴尬”点类型选择的动机感到困惑。

    在我看来

    • 如果您的积分很少,那么分享似乎不是必需的
    • 如果您这样做,那么 shared_ptr 的开销(2x 指针开销和原子引用计数锁定)似乎会阻止缩放。

    为了两全其美(点可以同时位于多个集合中),您是否考虑过直接指针,或者甚至可能是 Boost Intrusive 容器(不会对包含的元素拥有所有权)?

    抛开所有问题,这里有一种方法:


    您可以在 shared_ptr 周围做一个简单的包装器,让您以这种方式使用它:

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

    如您所见,它将默认构造为一个新实例;看到它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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2020-09-12
      • 2015-11-06
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多