1
0

fp_path.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef _FP_PATH_HPP_
  2. #define _FP_PATH_HPP_
  3. #include <math.h>
  4. #include <assert.h>
  5. #include <vector>
  6. #include "base_data.h"
  7. #include "../point.h"
  8. struct fixed_point:point_2
  9. {
  10. fixed_point(double x,double y)
  11. :point_2(x,y)
  12. ,cos_(0),sin_(0)
  13. ,dist_(0)
  14. {}
  15. fixed_point(const point_2&pt)
  16. :point_2(pt)
  17. ,cos_(0),sin_(0)
  18. ,dist_(0)
  19. {}
  20. fixed_point(const fixed_point&pt)
  21. :point_2(pt)
  22. ,cos_(pt.cos_),sin_(pt.sin_)
  23. ,dist_(pt.dist_)
  24. {}
  25. double cos_;
  26. double sin_;
  27. double dist_;
  28. double set_next_point(const point_2&p)
  29. {
  30. dist_ = dist_to(p);
  31. assert(dist_>0);
  32. double deta_x = p.x_-x_;
  33. cos_ = deta_x/dist_;
  34. double deta_y = p.y_-y_;
  35. sin_ = deta_y/dist_;
  36. return dist_;
  37. }
  38. };
  39. struct fp_path
  40. {
  41. private:
  42. std::vector<fixed_point> path_;
  43. fp_path(const fp_path&);
  44. double m_total_length;
  45. private:
  46. static inline bool eq(double x1,double x2)
  47. {
  48. return fabs(x1-x2)<0.1;
  49. }
  50. public:
  51. fp_path()
  52. :m_total_length(0)
  53. {
  54. path_.reserve(8);
  55. }
  56. template<typename iterator>
  57. fp_path(iterator begin,iterator end)
  58. :m_total_length(0)
  59. {
  60. for (;begin != end; ++ begin)
  61. {
  62. add_point(*begin);
  63. }
  64. }
  65. void add_point(double x,double y)
  66. {
  67. add_point(point_2(x,y));
  68. }
  69. void add_point(const point &pt)
  70. {
  71. point_2 p(pt.x,pt.y);
  72. add_point(p);
  73. }
  74. void add_point(const point_2&pt)
  75. {
  76. if(!path_.empty())
  77. {
  78. double d = path_.back().set_next_point(pt);
  79. m_total_length += d;
  80. }
  81. path_.push_back(pt);
  82. }
  83. double map(double x,double y)
  84. {
  85. if(path_.size()<2)
  86. return -1.;
  87. double rc=0.;
  88. auto it1=path_.begin();
  89. double l1=it1->dist_to(x,y);
  90. auto it2=path_.begin(); ++it2;
  91. for(; it2!=path_.end();++it2)
  92. {
  93. double l2=it2->dist_to(x,y);
  94. if(eq(l1+l2, it1->dist_))
  95. {
  96. return rc+l1;
  97. }
  98. rc+=it1->dist_;
  99. it1=it2;
  100. l1=l2;
  101. }
  102. return -1.;
  103. }
  104. double map(const point_2& p)
  105. {
  106. return map(p.x_, p.y_);
  107. }
  108. point_2 map(double dist)
  109. {
  110. if(path_.size()>1)
  111. {
  112. for(auto it=path_.begin(), _e=path_.end()-1; it!=_e;++it)
  113. {
  114. if(dist<=it->dist_)
  115. {
  116. return point_2(it->x_+it->cos_*dist,it->y_+it->sin_*dist);
  117. }
  118. dist-=it->dist_;
  119. if(dist<0)
  120. break;
  121. }
  122. }
  123. return point_2::invalid_point();
  124. }
  125. double get_total_length()
  126. {
  127. return m_total_length;
  128. }
  129. };
  130. #endif