Best data structure for insert/update/delete and search of 2d point - indexing

I have a infrastructure for massive entities on a GIS map - using the graphic card for drawing (WebGL).
Today, I'm using quad-tree for indexing the data and querying - for selection/drawing purposes.
Recently I've found a way to enable my users to update the location of the entities and draw the change very fast. for example updating 15000 locations, and redrawing took less then 0.05 ms.
The problem now is updating my data structure. It is very very slow.
I've ran over many data structures, such as R-tree, B-Tree and more.. but not yet found satisfied result.
My question is
what is the optimal data structure for 2d points from performances point of view, for inserting/updating and query (by distance from point, rectangle) ?
Maybe there is a web gl solution for this ?

what is the optimal data structure for 2d points from performances
point of view, for inserting/updating and query (by distance from
point, rectangle) ?
It's hard to find a data structure that satisfies all of these beautifully. Typically for searches to be fastest, insertion has to be slower, for insertion to be faster, searches have to be slower, so it's a balancing act.
However, given your needs, a simple NxM grid might actually be ideal. I come originally from a gaming background where sprites can be moving at every single frame. Quad-trees are often too expensive for such dynamic content that wants to update the structure every single frame. As a result, a simple NxM grid often works better for those cases.
The NxM grid can do a rectangular query potentially even faster than the quad-tree, as it simply has to determine which grid cells overlap the rectangle and then simply check the elements within the cells to see if the overlap/are inside the rectangle. Same goes for a search within a radial parameter (circle).
Insertion and removal is dirt cheap since we only need to update the cells in which the elements overlap. If you're dealing with points, a point only belongs in one cell, and it becomes an extremely simple constant-time operation. In fact, if each point stores a list pointer for the next element, moving it from one cell to another (along with removing it from a cell and inserting it to a cell) only requires changing pointers around -- no memory allocation required. The grid then becomes a grid of head pointers for a singly-linked list.
Another approach is that I imagine you're currently using one massive quad-tree for the entire world. You can use a hybrid of these techniques by using a quad-tree to represent a small region, e.g., with quad-trees tiled together to form a grid of quad-trees. This can significantly reduce the cost to update a quad-tree for a specific region since each quad-tree stores only the data for that region.

Related

Computational complexity and shape nesting

I have SVG abirtrary paths which i need to pack as efficiently as possible within a given rectangle(as less waste of space as possible). After some research i found the bin packing algorithms which seems to be dealing with boxes and not curved random shapes(my SVG shapes are quite complex and include beziers etc.).
AFAIK, there is no deterministic algorithm for actually packing abstract shapes.
I wish to be proven wrong here which would be ideal(having a mathematical deterministic method for packing them). In case I am right however and there is not, what would be the best approach to this problem
The subject name is Shape Nesting, Nesting Problem or Nesting Process.
In Shape Nesting there is no single/uniform algorithm or mathematical method for nesting shapes and getting the least space waste possible.
The 1st method is the packing algorithm(creates an imaginary bounding
box for each shape and uses a rectangular 2D algorithm to pack the
bounding boxes).
This method is fast but the least efficient in regards to space
waste.
The 2nd method is some kind of incremental rotation. The algorithm
rotates the shape at incremental steps and checks if it fits in the
space. This is better than the packing method in regards to space
waste but it is painstakingly slow,
What are some other classroom examples for achieving a solution to this problem?
[Edit1] new answer
as mentioned before bin-packing is NP complete (hard) so forget about algebraic solution
known approaches are:
generate and test
either you test all possibility of the problem and remember the best solution or incrementally add items (not all at once) one by one with the same way. It is basically what you are doing now without proper heuristic is unusably slow. But has the best space efficiency (the first one is much better but much slower) O(N!)
take advantage of sorting items by size
something like this it is much faster almost O(N.log(N)) (according to used sorting algorithm). Space efficiency strongly depends on the items size range and count. For rectangular shapes is this the best approach (fastest and usable even for N>1000). For complex shapes is this not a good way but look at it anyway maybe you get some idea ...
use of Neural network
This is extremly vague approach without any warrant of solution but possible best space efficiency/runtime ratio
I think there could be some field approach out there
I sow a few for generating graph layouts. All items create fields (booth attractive and repulsive) so they are moving to semi-stable state.
At first all items are at random locations
When the movement stop remember best solution and shake all items a little or randomize their position again.
Cycle this few times
This approach is much faster then genere and test and can provide very close solution to it but it can hang in local min/max or oscillate if the fields are not optimally choosed. For example all items can have constant attractive force to each other and repulsive force getting stronger only when the items are very close. You have to prevent overlapping of items (either by stronger repulsion or by collision tests). You have also to create some rotation moment for example with that repulsive force. It differs on any vertex so it creates a rotation moment (that can automatically align similar sides closer together). Also you can have semi-stable state with big distances between items and after finding best solution just turn off repulsion fields so they stick together. Sometimes it can have better results some times not ... here is nice example for graph layout computation
Logic to strategically place items in a container with minimum overlapping connections
Demo from the same QA
And here solver for placing sliders in 2D:
How to implement a constraint solver for 2-D geometry?
[Edit0] old answer before reformulating the question
I am not clear what you want to achieve.
have SVG picture and want to separate its parts to rectangular regions
as filled as can be
least empty space in them
no shape change in picture
have svg picture and want to change its shapes according to some purpose
if this is the case some additional info is needed
[solution for 1]
create a list of points for whole SVG in global SVG space (all points are transformed)
for line you need add 2 points
for rectangles 4 points
circle/elipse/bezier/eliptic arc 8 points
find local centres of mass
use classical approach
or can speed things up by computing the average density of points per x,y axis separately and after that just check all combinations of found positions of local max of densities if they really are sub cluster center or not.
all sub cluster center is the center of your region
now find the most far points which are still part of your cluster (the are close enough to neighbour points)
create rectangular area that cover all points from sub cluster.
you also can remove all used points from list
repeat fro all valid sub clusters
until all points are used
another not precise but simpler approach is:
find SVG size
create planar map of svg with some precision for example int map[256][256].
size of map can be constant or with the same aspect as SVG
clear map with 0
for any point of SVG set related map point to 1 (or inc or whatever)
now just segmentate map and you will have find your objects
after segmentation you have position and size of all objects
so finding of bounding boxes should be easy
You can start with a variant of the rectangle bin-packing algorithm and add rotation. There is a method "Guillotine bin packer" and you can download a paper and a library at github.

