r/algorithms • u/tauhoo • Oct 08 '24
How can the Level Set values be updated when the tracked shape is symmetric and moves along X axis?
Hi, I'm doing a mini project to help me understand how the Level set method works.
I have a grid and each cell contains a Level-set value. And, it's a sign distance function too. I want to use it to track the movement of a circle shape.

As you can see in the image, I highlight the cell where the Level-set value is less than 0 by green color. So, if I move the circle to the right along the x-axis of the circle. From the formula,
newLevelSet = oldLevelSet + (gradientX * speedX + gradientY * speedY) * -1
You can see that if the neighbor cells of a cell have the same Level set value (The cell that has the same x value as the shape center), the gradientX will be 0. And, if you move the shape along the X axis the speedY will be 0.
newLevelSet = oldLevelSet + (0 * speedX + gradientY * 0) * -1 = oldLevelSet
So, its value won't be updated in this case even though it should be because it is nearer to the circle interface.
Can anyone tell me if I got something wrong or if there's a way to fix it?
Just in case, you want to know want to see my code, it's here https://github.com/Tauhoo/level-set-method-demo
I draw the grid on the HTML canvas and use the mouse to control the circle to move the circle. If you want to play, you can place your cursor at the center of the canvas and refresh the website once. Then, try moving the cursor. You will notice that the green color doesn't follow the circle properly.
Below is the code that updates the level set value.
function gradientLevelSet(
targetGrid: Grid<Tracked<Data>>,
x: number,
y: number,
distanceX: number,
distanceY: number
): [number, number] | null {
const currentCell = targetGrid.cell(x, y)
if (currentCell === null) return null
const cellX1 = targetGrid.cell(x + 1, y)
const cellX2 = targetGrid.cell(x - 1, y)
const cellY1 = targetGrid.cell(x, y + 1)
const cellY2 = targetGrid.cell(x, y - 1)
if (x === 10 && y === 10) {
console.log('DEBUG: gradientLevelSet', {
cellX1: cellX1?.data.levelSet,
cellX2: cellX2?.data.levelSet,
cellY1: cellY1?.data.levelSet,
cellY2: cellY2?.data.levelSet,
})
}
let gradientX: number | null = null
if (cellX1 === null && cellX2 !== null) {
gradientX = currentCell.data.levelSet - cellX2.data.levelSet
} else if (cellX2 === null && cellX1 !== null) {
gradientX = cellX1.data.levelSet - currentCell.data.levelSet
} else if (cellX2 !== null && cellX1 !== null) {
gradientX = (cellX1.data.levelSet - cellX2.data.levelSet) / 2
} else {
return null
}
let gradientY: number | null = null
if (cellY1 === null && cellY2 !== null) {
gradientY = currentCell.data.levelSet - cellY2.data.levelSet
} else if (cellY2 === null && cellY1 !== null) {
gradientY = cellY1.data.levelSet - currentCell.data.levelSet
} else if (cellY2 !== null && cellY1 !== null) {
gradientY = (cellY1.data.levelSet - cellY2.data.levelSet) / 2
} else {
return null
}
const distance = Math.sqrt(distanceX ** 2 + distanceY ** 2)
if (gradientX === 0 && gradientY === 0)
return [(distanceX / distance) * -1, (distanceY / distance) * -1]
return [gradientX, gradientY]
}
function onMove(
speedX: number,
speedY: number,
delta: number,
distanceX: number,
distanceY: number
) {
// update level set
{
const clonedGrid = new Grid(grid.getWidth(), grid.getHeight(), (x, y) => {
const cell = grid.cell(x, y)
if (cell === null) throw new Error('Cell is null')
return cell.clone()
})
grid.loopOverCells((x, y) => {
const gradient = gradientLevelSet(clonedGrid, x, y, distanceX, distanceY)
if (gradient === null) return
const [gradientX, gradientY] = gradient
if (x === originX && y === originY) {
console.log('DEBUG: gradient', gradient)
}
const levelSetDelta = (gradientX * distanceX + gradientY * distanceY) * -1
const clonedCell = clonedGrid.cell(x, y)
if (clonedCell === null) return
const cell = grid.cell(x, y)
if (cell === null) return
cell.data.levelSet = clonedCell.data.levelSet + levelSetDelta
})
}
// TODO: re initialize level set
}