line.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #ifndef _LINE_HPP_
  2. #define _LINE_HPP_
  3. #include <deque>
  4. #include "point.h"
  5. #include "log.h"
  6. struct line
  7. {
  8. double a,b,c;
  9. line()
  10. :a(0)
  11. ,b(0)
  12. ,c(0)
  13. {
  14. }
  15. line(double k,double b_)
  16. :a(k)
  17. ,b(-1)
  18. ,c(b_)
  19. {
  20. }
  21. line(double a_,double b_,double c_)
  22. :a(a_)
  23. ,b(b_)
  24. ,c(c_)
  25. {
  26. }
  27. line(const point&p0,const point&p1)
  28. {
  29. set(p0,p1);
  30. }
  31. void clear()
  32. {
  33. a=b=c=0;
  34. }
  35. bool empty() const
  36. {
  37. return a==0 && b==0 && c==0;
  38. }
  39. void set(const point&p0,const point&p1)
  40. {
  41. auto abc=p0.get_abc(p1);
  42. a=std::get<0>(abc);
  43. b=std::get<1>(abc);
  44. c=std::get<2>(abc);
  45. }
  46. std::string to_string()const
  47. {
  48. char buf[256];
  49. int len=sprintf(buf,"a=%.3lf,b=%.3lf,c=%.3lf",a,b,c);
  50. return std::string(buf,len);
  51. }
  52. double cos_k()const
  53. {
  54. if(b==0)
  55. return 0;
  56. double k=-a/b;
  57. return 1/sqrt(1+k*k);
  58. }
  59. double sin_k()const
  60. {
  61. if(b==0)
  62. return 1;
  63. double k=-a/b;
  64. return k/sqrt(1+k*k);
  65. }
  66. double arg()const
  67. {
  68. return acos(cos_k())*(sin_k()>0?1:-1);
  69. }
  70. static bool eq(double a,double b,double e)
  71. {
  72. return fabs(a-b)<e;
  73. }
  74. point projection(const point&lv)const
  75. {
  76. line pp(-b,a,b*lv.x-a*lv.y);
  77. return crossing(pp);
  78. }
  79. double dist(const point&v)const
  80. {
  81. return v.dist(projection(v));
  82. }
  83. bool parallel(const line&l2)const
  84. {
  85. return eq(cos_k(),l2.cos_k(),0.0001) && eq(sin_k(),l2.sin_k(),0.0001);
  86. }
  87. point crossing(const line&l)const
  88. {
  89. if(parallel(l))
  90. return point();
  91. return point ( (c*l.b-l.c*b) /(l.a*b-a*l.b) ,
  92. (c*l.a-l.c*a) /(l.b*a-b*l.a) );
  93. }
  94. bool contain(const point&p,double e)const
  95. {
  96. return contain(p.x,p.y,e);
  97. }
  98. bool contain(double x,double y,double e)const
  99. {
  100. return fabs(a*x+b*y+c)<e;
  101. }
  102. };
  103. struct line_v:line//线段
  104. {
  105. std::array<point,2> v;
  106. point angle;
  107. line_v()
  108. {
  109. v[0].set(0,0);
  110. v[1].set(0,0);
  111. }
  112. line_v(const point&p0,const point&p1)
  113. :line(p0,p1)
  114. {
  115. v[0]=p0;
  116. v[1]=p1;
  117. recalc();
  118. }
  119. line_v widen(double length) const
  120. {
  121. point pt1(v[0].x-length*cos(),v[0].y-length*sin());
  122. point pt2(v[1].x+length*cos(),v[1].y+length*sin());
  123. return line_v(pt1,pt2);
  124. }
  125. void swap_point()
  126. {
  127. point tmp=v[0];
  128. v[0]=v[1];
  129. v[1]=tmp;
  130. recalc();
  131. }
  132. // added by zengminguo
  133. void calc_angle()
  134. {
  135. double dx = v[1].x - v[0].x;
  136. double dy = v[1].y - v[0].y;
  137. double r = sqrt(dx*dx + dy * dy);
  138. double cos = dx / r;
  139. double sin = dy / r;
  140. angle = point(cos, sin);
  141. }
  142. //将线段转成点阵列 added by zengminguo
  143. bool calc_point_list(std::deque<point>& queue_point)
  144. {
  145. bool bRet = true;
  146. calc_angle();
  147. auto length = this->length();
  148. log_info("site_length:%f", length);
  149. //从线段的起始点往后面依次加点,含头节点不含尾节点,由于线段是首尾点相连,所以这样处理
  150. //恰好能组成一个没有重复的巷道路径集。
  151. for (int i = 0; i < length; i++)
  152. {
  153. point p(v[0].x + angle.x*i, v[0].y + angle.y*i, 0);
  154. queue_point.push_back(p);
  155. }
  156. return bRet;
  157. }
  158. void recalc()
  159. {
  160. return;
  161. double dx = v[1].x - v[0].x;
  162. double dy = v[1].y - v[0].y;
  163. double r = sqrt(dx*dx + dy*dy);
  164. double cos = dx/r;
  165. double sin = dy/r;
  166. angle = point(cos,sin);
  167. }
  168. void set_point(int i,const point&p)
  169. {
  170. v[i]=p;
  171. recalc();
  172. }
  173. double cos() const {return angle.x;}
  174. double sin() const {return angle.y;}
  175. point&operator[](int i) { return v[i]; }
  176. const point&operator[](int i)const { return v[i]; }
  177. std::string to_string()const
  178. {
  179. char buf[256];
  180. int len=sprintf(buf,"[(%.3lf,%.3lf),(%.3lf,%.3lf)]",v[0].x,v[0].y,v[1].x,v[1].y);
  181. return std::string(buf,len);
  182. }
  183. point crossing(const line&l)const
  184. {
  185. point pt=line::crossing(l);
  186. return contain(pt)?pt:point();
  187. }
  188. point crossing(const line_v&l)const
  189. {
  190. point pt=line::crossing(l);
  191. return contain(pt)&&l.contain(pt)?pt:point();
  192. }
  193. bool is_same(const line_v&l,double e=0.0001)const
  194. {
  195. return line::contain(l[0],e) && line::contain(l[1],e);
  196. }
  197. point projection(const point&p)const
  198. {
  199. point pt=line::projection(p);
  200. return contain(pt)?pt:point();
  201. }
  202. line_v projection(const line_v&lv)const
  203. {
  204. std::array<point,2> o,t;
  205. o[0]=line::projection(lv[0]);
  206. o[1]=line::projection(lv[1]);
  207. if(o[1]<o[0]) o[0].swap(o[1]);
  208. t[0]=v[0];
  209. t[1]=v[1];
  210. if(t[1]<t[0]) t[0].swap(t[1]);
  211. if(o[1] < t[0] || t[1] < o[0])
  212. return line_v(point(0,0),point(0,0));
  213. return line_v(point::max(t[0],o[0]),point::min(t[1],o[1]));
  214. }
  215. bool empty()const
  216. {
  217. return length()<0.01;
  218. }
  219. double length()const
  220. {
  221. return v[0].dist(v[1]);
  222. }
  223. bool contain(const point&p,double e=0.0001)const
  224. {
  225. return contain(p.x,p.y,e);
  226. }
  227. bool contain(double x,double y,double e=0.0001)const
  228. {
  229. return eq(v[0].dist(v[1]),v[0].dist(x,y)+v[1].dist(x,y),e);
  230. }
  231. double dist(const point&v)const
  232. {
  233. point p=projection(v);
  234. if(p.empty())
  235. return -1;
  236. return v.dist(p);
  237. }
  238. const line &as_line()const
  239. {
  240. return *this;
  241. }
  242. };
  243. struct line_r:line
  244. {
  245. std::array<point,2> v;
  246. line_r()
  247. {
  248. v[0].set(0,0);
  249. v[1].set(0,0);
  250. }
  251. line_r(const point&p0,const point&p1)
  252. :line(p0,p1)
  253. {
  254. v[0]=p0;
  255. v[1]=p1;
  256. }
  257. point&operator[](int i) { return v[i]; }
  258. const point&operator[](int i)const { return v[i]; }
  259. std::string to_string()const
  260. {
  261. char buf[256];
  262. int len=sprintf(buf,"[(%.3lf,%.3lf),(%.3lf,%.3lf)]",v[0].x,v[0].y,v[1].x,v[1].y);
  263. return std::string(buf,len);
  264. }
  265. point crossing(const line&l)const
  266. {
  267. point pt=line::crossing(l);
  268. return contain(pt)?pt:point();
  269. }
  270. point crossing(const line_r&l)const
  271. {
  272. point pt=line::crossing(l);
  273. return contain(pt)&&l.contain(pt)?pt:point();
  274. }
  275. point projection(const point&p)const
  276. {
  277. point pt=line::projection(p);
  278. return contain(pt)?pt:point();
  279. }
  280. bool invalid()const
  281. {
  282. return v[0].dist(v[1])<0.01;
  283. }
  284. bool contain(const point&p,double e=0.0001)const
  285. {
  286. return contain(p.x,p.y,e);
  287. }
  288. bool is_same_direction(const line_r &lr)
  289. {
  290. bool f=false;
  291. if(v[0]==lr[0]){
  292. if(contain(lr[1]))
  293. f=true;
  294. }
  295. else{
  296. f=(contain(lr[0]) && contain(lr[1]) && !lr.contain(v[0]))|| (lr.contain(v[0]) && lr.contain(v[1]) && !contain(lr[0]));
  297. }
  298. return f;
  299. }
  300. bool contain(double x,double y,double e=0.0001)const
  301. {
  302. return line::contain(x,y,e)?in_contain(x,y,e)||out_contain(x,y,e):false;
  303. }
  304. bool in_contain(const point &p,double e=0.0001)
  305. {
  306. return in_contain(p.x,p.y,e);
  307. }
  308. bool in_contain(double x,double y,double e=0.0001)const
  309. {
  310. return line::contain(x,y,e)?eq(v[0].dist(v[1]),v[0].dist(x,y)+v[1].dist(x,y),e):false;
  311. }
  312. bool out_contain(double x,double y,double e=0.0001)const
  313. {
  314. return line::contain(x,y,e)?(v[0].dist(x,y)>=v[1].dist(x,y)):false;
  315. }
  316. double dist(const point&v)const
  317. {
  318. point p=projection(v);
  319. if(p.empty())
  320. return -1;
  321. return v.dist(p);
  322. }
  323. const line &as_line()const
  324. {
  325. return *this;
  326. }
  327. };
  328. #endif