Minimizing the sum of distances along mesh pieces to find their geometric median point. Results in a point group with individual mesh centers with shortest average surface distance to any other point.
The foreach loop iterates over all points of the same piece to store all surface distances. The average of all these distances gets exported as f@dist_avg. Also a vector called v@distance is composed of the average distance to allow looking up the nearest point inside a space geo using the geounwrap function which in this case returns the point with the most minimal average distance among each piece for grouping as 'center'.
// RUN OVER: POINTS
string grp = '@piece==' + itoa(i@piece);
int pts[] = expandpointgroup(0, grp);
removevalue(pts, i@ptnum);
float dists[] = {};
foreach(int pt; pts){
float dist_pt = surfacedist(0, itoa(pt), 'P', i@ptnum, set(0), 'surface');
append(dists, dist_pt);
}
f@dist_avg = avg(dists);
v@distance = set(f@dist_avg, 0.0, 0.0);
// RUN OVER: NUMBERS
string geo = geounwrap(0, 'distance');
string grp = '@piece==' + itoa(i@elemnum);
int pt_near = nearpoint(geo, grp, vector(0.0));
setpointgroup(0, 'center', pt_near, 1, 'set');