Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How to get disatnce from one point to Line in 3D space using openGL math library glm?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Newbie Newbie
    Join Date
    Oct 2017
    Posts
    2

    Lightbulb How to get disatnce from one point to Line in 3D space using openGL math library glm?

    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 Click image for larger version. 

Name:	distance.jpg 
Views:	27 
Size:	6.9 KB 
ID:	2507.

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

    Code :
    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.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,526
    Use the GLM_GTX_closest_point module.

  3. #3
    Newbie Newbie
    Join Date
    Oct 2017
    Posts
    2
    Quote Originally Posted by GClements View Post
    Use the GLM_GTX_closest_point 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?
    Last edited by fochka; 10-05-2017 at 11:09 PM.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,526
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •