[Math] Check if a general point is inside a given cylinder

euclidean-geometrygeometry

For a particular purpose, I want to define a cylinder in 3D space and go through a list of given 3D points and tell if the point is inside or outside the cylinder volume.
I can define the cylinder by specifying 2 points along the axis, and the radius of the cylinder.

A (x1, y1, z1 )
B (x2, y2, z2 )
and radius = R

right now what I'm doing is that I find the vector AB, connecting A and B by

AB = A - B

then calculate the shortest distance from each point to the vector AB, if the distance is less than R, the point is inside.

The problem with this method is that it only works if either A or B is the origin.

for example, If I try to find the points inside the cylinder connecting

p1 ( 100,10,20)
p2 ( 100,-10,20)

we get the points inside the cylinder ( 0,20,0) [ which is actually the cylinder formed by ( 0,0,0) and (0,20,0) ]

certainly, I'm missing something, can anyone point it out?

N.B: For some complicated reason, I can't use an auxiliary coordinate system or shift the origin. What I'm looking for is some pure mathematical expression ( if it exists ), which can take the particulars of the cylinder and the required point and give if it is inside or outside.

similar to Empty2's answer on this question

Best Answer

Solution

Consider the line coordinates with direction $\boldsymbol{e} = \boldsymbol{r}_B-\boldsymbol{r}_A$ and moment $\boldsymbol{m} = \boldsymbol{r}_A \times \boldsymbol{r}_B$. These two vectors represent the infinite line between ${A}$ and ${B}$.

A point ${P}$ with position $\boldsymbol{r}_{P}$ lies in the cylinder between $\boldsymbol{A}$ and $\boldsymbol{B}$ and radius $R$ if:

  1. Distance of $P$ to line $AB$ is equal or less than $R$ $$d = \frac{\|\boldsymbol{m} + \boldsymbol{e}\times\boldsymbol{r}_{P}\|}{\|\boldsymbol{e}\|}\leq R$$

    or the alternate formulation of the above

    $$d = \frac{\| \boldsymbol{e}\times\left(\boldsymbol{r}_{P}-\boldsymbol{r}_{A}\right) \|}{\|\boldsymbol{e}\|}\leq R$$

  2. Closest point $Q$ on line to $P$ is $$\boldsymbol{r}_{Q}=\boldsymbol{r}_{P}+\frac{\boldsymbol{e}\times\left(\boldsymbol{m}+\boldsymbol{e}\times\boldsymbol{r}_{P}\right)}{\|\boldsymbol{e}\|^{2}}$$

  3. The barycentric coordinates of $Q$ $\begin{pmatrix}w_{A} & w_{B}\end{pmatrix}$ such that $\boldsymbol{r}_{Q}=w_{A}\boldsymbol{r}_{A}+w_{B}\boldsymbol{r}_{B}$ are

    $$\begin{array}{c} w_{A}=\frac{\|\boldsymbol{r}_{Q}\times\boldsymbol{r}_{B}\|}{\|\boldsymbol{m}\|}\\ w_{B}=\frac{\|\boldsymbol{r}_{Q}\times\boldsymbol{r}_{A}\|}{\|\boldsymbol{m}\|} \end{array}$$ This works only when $\|\boldsymbol{m}\| >0$, or the cylinder axis does not go through the origin.

  4. Check that point $Q$ lies between $A$ and $B$ by making sure the barycentric coordinates are between 0 and 1

    $${\rm inside}=\left(w_{A}\ge0\right){\rm and}\left(w_{A}\le1\right){\rm and}\left(w_{B}\ge0\right){\rm and}\left(w_{B}\le1\right)$$


Example

The end points of a cylinder with $R=1.2$ are located at $$ \begin{aligned} \boldsymbol{r}_A & = \pmatrix{3\\0\\0} \\ \boldsymbol{r}_B & = \pmatrix{0\\7\\0} \end{aligned} $$

The target point has coordinates $$ \boldsymbol{r}_P = \pmatrix{1\\2\\0} $$

  1. The infinite line is described by $$ \begin{aligned} \boldsymbol{e} & = \pmatrix{-3 \\ 7 \\ 0} \\ \boldsymbol{m} & = \pmatrix{0 \\ 0 \\ 21} \end{aligned} $$
  2. The distance of the target point to the line is $$ d = \frac{ \pmatrix{0 \\ 0 \\ 21} + \pmatrix{-3 \\ 7 \\ 0} \times \pmatrix{1\\2\\0}}{ \| \pmatrix{-3 \\ 7 \\ 0} \|} = \frac{\| \pmatrix{0 \\ 0 \\ 8} \|}{\sqrt{58}} = \tfrac{8}{\sqrt{58}} = 1.0505 $$

  3. Point on line closest to target point is $$ \boldsymbol{r}_Q = \pmatrix{1\\2\\0} + \frac{\pmatrix{-3 \\ 7 \\ 0} \times \pmatrix{0 \\ 0 \\ 8}}{58} = \pmatrix{1\\2\\0} + \frac{ \pmatrix{56\\24\\0}}{58} = \pmatrix{\tfrac{57}{29} \\ \tfrac{70}{29} \\ 0} = \pmatrix{1.9655 \\ 2.41379 \\ 0} $$

  4. Barycentric coordinates are $$ \begin{aligned} w_A & = \frac{ \| \pmatrix{\tfrac{57}{29} \\ \tfrac{70}{29} \\ 0} \times \pmatrix{0\\7\\0} \|}{\| \pmatrix{0\\0\\21} \|} = \frac{ \| \pmatrix{0\\0\\ \frac{399}{29}} \|}{21} = \tfrac{19}{29} = 0.6552 \\ w_B & = \frac{ \| \pmatrix{\tfrac{57}{29} \\ \tfrac{70}{29} \\ 0} \times \pmatrix{3\\0\\0} \|}{\| \pmatrix{0\\0\\21} \|} = \frac{ \| \pmatrix{0\\0\\ -\frac{210}{29}} \|}{21} = \tfrac{10}{29} = 0.34483 \end{aligned}$$

  5. Check that $w_A \ge 0\;\checkmark$, $w_A \le 1\;\checkmark$, $w_B \ge 0\;\checkmark$, $w_B \le 1\;\checkmark$.


Edit 1

As noted this method falls flat when the axis of the cylinder goes through the origin. This is because the barycentric calculation above is actually a shortcut of a more general method. To solve for the barycentric coordinates in the general case, you need to solve the following 2×2 system

$$ \left| \begin{matrix}1 + \boldsymbol{r}_A \cdot \boldsymbol{r}_A & 1 + \boldsymbol{r}_A \cdot \boldsymbol{r}_B \\ 1 + \boldsymbol{r}_A \cdot \boldsymbol{r}_B & 1 + \boldsymbol{r}_B \cdot \boldsymbol{r}_B \end{matrix} \right| \pmatrix{w_A \\ w_B} = \pmatrix{1 + \boldsymbol{r}_Q \cdot \boldsymbol{r}_A \\ 1 + \boldsymbol{r}_Q \cdot \boldsymbol{r}_B } $$

where $\cdot$ is the dot product of two vectors resulting in a scalar value.