function convolution(matrix, oriArr, loc, index = 0) {
const locX = loc[0], locY = loc[1];
const oriH = oriArr.length
const oriW = oriArr[0].length;
const matSizeX = matrix.length;
if (matSizeX % 2 === 0 || matSizeX < 1) return oriArr[locY,locX];
const matSizeY = matrix[0].length;
if (matSizeY % 2 === 0 || matSizeY < 1) return oriArr[locY,locX];
const offsetX = Math.floor(matSizeX / 2);
const offsetY = Math.floor(matSizeY / 2);
let res = 0;
for (let y = 0; y < matSizeY; y ++) {
for (let x = 0; x < matSizeX; x ++) {
const matValue = matrix[y][x];
const readX = (oriW + (locX - offsetX + x)) % oriW;
const readY = (oriH + (locY - offsetY + y)) % oriH;
const readState = oriArr[readY][readX].state;
const reading = Array.isArray(readState) ? readState[index] : readState;
res += matValue * reading;
}
}
return res;
}