Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Boost.Geometry adapt to legacy objects
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/range.hpp>
#include <vector>
// Legacy object hierarchy
class QPoint {
public:
long x;
long y;
QPoint(long x, long y) : x(x), y(y) {}
};
class QLineString {
public:
bool cw;
std::vector<QPoint> points;
};
class QRing {
public:
std::vector<QLineString> lines;
};
class Polygon {
QRing exterior;
public:
std::vector<QRing> interiors;
};
//
// Point specialization
//
namespace boost {
namespace geometry {
namespace traits {
template<> struct tag<QPoint>
{ typedef point_tag type; };
template<> struct coordinate_type<QPoint>
{ typedef double type; };
template<> struct coordinate_system<QPoint>
{ typedef cs::cartesian type; };
template<> struct dimension<QPoint> : boost::mpl::int_<2> {};
template<>
struct access<QPoint, 0>
{
static double get(QPoint const p)
{
return p.x;
}
static void set(QPoint p, double const& value)
{
p.x = value;
}
};
template<>
struct access<QPoint, 1>
{
static double get(QPoint const p)
{
return p.y;
}
static void set(QPoint p, double const& value)
{
p.y = value;
}
};
}
}
} // namespace boost::geometry::traits
//
// Linestring specialization
//
// boost range metafunctions
// ( modified the example from: http://www.boost.org/doc/libs/1_48_0/libs/range/doc/html/range/reference/extending/method_2.html )
namespace boost
{
//
// Specialize metafunctions. We must include the range.hpp header.
// We must open the 'boost' namespace.
//
template<>
struct range_iterator< QLineString >
{
typedef std::vector<QPoint>::iterator type;
};
template<>
struct range_const_iterator< QLineString >
{
//
// Remark: this is defined similar to 'range_iterator'
// because the 'Pair' type does not distinguish
// between an iterator and a const_iterator.
//
typedef std::vector<QPoint>::const_iterator type;
};
template<>
struct range_size< QLineString >
{
typedef std::size_t type;
};
} // namespace boost
//
// The required functions. These should be defined in
// the same namespace as 'Pair', in this case
// in namespace 'Foo'.
//
inline std::vector<QPoint>::iterator range_begin( QLineString& x )
{
return x.points.begin();
}
inline std::vector<QPoint>::const_iterator range_begin( const QLineString& x )
{
return x.points.begin();
}
inline std::vector<QPoint>::iterator range_end( QLineString& x )
{
return x.points.end();
}
inline std::vector<QPoint>::const_iterator range_end( const QLineString& x )
{
return x.points.end();
}
inline std::vector<QPoint>::size_type
range_size( const QLineString& x )
{
return x.points.size();
}
// specialize linestring
//BOOST_GEOMETRY_REGISTER_LINESTRING(QLineString);
namespace boost {
namespace geometry {
namespace traits {
template
<>
struct tag<QLineString>
{
typedef linestring_tag type;
};
}
}
}
int main()
{
QPoint pP1 = QPoint(1,2);
QPoint pP2 = QPoint(2,3);
QPoint pP3 = QPoint(3,4);
QLineString pls = QLineString();
pls.cw = true;
pls.points.push_back(pP1);
pls.points.push_back(pP2);
pls.points.push_back(pP3);
std::cout << boost::geometry::wkt(pls) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.