I am working through the Firmware Framework code and I came across this routine for center of mass calculation, which did not make sense at first glance.
I can't find anything online to confirm that this is the correct or maybe best approach. I did find this on stackoverflow but it's not really helpful in confirming if code methodology correct: https://stackoverflow.com/questions/66175912/center-of-mass-algorithm
void calcCenterOfMass(const int pixels[], const unsigned int
xres, const unsigned int yres, float *cmx, float *cmy, int *totalmass)
{
int cmx_numer=0, cmy_numer=0;
for (unsigned int i = 0; i < xres*yres; i++) {
cmx_numer += (i%xres)*pixels[i];
cmy_numer += (i/xres)*pixels[i];
*totalmass += pixels[i];
}
if (*totalmass == 0) {
*totalmass = 1; // avoid NaN
}
*cmx = (float)cmx_numer/(float)(*totalmass);
*cmy = (float)cmy_numer/(float)(*totalmass);
}
For example, when calculating cmx_numer and cmy_numer, one uses modulus of xres and the other divides by xres. Struggling to work out if correct.
I'm also wondering about the "for" routine too.
So I am seeking feedback.