Henkk

导航

图论---可视区域获取(C++)

1.库获取

    开源库地址:http://en.wikipedia.org/wiki/Visibility_graph

    网盘地址:链接:https://pan.baidu.com/s/1AMwkpKqWbDuGRUedK28MOw  提取码:zs9o

2. 使用

  使用处包含头文件 #include "visilibity.hpp" 即可,以下面在Qt中使用为例:

  1 /*
  2 =========A VisiLibity Example Program=========
  3 This program provides a text interface which will
  4 
  5 (1) read an environment and guard locations from files given as
  6 command line arguments,
  7 
  8 (2) check the geometric validity of these inputs (edges of the
  9 environment do not cross each other, guards are contained in the
 10 environment, etc.),
 11 
 12 (3) display environment and guards' data and statistics, and
 13 
 14 (4) compute and display the visibility polygon of a guard chosen by
 15     the user.
 16 
 17 The environment representation and the guard locations can be read
 18 from any files in the (human-readable) format demonstrated in
 19 example.environment and example.guards.
 20 
 21 Instructions: use the accompanying makefile to compile, then from
 22 command line run
 23 ./main [environment file] [guards file]
 24 ,e.g., using the example files included with your VisiLibity download,
 25 ./main example.environment example.guards
 26 */
 27 
 28 #include "visilibity.hpp" //VisiLibity header file
 29 #include <cmath>          //Puts math functions in std namespace
 30 #include <cstdlib>        //Gives rand, srand, exit
 31 #include <cstring>        //Gives C-string manipulation
 32 #include <ctime>          //Gives Unix time
 33 #include <fstream>        //File I/O
 34 #include <iostream>       //std I/O
 35 #include <sstream>        //Gives string streams
 36 #include <string>         //Gives string class
 37 #include <vector>         //std vectors
 38 //#define NDEBUG           //Turns off assert.
 39 #include <cassert>
 40 
 41 // ASCII escape sequences for colored terminal text.
 42 std::string alert("\a");       // Beep
 43 std::string normal("\x1b[0m"); // Designated fg color default bg color
 44 std::string red("\x1b[31m");
 45 std::string red_blink("\x1b[5;31m");
 46 std::string black("\E[30;47m");
 47 std::string green("\E[32m");
 48 std::string yellow("\E[33;40m");
 49 std::string blue("\E[34;47m");
 50 std::string magenta("\x1b[35m");
 51 std::string cyan("\E[36m");
 52 std::string white_bold("\E[1;37;40m");
 53 std::string clear_display("\E[2J");
 54 
 55 //=========================Main=========================//
 56 int main(int argc, char *argv[]) {
 57     std::cout << argv[0] << std::endl;
 58     std::cout << argv[1] << std::endl;
 59     std::cout << argv[2] << std::endl;
 60 
 61   // Check input validity
 62   if (argc > 3) {
 63     std::cerr << "Error: too many input arguments" << std::endl;
 64     exit(1);
 65   }
 66 
 67   // Set iostream floating-point display format
 68   const int IOS_PRECISION = 10;
 69   std::cout.setf(std::ios::fixed);
 70   std::cout.setf(std::ios::showpoint);
 71   std::cout.precision(IOS_PRECISION);
 72 
 73   // Seed the rand() fnc w/Unix time
 74   //(only necessary once at the beginning of the program)
 75   std::srand(std::time(NULL));
 76   rand();
 77 
 78   // Set geometric robustness constant
 79   //:WARNING:
 80   // may need to modify epsilon for Environments with greatly varying
 81   // scale of features
 82   double epsilon = 0.000000001;
 83   std::cout << green << "The robustness constant epsilon is set to " << epsilon
 84             << normal << std::endl;
 85 
 86   /*----------Load Geometry from Files----------*/
 87 
 88   // Load geometric environment model from file
 89   std::cout << "Loading environment file ";
 90   std::string environment_file(argv[1]);
 91   // Print environment filename to screen
 92   std::cout << environment_file << " . . . ";
 93   // Construct Environment object from file
 94   VisiLibity::Environment my_environment(environment_file);
 95   std::cout << "OK" << std::endl;
 96 
 97   // Load guard positions from file
 98   std::cout << "Loading guards file ";
 99   std::string guards_file(argv[2]);
100   // Print guards filename to screen
101   std::cout << guards_file << " . . . ";
102   // Construct Guards object from file
103   VisiLibity::Guards my_guards(guards_file);
104   std::cout << "OK" << std::endl;
105 
106   /*----------Check Validity of Geometry----------*/
107 
108   // Check Environment is epsilon-valid
109   std::cout << "Validating environment model . . . ";
110   if (my_environment.is_valid(epsilon))
111     std::cout << "OK" << std::endl;
112   else {
113     std::cout << std::endl
114               << red << "Warning:  Environment model "
115               << "is invalid." << std::endl
116               << "A valid environment model must have" << std::endl
117               << "   1) outer boundary and holes pairwise "
118               << "epsilon -disjoint simple polygons" << std::endl
119               << "   (no two features should come "
120               << "within epsilon of each other)," << std::endl
121               << "   2) outer boundary is oriented ccw, and" << std::endl
122               << "   3) holes are oriented cw." << std::endl
123               << normal;
124     exit(1);
125   }
126 
127   // Check Guards are all in the Environment
128   std::cout << "Checking all guards are "
129             << "in the environment and noncolocated . . . ";
130   my_guards.snap_to_boundary_of(my_environment, epsilon);
131   my_guards.snap_to_vertices_of(my_environment, epsilon);
132   for (unsigned i = 0; i < my_guards.N(); i++) {
133     if (!my_guards[i].in(my_environment, epsilon)) {
134       std::cout << std::endl
135                 << red << "Warning:  guard " << i << " not in the environment."
136                 << normal << std::endl;
137       exit(1);
138     }
139   }
140   if (!my_guards.noncolocated(epsilon)) {
141     std::cout << std::endl
142               << red << "Warning:  Some guards are colocated." << normal
143               << std::endl;
144     exit(1);
145   } else
146     std::cout << "OK" << std::endl;
147 
148   /*----------Print Data and Statistics to Screen----------*/
149 
150   // Environment data
151   std::cout << "The environment model is:" << std::endl;
152   std::cout << magenta << my_environment << normal;
153 
154   // Environment stats
155   std::cout << "This environment has " << cyan << my_environment.n()
156             << " vertices, " << my_environment.r() << " reflex vertices, "
157             << my_environment.h() << " holes, "
158             << "area " << my_environment.area() << ", "
159             << "boundary length " << my_environment.boundary_length() << ", "
160             << "diameter " << my_environment.diameter() << "." << normal
161             << std::endl;
162 
163   // Guards data
164   std::cout << "The guards' positions are:" << std::endl;
165   std::cout << magenta << my_guards << normal;
166 
167   // Guards stats
168   std::cout << "There are " << cyan << my_guards.N() << " guards." << normal
169             << std::endl;
170 
171   /*----------Compute the Visibility Polygon
172                    of a Guard Chosen by User----------*/
173 
174   // Prompt user
175   int guard_choice(0);
176   std::cout << "Which guard would you like "
177             << "to compute the visibility polygon of "
178             << "(0, 1, 2, ...)? " << std::endl;
179   std::cin >> guard_choice;
180   std::cout << normal;
181 
182   // Compute and display visibility polygon
183   VisiLibity::Visibility_Polygon my_visibility_polygon(my_guards[guard_choice],
184                                                        my_environment, epsilon);
185   std::cout << "The visibility polygon is" << std::endl
186             << magenta << my_visibility_polygon << normal << std::endl;
187 
188   /*
189   //To save the visibility polygon in an Environment file
190   VisiLibity::Environment(my_visibility_polygon)
191     .write_to_file("./example_visibility_polygon.cin", IOS_PRECISION);
192   */
193 
194   return 0;
195 }
View Code

    注:运行时需要将两个输入文件添加到运行参数同时将这两个文件放入到运行目录。

     

     example1.environment文件说明: 

    

   example1.guards文件说明:

   

  运行程序结果: 

  

 

posted on 2024-01-24 09:42  Henkk  阅读(2)  评论(0编辑  收藏  举报