# SOLVEDCompute distance from a point to a mesh

Hi guys,
I would like to compute the distance from a point to a mesh while editing a document.

• Requested method:
- [IN] start point
- [IN] direction vector
- [IN] radius (area to look)
- [OUT] position of a hit if it exist (or a distance to the starting point).

• Current plugin structure implementation:

MessagePlugin objects' scene coordinates.

• Example:

Here I would like to know the first element which is hit by the blue line from the camera.
I took here the camera to have a visualization, however it can be a random start point.

I considered few options:

1. Geraycollider: gives the distance if you know the target object, however I don't know it.
2. VideoPostData plugin to get VolumeData and then use TraceGeometry method. However, it seems that it is not called and the time but only when a rendering is done.

Do you have any hint or recommended method to do that?

Best

Hello @user168462,

Thank you for reaching out to us. You clearly tried to be precise in your question and we appreciate this, but there are still some ambiguities we must resolve first.

Compute distance from a point to a mesh
[...] which is hit by the blue line from the camera [...]

You talk first about points and meshes, and then about points and lines, and show a bunch of null objects and joints. Intersection testing (sets of) line segments and (sets of) triangles requires different approaches.

In general, one should also add that intersection testing two lines in 3D (or similar cases as a line segment and a ray) is very unlikely to yield an intersection due to the vastness of 3D space and floating-point precision. What one usually does there is define a tolerance, where one then searches the closest point pair on the two line segments, tests if the distance of that pair is below that tolerance, and then considers these two lines to intersect (although they technically do not).

Connected two that is your fuzziness regarding what you want to compute: An intersection or the projection? Your thread-title implies that you are interested in projections (i.e., computing the closest point p on some geometry G for a query point q). However, later you switch to terminology which more implies ray-casting/intersection testing as you give a ray direction and use words as along. But ray casting will not "compute [the] distance from a point to a mesh" but the intersection point(s) (when there is a hit) for the geometry and the ray. The line segment formed by the query point and intersection points has a length, but it would be pure coincidence if it were equal to the shortest distance (projection) of the query point and geometry.

When you are interested in the shortest distance between a query point q and some geometry G, ray casting is not the correct method, you must project q onto G. Your example mentions some radius to look in, which I interpret as sort of a search window for the ray casting, but this is not only very error prone or very computationally complex as you might have to ray-cast millions of times to fill that window, but also unnecessary, you can just project the point.

This is all slightly out of scope of support, as these are more principal techniques rather than things in our APIs.

1. `GeRayCollider` is ray-triangle intersection testing helper class with some light optimization built into it. You cannot carry out ray-line-segment intersections with it. If you want to intersection test multiple objects, you have to intersection test them one by one.
2. `VolumeData::TraceGeometry` is bound to the rendering process.
3. There is also the class `ViewportSelect` with which you can "pick" objects in a viewport.

Added to that is that there is no real geometry in your screenshot, there are joints, null objects, and camera object. None of which provides any explicit discrete geometry as polygons or line segments. You can of course "write stuff around that" which then fills in the data for the visualizations these objects use. But you cannot just intersection test a joint object or a null object which is displayed as a circle.

Cheers,
Ferdinand

A point-something projection will also give you a "hit-point" in some sense. The question is if you want to find the closest point for a point on some geometry (projection), or a point on a geometry for a point and a ray (intersection). `GeRayCollider` realizes ray-triangle intersection testing for `PolygonObject` instances. For multiple objects you must initialize multiple collider instances (or reinitialize one instance). But you cannot intersection test a null, a spline, or joint object as shown in your screenshot as they do not contain any triangles. When you are just interested in the closest point in some discrete geometry, you can use `ViewportSelect` or the more modern maxon API equivalent `maxon::DistanceQueryInterface` with a slightly different and more abstract purpose. `ViewportSelect` can also be used to pick the object in a scene which is closest to a point (for the cursor for example) for further testing with other methods.
In `maxon::GeometryUtilsInterface` you will find some basic building blocks for writing something of your own.