Fast way to smooth a 2D polygon

Started by
18 comments, last by snaileri 11 years, 10 months ago

youtube vid or it didn't happen ;)

Here :)
Advertisement
Woo! I contributed too something...

Reguarding border, if you want to continue with simple geometry manipulation, you could simply extrude the edges, ie: from each edge build a trapezium.

...
that looks really nice in motion, indeed!

Reguarding border, if you want to continue with simple geometry manipulation, you could simply extrude the edges, ie: from each edge build a trapezium.


Okay, I drew a picture of how I could calculate the outline's corner points:
kNku4.jpg

This way I get an ouline which is approximately as thick as the length of the nearby edge.
How do I alter the thickness? In other words, how do I make the mean of the two edge normals to move closer or farther from the corner vertice?
I have a feeling it's a very simple math trick but I can't figure it out.


How to keep the outline's thickness uniform when the edges have different lengths?
Right now, with this method the local thickness of the outline depends on how much the local edge is streched. I need uniform thickness.
Normalize the normal vectors and the resulting mean, multiply by a constant factor. This should give you uniform thickness.
Okay, so normalizing a vector means finding the unit vector (a vector with magnitude 1). Finding the magnitude requires the use of the square root, which, to my knowledge, is computationally very heavy. Are there faster ways to normalize a vector, or is that it? Do you think it would affect the frame-rate much if every frame the cpu does dozens of square root calculations?
I'm not sure if you can avoid the square root (maybe there are some mathematical tricks that I am unaware of), but a few square root computations won't be noticable. If they are, you can still use something like http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Approximations_that_depend_on_IEEE_representation or outsource the calculation to the GPU to get it real fast.
Would that GPU calculation approach be easy to implement?
I don't have any idea how that's done.
Using the GPU is probably only practical if you already use the GPU for rendering. Most likely, setup and transfer times will kill every performance gain if you use the CPU for rendering. But fast inverse square roots are really fast, so don't look into GPU offloading if you use software rendering.
Okay, after a while of googling, here's the Fast inverse square root algorithm in Java

public static void main(String[] args) {
System.out.println("Accurate: " + Math.sqrt(3*3+4*4));
System.out.println("Approximation: " + 1/fastInverseSqrt(3*3+4*4));
}
static final public float fastInverseSqrt(float x)
{
float xhalf = 0.5f*x;
int bitValue = Float.floatToRawIntBits(x);
bitValue = 0x5f3759df - (bitValue >> 1);
x = Float.intBitsToFloat(bitValue);
x = x*(1.5f - xhalf*x*x);
x = x*(1.5f - xhalf*x*x);
// iterate for more accuratcy or not.
// inlined for speed-speed
//x = x*(1.5f - xhalf*x*x);
//x = x*(1.5f - xhalf*x*x);
//x = x*(1.5f - xhalf*x*x);
return x;
}



Accurate: 5.0
Approximation: 5.000018

This topic is closed to new replies.

Advertisement