c01_custom_point_example.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Custom point Example
  10. #include <iostream>
  11. #include <boost/geometry/algorithms/distance.hpp>
  12. #include <boost/geometry/algorithms/make.hpp>
  13. #include <boost/geometry/geometries/register/point.hpp>
  14. #include <boost/geometry/strategies/strategies.hpp>
  15. #include <boost/geometry/io/dsv/write.hpp>
  16. // Sample point, defining three color values
  17. struct my_color_point
  18. {
  19. double red, green, blue;
  20. };
  21. // Sample point, having an int array defined
  22. struct my_array_point
  23. {
  24. int c[3];
  25. };
  26. // Sample point, having x/y
  27. struct my_2d
  28. {
  29. float x,y;
  30. };
  31. // Sample class, protected and construction-time-only x/y,
  32. // Can (of course) only used in algorithms which take const& points
  33. class my_class_ro
  34. {
  35. public:
  36. my_class_ro(double x, double y) : m_x(x), m_y(y) {}
  37. double x() const { return m_x; }
  38. double y() const { return m_y; }
  39. private:
  40. double m_x, m_y;
  41. };
  42. // Sample class using references for read/write
  43. class my_class_rw
  44. {
  45. public:
  46. const double& x() const { return m_x; }
  47. const double& y() const { return m_y; }
  48. double& x() { return m_x; }
  49. double& y() { return m_y; }
  50. private:
  51. double m_x, m_y;
  52. };
  53. // Sample class using getters / setters
  54. class my_class_gs
  55. {
  56. public:
  57. const double get_x() const { return m_x; }
  58. const double get_y() const { return m_y; }
  59. void set_x(double v) { m_x = v; }
  60. void set_y(double v) { m_y = v; }
  61. private:
  62. double m_x, m_y;
  63. };
  64. // Sample point within a namespace
  65. namespace my
  66. {
  67. struct my_namespaced_point
  68. {
  69. double x, y;
  70. };
  71. }
  72. BOOST_GEOMETRY_REGISTER_POINT_3D(my_color_point, double, cs::cartesian, red, green, blue)
  73. BOOST_GEOMETRY_REGISTER_POINT_3D(my_array_point, int, cs::cartesian, c[0], c[1], c[2])
  74. BOOST_GEOMETRY_REGISTER_POINT_2D(my_2d, float, cs::cartesian, x, y)
  75. BOOST_GEOMETRY_REGISTER_POINT_2D_CONST(my_class_ro, double, cs::cartesian, x(), y())
  76. BOOST_GEOMETRY_REGISTER_POINT_2D(my_class_rw, double, cs::cartesian, x(), y())
  77. BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(my_class_gs, double, cs::cartesian, get_x, get_y, set_x, set_y)
  78. BOOST_GEOMETRY_REGISTER_POINT_2D(my::my_namespaced_point, double, cs::cartesian, x, y)
  79. int main()
  80. {
  81. // Create 2 instances of our custom color point
  82. my_color_point c1 = boost::geometry::make<my_color_point>(255, 3, 233);
  83. my_color_point c2 = boost::geometry::make<my_color_point>(0, 50, 200);
  84. // The distance between them can be calculated using the cartesian method (=pythagoras)
  85. // provided with the library, configured by the coordinate_system type of the point
  86. std::cout << "color distance "
  87. << boost::geometry::dsv(c1) << " to "
  88. << boost::geometry::dsv(c2) << " is "
  89. << boost::geometry::distance(c1,c2) << std::endl;
  90. my_array_point a1 = {{0}};
  91. my_array_point a2 = {{0}};
  92. boost::geometry::assign_values(a1, 1, 2, 3);
  93. boost::geometry::assign_values(a2, 3, 2, 1);
  94. std::cout << "color distance "
  95. << boost::geometry::dsv(a1) << " to "
  96. << boost::geometry::dsv(a2) << " is "
  97. << boost::geometry::distance(a1,a2) << std::endl;
  98. my_2d p1 = {1, 5};
  99. my_2d p2 = {3, 4};
  100. std::cout << "float distance "
  101. << boost::geometry::dsv(p1) << " to "
  102. << boost::geometry::dsv(p2) << " is "
  103. << boost::geometry::distance(p1,p2) << std::endl;
  104. my_class_ro cro1(1, 2);
  105. my_class_ro cro2(3, 4);
  106. std::cout << "class ro distance "
  107. << boost::geometry::dsv(cro1) << " to "
  108. << boost::geometry::dsv(cro2) << " is "
  109. << boost::geometry::distance(cro1,cro2) << std::endl;
  110. my_class_rw crw1;
  111. my_class_rw crw2;
  112. boost::geometry::assign_values(crw1, 1, 2);
  113. boost::geometry::assign_values(crw2, 3, 4);
  114. std::cout << "class r/w distance "
  115. << boost::geometry::dsv(crw1) << " to "
  116. << boost::geometry::dsv(crw2) << " is "
  117. << boost::geometry::distance(crw1,crw2) << std::endl;
  118. my_class_gs cgs1;
  119. my_class_gs cgs2;
  120. boost::geometry::assign_values(cgs1, 1, 2);
  121. boost::geometry::assign_values(cgs2, 3, 4);
  122. std::cout << "class g/s distance "
  123. << boost::geometry::dsv(crw1) << " to "
  124. << boost::geometry::dsv(crw2) << " is "
  125. << boost::geometry::distance(cgs1,cgs2) << std::endl;
  126. my::my_namespaced_point nsp1 = boost::geometry::make<my::my_namespaced_point>(1, 2);
  127. my::my_namespaced_point nsp2 = boost::geometry::make<my::my_namespaced_point>(3, 4);
  128. std::cout << "namespaced distance "
  129. << boost::geometry::dsv(nsp1) << " to "
  130. << boost::geometry::dsv(nsp2) << " is "
  131. << boost::geometry::distance(nsp1,nsp2) << std::endl;
  132. return 0;
  133. }