Boost logo

Geometry :

Subject: [geometry] Custom Line string in Boost Geometry Library
From: Suresh Jakka (sjakka_at_[hidden])
Date: 2013-09-20 13:12:14


Hello all,
I am new to Boost library and I am trying to register our geometry types with boost. I am having trouble with LineString. Here is my sample that I could reproduce the problem. Could someone please let me know what I am doing wrong? Thank you in advance.

#include <iostream>
#include <vector>
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/foreach.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range.hpp>
#include <boost/geometry.hpp>
template<class T>
class QPoint
{
    public:
        T x;
        T y;
        QPoint(T x, T y) : x(x), y(y) {}
};

template<class T>
class QLineString
{
    public:
        bool cw;
        std::vector<QPoint<T>> points;
};
namespace boost
{
    namespace geometry
    {
        namespace traits
        {
            // Adapt QPoint to Boost.Geometry

            template<class T> struct tag<QPoint<T>>
            { typedef point_tag type; };

            template<class T> struct coordinate_type<QPoint<T>>
            { typedef T type; };

            template<class T> struct coordinate_system<QPoint<T>>
            { typedef cs::cartesian type; };

            template<class T> struct dimension<QPoint<T>> : boost::mpl::int_<2> {};

            template<class T>
            struct access<QPoint<T>, 0>
            {
                static T get(QPoint<T> const& p)
                {
                    return p.x;
                }

                static void set(QPoint<T>& p, T const& value)
                {
                    p.x = value;
                }
            };

            template<class T>
            struct access<QPoint<T>, 1>
            {
                static T get(QPoint<T> const& p)
                {
                    return p.y;
                }

                static void set(QPoint<T>& p, T const& value)
                {
                    p.y = value;
                }
            };
        }
    }
} // namespace boost::geometry::traits

namespace boost
{
    namespace geometry
    {
        namespace traits
        {
            template<class T>
            struct tag<QLineString<T>>
            {
                typedef linestring_tag type;
            };
        }
    }
} // namespace boost::geometry::traits

namespace boost
{
    template <class T>
    struct range_iterator<QLineString<T>>
    { typedef std::vector<QPoint<T>>::iterator type; };

    template<class T>
    struct range_const_iterator<QLineString<T>>
    { typedef std::vector<QPoint<T>>::const_iterator type; };
}

template<class T>
inline std::vector<QPoint<T>>::iterator range_begin(QLineString<T>& qls)
{
    return qls.points.begin();
}

template<class T>
inline std::vector<QPoint<T>>::iterator range_end(QLineString<T>& qls)
{
    return qls.points.end();
}

template<class T>
inline std::vector<QPoint<T>>::const_iterator range_begin(const QLineString<T>& qls)
{
    return qls.points.begin();
}

template<class T>
inline std::vector<QPoint<T>>::const_iterator range_end(const QLineString<T>& qls)
{
    return qls.points.end();
}
int main()
{
    QPoint<int> p(2,3);
    QPoint<int> p1(5,3);
    QPoint<int> p2(8,3);
    QPoint<int> p3(12,3);

    QLineString<int> ln;
    ln.points.push_back(p);
    ln.points.push_back(p1);
    ln.points.push_back(p2);
    ln.points.push_back(p3);

    double length = boost::geometry::length(ln);
}

Thanks,
Suresh



Geometry list run by mateusz at loskot.net