void run(void) { char win[] = "source"; int i; CvRect rect = { 0, 0, 600, 600 }; CvMemStorage* storage; CvSubdiv2D* subdiv; IplImage* img; CvScalar active_facet_color, delaunay_color, voronoi_color, bkgnd_color; active_facet_color = CV_RGB( 255, 0, 0 ); delaunay_color = CV_RGB( 0,0,0); voronoi_color = CV_RGB(0, 180, 0); bkgnd_color = CV_RGB(255,255,255); img = cvCreateImage( cvSize(rect.width,rect.height), 8, 3 ); cvSet( img, bkgnd_color, 0 ); cvNamedWindow( win, 1 ); storage = cvCreateMemStorage(0); subdiv = init_delaunay( storage, rect ); printf("Delaunay triangulation will be build now interactively.\n" "To stop the process, press any key\n\n"); for( i = 0; i < 200; i++ ) { CvPoint2D32f fp = cvPoint2D32f( (float)(rand()%(rect.width-10)+5), (float)(rand()%(rect.height-10)+5)); locate_point( subdiv, fp, img, active_facet_color ); cvShowImage( win, img ); if( cvWaitKey( 100 ) >= 0 ) break; cvSubdivDelaunay2DInsert( subdiv, fp ); cvCalcSubdivVoronoi2D( subdiv ); cvSet( img, bkgnd_color, 0 ); draw_subdiv( img, subdiv, delaunay_color, voronoi_color ); cvShowImage( win, img ); if( cvWaitKey( 100 ) >= 0 ) break; } cvSet( img, bkgnd_color, 0 ); paint_voronoi( subdiv, img ); cvShowImage( win, img ); cvWaitKey(0); cvReleaseMemStorage( &storage ); cvReleaseImage(&img); cvDestroyWindow( win ); }
int delaunay2( int argc, char** argv ) { cv::CommandLineParser parser(argc, argv, "{help h||}"); if (parser.has("help")) { help(); return 0; } Scalar active_facet_color(0, 0, 255), delaunay_color(255,255,255); Rect rect(0, 0, 600, 600); Subdiv2D subdiv(rect); Mat img(rect.size(), CV_8UC3); img = Scalar::all(0); string win = "Delaunay Demo"; imshow(win, img); for( int i = 0; i < 200; i++ ) { Point2f fp( (float)(rand()%(rect.width-10)+5), (float)(rand()%(rect.height-10)+5)); locate_point( img, subdiv, fp, active_facet_color ); imshow( win, img ); if( waitKey( 100 ) >= 0 ) break; subdiv.insert(fp); img = Scalar::all(0); draw_subdiv( img, subdiv, delaunay_color ); imshow( win, img ); if( waitKey( 100 ) >= 0 ) break; } img = Scalar::all(0); paint_voronoi( img, subdiv ); imshow( win, img ); waitKey(0); return 0; }
int main(int argc, char *argv[]) { if (argc != 4) { std::cout << usage << std::endl; return -1; } std::string line; std::ifstream input(argv[1], std::ifstream::in); std::getline(input, line); std::istringstream seiss(line); double sx, sy, ex, ey; seiss >> sx >> sy >> ex >> ey; point start(sx, sy), end(ex, ey); std::getline(input, line); std::istringstream niss(line); int count = 0; niss >> count; for (int i = 0; i < count; i++) { std::getline(input, line); std::istringstream iss(line); double x, y; iss >> x >> y; polygon_points.push_back(point(x, y)); } int vertices_count = polygon_points.size(); int triangles_count = polygon_points.size() - 2; double (*vertices)[2] = new double[vertices_count + 1][2]; int (*triangles)[3] = new int[triangles_count][3]; for (int i = 1; i <= polygon_points.size(); i++) { vertices[i][0] = polygon_points[i - 1].x; vertices[i][1] = polygon_points[i - 1].y; } int ncontours = 1; int cntr[1] = {polygon_points.size()}; triangulate_polygon(ncontours, cntr, vertices, triangles); for (int i = 0; i < triangles_count; i++) { triangles[i][0]--; triangles[i][1]--; triangles[i][2]--; } std::ofstream output(argv[2], std::ofstream::out); int start_tri = locate_point(triangles, triangles_count, start); int end_tri = locate_point(triangles, triangles_count, end); bool distance = false; if (strcmp(argv[3], "path") == 0) distance = false; else if (strcmp(argv[3], "distance") == 0) distance = true; if (start_tri == end_tri) { if (distance) output << (int)ceil(dist(start, end)) << std::endl; else { output << "s" << std::endl; output << "e" << std::endl; } } else { std::vector<int> triangles_list = dfs(triangles, triangles_count, start_tri, end_tri); std::vector<diagnal> d_list = diagnals_list(triangles, triangles_list); path shortest_path = simple_stupid_funnel(d_list, start, end); if (distance) { double total_dist = 0; for (int i = 0; i < shortest_path.size(); i++) { point last; point current = polygon_points[shortest_path[i]]; if (i == 0) last = start; else last = polygon_points[shortest_path[i - 1]]; total_dist += dist(last, current); } if (shortest_path.size() > 0) total_dist += dist(polygon_points[shortest_path[shortest_path.size() - 1]], end); else total_dist += dist(start, end); output << (int)ceil(total_dist) << std::endl; } else { output << "s" << std::endl; for (int i = 0; i < shortest_path.size(); i++) output << shortest_path[i] << std::endl; output << "e" << std::endl; } } delete[] vertices; delete[] triangles; return 0; }