Very fast 3D distance check?

后端 未结 13 1894
南方客
南方客 2021-01-31 15:40

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. I use STL sort like this

13条回答
  •  难免孤独
    2021-01-31 16:22

    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.

提交回复
热议问题