Dynamic Programming: Variables Used

l is used only in the initialization of the first column of the energy matrix energyMtx to zero.

i is the snaxel number being processed. One iteration involves i going from zero to one less than the number of snaxels in the snake, NUM_SNAXELS-1.

j steps through the neighbors of the next node. Each possible neighbor of the next node must be considered and put into the energy and position matrix, so j takes on NUM_NEIGHBORS values, one for each neighbor position.

k steps through the neighbors of the current node. Each possible neighbor of the current node must be considered in finding the minimum energy associated with the next node.

m is used only as an index in searching the final column of the energy matrix for the minimum value.

min_position keeps track of the value of k (the current node) corresponding to the minimum energy found so far in the search using k over all neighbors of the current node.

min_final_position is used only in searching the final column of the energy matrix to keep track of the value of m that corresponds to the minimum energy found so far. Once we have searched through NUM_NEIGHBORS values of m, min_final_position contains the neighbor offset that corresponds the the actual minimum energy.

min_energy keeps track of the minimum energy found so far in the search using k over all neighbors of the current node.

energy is used in the search over the neighbors of the current node as a temporary storage of the energy computed.

min_final_energy is used only in searching the final column of the energy matrix to keep track of the value of the minimum energy found so far. Once we have searched through all the entries in the last column, min_final_energy contains the actual minimum energy.

nextNode contains the coordinates, for the neighbor of the next node indexed by j. These coordinates are used only to compute the energy associated with the associated choice of snaxel positions.

currNode contains the coordinates, for the neighbor of the current node indexed by k. These coordinates are used only to compute the energy associated with the associated choice of snaxel positions.

snaxel[NUM_SNAXELS] stores the coordinates of the snaxel positions. Once the optimal new position is found, these coordinates are updated accordingly.

neighbor[NUM_NEIGHBORS] contains the relative coordinates of the allowable positions of the neighbors. This is typically a 3x3 grid centered at the current snaxel position. In this case, neighbor[] would be of size 9.

/* initialize first column of energy matrix */

for (l=0;l<NUM_NEIGHBORS;l++)

energyMtx[0][l]=0.0;

This block of code simply sets the first column of the energy matrix to zero, since there is no energy associated solely with the first snaxel; energy is based on distance, and there is no distance associated with a single snaxel. It is only when we get to the second snaxel, that we have some measure of distance.

NUM_SNAXELS is simply the number of snaxels in the snake.

NUM_NEIGHBORS is simply the number of entries in neighbors[], i.e. the number of neighbors to be considered for the choice of moving each snaxel.

BIG is the largest number representable with a float. min_energy is set to BIG to be sure that it is initialized larger or equal to the largest value possible for the energy. Then, after the first pass through the minimum-energy-finding loop, min_energy and min_postition will reflect valid choices for energy and position.

E(currNode, nextNode) is a function, that, given values of two adjascent snaxels, returns a float representing the energy associated with those snaxels.

if (energy < min_energy)

{

min_energy = energy
min_position = k

}

This block of code simply sets the first column of the energy matrix to zero, since there is no energy associated solely with the first snaxel; energy is based on distance, and there is no distance associated with a single snaxel. It is only when we get to the second snaxel, that we have some measure of distance.

pos = min_final_position

This initializes pos to the position (in the last column) of the snaxel that corresponds with the minimum total energy. We are going to do a backwards recursion starting with the last pixel. When we have finished this search, we will have the updated optimal snaxel positions for this iteration.

redraw snake

This redraws the snake on the display so the user can observe the snake movement.