Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Created March 18, 2023 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdel101/3cd79b7aa0a92944a1f3b1f8c092ecab to your computer and use it in GitHub Desktop.
Save chrisdel101/3cd79b7aa0a92944a1f3b1f8c092ecab to your computer and use it in GitHub Desktop.
Ray Box Inter
int rayBoxIntersection(float P0[3], float V[3], float ts[2], int sides, int n)
{
/* Intersect the line with the CT cube to find
two intersection points, t0 and t1, corresponding to the entry and exit points respectively.
Notes: the six sides of the CT cube are defined
in the world coordinates by:
x = 0;
x = 127;
y = 0;
y = 127;
z = 0;
z = 127.
*/
if(n == 2 || sides == 0)
{
return n;
}
switch (sides)
{
// x = 0;
case 6:
{
// /Example: for side x = 0
// x(t) = P0[0] + V[0] * t = 0.0
// printf("%f ", -P0[0]);
// printf("%f ", -V[0]);
float t = -P0[0]/V[0];
// t can be solved using the above equation.
// Then, substitute t into the following two equations
// to compute y and z:
// y(t) = p0[1] + V0[1] * t
float y_t = P0[1] + (V[1] * t);
// z(t) = P0[2] + V0[2] * t
float z_t = P0[2] + (V[2] * t);
// The y and z need to be checked against the rectangular
// boundaries (this is so much easier than the general
// boundary checking that you did in the Assignment 2):
if(y_t > 0 && y_t < ROWS && z_t > 0 && z_t < SLCS)
{
// Save the t value into ts[] and update n.
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
// x = 127;
case 5:
{
float t = (127-P0[0])/V[0];
// y(t) = p0[1] + V0[1] * t
float y_t = P0[1] + (V[1] * t);
// z(t) = P0[2] + V0[2] * t
float z_t = P0[2] + (V[2] * t);
if(y_t > 0 && y_t < ROWS && z_t > 0 && z_t < SLCS)
{
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
// y = 0;
case 4:
{
float t = -P0[1]/V[1]; //y
float x_t = P0[0] + (V[0] * t); //x
// z(t) = P0[2] + V0[2] * t
float z_t = P0[2] + (V[2] * t); //z
if(x_t > 0 && x_t < COLS && z_t > 0 && z_t < SLCS)
{
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
// y = 127;
case 3:
{
float t = (127-P0[1])/V[1]; //y
float x_t = P0[0] + (V[0] * t); //x
// z(t) = P0[2] + V0[2] * t
float z_t = P0[2] + (V[2] * t); //z
if(x_t > 0 && x_t < COLS && z_t > 0 && z_t < SLCS)
{
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
case 2:
{
float t = -P0[2]/V[2]; //z
float x_t = P0[0] + (V[0] * t); //x
// z(t) = P0[2] + V0[2] * t
float y_t = P0[1] + (V[1] * t); //y
if(x_t > 0 && x_t < COLS && y_t > 0 && y_t < ROWS)
{
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
case 1:
{
float t = (127-P0[2])/V[2]; //z
float x_t = P0[0] + (V[0] * t); //x
// z(t) = P0[2] + V0[2] * t
float y_t = P0[1] + (V[1] * t); //y
if(x_t > 0 && x_t < COLS && y_t > 0 && y_t < ROWS)
{
ts[n] = t;
// run on next side
return rayBoxIntersection(P0, V, ts, sides-1, n+1);
}
break;
}
default:
{
puts("Error in switch: Wrong value");
exit(0);
}
}
// run on next side - don't update n
return rayBoxIntersection(P0, V, ts, sides-1, n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment