[cairo] Hit detection

Dov Grobgeld dov.grobgeld at gmail.com
Tue Nov 28 01:27:10 PST 2006


One approach that is currently not possible to do reasonably fast with
cairo is to precalculate a hit buffer with labels indicating what
object is positioned at each pixel. With non-antialiased graphics
primitives this is possible by drawing all graphics primitives
sequentially in two buffers, first the graphics buffer with the user
color antialiased, and then in the label buffer, with a solid color
that represents a label of the region, non-antialiased.

Creating the same label image by cairo could be done as follows:

1. Prepare a label matrix the size of the drawing image filled with 0.
2. Prior to drawing each graphics object create an element image and
fill it with black the size of the bounding box of the graphics
object. Draw the graphics object with color white.
3. Loop through all the pixels pixels of the element image and set the
corresponding pixel in the label matrix to the label number if the
element pixel is greater from 128 (or 0 if you want to pick up your
objects on border pixels as well).
4. Repeat for all elements.

But this would be quite slow as it requires looping through a lot of
bounding boxes.

If cairo could draw non-antialiased this would be much easier.

Just my two öre...

Regards,
Dov

On 11/28/06, Carl Worth <cworth at cworth.org> wrote:
> On Mon, 27 Nov 2006 13:14:35 +1300, Piotr Zurek wrote:
> > That's all good for rectangular (or almost rectangular) objects but what
> > about things like bezier curves. Of course, I could calculate that as
> > well but I hope there exists an easier way.
>
> For what it's worth, the polygon formed by the control points forms a
> nice, easy-to-work-with bounds for the spline, (it's guaranteed to be
> contained within the convex hull of the points).
>
> > I guess I should simply ask what's the best way to utilize in_fill and
> > in_stroke but maybe someone has different suggestion?
>
> I'd suggest using them as a last resort. Test against bounding
> rectangles first, then against more precise bounding polygons, then
> use cairo_in_fill and cairo_in_stroke if necessary.
>
> Or something like that anyway, (I haven't really done much application
> code with cairo yet, but I would guess this is how I would likely
> approach the problem).
>
> -Carl
>
>
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://cairographics.org/cgi-bin/mailman/listinfo/cairo
>
>
>


More information about the cairo mailing list