Here’s a little CUDA “object” that allows one to test whether two pieces of line segments intersect in 2D euclidean space. I tried to keep it branchless. It is loosely based on a code snippet I found in a game programming forum.

Although I’ve been doing CUDA for more than 2 years I am still convinced that someone else may know some more optimal way of solving things. Would you do it any differently?

My walls are going to be defined in constant memory, whereas my ray coordinates will dynamically depend on the threadIdx/blockIdx. I will also express the ray as a segment and simply use the intersection test below.

/**
* A CUDA "object" to represent a wall segment.
* Implemented without a constructor for maximum toolkit compatibility.
*/
typedef struct _segment
{
/** intersection test */
__host__ __device__ bool intersects(const _segment &seg) const
{
float2 u = make_float2(b.x - a.x, b.y - a.y); // b - a
float2 v = make_float2(seg.b.x-seg.a.x, seg.b.y-seg.a.y); // seg.b-seg.a
float2 w = make_float2(a.x-seg.a.x, a.y-seg.a.y); // a - seg.a
// branchless version
float D = u.x * v.y - u.y * v.x;
float s = v.x * w.y - v.y * w.x;
float t = u.x * w.y - u.y * w.x;
return (fabs(D) > 0.000001f) && // parallel test
(s >= 0 && s <= D) && // inside first segment?
(t >= 0 && t <= D); // inside the other segment?
//float2 intersectPt = a + u * (s/d); //or b + v * (t/d);
}
public:
float2 a; /**< wall start point */
float2 b; /**< wall end point */
} segment;

What about the case when they are parallel, but overlapping (or at least a portion of them overlaps?)

And also, if you are planning on using this on pre-fermi hardware, I’d use the __fmul and __fadd instructions when calculating D, s and t, because I’ve experienced the fmad mucking up my intersection tests before.

What about the case when they are parallel, but overlapping (or at least a portion of them overlaps?)

And also, if you are planning on using this on pre-fermi hardware, I’d use the __fmul and __fadd instructions when calculating D, s and t, because I’ve experienced the fmad mucking up my intersection tests before.

Ok, I am not considering this case because it is very rare.

But I noticed that my code was not giving correct results for D < 0. How would I do this more elegantly? This is really ugly now.

return ( (D > +0.000001f) && // parallel test
(s >= 0 && s <= D) && // inside first segment?
(t >= 0 && t <= D) ) // inside the other segment?
||
( (D < -0.000001f) && // parallel test
(s >= D && s <= 0) && // inside first segment?
(t >= D && t <= 0) ); // inside the other segment?

Ok, I am not considering this case because it is very rare.

But I noticed that my code was not giving correct results for D < 0. How would I do this more elegantly? This is really ugly now.

return ( (D > +0.000001f) && // parallel test
(s >= 0 && s <= D) && // inside first segment?
(t >= 0 && t <= D) ) // inside the other segment?
||
( (D < -0.000001f) && // parallel test
(s >= D && s <= 0) && // inside first segment?
(t >= D && t <= 0) ); // inside the other segment?

You could at the cost of two divisions (use __fdividef), divide s and t by D and then you only need to check if s and t are between 0 and 1.

if (fabs(D) > .000001f) {
s = __fdividef(s, D);
t = __fdividef(t, D);
return (s >= 0 && s <= 1) && (t >= 0 && t <=1);
}
else {
//you can choose to add code to handle the parallel intersecting case or not
//but I've found that eventually you'll hit it, so why not add the code
//since this branch will almost never be taken, you're not really incurring
//a penalty by its existince
}

Wanted to add that if you really don’t care about the else branch, then you don’t need the if either. If s or t becomes NAN due to division by 0, the comparisons will fail and it will return false.

You could at the cost of two divisions (use __fdividef), divide s and t by D and then you only need to check if s and t are between 0 and 1.

if (fabs(D) > .000001f) {
s = __fdividef(s, D);
t = __fdividef(t, D);
return (s >= 0 && s <= 1) && (t >= 0 && t <=1);
}
else {
//you can choose to add code to handle the parallel intersecting case or not
//but I've found that eventually you'll hit it, so why not add the code
//since this branch will almost never be taken, you're not really incurring
//a penalty by its existince
}

Wanted to add that if you really don’t care about the else branch, then you don’t need the if either. If s or t becomes NAN due to division by 0, the comparisons will fail and it will return false.