PDA

fochka
10-05-2017, 06:15 AM
I have set of points (3D). What i am doing is getting a line that connect first and last point of data set. Then trying to find index of point that has max distance from line. Just to express my problem 2507.

I wrote a function for that which uses Eigen library. Which is as below

std::vector<Eigen::Vector3f> points; // input set of points

typedef Eigen::ParametrizedLine<float, 3> Line3;
static Line3 start_to_end = Line3::Through(points[0], points.back());
float max_dist_squared = 0.0f;
size_t max_idx = 0;
for (size_t i = 0; i<points.size(); ++i)
{
float dist2 = start_to_end.distance(points[i]);
if (dist2 > max_dist_squared)
{
max_dist_squared = dist2;
max_idx = i;
std::cout << "\n Distance is " << dist2 << " and i " << i << std::endl;
}
}

PROBLEM : I want to write same method using GLM. I mean to ask is any method that help me to find line and distance from a point. Or how can i convert above code Using GLM library.

GClements
10-05-2017, 04:00 PM
Use the GLM_GTX_closest_point (https://glm.g-truc.net/0.9.8/api/a00183.html) module.

fochka
10-05-2017, 11:03 PM
Use the GLM_GTX_closest_point (https://glm.g-truc.net/0.9.8/api/a00183.html) module.

What i understood is, this method will return a point on a line connecting two end point? Then i need to find distance from given point and calculated point. or is any another way of doing that? Please correct me if i am wrong, this method's second and third argument is two end point of the line. And first argument is point, from where closed point on line will be calculated.

Am i in right direction?

In addition to that, i just wanted to know will it work correctly if projection point lies outside of the two given line point?

GClements
10-06-2017, 06:35 AM
You're correct in your interpretation of the arguments.

However, the function specifically clamps the point to the endpoints, i.e. it returns the closest point on the line segment rather than on the projected line.

GLM doesn't have an equivalent function for the projected line. For that, I'd suggest taking the existing code (in glm/gtx/closest_point.inl) and simply removing the endpoint checks.

You can then use glm::distance() to find the distance between the initial point and the closest point on the line.

Note that if you're working in 2D, it will be far more efficient to first find the implicit equation for the line; then each distance can be calculated as a linear function of the point's coordinates.