fp_path.h.bak 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef _FP_PATH_HPP_
  2. #define _FP_PATH_HPP_
  3. #include <math.h>
  4. #include <assert.h>
  5. #include <vector>
  6. #include <iostream>
  7. #include "base_data.h"
  8. namespace POS_21
  9. {
  10. //std::ostream& operator<< (std::ostream&stm, const point&pt)
  11. //{
  12. // return stm<<"("<<pt.x_<<", "<<pt.y_<<")";
  13. //}
  14. //猴车路径顶点
  15. struct fixed_point:point_2
  16. {
  17. fixed_point(double x,double y)
  18. :point_2(x,y)
  19. ,cos_(0),sin_(0)
  20. ,dist_(0)
  21. {}
  22. fixed_point(const point_2&pt)
  23. :point_2(pt)
  24. ,cos_(0),sin_(0)
  25. ,dist_(0)
  26. {}
  27. fixed_point(const fixed_point&pt)
  28. :point_2(pt)
  29. ,cos_(pt.cos_),sin_(pt.sin_)
  30. ,dist_(pt.dist_)
  31. {}
  32. double cos_; //当前点与下一点构成直线的余弦
  33. double sin_; //当前点与下一点构成直线的正弦
  34. double dist_; //当前点与下一点之间的距离
  35. //根据下一路径点,计算this与下一路径点连接线段的斜率(cos,sin)和长度(dist)
  36. double set_next_point(const point_2&p)
  37. {
  38. dist_ = dist_to(p);
  39. assert(dist_>0);
  40. double deta_x = p.x_-x_;
  41. cos_ = deta_x/dist_;
  42. double deta_y = p.y_-y_;
  43. sin_ = deta_y/dist_;
  44. return dist_;
  45. }
  46. };
  47. //std::ostream& operator<< (std::ostream&stm, const fixed_point&pt)
  48. //{
  49. // return stm<<"("<<pt.x_<<", "<<pt.y_<<", dist="<<pt.dist_<<")";
  50. //}
  51. //一维、二维坐标进行转换的工具类
  52. //使用this->add_point按照路径顺序加入途径点
  53. //使用this->map进行一维、二维坐标之间的相互转换
  54. struct fp_path
  55. {
  56. private:
  57. std::vector<fixed_point> path_;
  58. fp_path(const fp_path&);
  59. double m_total_length;
  60. private:
  61. static inline bool eq(double x1,double x2)
  62. {
  63. return fabs(x1-x2)<0.1; //小于10厘米的误差认为是一个点
  64. }
  65. public:
  66. fp_path()
  67. :m_total_length(0)
  68. {
  69. path_.reserve(8);
  70. }
  71. template<typename iterator>
  72. fp_path(iterator begin,iterator end)
  73. :m_total_length(0)
  74. {
  75. for (;begin != end; ++ begin)
  76. {
  77. add_point(*begin);
  78. }
  79. }
  80. //加入路径坐标
  81. void add_point(double x,double y)
  82. {
  83. add_point(point_2(x,y));
  84. }
  85. //加入路径坐标
  86. void add_point(const point_2&pt)
  87. {
  88. if(!path_.empty())
  89. {
  90. double d = path_.back().set_next_point(pt);
  91. m_total_length += d;
  92. }
  93. path_.push_back(pt);
  94. }
  95. /*
  96. 输入路径上的二维点(x,y),输出该点的一维坐标
  97. return -1. : (x,y) 不在路径上或者当前对象不包含合法路径
  98. return >=0 : (x,y) 一维坐标
  99. */
  100. double map(double x,double y)
  101. {
  102. if(path_.size()<2)
  103. return -1.;
  104. double rc=0.;
  105. auto it1=path_.begin();
  106. double l1=it1->dist_to(x,y);
  107. auto it2=path_.begin(); ++it2;
  108. for(; it2!=path_.end();++it2)
  109. {
  110. double l2=it2->dist_to(x,y);
  111. if(eq(l1+l2, it1->dist_)) //当前点在两点之间的连线上
  112. {
  113. return rc+l1;
  114. }
  115. rc+=it1->dist_;
  116. it1=it2;
  117. l1=l2;
  118. }
  119. return -1.;
  120. }
  121. double map(const point_2& p)
  122. {
  123. return map(p.x_, p.y_);
  124. }
  125. /*
  126. 输入一维坐标(dist),转换为二维坐标输出(point)
  127. 如果给定一维坐标超出当前路径长度,返回 point::invalid_point()
  128. */
  129. point_2 map(double dist)
  130. {
  131. if(path_.size()>1)
  132. {
  133. for(auto it=path_.begin(), _e=path_.end()-1; it!=_e;++it)
  134. {
  135. if(dist<=it->dist_)
  136. {
  137. return point_2(it->x_+it->cos_*dist,it->y_+it->sin_*dist);
  138. }
  139. dist-=it->dist_;
  140. if(dist<0)
  141. break;
  142. }
  143. }
  144. return point_2::invalid_point();
  145. }
  146. double get_total_length()
  147. {
  148. return m_total_length;
  149. }
  150. //void debug_out()
  151. //{
  152. // for(auto p:path_)
  153. // {
  154. // std::cout<<p<<"\n";
  155. // }
  156. //}
  157. };
  158. }
  159. #endif