Hello @inevestenko,
thank you for reaching out to us. Please note that we require users to add information relevant to solving a question to their topics with tags. You can read more about tags in the Forum Guidelines. I have already added the Python tag for you, which I did assume from the context and prior postings of yours to be the programming environment. Please add the missing version and OS tags, since we will need them if we decide to provide example code. Please note also that the scope of support is limited to Cinema 4D SDK questions as stated in our forum guidelines. Your question is effectively a math and algorithmic question. Which is therefore out of scope of support. You might get community answers here, but we, Maxon, cannot help you through every step of the way.
That aside, a few points:
- Computing the closest distance between two arbitrary geometries, e.g., a spline and a polygon object as you give as an example, can be quite complex.
- Simplifying this to the distance between a point and an arbitrary polygon object will help you to understand this and must be implemented even if you want to do point one in the end.
- You do not define what you consider to be the closest distance. For your example image you could ask for the shortest distance between the center of the plane gizmo and any of the center points of the polygons in the cube. But you could also ask for the two points contained in the plane gizmo and the cube object which form the shortest line-segment connecting these. And there are many other variants of this in between.
The Cinema 4D Python SDK does not offer you much which would help you here (the C++ SDK on the other hand has quite a few interfaces which would help). What you could use in Python is c4d.utils.ViewportSelect
, although that would very much be an unintended usage. It is intended to carry out selections in a viewport. When you align the view transform, the camera, of a viewport with your plane gizmo, you could (try to) use it to retrieve the closest polygon. The method to retrieve the closest polygon, ViewportSelect.GetNearestPolygon
does operate however with screen coordinates, as it is intended to handle mouse inputs, which makes this route unpractical.
The most simplistic interpretation of computing the distance would be to simply pick a query point p, then compute the polygon center points by taking the arithmetic mean of their vertices. The distance for each (p, center point) tuple is then the length of the vector difference of the two, e.g., dist_3 = (center_point_polygon_3 - p).GetLength()
. To find the closest point in a polygon, i.e., not just pick the center point, you will have to do point plane projections. Assuming a mesh of triangles, project the query point onto plane defined by the triangle, using the dot product. Bring the projected point into barycentric coordinates and clamp them to the range [0, 1] which then will be the closest point q. Convert the barycentric coordinates back to cartesian and compute the distance between p and q. As before, do that for all polygons to find the closest one.
Helpful in this context might also be c4d.utilsNiehgbor
. The more advanced acceleration data structures to make such computations fast, e.g., the Binary Space Partitioning stuff, are not exposed in the Python SDK. Finally, I must stress again, that your question is out of scope of support, which is why we cannot provide any further help here.
Cheers,
Ferdinand