Fast way to calculate 2D signed distance field

Started by
7 comments, last by agleed 12 years ago
Hi,

I am currently searching for a fast way to compute a signed distance field in 2D. I can only think of the brute force method i.e. (pseudocode):


for each pixel in bitmap do
closest = max number
for each other pixel do
dist = distance(pixel, other pixel)
if dist < closest then
closest = dist
end
end
end


Are there any obious optimizations? Did anybody maybe try to do it on the GPU? I know that the game pixel junk shooter computes a distance field each frame for dynamic collision objects so there has to be a way to get this pretty fast. Every idea, papers, links welcome!

Thank you!
Advertisement
The fastest CPU algorithm I know is the one described in this paper: http://perso.telecom-paristech.fr/~bloch/ANIM/Danielsson.pdf
It is linear in the number of pixels (unlike the algorithm you describe, which is quadratic).
"Math is hard" -Barbie
Also I have an implementation of 3D signed distance transform in GPU Gems 2, chapter 8. You can find it here: ftp://download.nvidia.com/developer/GPU_Gems_2/CD/Copy%20of%20Index.html
The code is in dispmap/distance.cpp.
"Math is hard" -Barbie
The brute force way is slow, but accurate. As far as I can tell, all of the faster algorithms sacrifice varying degrees of accuracy to achieve their speed. While researching systems for rendering text for my current project I came across the following papers:

http://infoscience.e...ch/record/86623 (This link proposes a few corrections to Danielsson's technique)

http://citeseerx.ist...p=rep1&type=pdf (The "dead reckoning" signed distance transform)

I ended up implementing the transform outlined in the second paper, which is pretty quick, but has a few flaws.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
thanks guys, I will look into these papers!

Edit: The last paper looks very good, thank you!
Just an FYI, jump to page 43 of this: http://fumufumu.q-games.com/gdc2010/shooterGDC.pdf to get a better look at PixelJunk's approach(es). They settle on some pretty SPU-intensive algorithms, so it may be of limited use to you. They do reference a few methods though.

If you don't anything that accurate (i.e. just edge detection), then you can always do a simple blur for the field approximation near edges, or the iterative style (Only comparing immediately neighbouring cells) for just a few repeats can also give a similar edge detection. Basically, it's worth asking yourself - Do I *need* the entire field, or just the edges? smile.png
yeah I actually also thought about an iterative approach to circularily iterate surrounding pixels. that should be pretty fast in a pixel shader :). thanks for the paper, I will browse through it!
Should it not be possible to create a good approximation in practically zero time by rendering a series of textured quads with additive blending? I'm considered unsigned now because it's conceptually easier to grok, but I guess signed works just the same by dividing by 2 and offsetting by 1/2.

The idea is this:
A distance map encodes the distance to the nearest set pixel (in a value from 0 to 255, assuming an 8 bit texture). Pixels that are "in" or very close to are white or nearly so, pixels that are far away are black. The closer they get to "in", the lighter a shade of grey they take.
Also, you could say that the closer a pixel is to "in", the higher the likelihood that at any uniformly distributed random sample from a disk with a certain radius will "hit" the "in" pixel.

Thus, enable additive blending, set the scale to 1/256, and render 256 fullscreen quads which are biased by an evenly distributed random offset within some small radius (ideally 128, I figure?). The 256 quads could all go into one vertex buffer and render with one draw call.

Pixels will end up brighter the closer they are to an "in" texel. If this is not accurate enough, use a 16 bit texture (or enable/disable writes to the 3 color channels of a typical RGBA8 texture one after another) and do the same thing a few thousand times.

Graphics cards are ridiculously fast at drawing textured quads. On a "typical" texture size of, say 512x512, I would expect drawing a few hundred textured quads at several hundred FPS, even on a not-so-high-end-card.
hah, crazy idea! Not sure if the method is accurate enough for collision detection though, but its certainly a creative thought!
Hey, mokaschitta, have you solved your problem yet and what did you end up using? I'm currently looking for a way to create a 2D signed distance field, too, but all those papers are awfully long and hard to understand. I hope you can shed some light on all of this.

Greetings

This topic is closed to new replies.

Advertisement