How to check if two cylinders are overlapping I have two cylinders defined by their end points with a fixed radius $r$ of 0.01.
Say
$$
Cyl_1 = \left\{\begin{matrix}
p_{\rm start} = \left\{x_1, y_1, z_1 \right\} \\
p_{\rm end} = \left\{x_2, y_2, z_2 \right\}
\end{matrix}\right.
$$
$$
Cyl_2 = \left\{\begin{matrix}
p_{\rm start} = \left\{x_3, y_3, z_3 \right\} \\
p_{\rm end} = \left\{ x_4, y_4, z_4 \right\}
\end{matrix}\right.
$$
How can I check if they are overlapping in space?
How can this be extended to a cylinder being inserted into an ensamble of cylinders?
Best Regards
 A: You can begin by parameterizing the axes of the cylinders by the general formula:
$$ \boldsymbol{\gamma}(\lambda) = \boldsymbol{p_{start}} + \lambda (\boldsymbol{p_{end}-p_{start}}) $$
$$\lambda \in [0,1], \boldsymbol{\gamma}(\lambda) \in \mathbb{R}^3$$
A bit more concretely then your two axes will be:
$$\boldsymbol{\gamma_1}(\lambda_1) = \begin{bmatrix}x_1 + (x_2-x_1)\lambda_1 \\ y_1 + (y_2-y_1)\lambda_1 \\ z_1 + (z_2-z_1)\lambda_1\end{bmatrix}$$
$$\boldsymbol{\gamma_2}(\lambda_2) = \begin{bmatrix}x_3 + (x_4-x_3)\lambda_2 \\ y_3 + (y_4-y_3)\lambda_2 \\ z_3 + (z_4-z_3)\lambda_2\end{bmatrix}$$
And then you basically want to look for any $\lambda_1 , \lambda_2 \in [0,1]$ for which:
$$ ||\boldsymbol{\gamma_1}(\lambda_1) - \boldsymbol{\gamma_2}(\lambda_2)|| \le 2r $$
Indeed as @student91 mentioned in the comments, it comes down to finding the distance between two lines, cf.
Find shortest distance between lines in 3D
However, because the orientation of both cylinders I am assuming, is not necessarily the same (angles may be different) this method can give you a false positive. After finding a candidate for intersection, you will have to find the cross section (aka circle) corresponding to $\lambda_1$ and $\lambda_2$ and ensure that indeed there exists a point which is common to both circles. Adding diagram to explain it more clearly:
               
Note that the red line may be of length less than $2r$ and yet the circles that belong to both end points do not intersect, which is why you need this additional check.
Perhaps someone can come up with a more efficient and cleverer method :)
P.S. Nice applet to play around with intersection of two cylinders, with some interesting information.
