tags:

views:

1825

answers:

11

Is there a way to do a quick and dirty 3D distance check where the results are rough, but it is very very fast? I need to do depth sorting and im using stl sort like this:

    bool sortfunc(CBox* a, CBox* b)
    {
        return a->Get3dDistance(Player.center,a->center) <
            b->Get3dDistance(Player.center,b->center);
    }

float CBox::Get3dDistance( Vec3 c1, Vec3 c2 )
{
    //(Dx*Dx+Dy*Dy+Dz*Dz)^.5 
    float dx = c2.x - c1.x;
    float dy = c2.y - c1.y;
    float dz = c2.z - c1.z;

return sqrt((float)(dx * dx + dy * dy + dz * dz));
}

Is there possibly a way to do it without a square root or without possibly multiplication?

Thanks

+45  A: 

You can leave out the square root because for all positive (or really, non-negative) numbers x and y, if sqrt(x) < sqrt(y) then x < y. Since you're summing squares of real numbers, the square of every real number is non-negative, and the sum of any positive numbers is positive, the square root condition holds.

You cannot eliminate the multiplication, however, without changing the algorithm. Here's a counterexample: if x is (3, 1, 1) and y is (4, 0, 0), |x| < |y| because sqrt(1*1+1*1+3*3) < sqrt(4*4+0*0+0*0) and 1*1+1*1+3*3 < 4*4+0*0+0*0, but 1+1+3 > 4+0+0.

Since modern CPUs can compute a dot product faster than they can actually load the operands from memory, it's unlikely that you would have anything to gain by eliminating the multiply anyway (I think the newest CPUs have a special instruction that can compute a dot product every 3 cycles!).

I would not consider changing the algorithm without doing some profiling first. Your choice of algorithm will heavily depend on the size of your dataset (does it fit in cache?), how often you have to run it, and what you do with the results (collision detection? proximity? occlusion?).

Gabe
Counterexamples actually don't prove you can't leave out the multiplication in this case - depth sorting doesn't have to be perfect as it's primarily a performance issue (z buffering deals with occasional inaccuracy), so if you get enough performance gain by doing imperfect depth sorting that it outweighs the lost rendering performance... probably unlikely to happen in reality, but I'm only saying that the proof here is broken - it proves the wrong thing.
Steve314
Steve314: Good point. I qualified my statement to make it true (hopefully).
Gabe
I agree, it's pretty common to leave the `sqrt` apart when one doesn't need the distance itself, but only to compare two distances.
Matthieu M.
+6  A: 

Note that for 2 (non-negative) distances A and B, if sqrt(A) < sqrt(B), then A < B. Create a specialized version of Get3DDistance() (GetSqrOf3DDistance()) that does not call sqrt() that would be used only for the sortfunc().

Bert F
Or better, if `Vec3` implements subtraction and the dot product, then this is adequate: `Vec3 d = A - B; float dist = dot(d,d);`
greyfade
+19  A: 

What I usually do is first filter by Manhattan distance

float CBox::Within3DManhattanDistance( Vec3 c1, Vec3 c2, float distance )
{
    float dx = abs(c2.x - c1.x);
    float dy = abs(c2.y - c1.y);
    float dz = abs(c2.z - c1.z);

    if (dx > distance) return 0; // too far in x direction
    if (dy > distance) return 0; // too far in y direction
    if (dz > distance) return 0; // too far in z direction

    return 1; // we're within the cube
}

Actually you can optimize this further if you know more about your environment. For example, in an environment where there is a ground like a flight simulator or a first person shooter game, the horizontal axis is very much larger than the vertical axis. In such an environment, if two objects are far apart they are very likely separated more by the x and y axis rather than the z axis (in a first person shooter most objects share the same z axis). So if you first compare x and y you can return early from the function and avoid doing extra calculations:

float CBox::Within3DManhattanDistance( Vec3 c1, Vec3 c2, float distance )
{
    float dx = abs(c2.x - c1.x);
    if (dx > distance) return 0; // too far in x direction

    float dy = abs(c2.y - c1.y);
    if (dy > distance) return 0; // too far in y direction

    // since x and y distance are likely to be larger than
    // z distance most of the time we don't need to execute
    // the code below:

    float dz = abs(c2.z - c1.z);
    if (dz > distance) return 0; // too far in z direction

    return 1; // we're within the cube
}

Sorry, I didn't realize the function is used for sorting. You can still use Manhattan distance to get a very rough first sort:

float CBox::ManhattanDistance( Vec3 c1, Vec3 c2 )
{
    float dx = abs(c2.x - c1.x);
    float dy = abs(c2.y - c1.y);
    float dz = abs(c2.z - c1.z);

    return dx+dy+dz;
}

