The linestring example shows how linestrings can be declared and used and shows some more algorithms. One of the important concepts of the Generic Geometry Library is that it is totally built upon the standard library, using the standard containers such as std::vector.
A linestring is, as explained elsewhere in this documentation, not much more than a vector of points. Most algorithms run on linestrings, but can also run on any iterator pair. And all algorithms on std::vector can be used on geometry::linestring.
This documentation illustrates the simplify algorithm and the intersection algorithm with some pictures.
The simplify algorithm simplifies a linestring. Simplification means that the less important points are removed from the line and that the points that are most important for the shape of a line are kept. Simplification is done using the well known Douglas Peucker algorithm. The library user can specify the distance or tolerance, which indicates how much the linestring should be simplified.
The blue line is the original linestring; the red line is the simplified line which has one point less. In geographical applications simplification can reduce a linestring to its basic form containing only 10% of its original points.
The intersection algorithm intersects two geometries which each other, delivering a third geometry. In the case of the example a linestring is intersected with a box. Intersection with a box is often called a clip. The image below illustrates the intersection.
The yellow line is intersected with the blue box. The intersection result, painted in red, contains three linestrings.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
#include <boost/geometry/geometry.hpp>
template<typename P>
inline void translate_function(P& p)
{
p.x(p.x() + 100.0);
}
template<typename P>
struct scale_functor
{
inline void operator()(P& p)
{
p.x(p.x() * 1000.0);
p.y(p.y() * 1000.0);
}
};
template<typename Point>
struct round_coordinates
{
coordinate_type m_factor;
inline round_coordinates(coordinate_type const& factor)
: m_factor(factor)
{}
template <int Dimension>
inline void round(Point& p)
{
coordinate_type c = boost::geometry::get<Dimension>(p) / m_factor;
int rounded = c;
boost::geometry::set<Dimension>(p, coordinate_type(rounded) * m_factor);
}
inline void operator()(Point& p)
{
round<0>(p);
round<1>(p);
}
};
int main(void)
{
using namespace boost::geometry;
linestring_2d ls;
ls.push_back(make<point_2d>(1.1, 1.1));
point_2d lp;
std::cout << dsv(ls) << std::endl;
box_2d b;
std::cout << dsv(b) << std::endl;
std::cout <<
"length: " <<
length(ls) << std::endl;
std::cout << "number of points 1: " << ls.size() << std::endl;
std::cout << "number of points 2: " << boost::size(ls) << std::endl;
std::cout <<
"number of points 3: " <<
num_points(ls) << std::endl;
point_2d p(1.9, 1.2);
std::cout << "distance of " << dsv(p)
<<
" to line: " <<
distance(p, ls) << std::endl;
std::cout << "distance: " << d << std::endl;
const double c[][2] = { {3.1, 3.1}, {4.9, 1.1}, {3.1, 1.9} };
std::cout << "appended: " << dsv(ls) << std::endl;
{
std::vector<point_2d> v;
std::cout
<< "as vector: "
<< dsv(v)
<< std::endl;
}
std::cout << "reversed: " << dsv(ls) << std::endl;
std::vector<point_2d> pv(ls.begin(), ls.end());
std::cout <<
"length: " <<
length(pv) << std::endl;
{
point_2d last = ls.back(), first = ls.front();
ls.push_back(last);
ls.insert(ls.begin(), first);
}
std::cout << "extra duplicate points: " << dsv(ls) << std::endl;
{
linestring_2d ls_copy;
ls = ls_copy;
std::cout << "uniquecopy: " << dsv(ls) << std::endl;
}
linestring_2d ls_simplified;
std::cout << "simplified: " << dsv(ls_simplified) << std::endl;
{
linestring_2d lscopy = ls;
std::for_each(lscopy.begin(), lscopy.end(), translate_function<point_2d>);
std::cout << "modified line: " << dsv(lscopy) << std::endl;
}
box_2d cb(point_2d(1.5, 1.5), point_2d(4.5, 2.5));
std::vector<linestring_2d> clipped;
std::vector<std::vector<point_2d> > vector_out;
std::cout << "clipped output as vector:" << std::endl;
for (std::vector<std::vector<point_2d> >::const_iterator it
= vector_out.begin(); it != vector_out.end(); ++it)
{
std::cout << dsv(*it) << std::endl;
}
std::cout << "Convex hull:" << dsv(hull) << std::endl;
line3.push_back(make<point_3d>(1,2,3));
line3.push_back(make<point_3d>(4,5,6));
line3.push_back(make<point_3d>(7,8,9));
std::cout <<
"3D: length: " <<
length(line3) <<
" line: " << dsv(line3) << std::endl;
std::cout << "JSON: "
<< dsv(ls, ", ", "[", "]", ", ", "[ ", " ]")
<< std::endl;
return 0;
}