Commit 92de8d13 authored by Nico Mack's avatar Nico Mack

Documentation of clustering code.

parent 1837b3b1
...@@ -78,6 +78,55 @@ public class ClusterManager<P extends Positionable> { ...@@ -78,6 +78,55 @@ public class ClusterManager<P extends Positionable> {
// *************************************************************************** // ***************************************************************************
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
private boolean cluster(List<P> candidates, List<P> retained, List<P> coalesced) {
boolean clusterDetected = false;
// Iterate over all candidates.
for (P candidate : candidates) {
// Check whether candidate is indeed clusterable and has not already been
// coalesced into a cluster.
if ((candidate instanceof Clusterable) && !coalesced.contains(candidate)) {
// Try to find items within a specific radius.
List<P> coalescing = kdTree.findNearest(candidate, radius);
// If there were items within that zone, than we the candidate will be
// a cluster, representing the matching items.
boolean isClustered = (!coalescing.isEmpty());
// However, since we don't rebuild the kdTree for performance reasons
// on each iteration, the tree will always hold to the totality of
// available items. We thus have to remove previously coalesced items
// in order to find out whether we're in the presence of a new cluster
// or not.
coalescing.removeAll(coalesced);
clusterDetected |= (!coalescing.isEmpty());
// Add remaining coalescing items to list of already coalesced items
// and make sure none of them remains in the retained list.
if (isClustered) {
coalesced.addAll(coalescing);
retained.removeAll(coalescing);
}
// Last but not least, set the candidates' clustered flag and add
// it to the list of retained items.
((Clusterable) candidate).setClustered(isClustered);
if (!retained.contains(candidate)) {
retained.add(candidate);
}
}
}
return clusterDetected;
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// *************************************************************************** // ***************************************************************************
// * Class Body // * Class Body
...@@ -133,27 +182,7 @@ public class ClusterManager<P extends Positionable> { ...@@ -133,27 +182,7 @@ public class ClusterManager<P extends Positionable> {
int iterations = 0; int iterations = 0;
do { do {
clusterDetected = false; clusterDetected = this.cluster(candidates, retained, coalesced);
for (P candidate : candidates) {
if ((candidate instanceof Clusterable) && !coalesced.contains(candidate)) {
List<P> coalescing = kdTree.findNearest(candidate, radius);
boolean isClustered = (!coalescing.isEmpty());
coalescing.removeAll(coalesced);
clusterDetected |= (!coalescing.isEmpty());
if (isClustered) {
coalesced.addAll(coalescing);
retained.removeAll(coalescing);
}
((Clusterable) candidate).setClustered(isClustered);
if (!retained.contains(candidate)) {
retained.add(candidate);
}
}
}
candidates.clear(); candidates.clear();
candidates.addAll(retained); candidates.addAll(retained);
iterations++; iterations++;
...@@ -162,7 +191,7 @@ public class ClusterManager<P extends Positionable> { ...@@ -162,7 +191,7 @@ public class ClusterManager<P extends Positionable> {
synchronized (clustered) { synchronized (clustered) {
clustered.clear(); clustered.clear();
clustered.addAll(retained); clustered.addAll(retained);
LOGGER.info("Clustering completed in {} iteration(s)! {} Marker(s) remaining!", iterations, clustered.size()); LOGGER.info("Clustering completed in {} iteration(s)! {} Marker(s) remaining!", iterations, clustered.size()); //$NON-NLS-1$
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment