This question is more about the algorithm and the right use of function rather than the actual code.
In my code, I’m using map to simulate boxes. The maps elements are composed of vector<int> as keys and set<shared_ptr<foo> > as values.
I’m doing a nested loop to go over all the boxes:
mit1 = boxes.begin(); //mit1 is an appropriate iterator
int edge = 10;//represnd periodic boundary conditions
while (mit1 != boxes.end()){
vector<t> = mit1->first;
mit2 = mit1++;
while (mit2 != boxes.end()){
vector<int> u = (mit2++)->first;
bool good = true;
for (int i = 0; i < 3 && good; i++){
u[i] = (int)fabs(u[i] - t[i]);
good = u[i] == 0 || u[i] == 1 || u[i] == edge;
}
if (!good) continue;
}
}
My concern is with the whole nested loop as well as the for loop.
Would you consider it to be more efficient to have a function to calculate all the neighboring boxes? Do you know of any better way to do the for loop test?
Same advice as with every collision detection: Use some algorithm or data structure that allows you to pre-filter candidates for your loop by their spatial distance, and allows to do this pre-filtering in O(n). A quad tree comes to mind, or a coarse-grained spatial map.
Edit: To make the whole idea a little bit clearer, consider the following algorithm: You have N particles in a 3D space and want to find out which particles are closer than a distance D to each other. You build a 3D array, with each bin representing a cubic volume of your target space, and each volume must be at least of size D. To find all particles that might be closer than D to a given particle X, you determine the array cell Ax in which X is currently and select all particles from the Ax and all surrounding cells. You only need to check this small subset for collisions.
When using M array cells, the average-case computational complexity for the whole distance check is now O(N*N/M) instead of O(N^2), however at the cost of O(M^2) space.
If your target space is unbounded, use a quad tree (2D) or an oct tree (3D).