首页 > 编程语言 >图论---可视区域获取(C++)

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

时间:2024-01-24 09:58:44浏览次数:24  
标签:std environment 图论 string C++ --- guards include Gives

1.开源库获取

     地址:http://en.wikipedia.org/wiki/Visibility_graph

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文件说明:

   

  运行程序结果: 

  

 

标签:std,environment,图论,string,C++,---,guards,include,Gives
From: https://www.cnblogs.com/henkk/p/17983938

相关文章

  • VCL界面组件DevExpress VCL v23.2亮点 - 高DPI / SVG支持
    DevExpressVCL是Devexpress公司旗下最老牌的用户界面套包,所包含的控件有:数据录入、图表、数据分析、导航、布局等。该控件能帮助您创建优异的用户体验,提供高影响力的业务解决方案,并利用您现有的VCL技能为未来构建下一代应用程序。DevExpressVCLv23.2已于日前正式发布,新版本宣......
  • 基于 pytorch-openpose 实现 “多目标” 人体姿态估计
    前言还记得上次通过MediaPipe估计人体姿态关键点驱动3D角色模型,虽然节省了动作K帧时间,但是网上还有一种似乎更方便的方法。MagicAnimate就是其一,说是只要提供一张人物图片和一段动作视频(舞蹈武术等),就可以完成图片人物转视频。于是我就去官网体验了一下,发现动作的视频......
  • 【2024-01-23】增肌保养
    20:00是的春天需要你。经常会有一颗星,等着你抬头去看。                                                 ——里尔克这两天挺冷的,不过有了上个月十几天的速冻经验,衣着......
  • 【2024-01-22】回乡所闻
    20:00我的意识中,母亲像一棵树,父亲像一座山。他们教育我很多朴素的为人处世的道理,令我终生受益。我觉得,对于每一个人,父母早期的家教都具有初级的朴素的人文元素。                                    ......
  • 路由策略(前缀列表,策略工具-filter-policy,策略工具-Router-policy,双点双向路由重发布)
    1.前缀列表默认是拒绝,如果没写允许,就都是拒绝Greater-equal26less-equal32从子网掩码26-32被匹配,其他的被拒绝2.策略工具1:filter-policy(过滤策略)Export只对引入的路由,,对引入的路由在过滤,是不是发给我的邻居使用,import对所有路由器都可用*ospf:import*R1传......
  • 其他-代码折叠-Markdown-HTML
    亲测有效-兼容性强但是需要代码中间没有空格,其他的语法折叠方法兼容性若容易崩。Codeintfound(inta[],intleft,intright,intx){while(left<right){intmid=(right+left)>>1;if(a[mid]<x)left=mid+1;else......
  • react-bmapgl地图的使用
    importReact,{Component}from'react';import{Map,Marker,NavigationControl,InfoWindow,MapApiLoaderHOC,ZoomControl}from'react-bmapgl';classAppextendsComponent{render(){return(<divclass......
  • 《数学分析习题课讲义2.1-2.2》
    ......
  • 集训队论文浅读 - 信息学竞赛中构造题的常用解题方法
    抽屉原理把\(n\)个物品放入\(k\)个抽屉中,其中至少有一个抽屉中有\(\lceil\dfrac{n}{k}\rceil\)个物品,并一定有一个抽屉包含\(\lfloor\dfrac{n}{k}\rfloor\)个物品。构造题中考虑构造不同情况的抽屉,应对构造权值类问题。对于取整符号要敏感。Codeforces1450C2构......
  • [转帖]深入JVM - Code Cache内存池
    深入JVM-CodeCache内存池1.本文内容本文简要介绍JVM的CodeCache(本地代码缓存池)。2.CodeCache简要介绍简单来说,JVM会将字节码编译为本地机器码,并使用CodeCache来保存。每一个可执行的本地代码块,称为一个nmethod。nmethod可能对应一个完整的Java方法,或......