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