polygon enclosing circles

Hi

Does anyone know of a link/paper on how to find the minimum convex/concave polygon that encloses a set of circles?

I'm modelling a system by a series of circles and at each step I want to draw the polygon that encloses the circles. This is similar to the blob structure, but the boundary circles are continually changing.

Graham
I would look at what the fluid simulators do. Marching cubes is the standard technique.
i dont have any links but i think its not that hard to implement yourself.
First find the center of the polygon, i reckon this can be done by averaging all circles positions. Then you raycast from the center for every n radians/degrees and store all circles which are furthest away from the center, then remove any duplicates. After this phase you end up with the circles stored in succession, so you now have to calculate lines in between the circles. I reckon this is not too hard, first calculate distance vector between two circles centers, next translate this vector perpendicularly to its direction, to the direction which is closer to angle of a raycast x. Raycast x is raycast from polygon center to the middle between the two circles. after you might still get some gaps, or intersections depending on the angles between your circles, these gaps are where the circle is on the perimeter of the polygon, and part of its shape must be copied to the polygon, so you need to make an arc between the ends of the lines going to connected circles. Intersections, i reckon, happen only if you get concave resulting polygon, so by doing a search for concave angles you will fix the intersections too.

Sorry if i was unclear, its all out the top of my head, also note taht this is not optimized in any way, and maybe there are already better thought out solutions to this problem, this is only my go at it. Also for box2d you would need to split it to compensate for 8 max vertices.
Hi
Thanks for your reply.

The code below uses 2 ideas of simply counting the number of adjacent circles, the idea being ~8 for fully packed interior circles and less for an exterior circle. The 2nd idea counts the number of circle-ray intersections for the 8 cases of N, NE, etc. The circles are stored using a kd-tree to accelerate nearest-neighbour lookup.

I like your idea of a convex-hull type Graham-scan since it orders the circles when finished, thus assisting with joining the circle centes to form an "interior" polygon. The code below simply returns a set of exterior circles without ordering and will therefore require subsequent sorting. I'll try and incorporate your suggestion. I have some code for generating an edge-parallel exterior polygon from a given polygon, which is your suggestion of a radius-translated polygon from the exterior circle centres.

I forgot to say in my previous post that the circles have UserData attributes such as 0, 1, etc the numbers reflecting layers of circles. During the simulation the layer circles bounce around and the layers split and separate, thus creating multiple sub-layers. Thus, the problem is complicated by having to find multiple disconnected clusters of circles of given type.

Anyhow, thanks again for your reply. I'll stick at it!!

Code:
public class SameSizeCirclesCollection2D extends CirclesCollection2D
{

    //////////////////
    // constructors //
    //////////////////

    /**
     * Default constructor.
     */
    public SameSizeCirclesCollection2D()
    {
        super();
    }

    /**
     * Constructor.
     * @param circles KD-Tree of circles.
     */
    public SameSizeCirclesCollection2D(final KDTreePoint2D<Circle2D> circles)
    {
        super(circles);
    }

    /**
     * Determines the sets of interior and boundary circles.
     * @param searchRadius Search radius of circle to search for neighbouring circles.
     * @param numberAdjacentCirclesToBeConsideredInterior The number of adjacent circles to a given circle for it to be considered an interior circle.
     * @param method Method: 0( interiorAndBoundaryCircles_NumberAdjacent() ), 1( interiorAndBoundaryCircles_NumberItersections() ).
     * @param interiorCircles Set upon return to the set of interior circles.
     * @param boundaryCircles Set upon return to the set of exterior circles.
     * @throws IllegalArgumentException Thrown if an invalid method is specified.
     */
    public void interiorAndBoundaryCircles(final double searchRadius, final int numberAdjacentCirclesToBeConsideredInterior, final int method,
                                           HashSet<Circle2D> interiorCircles, HashSet<Circle2D> boundaryCircles) throws IllegalArgumentException
    {
        if (method == 0)
        {
            interiorAndBoundaryCircles_NumberAdjacent(searchRadius,numberAdjacentCirclesToBeConsideredInterior,interiorCircles,boundaryCircles);
        }
        else if (method == 1)
        {
            interiorAndBoundaryCircles_NumberItersections(searchRadius,numberAdjacentCirclesToBeConsideredInterior,interiorCircles,boundaryCircles);
        }
        else
        {
            throw new IllegalArgumentException("SameSizeCirclesCollection2D.interiorAndBoundaryCircles(...,method,...); invalid method");
        }
    }

