# Distance / Penetration distance of segment to/in a cone

Given a segment defined by two points in $$\Bbb{R}^3$$, $$P_0$$ and $$P_1$$ and a cone defined by an origin $$C_0$$ and axis $$\vec{C}$$ and a cone angle $$\phi$$ how to find either the minimum distance of the segment to the cone if it is not intersecting or the penetration depth of the segment if it is intersecting.

To clarify what distance and penetration depth means:

1. Distance is the distance one must offset a non intersecting cone so that it is just touching the segment.
2. Penetration depth is the distance one must offset an intersecting cone so that it no longer intersecting and is just touching the segment.

Thus 1 and 2 are actually identical and can be considered a signed distance. The result required is the offset from the reference cone so that the segment is outside the cone and just touching.

The cone is only interesting on the positive side of the $$\vec{C}$$

• For 1. I imagine that you mean the minimum offset distance, whatever the direction. May 31 at 16:12
• Yes that is what I mean. The answer could be positive or negative. May 31 at 16:14
• Currently I use a variation on point to cone distance and find the minimum over the segment using a brent solver. But that is fast for a single segment and slow when I have a million segments. Hoping for a direct solution if it exists. May 31 at 16:16
• There exists most likely a direct solution. Have you first looked at the problem where the segment is replaced with an (infinite) line? May 31 at 16:18
• That would probably be simpler but there are obviously cases when the infinite line penetrates the infinite cone and no offsetting will change that. That condition is when the angle between the line and the cone axis is less thant $\phi/2$ May 31 at 16:20

The idea is to project $$P_0P_1$$ along $$\vec C$$ until it gets in contact with the cone. To reduce the complexity:

• Translate and rotate the scene so that $$C_0$$ is in the origin and $$\vec C$$ is pointing along positive $$z$$ direction.
• Rotate the scene around $$z$$ so that the transformed $$P_0$$ and $$P_1$$ have equal $$y$$ coordinates. Rotation angle can be found from normal of segment projection onto XoY plane.

You should get this scene when view from the top .

Transformed segment lies in YoZ-parallel plane. This plane is creating hyperbolic section on conic surface. We can find the shortest distance along $$z$$ between hyperbola and line segment ,

which should give the answer. Closest points would either be segment ends or a witness point of hyperbola vertex projection onto the segment. There are some corner cases to be handled though like segment being parallel to generatrix of a cone or dealing with axial section.

• @bradgonesurfing I voted to reject your "gif" edit as I felt it would be better if you suggested this edit in a comment to the answer. It is quite a significant edit so I feel that pabababa should have the main say in it. Jun 2 at 12:53