void Graph::generateNavGraph() { int l = map->getLength(); int h = map->getHeight(); int xExcess = l % proximity; int yExcess = h % proximity; if ( xExcess == 0 ) xExcess = proximity; if ( yExcess == 0 ) yExcess = proximity; cout << "Generating navGraph... xExcess=" << xExcess << ", yExcess=" << yExcess << ", proximity=" << proximity << endl; // create nodes Node * n; int index = 0; for( int x = xExcess/2; x < l; x += proximity ) { for( int y = yExcess/2; y < h; y += proximity ) { n = new Node(index, x, y); nodes.push_back(*n); index++; } } // this is only for a grid graph where cost of edges can only be sides of a square or diagonal of it. // also it assumes the grid base is parallel to x and y axis. in short this is a really specific code // not generic! double longedge = get_euclidian_distance(0, 0, proximity, proximity); double shortedge = get_euclidian_distance(0, 0, proximity, 0 ); // create edges vector<Node>::iterator iter; for( iter = nodes.begin(); iter != nodes.end(); iter++ ) { vector<Node> nbrs = getNeighbors(*iter); vector<Node>::iterator it; for( it = nbrs.begin(); it != nbrs.end(); it++ ) { Edge * e = new Edge( iter->getID(), it->getID() ) ; if ( iter->getX() == it->getX() || iter->getY() == it->getY() ) e->setCost(shortedge); else e->setCost(longedge); if ( !isEdge(*e) ) edges.push_back(*e); } } }
double Graph::calcCost(Node n1, Node n2) { return get_euclidian_distance(n1.getX(), n1.getY(), n2.getX(), n2.getY()); }
void analyze_2d (BinaryData *binary) { printf("Analyze 2D started (%s : %ld bytes)\n", binary->name, binary->size); // Pixel unsigned char pixel_pos[2]; unsigned char pixel_last_pos[2]; unsigned char pixel_color; // File Offsets long int cur_offset = 0; long int start_offset = 0; int first_iteration = 1; int first_curve_point = 1; unsigned int c; // Frame related unsigned int points_in_frame = 0; unsigned int points_far = 0; double frame_distance = 0.0; double frame_distance_square = 0.0; double last_distance = -1.0; // Distance curve double last_curve_progress = 0.0; double last_curve_ecart_type = -1.0; double last_curve_moyenne = -1.0; double last_curve_rsd = -1.0; unsigned int points_in_curve = 0; double curve_distance_square = 0.0; double curve_distance = 0.0; // Colors rgb_pixel_t red = {0, 0, 255, 0}; rgb_pixel_t blue = {255, 128, 128, 0}; rgb_pixel_t green = {128, 255, 128, 0}; rgb_pixel_t black = {0, 0, 0, 0}; printf (" Offset | Distance\n"); pixel_pos[1] = fgetc (binary->handler); while ((c = fgetc (binary->handler)) != EOF) { // Cortesi algorithm pixel_pos[0] = pixel_pos[1]; pixel_pos[1] = c; points_in_frame++; points_in_curve++; // Color computation double progress = ((double) cur_offset / binary->size); pixel_color = progress * 256; // Draw in bitmap rgb_pixel_t pixel = {255 - pixel_color, 128 - (pixel_color / 2.0), pixel_color, 0}; binary_draw_pixel (binary, pixel_pos, pixel); // Get the distance from the last point if (first_iteration) { pixel_last_pos[0] = pixel_pos[0]; pixel_last_pos[1] = pixel_pos[1]; } double distance = get_euclidian_distance (pixel_pos[0], pixel_pos[1], pixel_last_pos[0], pixel_last_pos[1]); frame_distance += distance; frame_distance_square += pow (distance, 2); curve_distance += distance; curve_distance_square += pow (distance, 2); if (first_iteration) last_distance = distance; double moyenne = compute_moyenne (frame_distance, points_in_frame); double ecart_type = compute_ecart_type (moyenne, frame_distance_square, points_in_frame); double last_moyenne = compute_moyenne (distance + last_distance, 2); // Draw frames if (last_moyenne > ecart_type * (moyenne / last_moyenne)) { long int maxsize = binary->size / 10; if (maxsize > 256 * 256) maxsize = 256 * 256; if (points_far++ > maxsize && frame_distance > 800000.0) { printf ("%.8lx-%.8lx | %f\n", start_offset, cur_offset, frame_distance); binary_save (binary, start_offset, cur_offset); bresenham (binary, (int)(progress * CURVE_WIDTH), CURVE_HEIGHT, (int)(progress * CURVE_WIDTH), 0, black ); points_in_frame = 0; frame_distance = 0.0; frame_distance_square = 0.0; points_far = 0; start_offset = cur_offset; } } // Draw distance curve if ((int)(CURVE_WIDTH * progress) != (int)(CURVE_WIDTH * last_curve_progress)) { double curve_moyenne = compute_moyenne (curve_distance, points_in_curve); double curve_ecart_type = compute_ecart_type (curve_moyenne, curve_distance_square, points_in_curve); double curve_rsd = compute_rsd (curve_ecart_type, curve_moyenne) * (CURVE_HEIGHT / 2.0); if (first_curve_point) { last_curve_ecart_type = curve_ecart_type; last_curve_moyenne = curve_moyenne; last_curve_rsd = curve_rsd; } bresenham (binary, (int)(last_curve_progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - last_curve_ecart_type - 1), (int)(progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - curve_ecart_type - 1), red ); bresenham (binary, (int)(last_curve_progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - last_curve_moyenne - 1), (int)(progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - curve_moyenne - 1), blue ); bresenham (binary, (int)(last_curve_progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - last_curve_rsd - 1), (int)(progress * CURVE_WIDTH), (int)(CURVE_HEIGHT - curve_rsd - 1), green ); last_curve_ecart_type = curve_ecart_type; last_curve_moyenne = curve_moyenne; last_curve_rsd = curve_rsd; last_curve_progress = progress; points_in_curve = 0; curve_distance = 0.0; curve_distance_square = 0.0; first_curve_point = 0; } // Final computation before next iteration pixel_last_pos[0] = pixel_pos[0]; pixel_last_pos[1] = pixel_pos[1]; cur_offset++; last_distance = distance; first_iteration = 0; } if (points_in_frame > 0) binary_save (binary, start_offset, cur_offset); bmp_save_distance_curve (binary); }