I have an array of data (generic vertexdata). I need to be able to search for elements of the array based on a position. Currently, each vertex element records its own position and I simply use a for-loop
to search through every element and compare the positions. I have to do this a LOT in my program and it's fairly performance-critical. Looping through every single element seems really, really inefficient.
I using C++, btw.
My question is this: Is there a better way? That is, is there a way of accessing the necessary element directly based on a 3D position? The positions are ints, so that might help.
I thought about simply using a 3D array (ie. vertex[256][256][256]), but I can't afford the wasted memory, since only about 30-50% of the vertex positions actually contain a vertex. Maybe this could be achieved with pointers? Do they use memory when not assigned?
The other problem with a 3D array is that the vertices can be spread across a virtually infinite area, which would make for a very, very large array. Also, the vertices are effectively added dynamically which means they could be added at a <0 position, meaning the array would have to be shifted backwards and every element reassigned.
If anyone has any suggestions, I'd be very grateful :)