Get closest line segment

Hi! I have a polyData consisting of lines. When I click on the screen, how to get the closest line/cell.

My idea is the following.

  1. Get line or two points of the ray representing downwards line from camera (how? where to look for example?)
  2. Get polyData lines: let lines = polyData.getLines()
  3. Iterate lines (how?)
  4. Get two end points (how?)
  5. Calculate distance from the ray to particular line. I think to port DistanceBetweenLineSegments from C++

Please advise if there is a better solution

  1. When you say “downwards”, I assume you mean you want a ray starting at the camera’s position and intersecting some point corresponding to your click. You will need to convert your click coordinates into a 3D position, and, in conjunction with the camera’s world position, you get a line. You can look at the Picker’s code for creating such a line/ray.
  2. lines.getData() returns an array in the form [N, v1, v2, ..., vN, ...], where N is the number of points in the line, and v_i is a vertex ID. You can get the point coordinates with lines.getPoints().getPoint(v_i). Note this line is really a polyline, so getting your actual line is a matter of picking 2 adjacent vertices in the array.
  3. Do you mean the endpoints of the line? That’s defined by the line that you’re looking at.
  4. There is a distanceToLine method in DataModel/Line, but I don’t think we have distance between line segments. That would be a useful function to port.