After the rough first sort you can then take the topmost results, say the top 10 closest players, and re-sort using proper distance calculations.

slebetman
I'm not sure how this works to find if the distance from the player to a cube is less than the distance from the player to another cube?
Milo
What he's trying to do is sort by distance from some point, not determine if two points are less than a certain distance apart.
Gabe
I'm worried about the second part. It seems to me that `>` is probably as slow as `-`, with `abs` not really costing much more, but that each `if` creates a conditional branch - and therefore a branch prediction that may be predicted wrongly. For a simple Manhattan distance (or for that matter a simple sum-of-three-products square-of-distance) I wouldn't add complexity without good profiling evidence that it's beneficial. The sqrt is worth eliminating, but this Manhattan distance needs care, and even in the first version I'd use a single return with an and operator.
Steve314
Steve314: Indeed, the `(x1-x2)^2+(y1-y2)^2+(z1-z2)^2` operationg can easily be vectorized with 4 operations: add, mul, add, add. Considering that the operations on both sets of vectors could easily be pipelined, you could probably make the full comparison version faster on average than the Manhattan distance version.
Gabe
+1  A: 

If this is simply a value for sorting, then you can swap the sqrt() for a abs(). If you need to compare distances against set values, get the square of that value.

E.g. instead of checking sqrt(...) against a, you can compare abs(...) against a*a.

Alexander Rafferty
Since he's summing squares, the argument to `sqrt` will always be positive, making `abs` redundant.
Gabe
+14  A: 

Here's an equation that might help you get rid of both sqrt and multiply:

max(|dx|, |dy|, |dz|) <= distance(dx,dy,dz) <= |dx| + |dy| + |dz|

This gets you a range estimate for the distance which pins it down to within a factor of 3 (the upper and lower bounds can differ by at most 3x). You can then sort on, say, the lower number. You then need to process the array until you reach an object which is 3x farther away than the first obscuring object. You are then guaranteed to not find any object that is closer later in the array.

By the way, sorting is overkill here. A more efficient way would be to make a series of buckets with different distance estimates, say [1-3], [3-9], [9-27], .... Then put each element in a bucket. Process the buckets from smallest to largest until you reach an obscuring object. Process 1 additional bucket just to be sure.

By the way, floating point multiply is pretty fast nowadays. I'm not sure you gain much by converting it to absolute value.

Keith Randall
+1 for the buckets
Steven Sudit
+3  A: 

You could compare squares of distances instead of the actual distances, since d2 = (x1-x2)2 + (y1-y2)2+ (z1-z2)2. It doesn't get rid of the multiplication, but it does eliminate the square root operation.

Loadmaster
+1  A: 

You may want to consider caching the distance between the player and the object as you calculate it, and then use that in your sortfunc. This would depend upon how many times your sort function looks at each object, so you might have to profile to be sure.

What I'm getting at is that your sort function might do something like this:

compare(a,b);
compare(a,c);
compare(a,d);

and you would calculate the distance between the player and 'a' every time.

As others have mentioned, you can leave out the sqrt in this case.

ngoozeff
A: 

How often are the input vectors updated and how often are they sorted? Depending on your design, it might be quite efficient to extend the "Vec3" class with a pre-calculated distance and sort on that instead. Especially relevant if your implementation allows you to use vectorized operations.

Other than that, see the flipcode.com article on approximating distance functions for a discussion on yet another approach.

Christoffer
+4  A: 

If you worry about performance, you should also take care of the way you send your arguments:

float Get3dDistance( Vec3 c1, Vec3 c2 );

implies two copies of Vec3 structure. Use references instead:

float Get3dDistance( Vec3 const & c1, Vec3 const & c2 );
tibur
A: 

If you could center your coordinates around the player, use spherical coordinates? Then you could sort by the radius.

That's a big if, though.

ccook
+1  A: 

Depending slightly on the number of points that you are being used to compare with, what is below is pretty much guaranteed to be the get the list of points in approximate order assuming all points change at all iteration.

1) Rewrite the array into a single list of Manhattan distances with out[ i ] = abs( posn[ i ].x - player.x ) + abs( posn[ i ].y - player.y ) + abs( posn[ i ].z - player.z );

2) Now you can use radix sort on floating point numbers to order them.

Note that in practice this is going to be a lot faster than sorting the list of 3d positions because it significantly reduces the memory bandwidth requirements in the sort operation which all of the time is going to be spend and in which unpredictable accesses and writes are going to occur. This will run on O(N) time.

If many of the points are stationary at each direction there are far faster algorithms like using KD-Trees, although implementation is quite a bit more complex and it is much harder to get good memory access patterns.

Andrew Cross