Friday, June 17, 2011

Generating the raster image

What I'm working on at the moment is turning the vector mask into an image that can be used in the node editor. I have tried a couple of different simple techniques, like a scan line fill, but none of them really worked. Then I found this great article: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch25.html. Based on this article I have decided to divide it into a series of steps:
  1. Triangulate the polygon consisting of only the points (no handles).
  2. Fill these triangles with a simple scan line fill.
  3. Form a triangle (two points with one handle between them) or a quad (two points with two handles between them) for every connected pair of points.
    1. If it's a quad divide it into two triangles.
  4. For every triangle:
    1. Determine if it contains a concave or a convex curve.
    2. If it's convex then set all pixels that lie within the curve to 1.
    3. If it's concave then set all pixels that lie within the curve to 0.
For the triangulation I found a great example here: http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml. It was pretty easy to translate the code to Python and, lo and behold, it worked right away! That's something I'm not used to :)
The scan line fill is a simplified version of the technique described here: http://www.cs.fit.edu/~wds/classes/graphics/Rasterize/rasterize/rasterize.html. Because it only has to fill triangles it doesn't have to check and sort multiple scan line intersections. It simply uses Bresenham's line algorithm to determine x for the left and the right edge for every line y. These are stored in a list and used afterwards  to fill the pixels.
Figuring out if a curve is concave or convex is simple since the triangulation function already requires all points to be sorted counter-clockwise. That means that if the handle point lies to the left of the line between the two points the curve is concave, else it's convex.

So, steps 1,2,3 & 4.1 are covered and implemented but steps 4.2 & 4.3 are the tricky ones. To be continued...

No comments:

Post a Comment