给下列程序添加注释:bool DijkstraExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y, int cycles, float* potential) { cells_visited_ = 0; // priority buffers threshold_ = lethal_cost_; currentBuffer_ = buffer1_; currentEnd_ = 0; nextBuffer_ = buffer2_; nextEnd_ = 0; overBuffer_ = buffer3_; overEnd_ = 0; memset(pending_, 0, ns_ * sizeof(bool)); std::fill(potential, potential + ns_, POT_HIGH); // set goal int k = toIndex(start_x, start_y); if(precise_) { double dx = start_x - (int)start_x, dy = start_y - (int)start_y; dx = floorf(dx * 100 + 0.5) / 100; dy = floorf(dy * 100 + 0.5) / 100; potential[k] = neutral_cost_ * 2 * dx * dy; potential[k+1] = neutral_cost_ * 2 * (1-dx)*dy; potential[k+nx_] = neutral_cost_*2*dx*(1-dy); potential[k+nx_+1] = neutral_cost_*2*(1-dx)*(1-dy);//*/ push_cur(k+2); push_cur(k-1); push_cur(k+nx_-1); push_cur(k+nx_+2); push_cur(k-nx_); push_cur(k-nx_+1); push_cur(k+nx_*2); push_cur(k+nx_*2+1); }else{ potential[k] = 0; push_cur(k+1); push_cur(k-1); push_cur(k-nx_); push_cur(k+nx_); }
时间: 2024-03-31 07:32:24 浏览: 50
// This function calculates the potentials of all the cells in the grid based on Dijkstra's algorithm.
// It takes in a 2D array of costs, the starting and ending coordinates, the number of iterations to perform, and an array to store the potentials.
bool DijkstraExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y, int cycles, float* potential) {
// Initialize variables
cells_visited_ = 0;
threshold_ = lethal_cost_;
currentBuffer_ = buffer1_;
currentEnd_ = 0;
nextBuffer_ = buffer2_;
nextEnd_ = 0;
overBuffer_ = buffer3_;
overEnd_ = 0;
memset(pending_, 0, ns_ * sizeof(bool));
std::fill(potential, potential + ns_, POT_HIGH);
// Set starting cell as goal cell
int k = toIndex(start_x, start_y);
// If precise mode is enabled, calculate potentials for each subcell
if(precise_) {
double dx = start_x - (int)start_x, dy = start_y - (int)start_y;
dx = floorf(dx * 100 + 0.5) / 100;
dy = floorf(dy * 100 + 0.5) / 100;
potential[k] = neutral_cost_ * 2 * dx * dy;
potential[k+1] = neutral_cost_ * 2 * (1-dx)*dy;
potential[k+nx_] = neutral_cost_*2*dx*(1-dy);
potential[k+nx_+1] = neutral_cost_*2*(1-dx)*(1-dy);
// Add surrounding cells to priority buffer
push_cur(k+2);
push_cur(k-1);
push_cur(k+nx_-1);
push_cur(k+nx_+2);
push_cur(k-nx_);
push_cur(k-nx_+1);
push_cur(k+nx_*2);
push_cur(k+nx_*2+1);
}
// If precise mode is disabled, calculate potentials for each cell in the grid
else {
potential[k] = 0;
push_cur(k+1);
push_cur(k-1);
push_cur(k-nx_);
push_cur(k+nx_);
}
}
阅读全文