views:

41

answers:

1

I'm writing an implementation of an R-Tree based on Guttman's original paper. I was thinking about using an R-Tree for a program I'm writing that involves a lot of rectangles on the screen that can be moved/re-sized with the mouse.

I want to efficiently only select rectangles that are in a particular rectangle and draw them (instead of iterating through potentially 100+ items and checking if bounds intersect). The problem that I'm finding after a couple reads of Guttman's paper is that z-order can't be maintained for 2D objects.

For example, if I move an object, it'd be deleted and then re-inserted. When it's re-inserted, the node it's inserted to wouldn't be able to keep track of the proper order. Most implementation's of an R-Tree that I've seen use an array and just find the empty position. The re-insertion would essentially destroy any z-order positioning.

So when I go to draw all rectangles that intersect with a rectangle, the order they're returned isn't necessarily correct.

Am I wrong on this assumption? I was thinking instead of using an array, I could use an AVL or Red-Black tree and use a Comparer that compares on z-index to insert into the tree. This way, z-order is always maintained (and it's the most important factor).

I was also just thinking of sorting them when they're returned, but this could be more expensive I'm thinking.

+2  A: 

R-tree is not supposed to order answer records somehow.

Just sort answer. It's not too slow.

BTW, I can mail you my r-tree code. It works fine for me, but it would be very useful if someone would check is it what Guttman or Beckman wrote in theirs papers...

Order of spatial indexing is essentially different from strict order...it's difference between spatial indexing and just B+-tree.

You can also have two indexes and join them. Are you really shure you need index? Something works slow?

Andrew_B
@Andrew - Yeah I need an index because the app that I'm writing has many objects on the screen and when I re-draw a particular area, I don't want to have to search *all* rectangles to see if they intersect with the draw bounds. I'm going to accept your answer for now as I'm going to just do a Radix sort when the intersecting items are found. Thanks!
TheCloudlessSky
In CGI area quad-tree is also used.Using quad-tree, you have easy merge-sort.Usually UI does not redraw particular area. It just invaludates everything.Most of the time I hate foreaching over all elements to find few, but, I really belive that using index should go just after something is working slow.
Andrew_B
@Andrew - The reason I'm only re-drawing a particular area is because it's using GDI and GDI isn't hardware accelerated. Currently with looping over the whole list is growing linearly (as expected) as the canvas with more elements grows. This is why I chose to index.
TheCloudlessSky
Hardware acceleration is just a magic word for a complex effect. Bresenham algorithm is shurely accelerated everywhere. Gradients are not accelerated everywhere. Alfa blending with z-order is slow everywhere.Cohen–Sutherland clipping algorithm, used including in determining what not to draw at all, can also be accelerated.GDI is not slow since 2000. If you do not draw complex vector graphics such as GIS.
Andrew_B
@Andrew try to Google GDI+(or System.Drawing) is slow )).
Sergey Mirvoda
@Sergey "GDI+ is fast" also can be Googled (:Interop is relatively slow.CG algorithms base is quite wide. (I'm not good in English, is this expression correct?) And R-tree is rarely used in graphics. But quad tree is used very often, not just because it's easiely implemented.
Andrew_B
@Andrew>R-tree is rarely used in graphics http://stackoverflow.com/questions/2800920/which-applications-use-r-trees
Sergey Mirvoda