• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6 
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Custom pointer-to-point example
12 
13 #include <iostream>
14 
15 #include <boost/foreach.hpp>
16 
17 #include <boost/geometry/algorithms/distance.hpp>
18 #include <boost/geometry/algorithms/length.hpp>
19 #include <boost/geometry/algorithms/make.hpp>
20 #include <boost/geometry/algorithms/intersection.hpp>
21 #include <boost/geometry/geometries/geometries.hpp>
22 #include <boost/geometry/geometries/point_xy.hpp>
23 #include <boost/geometry/geometries/register/linestring.hpp>
24 #include <boost/geometry/strategies/strategies.hpp>
25 
26 BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::vector)
27 BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::deque)
28 
29 // Sample point, having x/y
30 struct my_point
31 {
32     double x,y;
33 };
34 
35 
36 namespace boost { namespace geometry { namespace traits {
37 
38 template<> struct tag<my_point>
39 { typedef point_tag type; };
40 
41 template<> struct coordinate_type<my_point>
42 { typedef double type; };
43 
44 template<> struct coordinate_system<my_point>
45 { typedef cs::cartesian type; };
46 
47 template<> struct dimension<my_point> : boost::mpl::int_<2> {};
48 
49 template<>
50 struct access<my_point, 0>
51 {
getboost::geometry::traits::access52     static double get(my_point const& p)
53     {
54         return p.x;
55     }
56 
setboost::geometry::traits::access57     static void set(my_point& p, double const& value)
58     {
59         p.x = value;
60     }
61 };
62 
63 template<>
64 struct access<my_point, 1>
65 {
getboost::geometry::traits::access66     static double get(my_point const& p)
67     {
68         return p.y;
69     }
70 
setboost::geometry::traits::access71     static void set(my_point& p, double const& value)
72     {
73         p.y = value;
74     }
75 };
76 
77 }}} // namespace boost::geometry::traits
78 
79 
80 
main()81 int main()
82 {
83     typedef std::vector<my_point*> ln;
84     ln myline;
85     for (float i = 0.0; i < 10.0; i++)
86     {
87         my_point* p = new my_point;
88         p->x = i;
89         p->y = i + 1;
90         myline.push_back(p);
91     }
92 
93     std::cout << boost::geometry::length(myline) << std::endl;
94 
95     typedef boost::geometry::model::d2::point_xy<double> point_2d;
96     typedef boost::geometry::model::box<point_2d> box_2d;
97     box_2d cb(point_2d(1.5, 1.5), point_2d(4.5, 4.5));
98 
99     // This will NOT work because would need dynamicly allocating memory for point* in algorithms:
100     // std::vector<ln> clipped;
101     //boost::geometry::intersection(cb, myline, clipped);
102 
103     // This works because outputs to a normal struct point, no point*
104     typedef boost::geometry::model::linestring<point_2d> linestring_2d;
105     std::vector<linestring_2d> clipped;
106     boost::geometry::strategy::intersection::liang_barsky<box_2d, point_2d> strategy;
107     boost::geometry::detail::intersection::clip_range_with_box<linestring_2d>(cb,
108                     myline, std::back_inserter(clipped), strategy);
109 
110 
111     std::cout << boost::geometry::length(clipped.front()) << std::endl;
112 
113     // free
114     BOOST_FOREACH(my_point* p, myline)
115     {
116         delete p;
117     }
118 
119     return 0;
120 }
121