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 point Example
12
13 #include <iostream>
14
15 #include <boost/geometry/algorithms/distance.hpp>
16 #include <boost/geometry/algorithms/make.hpp>
17 #include <boost/geometry/geometries/register/point.hpp>
18 #include <boost/geometry/strategies/strategies.hpp>
19 #include <boost/geometry/io/dsv/write.hpp>
20
21 // Sample point, defining three color values
22 struct my_color_point
23 {
24 double red, green, blue;
25 };
26
27 // Sample point, having an int array defined
28 struct my_array_point
29 {
30 int c[3];
31 };
32
33 // Sample point, having x/y
34 struct my_2d
35 {
36 float x,y;
37 };
38
39 // Sample class, protected and construction-time-only x/y,
40 // Can (of course) only used in algorithms which take const& points
41 class my_class_ro
42 {
43 public:
my_class_ro(double x,double y)44 my_class_ro(double x, double y) : m_x(x), m_y(y) {}
x() const45 double x() const { return m_x; }
y() const46 double y() const { return m_y; }
47 private:
48 double m_x, m_y;
49 };
50
51 // Sample class using references for read/write
52 class my_class_rw
53 {
54 public:
x() const55 const double& x() const { return m_x; }
y() const56 const double& y() const { return m_y; }
x()57 double& x() { return m_x; }
y()58 double& y() { return m_y; }
59 private:
60 double m_x, m_y;
61 };
62
63 // Sample class using getters / setters
64 class my_class_gs
65 {
66 public:
get_x() const67 const double get_x() const { return m_x; }
get_y() const68 const double get_y() const { return m_y; }
set_x(double v)69 void set_x(double v) { m_x = v; }
set_y(double v)70 void set_y(double v) { m_y = v; }
71 private:
72 double m_x, m_y;
73 };
74
75 // Sample point within a namespace
76 namespace my
77 {
78 struct my_namespaced_point
79 {
80 double x, y;
81 };
82 }
83
84
85
BOOST_GEOMETRY_REGISTER_POINT_3D(my_color_point,double,cs::cartesian,red,green,blue)86 BOOST_GEOMETRY_REGISTER_POINT_3D(my_color_point, double, cs::cartesian, red, green, blue)
87 BOOST_GEOMETRY_REGISTER_POINT_3D(my_array_point, int, cs::cartesian, c[0], c[1], c[2])
88 BOOST_GEOMETRY_REGISTER_POINT_2D(my_2d, float, cs::cartesian, x, y)
89 BOOST_GEOMETRY_REGISTER_POINT_2D_CONST(my_class_ro, double, cs::cartesian, x(), y())
90 BOOST_GEOMETRY_REGISTER_POINT_2D(my_class_rw, double, cs::cartesian, x(), y())
91 BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(my_class_gs, double, cs::cartesian, get_x, get_y, set_x, set_y)
92
93 BOOST_GEOMETRY_REGISTER_POINT_2D(my::my_namespaced_point, double, cs::cartesian, x, y)
94
95
96 int main()
97 {
98 // Create 2 instances of our custom color point
99 my_color_point c1 = boost::geometry::make<my_color_point>(255, 3, 233);
100 my_color_point c2 = boost::geometry::make<my_color_point>(0, 50, 200);
101
102 // The distance between them can be calculated using the cartesian method (=pythagoras)
103 // provided with the library, configured by the coordinate_system type of the point
104 std::cout << "color distance "
105 << boost::geometry::dsv(c1) << " to "
106 << boost::geometry::dsv(c2) << " is "
107 << boost::geometry::distance(c1,c2) << std::endl;
108
109 my_array_point a1 = {{0}};
110 my_array_point a2 = {{0}};
111 boost::geometry::assign_values(a1, 1, 2, 3);
112 boost::geometry::assign_values(a2, 3, 2, 1);
113
114 std::cout << "color distance "
115 << boost::geometry::dsv(a1) << " to "
116 << boost::geometry::dsv(a2) << " is "
117 << boost::geometry::distance(a1,a2) << std::endl;
118
119 my_2d p1 = {1, 5};
120 my_2d p2 = {3, 4};
121 std::cout << "float distance "
122 << boost::geometry::dsv(p1) << " to "
123 << boost::geometry::dsv(p2) << " is "
124 << boost::geometry::distance(p1,p2) << std::endl;
125
126 my_class_ro cro1(1, 2);
127 my_class_ro cro2(3, 4);
128 std::cout << "class ro distance "
129 << boost::geometry::dsv(cro1) << " to "
130 << boost::geometry::dsv(cro2) << " is "
131 << boost::geometry::distance(cro1,cro2) << std::endl;
132
133 my_class_rw crw1;
134 my_class_rw crw2;
135 boost::geometry::assign_values(crw1, 1, 2);
136 boost::geometry::assign_values(crw2, 3, 4);
137 std::cout << "class r/w distance "
138 << boost::geometry::dsv(crw1) << " to "
139 << boost::geometry::dsv(crw2) << " is "
140 << boost::geometry::distance(crw1,crw2) << std::endl;
141
142 my_class_gs cgs1;
143 my_class_gs cgs2;
144 boost::geometry::assign_values(cgs1, 1, 2);
145 boost::geometry::assign_values(cgs2, 3, 4);
146 std::cout << "class g/s distance "
147 << boost::geometry::dsv(crw1) << " to "
148 << boost::geometry::dsv(crw2) << " is "
149 << boost::geometry::distance(cgs1,cgs2) << std::endl;
150
151 my::my_namespaced_point nsp1 = boost::geometry::make<my::my_namespaced_point>(1, 2);
152 my::my_namespaced_point nsp2 = boost::geometry::make<my::my_namespaced_point>(3, 4);
153 std::cout << "namespaced distance "
154 << boost::geometry::dsv(nsp1) << " to "
155 << boost::geometry::dsv(nsp2) << " is "
156 << boost::geometry::distance(nsp1,nsp2) << std::endl;
157
158
159 return 0;
160 }
161