使用 boost geometry 调整几何对象模型的问题

Problems in adapting a geometry object model using boost geometry

我尝试按照 https://www.boost.org/doc/libs/1_75_0/libs/geometry/doc/html/geometry/examples/example__adapting_a_legacy_geometry_object_model.html 给出的示例来实现以下不可变的无指针变体。

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/linestring.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/range.hpp>
#include <vector>

class Point
{
 public:
   Point( double x, double y ) : m_x( x ), m_y( y ) {}
   double getX() const { return m_x; }
   double getY() const { return m_y; }

 private:
   double m_x, m_y;
};

using TyPoints = std::vector< Point >;


class LineString
{
 public:
   LineString( TyPoints points ) : m_points( std::move( points ) ) {}
   const TyPoints& getPoints() const { return m_points; }

 private:
   TyPoints m_points;
};


// Provide read only Boost.Range for LineString
namespace boost
{
   template<>
   struct range_const_iterator< LineString >
   {
      typedef std::vector< Point >::const_iterator type;
   };
}

inline std::vector< Point >::const_iterator range_begin( const LineString& ls )
{
   return ls.getPoints().cbegin();
}

inline std::vector< Point >::const_iterator

   range_end( const LineString& ls )
{
   return ls.getPoints().cend();
}

BOOST_GEOMETRY_REGISTER_POINT_2D_CONST( Point, double, cs::cartesian, getX(), getY() );
BOOST_GEOMETRY_REGISTER_LINESTRING( LineString );


int main( int argc, char* argv[] )
{
   const LineString lineString( { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } } );

   boost::geometry::length( lineString );
   return 0;
}

但是,注册为 boost geometry 失败,我不知道如何解决这个问题。

In file included from /usr/include/boost/geometry/core/closure.hpp:24,
                 from /usr/include/boost/geometry/geometry.hpp:25,
                 from /usr/include/boost/geometry.hpp:17,
                 from main.cpp:1:
/usr/include/boost/range/value_type.hpp: In instantiation of ‘struct boost::range_value<LineString>’:
/usr/include/boost/geometry/core/point_type.hpp:82:59:   required from ‘struct boost::geometry::core_dispatch::point_type<boost::geometry::linestring_tag, LineString>’
/usr/include/boost/geometry/core/coordinate_type.hpp:58:62:   required from ‘struct boost::geometry::core_dispatch::coordinate_type<boost::geometry::linestring_tag, LineString>’
/usr/include/boost/geometry/core/coordinate_type.hpp:92:25:   required from ‘struct boost::geometry::coordinate_type<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:45:17:   required from ‘struct boost::geometry::resolve_strategy::default_length_result<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:55:8:   required from ‘struct boost::geometry::resolve_variant::default_length_result<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:82:8:   required from ‘struct boost::geometry::default_length_result<LineString>’
/usr/include/boost/geometry/algorithms/length.hpp:280:1:   required by substitution of ‘template<class Geometry> typename boost::geometry::default_length_result<Geometry>::type boost::geometry::length(const Geometry&) [with Geometry = LineString]’
main.cpp:62:40:   required from here
/usr/include/boost/range/value_type.hpp:26:12: error: no type named ‘type’ in ‘struct boost::range_iterator<LineString, void>’
   26 |     struct range_value : iterator_value< typename range_iterator<T>::type >
      |            ^~~~~~~~~~~
In file included from /usr/include/boost/geometry/core/coordinate_dimension.hpp:23,
                 from /usr/include/boost/geometry/geometry.hpp:26,
                 from /usr/include/boost/geometry.hpp:17,
                 from main.cpp:1:
