I am going through the list of vertices of a segment, stored in a vector of Eigen::Vector2D and then I am computing the distance from a point stored in Eigen::vector2d to a each segment of my polygon.

I calculate the distance using my own function and then I calculate it using boost function distance.
I declare a segment of type  boost::geometry::model::segment<double>  in which I add my two vertices that make the segment.

Then I display the result for each vertices and I get the same result for every segment using boost.

I hope it's more clear now.

Juliette

double closestDistance(std::vector<Eigen::Vector2d> polygon2D, Eigen::Vector2d P)
{
double minDist = DBL_MAX;
for( unsigned int i = 0 ; i < polygon2D.size() ; i++ )
{
Eigen::Vector2d A = polygon2D.at(i);
Eigen::Vector2d B;
if( i == polygon2D.size()-1 )
{
B = polygon2D.at(0);
}
else
{
B = polygon2D.at(i+1);
}
Eigen::Vector2d seg = B-A;

double U = (((P-A)*seg)+((P-A)*seg))/(seg.norm()*seg.norm());

double distance;
if( U < 0 )
{
distance = (A-P).norm();
}
else if( U > 1 )
{
distance = (B-P).norm();
}
else
{
Eigen::Vector2d Ph = A + U*seg;
distance = (Ph-P).norm();
}

// testing boos distance here
xsegment_t segment;
geom::append(segment,geom::make_point(A,A));
geom::append(segment,geom::make_point(B,B));
double distance2 = geom::distance(segment,geom::make_point(P,P));

std::cout << "Distance comparison : " << distance << " / " << distance2 << std::endl;

if( distance < minDist )
{
minDist = distance;
}
}
return minDist;
}

Result obtained:

Distance comparison : 36.6926 / 4.37843

Distance comparison : 19.5823 / 4.37843

Distance comparison : 20.6683 / 4.37843

Distance comparison : -nan / 4.37843     --> two vertices are at the same point

Distance comparison : 12.2823 / 4.37843

2016-04-21 3:51 GMT+02:00 Adam Wulkiewicz :
Hi Juliette,

Juliette Pera wrote:
Dear all,

I am currently trying to use the boost library in order to measure the shortest distance between a point and a list of segments.

I am declaring a segment to which I append the two points of my segment and the I calculate the distance from my point P to the segment.

xsegment_t segment;
geom::append(segment,geom::make_point(A,A));
geom::append(segment,geom::make_point(B,B));
double distance2 = geom::distance(segment,geom::make_point(P,P));

The issue is that I measure every time the same distance even if the segment is different.

Does anyone knows from where that issue can come from?

Sorry, but I don't have enough data to say what's the problem. It'd be the best if you could share some small but complete example showing this issue.

Does the algorithm return this result when geometries of types bg::model::segment and bg::model::point are passed?
What coordinate system do you use?

Or do you use your own types adapted to Segment and Point concepts?
If the answer is yes, then I'd start from checking if the adaptation was done correctly.

Regards,