views:

1606

answers:

5

I have a ray, I need to find the closest line segment that it hits. I think it's possible to do this in O(log n) time if I sort the line segments first, but I can't remember how to sort them... I think some sort of tree would work best, but how do I sort them by both start and end point? I would also like fast insertions into this data structure if possible.

There's lots of code for one ray vs one line segment, but I need something for one ray vs many line segments... I don't know what terms to google for.

A link to an appropriate article is good, C++ code is even better. Thanks! :)

PS: The line segments are actually the edges of a non-self-intersecting polygon, sorted in CCW order... but I think there may be some advantage to sorting them in a different fashion?

This is all 2D.


On second thought, I'm not entirely sure this is possible. Some sort of spatial partitioning might help, but otherwise, I can't think of any way to sort the lines so that they could be compared with an arbitrary ray.

+1  A: 

How are you certain that you'll hit any of them? If they're lines, it's unlikely.

If it's really a polygon (i.e. planar) that you're trying to test, the usual way to do this sort of thing is intersect with the plane first, then test that point (in the 2d coordinates) for inside/outside polygon.

Maybe I misunderstood what you're actually doing.

In general accelerating intersections with complex figures is done with spatial partitioning (and then techniques like mailboxing, if your tests are expensive).

[Update: I misread the original intent] You can still use (2d) spatial partitioning but the overhead may not be worth it. Individual test are cheap, if your polys aren't complicated it might be cheaper to just walk them. Hard to say from description.

simon
I can guarantee the ray will hit at least one line segment. The ray is cast from a vertex towards the interior of the polygon. (This is 2D)
Mark
If you knew the ray is always going to be cast towards the same point in the polygon, you could sort the edges by angle. Your ray also has an angle, so it should be possible to do a log(N) binary search.
Martin Konicek
+1  A: 

Are you looking for scanline/Active Edge Table based methods? You can take a look at the Wikipedia entry for Scanline Rendering or search the Graphics Gems directory for the algorithms (mostly C, but some C++ code as well).

dirkgently
At first glance, it looks like this only works because the polygons/line-segments are sorted by y-order and and the scanlines are strictly horizontal. My rays can be cast in any direction... this might be a major problem.
Mark
@Mark: If you could give us the big picture of what you are trying to do (e.g. polygon filling) then we may be able to point out some specialized algorithms.
dirkgently
+1  A: 

Keep in mind that sorting is an O(n log n) operation at best. You may be better off just checking each individually.

rlbond
That's fine with me. My entire algorithm will be O(n^2 log n) at best, and I need to cast a lot of rays...
Mark
Sorting is not an O(N log N) operation.Sorting using only bi-elelement transitive compares is, but not sorting in general.
SPWorley
+2  A: 

You could take a bounding box of the polygon (min-max x,y coordinates) and build a grid inside the box. Then, for each cell, remember all lines that cross the cell.

Find an intesection like this:

  • Find out which cell the ray hits first (O(1))
  • Use Grid traversal algorithm to "draw" a ray through the grid. When you hit non-empty cell, check all its lines, check if intersection is inside the cell and pick the closest intersection. If all intersections are outside the cell, continue (this is O(grid length)).

You could also make the grid hierarchical (ie. quadtree - a tree you were asking for), and walk it using the same algorithm. This is done in raytracing in 3D - This is O(sqrt(N))


Or, use the approach I did in my raytracer:

  • Build a quadtree containing the lines (building quadtree is desribed in the article) - you split nodes (=areas) if they contain too many objects into 4 sub-nodes (sub-areas)
  • Collect all leaf nodes of the quadtree that are hit by the ray:

    Compute ray-rectangle intersection (not hard) for the root. If the root is hit by the ray, proceed with its children recursively.

The cool thing about this is that when a tree node is not hit, you've skipped processing whole subtree (potentially a large rectangular area).

In the end, this is equivalent to traversing the grid - you collect the smallest cells on the ray's path, and then test all objects in them for intersection. You just have to test all of them and pick the closest intersection (so you explore all lines on ray's path).

This is O(sqrt(N)).

In grid traversal, when you find an intersection, you can stop searching. To achieve this with quadtree traversal, you would have to seach the children in the right order - either sort the 4 rect intersections by distance or cleverly traverse the 4-cell grid (an we are back to traversal).

This is just a different approach, comparatively same difficult to implement I think, and works well (I tested it on real data - O(sqrt(N))). Again, you would only benefit from this approach if you have at least a couple of lines, when the polygon has 10 edges the benefit compared to just testing all of them would be little I think.

Martin Konicek
No, the O(log n) I was thinking of was for horizontal or vertical line segments only I believe. Bresenham's algorithm looks like it could miss intersections though, can't it? It doesn't "fill" every cell it enters. Otherwise, this looks like a good approach.
Mark
You are right, grid traversal should be done by algorithm mentioned here http://www.devmaster.net/articles/raytracing_series/part4.php, look at section "Grid traversal". Note that you would probably only benefit if your polygon has many (~hundred) edges, as they show in the article.
Martin Konicek
Well, there probably won't he hundreds of edges, but the same edges might get used thousands of times. This looks like the best answer on what I thought was a dead question, so I'll give you the check ;) Thanks.
Mark
Thanks :) I voted +1 for your question, because I find it iteresting. If there will be only a few edges, then each ray-test will be very fast, but the structure won't help you speed up series of tests as a whole - each of them will be taken individually, so it's O(#rays*sqrt(#edges)). Hmm, interesting.. You could do the ray-tests in parallel to benefit from multicore CPUs since the tests are independent.
Martin Konicek
If you're interested, this question was about making this algorithm http://mnbayazit.com/406/bayazit more efficient. Related to: http://stackoverflow.com/questions/694108/decomposition-to-convex-polygons
Mark
A: 

Ask for clarification, is this correct?

  • You have a dynamic set of line segments L.
  • Query: given any point (x,y) and and any direction of the ray from this point, you want to determine the closest line in L (if any) ?

So the point (x,y) is not fix? (It could be any point, and any direction?)

jacob
That is correct, Jacob. The direction is given by another point though, not an angle, if that's relevant. Several rays will be compared against the same set of line segments, so I figure it may be worthwhile to perform some preprocessing on the lines if it helps.
Mark
Perhaps I should also specify that we're probably only looking at about 20 line segements, and 5 rays on average, but there is no hard upperbound. The reason this needs to be efficient is because its part of a recursive algorithm that can get pretty costly. (One of my implementations took about a minute to process ~13 lines, and beyond that you don't even want to try).
Mark
Even a brute-force implementation using 20 lines and 5 rays should take a few milliseconds? How come yours takes so long?
jacob
By brute force I mean: given a ray, check for all possible line segments if it intersects it, and determine the one with minimum distance. This should have runtime O(r*n) for r rays and n line segments.
jacob
could you give an outline, how you did the check?
jacob
You're right, it does only take a few milliseconds, but like I said, this is part of a larger algorithm. I cast a few rays, choose the best solution, and then break the polygon into two smaller parts and repeat.... over and over and over again.
Mark
The reason I was so concerned about this was because this subproblem affects the asymptotic running time of my entire algorithm.
Mark