    /**
     * Determines the sets of interior and boundary circles based on the number of adjacent circles.
     * A circle within the interior of a hexagonal or square packing will have 8 neighbouring circles, whereas boundary polygons will have less. A
     * value of 6 for numberAdjacentCirclesToBeConsideredInterior has been found to give approximately good results.
     * @param searchRadius Search radius of circle to search for neighbouring circles.
     * @param numberAdjacentCirclesToBeConsideredInterior The number of adjacent circles to a given circle for it to be considered an interior circle.
     * @param interiorCircles Set upon return to the set of interior circles.
     * @param boundaryCircles Set upon return to the set of exterior circles.
     * @throws IllegalArgumentException Thrown if an invalid method is specified.
     */
    public void interiorAndBoundaryCircles_NumberAdjacent(final double searchRadius, final int numberAdjacentCirclesToBeConsideredInterior,
                                                          HashSet<Circle2D> interiorCircles, HashSet<Circle2D> boundaryCircles)
    {
        Iterator<Entry<Point2D,Circle2D>> it = mCircles.iterator(null,null);
        while (it.hasNext())
        {
            Entry<Point2D,Circle2D> entry = it.next();
            Point2D  p  = entry.getKey();
            Circle2D c = entry.getValue();

            ArrayList<Circle2D> adjCircles = mCircles.valuesInsideCircle(p,searchRadius);
            int numberAdjCircles = adjCircles.size();
            if (numberAdjCircles >= numberAdjacentCirclesToBeConsideredInterior)
            {
                interiorCircles.add(c);
            }
            else
            {
                boundaryCircles.add(c);
            }
        }
    }

    /**
     * Determines the sets of interior and boundary circles based on the number of intersections with adjacent circles.
     * A circle within the interior of a hexagonal or square packing will have 8 neighbouring circles, whereas boundary polygons will have less. A
     * value of 6 for numberAdjacentCirclesToBeConsideredInterior has been found to give approximately good results.
     * @param searchRadius Search radius of circle to search for neighbouring circles.
     * @param numberAdjacentCirclesToBeConsideredInterior The number of adjacent circles to a given circle for it to be considered an interior circle.
     * @param interiorCircles Set upon return to the set of interior circles.
     * @param boundaryCircles Set upon return to the set of exterior circles.
     * @throws IllegalArgumentException Thrown if an invalid method is specified.
     */
    public void interiorAndBoundaryCircles_NumberItersections(final double searchRadius, final int numberIntersectionsToBeConsideredInterior,
                                                              HashSet<Circle2D> interiorCircles, HashSet<Circle2D> boundaryCircles)
    {
        Vector2D north      = new Vector2D(0,1);
        Vector2D northEast  = new Vector2D(1,1); northEast.normalise();
        Vector2D east       = new Vector2D(1,0);
        Vector2D southEast  = new Vector2D(1,-1); southEast.normalise();
        Vector2D south      = new Vector2D(0,-1);
        Vector2D southWest  = new Vector2D(-1,-1); southWest.normalise();
        Vector2D west       = new Vector2D(-1,0);
        Vector2D northWest  = new Vector2D(-1,1); northWest.normalise();

        Ray2D ray = new Ray2D();

        Iterator<Entry<Point2D,Circle2D>> it = mCircles.iterator(null,null);
        while (it.hasNext())
        {
            Entry<Point2D,Circle2D> entry = it.next();
            Point2D  p  = entry.getKey();
            Circle2D c = entry.getValue();

            int numberAdjIntersections = 0;
            boolean intersect = false;
            ArrayList<Circle2D> adjCircles = mCircles.valuesInsideCircle(p,searchRadius);
            for (int i=0; i<adjCircles.size(); i++)
            {
                Circle2D adjCircle = adjCircles.get(i);

                ray.setOrigin(p);

                ray.setDirection(north);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(northEast);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(east);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                    numberAdjIntersections++;
                ray.setDirection(southEast);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(south);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(southWest);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(west);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
                ray.setDirection(northWest);
                intersect = adjCircle.intersectionTest(ray);
                if (intersect)
                {
                    numberAdjIntersections++;
                    continue;
                }
            }

            if (numberAdjIntersections >= numberIntersectionsToBeConsideredInterior)
            {
                interiorCircles.add(c);
            }
            else
            {
                boundaryCircles.add(c);
            }
        }
    }

} // class SameSizeCirclesCollection2D

Hi

I forgot to say thanks to Erin for suggesting Marching Cubes.

I came across this article on Wikipedia:

http://en.wikipedia.org/wiki/Marching_squares

for the equivalent 2D case.

I've started coding this up and seems to be working ok. A problem is that I have to generate an auxilliary "background grid" since the marching squares algorithm requires a structured grid. I thus end up with "gridded" polygons and will then have to use some sort of smoothing such as bezier/bspline to round them off - haven't got that far yet!!

Graham

Original thread: https://box2d.org/forum/viewtopic.php?t=5591