Determining if a line passes through a rectangular prism

391 Views Asked by At

I am working on a programming project and I am trying to figure out when a line passes through a rectangular prism. I have learned about parametrization and how to figure out if a line passes through a plane but this time I need to be able to represent it with code which means I need to solve it completely different from what I am use to. For example I use to get problems in school where I had 2 points along with a planes equation which meant I had to turn those points into its parametric form before plugging them into the equation. I also solved some similar type questions with inequalities however those dont translate well into code.

So far I have a position vector, a directional vector, and the distance. Now turning this into its parametric form is very straight forward however I dont really know where to go from there. I know I can create a bounding box for the prism which will be defined by its min and max value for x, y, and z but I dont know what to do next.

In my specific problem I need to loop 3 times checking each time if the line passes through each prism. I included a 2d representation of what this may look like (you can ignore the red point at the bottom since I already compensated for that so my position is now at the top near the directional vector). The bold black line with an arrow represents my normalized direction vector and the dotted portion represents the distance I need to travel in that direction (for example 3 units). The red lines on my line represents the 3 sections I need to check if the line is passing through the prism.

enter image description here

Anyways I know I can set up a basic for loop to loop 3 times however I dont know what to include inside of that loop. How can I determine if each line segment passes through each prisms bounding box? I would appreciate any suggestions however I would highly appreciate any examples shown with code since I am mostly struggling with figuring out how to relay this mathematical concept into code.

1

There are 1 best solutions below

1
On BEST ANSWER

Sorry for the delay. I wanted to consider which of the many ways to accomplish this would be simplest in this case. Let $P$ be the "base" point on your line. $P = (20,62,18)$ in your example. Let $\mathbf n$ be the unit directional vector pointing along your line (if the directional vector does not have length $1$, then divide it by its length to get the unit directional vector). The line has parametric formula $$t \mapsto P + t\mathbf n$$

Let $B_0, \dots, B_7$ be the eight corners of the right-rectangular prism (a.k.a., a "box"). Find the closest $B_m$ to $P$ in the direction of $\mathbf n$. That is, the $B_m$ with the smallest value for $|(B_m - P)\cdot \mathbf n|$. If there is more than one such $B_m$, choose one of them. If the line intersects the box, it will be at one of the three faces meeting at this $B_m$. The other three faces cannot be intersected without the line also intersecting one of these three.

$B_m$ will have three neighboring corners $B_j, B_k, B_l$. Define $$\mathbf v_1 = B_j - B_m\\\mathbf v_2 = B_k - B_m\\\mathbf v_3 = B_l - B_m\\\mathbf w = P - B_m$$ and $$ w_1 = \mathbf w\cdot \mathbf v_1, \qquad w_2 = \mathbf w\cdot \mathbf v_2, \qquad w_3 = \mathbf w\cdot \mathbf v_3\\ n_1 = \mathbf n\cdot \mathbf v_1, \qquad n_2 = \mathbf n\cdot \mathbf v_2, \qquad n_3 = \mathbf n\cdot \mathbf v_3\\ s_1 = \mathbf v_1\cdot \mathbf v_1, \qquad s_2 = \mathbf v_2\cdot \mathbf v_2, \qquad s_3 = \mathbf v_3\cdot \mathbf v_3$$

The tests needed are

  • $(n_1 \ne 0$ and $0 \le w_2 - w_1 \frac{n_2}{n_1} \le s_2$ and $0 \le w_3 - w_1 \frac{n_3}{n_1} \le s_3)$, or
  • $(n_2 \ne 0$ and $0 \le w_1 - w_2 \frac{n_1}{n_2} \le s_1$ and $0 \le w_3 - w_2 \frac{n_3}{n_2} \le s_3)$, or
  • $(n_3 \ne 0$ and $0 \le w_1 - w_3 \frac{n_1}{n_3} \le s_1$ and $0 \le w_2 - w_3 \frac{n_2}{n_3} \le s_2)$.

The line intersects the box if and only if at least one of those three conditions is true.


Explanation:

Because the faces are all perpendicular to each other, the equations of the planes theses faces are given by $${\bf r \cdot v}_i = 0$$ for all points $B_m + \mathbf r$ on the plane through $B_m$ perpendicular to $\mathbf v_i$. Letting $\mathbf r = (P + t\mathbf n) - B_m = \mathbf w + t\bf n$, this gives$$t = \frac{-{\bf w\cdot v}_i}{{\bf n\cdot v}_i}\\\mathbf r = \mathbf w - \frac{{\bf w\cdot v}_i}{{\bf n\cdot v}_i}\bf n$$ for the intersection of the line with the plane of that face. (If ${\bf n\cdot v}_i = 0$, the line does not intersect that plane or else lies in it - but we can ignore that possibility, as the line will also intersect one of the other face planes.)

If it is on the plane perpendicular to $\mathbf v_i$, The point $\bf r$ will be within the face itself if: $$0 \le \mathbf r \cdot \mathbf v_n \le \mathbf v_n \cdot \mathbf v_n$$ for both of the indices $n \ne i$. I.e., if $$0 \le \left(\mathbf w - \frac{{\bf w\cdot v}_i}{{\bf n\cdot v}_i}\bf n\right)\cdot\mathbf v_n \le \mathbf v_n \cdot \mathbf v_n\\ 0 \le \mathbf w \cdot\mathbf v_n - \frac{{\bf w\cdot v}_i}{{\bf n\cdot v}_i}\bf n\cdot\mathbf v_n \le \mathbf v_n \cdot \mathbf v_n$$