c05_custom_point_pointer_example.cpp

This example shows how GGL can be used to adapt a pointer-to-a-point, used e.g. in a linestring

    //Boost.Geometry(akaGGL,GenericGeometryLibrary)

    //Copyright(c)2007-2012BarendGehrels,Amsterdam,theNetherlands.
    //Copyright(c)2008-2012BrunoLalande,Paris,France.
    //Copyright(c)2009-2012MateuszLoskot,London,UK.

    //Use,modificationanddistributionissubjecttotheBoostSoftwareLicense,
    //Version1.0.(SeeaccompanyingfileLICENSE_1_0.txtorcopyat
    //http://www.boost.org/LICENSE_1_0.txt)
    //
    //Custompointer-to-pointexample

    #include<iostream>

    #include<boost/foreach.hpp>

    #include<boost/geometry/algorithms/distance.hpp>
    #include<boost/geometry/algorithms/length.hpp>
    #include<boost/geometry/algorithms/make.hpp>
    #include<boost/geometry/algorithms/intersection.hpp>
    #include<boost/geometry/geometries/geometries.hpp>
    #include<boost/geometry/geometries/point_xy.hpp>
    #include<boost/geometry/geometries/register/linestring.hpp>
    #include<boost/geometry/strategies/strategies.hpp>

    BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::vector)
    BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::deque)

    //Samplepoint,havingx/y
    structmy_point
    {
    doublex,y;
    };

    namespaceboost{namespacegeometry{namespacetraits{

    template<>structtag<my_point>
    {typedefpoint_tagtype;};

    template<>structcoordinate_type<my_point>
    {typedefdoubletype;};

    template<>structcoordinate_system<my_point>
    {typedefcs::cartesiantype;};

    template<>structdimension<my_point>:boost::mpl::int_<2>{};

    template<>
    structaccess<my_point,0>
    {
    staticdoubleget(my_pointconst&p)
    {
    returnp.x;
    }

    staticvoidset(my_point&p,doubleconst&value)
    {
    p.x=value;
    }
    };

    template<>
    structaccess<my_point,1>
    {
    staticdoubleget(my_pointconst&p)
    {
    returnp.y;
    }

    staticvoidset(my_point&p,doubleconst&value)
    {
    p.y=value;
    }
    };

    }}}//namespaceboost::geometry::traits

    intmain()
    {
    typedefstd::vector<my_point*>ln;
    lnmyline;
    for(floati=0.0;i<10.0;i++)
    {
    my_point*p=newmy_point;
    p->x=i;
    p->y=i+1;
    myline.push_back(p);
    }

    std::cout<<boost::geometry::length(myline)<<std::endl;

    typedefboost::geometry::model::d2::point_xy<double>point_2d;
    typedefboost::geometry::model::box<point_2d>box_2d;
    box_2dcb(point_2d(1.5,1.5),point_2d(4.5,4.5));

    //ThiswillNOTworkbecausewouldneeddynamiclyallocatingmemoryforpoint*inalgorithms:
    //std::vector<ln>clipped;
    //boost::geometry::intersection(cb,myline,clipped);

    //Thisworksbecauseoutputstoanormalstructpoint,nopoint*
    typedefboost::geometry::model::linestring<point_2d>linestring_2d;
    std::vector<linestring_2d>clipped;
    boost::geometry::strategy::intersection::liang_barsky<box_2d, point_2d>strategy;
    boost::geometry::detail::intersection::clip_range_with_box<linestring_2d>(cb,
    myline,std::back_inserter(clipped),strategy);

    std::cout<<boost::geometry::length(clipped.front())<<std::endl;

    //free
    BOOST_FOREACH(my_point*p,myline)
    {
    deletep;
    }

    return0;
    }