/usr/include/boost/geometry/core/point_type.hpp: In instantiation of ‘struct boost::geometry::core_dispatch::point_type<boost::geometry::linestring_tag, LineString>’:
/usr/include/boost/geometry/core/coordinate_type.hpp:58:62:   required from ‘struct boost::geometry::core_dispatch::coordinate_type<boost::geometry::linestring_tag, LineString>’
/usr/include/boost/geometry/core/coordinate_type.hpp:92:25:   required from ‘struct boost::geometry::coordinate_type<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:45:17:   required from ‘struct boost::geometry::resolve_strategy::default_length_result<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:55:8:   required from ‘struct boost::geometry::resolve_variant::default_length_result<LineString>’
/usr/include/boost/geometry/strategies/default_length_result.hpp:82:8:   required from ‘struct boost::geometry::default_length_result<LineString>’
/usr/include/boost/geometry/algorithms/length.hpp:280:1:   required by substitution of ‘template<class Geometry> typename boost::geometry::default_length_result<Geometry>::type boost::geometry::length(const Geometry&) [with Geometry = LineString]’
main.cpp:62:40:   required from here
/usr/include/boost/geometry/core/point_type.hpp:82:59: error: no type named ‘type’ in ‘struct boost::range_value<LineString>’
   82 |     typedef typename boost::range_value<Linestring>::type type;
      |                                                           ^~~~
main.cpp: In function ‘int main(int, char**)’:
main.cpp:62:40: error: no matching function for call to ‘length(const LineString&)’
   62 |    boost::geometry::length( lineString );
      |                                        ^
In file included from /usr/include/boost/geometry/algorithms/detail/equals/implementation.hpp:38,
                 from /usr/include/boost/geometry/algorithms/equals.hpp:26,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/get_piece_turns.hpp:21,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/buffered_piece_collection.hpp:40,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp:34,
                 from /usr/include/boost/geometry/algorithms/buffer.hpp:41,
                 from /usr/include/boost/geometry/geometry.hpp:54,
                 from /usr/include/boost/geometry.hpp:17,
                 from main.cpp:1:
/usr/include/boost/geometry/algorithms/length.hpp:280:1: note: candidate: ‘template<class Geometry> typename boost::geometry::default_length_result<Geometry>::type boost::geometry::length(const Geometry&)’
  280 | length(Geometry const& geometry)
      | ^~~~~~
/usr/include/boost/geometry/algorithms/length.hpp:280:1: note:   substitution of deduced template arguments resulted in errors seen above
In file included from /usr/include/boost/geometry/algorithms/detail/equals/implementation.hpp:38,
                 from /usr/include/boost/geometry/algorithms/equals.hpp:26,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/get_piece_turns.hpp:21,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/buffered_piece_collection.hpp:40,
                 from /usr/include/boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp:34,
                 from /usr/include/boost/geometry/algorithms/buffer.hpp:41,
                 from /usr/include/boost/geometry/geometry.hpp:54,
                 from /usr/include/boost/geometry.hpp:17,
                 from main.cpp:1:
/usr/include/boost/geometry/algorithms/length.hpp:306:1: note: candidate: ‘template<class Geometry, class Strategy> typename boost::geometry::default_length_result<Geometry>::type boost::geometry::length(const Geometry&, const Strategy&)’
  306 | length(Geometry const& geometry, Strategy const& strategy)
      | ^~~~~~
/usr/include/boost/geometry/algorithms/length.hpp:306:1: note:   template argument deduction/substitution failed:
main.cpp:62:40: note:   candidate expects 2 arguments, 1 provided
   62 |    boost::geometry::length( lineString );
      |                                        ^

有人可以告诉我如何解决这个问题吗?非常感谢!

编译器抱怨缺少这个:

/usr/include/boost/geometry/core/point_type.hpp:82:59: 错误:‘struct boost::range_value’中没有名为‘type’的类型 82 | typedef typename boost::range_value::type 类型;

添加它会修复编译错误:

// Provide read only Boost.Range for LineString
namespace boost
{
   template<>
   struct range_const_iterator< LineString >
   {
       typedef std::vector< Point >::const_iterator type;
   };
   //adding the missing part
   template<>
   struct range_value< LineString >
   {
      typedef Point type;
   };
}