Efficient way to handle large runtime-generated tile maps?

I am coding a 2 dimensional, tile based (orthogonal tiles) iPhone game. All levels are procedurally generated when the app is first played, and then persist until the user wants a new map. Maps are rather large, being 1000 tiles in both width and height, and the terrain is destructible. At the moment it is rather similar to Terraria, but that will change.
To hold map/tile information I am currently using several 2 dimensional c style arrays. This works well, but I am concerned as to the amount of memory this takes up, as the arrays are all defined as short array[1000][1000], which takes up (1000 * 1000 * sizeof(short)) bytes of space.
This is not particularly desirable when the iPhone doesn't have an incredibly large amount of memory to work with, especially when the user is multitasking. The main problem is that there is no way that I can use a specific tile map format such as .tmx, because all the levels are procedurally generated. Performance could also be an issue, because if a tile is destroyed at index(x, y), then I need to change the data in that index. I have also thought about writing tile map data to a text file, but I think there would be difficulties or performance issues when accessing or changing data.
Keeping all this in mind, what would be an efficient and fast way to handle my tile data?
My gut feeling on this is Core Data structured such that each tile element has relationships to the tiles around it. There's some non-trivial overhead here, but the advantage is that you can release tiles that aren't onscreen from memory and fault them back when you need them. As you move in a direction, you can query for the tiles in that direction, and you can fairly cheaply dump memory when you're in the background. This would get rid of the "several" 2D arrays and move all the data into a single object. In principle, the grid could be infinite in size this way, since everything is by relationship rather than coordinate.
You could similarly approach the problem using SQLite, querying for rows and columns in a given range. You might mark the objects as NSDiscardableContent and put them in an NSCache, which could dramatically improve memory performance. You could still generate an effectively-infinite grid as long as you allow coordinates to be both positive and negative.

iOS - How to detect if two or more objects collide

