Quite interesting problem indeed. Here's my solution:
CODE
float $pos_array[] = getParticleAttr -at position -a 1
;
float $dist = 10000000000000000;
float $max = 0.00001; // for normalization
float $mdist_array[]={};
float $norm_array[] = {};
vector $a, $b;
int $k,$i;
for ($k = 0; $k < `size($pos\_array)`; $k += 3) {
$dist = 10000000000000000;
$a = <>;
for ($i = 0; $i < `size($pos\_array)`; $i += 3) {
if ($i!= $k) {
$b = <>;
float $mag = `mag($a-$b)`;
if($mag < $dist) $dist = $mag;
}
}
if($dist > $max) $max = $dist;
$mdist_array[$k/3] = $dist;
}
for ($k = 0; $k < `size($mdist\_array)`; $k ++) {
$norm_array[$k] = $mdist_array[$k] / $max;
}
This script compares each particle with every other particle ( -> (num particles)^2 iterations) and returns the array $mdist_array, that contains the minimum distance to the next particle by ID and for your purpose i added $norm_array: It normalizes the values from 0 to 1 (1 being the biggest distance), so organizing is easier.
Anyway, what i think you should do is rewrite it, so it averages between lets say 4 or 5 Neighbour particles, because the distance to one particle doesn't really represent the density.