Finding the side of a cube intersecting a line using the shortest computation Let a rectangular cuboid of size $(L_x, L_y, L_z)$ be located with one corner at the origin and aligned with the $(x,y,z)$ axes. Let $\overrightarrow{sr}$ be a vector from point $s$ to point $r$. $s$ is known to be outside the cube, while $r$ could be inside or outside the cube but neither is on the faces. The goal is to find if the line goes through the cube and which side it intersects first. If the line is on one of the planes, it falls under the definition of not going through the cube, namely, we are only interested on a single point crossing.
This can be done easily enough by parameterizing the line by $s+(r-s)t=p(t)$. The trivial calculation can be time-consuming. We need to intersect the line with 6 planes, constraint the results to the limits of the square on each plane, and finally determine the first encounter.
The thing is, due to the convenient location of the cube, this computation may contain many shortcuts. for example, if we define 6 normals directed outwards of the rectangular we can eliminate the last part by noticing the sign of the dot product between the line and each of the normals. A positive dot product indicates it is a first encounter while a negative one means it is not. Due to the relatively easy 6 normals, this dot multiplication is reduced to analyzing the sign of a single component in the direction vector of the line.
I wish to implement this in a program with a minimum amount of calculations. I am looking for the shortest, closed solution to such a problem under these assumptions.
I am looking for mathematical assumptions\trics\accelerations that can shorten the calculation and not programming optimization techniques.
 A: Let's move to 2d for a while. Assume we have a rectangle in a plane $\mathbb{R}^2$ and denote its edges as $a$, $b$, $c$ and $d$. Now, consider a ray from a point $r$ lying outside the rectangle. When looking for an edge the ray intersects as first we can narrow down the set of candidates to at most two of edges. In other words, from any point on the outside we can see no more than two edges of the rectangle. For example:

Any ray casted from a point $r$ cannot intersect edges $a$ and $d$ before intersecting one of $b$ or $c$.
Depending on the position of the point $r$ we can determine the edge(s) we should examine. Thus the outside of the rectangle can be divided into $8$ regions such that each of them defines all visible edges of the rectangle from any point in that region:

Note that these regions are delimited by extended sides of the rectangle (what about points on these extensions?). In general, testing a point to which of these regions it belongs needs to engage trigonometry, but in our case - when sides of the rectangle are aligned with axes - it is enough to compare the components.
In your three-dimensional analogue of this problem the similar reasoning still works, and things are only slightly more complicated. Instead of $8$ regions we now have $26$ such regions and for exactly eight of them we cannot limit the number of visible sides to two.