How can i detect if two or more objects collide?
I would like to use only default frameworks, such Core Graphics. Or i have to use Box2d or Cocos2d?
EDIT
You're right, the question isn't really clear.
So this is the situation :
i have multiple UIImageView which move with the accelerometer, but i want that when two or more images collides these isn't overlap each others. Is it clear?
Probably you want a multi-step process.
First, define a "center" and "radius" for each object, such that a line drawn around the center at the selected radius will entirely encompass the object without "too much extra". (You define how hard you work to define center and radius to prevent "too much".)
An optional next step is to divide the screen into quadrants/sections somehow, and compute which objects (based on their centers and radii) lie entirely within one quadrant, which straddle a quadrant boundary, which straddle 4 quadrants, etc. This allows you to subset the next step and only consider object pairs that are in the same quadrant or where one of the two is a straddler of one sort or another.
Then, between every pair of objects, calculate the center-to-center distance using the Pythagorean theorem. If that distance is less than the sum of the two objects' radii then you have a potential collision.
Finally, you have to get down and dirty with calculating actual collisions. There are several different approaches, depending on the shape of your objects. Obviously, circles are covered by the prior step, squares/rectangles (aligned to the X/Y axes) can be computed fairly well, but odd shapes are harder. One scheme is to, on a pair of "blank" canvases, draw the two objects, then AND together the two, pixel by pixel, to see if you come up with a 1 anywhere. There are several variations of this scheme.
As mentioned, your question is pretty vague, and therefore difficult to answer succinctly. But to give you some ideas to go by, you can do this with core animation, though some 3rd party gaming engines/frameworks may be more efficient.
Essentially, you create a timer that fires quite often (how often would depend on the size of the objects you're colliding and their speed - too slow and the objects can collide and pass each other before the timer fires - math is your friend here).
Every time the timer fires you check each object on screen for collisions with the others. For efficiencies sake you should ensure that you only check each pair once - ie. if you have A,B,C,D objects, check A & D but not D & A.
If you have a collision handle it however you want (animation/points/notification/whatever you want to do).
There's way too much to cover here in a post. I'd suggest checking out the excellent writeup on the Asteroids game at cocoawithlove, especially part 3 (though not iOS the principles are the same):
http://cocoawithlove.com/2009/03/asteroids-style-game-in-coreanimation.html

How can I speed up this 3D grid-based rendering system?

I have recently been developing an isometric, rendering system to map out 3D grids in Javascript. All of the items on the grid are cubes of equal dimensions, the only differences between each one is a texture to represent a value for that coordinate. My application requires large grids to be graphed, even though only a small portion is visible in the viewport at once.
Because I am using Canvas, which is slow to draw thousands of shapes per frame, I set my script to loop through each block but only draw its faces if they are 1.) next to an empty grid space and 2.) inside the viewport. This system works fine for smaller grids, but as my application will need considerably large ones (1000+x1000+x128), I will need to add some performance improvements for the final product.
Does anyone that has worked with rendering systems know any way I can further optimize my engine? One thing that I guess may be effective will be trying to not loop through each grid value, even if it is not being drawn. However, I do not know the most efficient way to know whether to loop through a grid value or not (I am currently going through EVERY value, then calculating whether it should be drawn).
If I have been too vague, please tell me and I will be happy to elaborate. Thank you for your time and expertise; I am a student and any help will greatly aid my learning.
Some pointers to you: you might want to have a look at classic culling algorithms using things like octree (or quadtrees in your case), ...

Per frame optimization for large datasets

Summary
New to iPhone programming, I'm having trouble picking the right optimization strategy to filter a set of view components in a scrollview with huge content. In what area would my app gain the most performance?
Introduction
My current iPad app-in-progress let's users explore fairly large binary tree structures. The trees contain between 30 to 900 nodes, and when drawing inside a scrollview (with limited zoom) it looks like this.
The nodes' contents are stored in a SQLite backed Core Data model. It's a binary tree and if a node has children, there are always exactly two. The x and y positions are part of the model, as are the dimensions of the node connections, shown as dotted lines.
Optimization
Only about 50 nodes fit the screen at any given time. With the largest trees containing up to 900 nodes, it's not possible to put everything in a scrollview controlled and zooming UIView, that's a recipe for crashes. So I have to do per frame filtering of the nodes.
And that's where my troubles start. I don't have the experience to make a well founded decision between the possible filtering options, and in addition I probably don't know about that really fast special magic buried deep in Objective-C or Cocoa Touch. Because the backing store is close to 200 MB in size (some 90.000 nodes in hundreds of trees), it's very time consuming to test every single tree on the iPad device. Which is why I'd like to ask you guys for advice.
For all my attempts I'm putting a filter method in the scrollViewDidScroll: and scrollViewDidZoom:. I'm also blocking the main thread with the filter, because I can't show the content without the nodes anyway. But maybe someone has an idea in that area?
Because all the positioning is present in the Core Data model, I might use NSFetchRequest to do the filtering. Is that really fast though? I have the idea it's not a very optimized method.
From what I've tried, the faulted managed objects seem to fit in memory at once, but it might be tricky for the larger trees once their contents start firing faults. Is it a good idea to loop over the NSSet of nodes and see what items should be on screen?
Are there other tricks to gain performance? Would you see ways where I could use multi threading to get the display set faster, even though the model's context was created on the main thread?
Thanks for your advice,
EP.
Ironically your binary tree could be divided using Binary Space Partitioning done in 2D so rendering will be very fast performant and a number of check close to minimum necessary.