简体   繁体   中英

Calculating distance from point to voronoi cell with boost

Good night. Has anyone encountered a similar problem?

Constructing Voronoi diagram has not caused problems. Voronoi cell is a polygon, at least for me. The library also allows you to find the distance from a point to a polygon. But the library function does not want to work with the cell. The compiler produces something in Elvish. Joke. In short, the compiler output can not help me. Is there a way to make a polygon from the cell?

Voronoi diagram is constructed on vpoints. The program should calculate the distance from the qpoints element to the corresponding cell. Here is my code:

#include <iostream>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/polygon/voronoi.hpp>
namespace bg = boost::geometry;

using boost::polygon::voronoi_diagram;
typedef voronoi_diagram<double>::cell_type cell_type;
typedef voronoi_diagram<double>::edge_type edge_type;
typedef voronoi_diagram<double>::vertex_type vertex_type;
typedef boost::polygon::point_data<double> point_type;

using namespace std;

int main() {

  vector< point_type > vpoints;
  vpoints.push_back(point_type(0.0, 0.0));
  vpoints.push_back(point_type(0.0, 4.0));
  vpoints.push_back(point_type(4.0, 4.0));
  vpoints.push_back(point_type(4.0, 0.0));
  vpoints.push_back(point_type(2.0, 2.0));

  vector< point_type > qpoints;
  qpoints.push_back(point_type(0.0, 0.0));
  qpoints.push_back(point_type(0.0, 2.0));
  qpoints.push_back(point_type(3.0, 3.0));
  qpoints.push_back(point_type(5.0, 5.0));
  qpoints.push_back(point_type(5.0, 5.0));

  voronoi_diagram<double> vd;
  construct_voronoi(vpoints.begin(), vpoints.end(), &vd);

  for (int i = 0; i < qpoints.size(); i++) {
    for (voronoi_diagram<double>::const_cell_iterator it = vd.cells().begin();
         it != vd.cells().end(); ++it) {
      if (i == it->source_index()) {
        cout << "v[i]=(" << vpoints[i].x() << "," << vpoints[i].y() << ")\t";
        cout << "q[i]=(" << qpoints[i].x() << "," << qpoints[i].y() << ")\t";
        cout << "Distance=";
        cout << bg::distance(qpoints[i], *it) << endl;
        cout << endl;
        break;
      }
    }
  } 

  return 0;
}

The message is

boost_1_57_0/boost/geometry/core/geometry_id.hpp|37 col 5| error: no matching function for call to ‘assertion_failed(mpl_::failed************ (boost::geometry::core_dispatch::geometry_id<void>::NOT_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE::************)(mpl_::assert_::types<void, mpl_::na, mpl_::na, mpl_::na>))’

Which is the NOT_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE assert. It happens doing geometry_id for reverse_dispatch :

/*!
\brief Meta-function returning the id of a geometry type
\details The meta-function geometry_id defines a numerical ID (based on
    boost::mpl::int_<...> ) for each geometry concept. A numerical ID is
    sometimes useful, and within Boost.Geometry it is used for the
    reverse_dispatch metafuntion.
\note Used for e.g. reverse meta-function
\ingroup core
*/
template <typename Geometry>
struct geometry_id : core_dispatch::geometry_id<typename tag<Geometry>::type>
{};

The same warning is triggered when you do

 cout << distance(qpoints[i], qpoints[i]) << endl;

So the problem is that your point type is not a reqistered geometry. Including

 #include <boost/geometry/geometries/adapted/boost_polygon.hpp>

makes that compile, but of course

 cout << distance(qpoints[i], *it) << endl;

still fails, this time because const boost::polygon::voronoi_cell<double> isn't a known geometry type to Boost Geometry.

I'd suggest not mixing the libraries unless you know why you want to.

It looks to me that a voronoi cell can be more than just a single thing ( contains_segment() and contains_point() are indications). You may have to write some switching logic to handle the possible cases separately, and perhaps use euclidean_distance from Boost Polygon in the process (as opposed to boost::geometry::distance`)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM