Sunday, May 19, 2013

Ray tracing using time to collision

Ray tracing tutorial

1 Motivation


Imagine a 2D grid world with obstacles and walls in the form of blocks. Imagine a robot with a laser sensor inside this world. I am given the position of the robot and the angle at which laser is emitted, I want to know the grid cell with which this laser collides and the exact position where it collides.

I had a look at Stage library, but I was not able to understand the code in world.cc properly. It cites a book called Graphics Gems IV to which I didn’t have access. So to understand the algorithm, I derived it from first principles, which you will see is not very difficult.

2 Notation


Let the size of grid cells be denoted by (Δx, Δy) and the unit vector along the ray be denoted by (dx,dy). The distance from the nearest grid walls in the direction of ray be denoted by (ex,ey). Assume that the ray is as given in Fig 1 and intersects consecutive grid lines at P1 and P2.


PIC
Figure 1: A ray intersecting with grid lines in a grid world

3 Derivation


For ray tracing we want to find all the grid cell from which the ray crosses. Since we can do it recursively, support we are at point P1 and want to find point P2. The first question to be answered is whether P2 lies on the Y-axes or X-axes. In the case of given diagram, P2 lies on the Y-axes, or we have taken one step along X-axes. However, if the slope was greater than 45 than the line would have crossed the gridline x = 0.5 earlier than y = 1.5. This can be easily detected by the sign of ey2.
ey2 = ey1 ex1 dy dx  (1)  (2)
We take a step along X-axes when ey2 > 0 or
ey1 ex1 dy dx>   0  (3) ey1 dy > ex1 dx  Assuming dy > 0  (4)
Now, we just need a recursive formula for ext and eyt. Let’s compute ex2 and ey2 for this case when we know that ey1 dy > ex1 dx .

ey2 = ey1 ex1 dy dx  (5) ey2 dy = ey1 dy ex1 dx  (6)
Aren’t we lucky that we got a formula of ey2 dy in terms of ey1 dy and ex1 dx ? Both the terms we already knew and needed to compute for comparison. Also note that ex2 dx is simply Δx dx .

Also doesn’t the fraction ex dx looks something meaningful? Note that ex dx is proportional to ex dxdt which is makes me think it in terms of distance to next gridline upon velocity. This term is nothing but time to collision to nearest gridline. Lets denote this terms as tx = ex dx. We can also think of Tx = Δx dx as maximum possible time to collision when travelling along X axes. Using this simpler notation our recursive formula are:

So the complete recursive formula can be written as:
txt+1,tyt+1 = Tx,tyt txt  if tyt > txt txt tyt,Ty   otherwise  (7)
Extension to 3D case is trivial:

txt+1,tyt+1,tzt+1 = Tx,tyt txt,tzt txt  if tyt > txt tzt > txt txt tyt,Ty,tzt tyt   if txt > tyt tzt > tyt txt tzt,tyt tzt,Tz   otherwise  (8)

4 Algorithm


------------------------------------------------------------------------------------------
   Data:  Initial point (x ,y ), Direction of ray (dx, dy), Cell size (Δx, Δy ), Collision
                         0  0
          checking function IsCellOccupied (i,j), Maximum   range r
   Result:  Collision cell (i,j) and position of collision (xc, yc)
   Let us assume  that grid lines are at integral multiples of cell size.;
   Initial cell (i,j ) ← (⌊ x0-⌋,⌊ y0⌋);
                        Δx    Δy
   Direction of movement  (Δi, Δj ) ← (sgn(dx ),sgn (dy));
   Initial error (ex,ey) ← ((i + Δi )Δx  − x0e, (j + Δj )Δy  − y0);
   Initial time  to collision (tx,ty) ← (edxx,dyy);
   Maximum    time  to collision (T ,T  ) ← (Δx-, Δy);
                              --x-r-y--    dx  dy
   Maximum    iterations n ←  ⌊min(Δx,Δy)⌋;
   while  n > 0 do
    |  n ←  n − 1;
    |  if t  < t then
    |   | x(i,j)y ←  (i + Δi,j);
    |   |
    |   | (tx,ty) ← (Tx,ty − tx);
    |  else
    |   | (i,j) ← (i,j + Δj );
    |   | (tx,ty) ← (tx − ty,Ty);
    |   --
    |  if|IsCellOccupied (i,j) then
    |   | (xc,yc) ← ((i + Δi)Δx  − txdx,(j + Δj )Δy −  tydy );
    |   --return  (i,j),(xc,yc)
------------------------------------------------------------------------------------------