monkey.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. struct context
  2. {
  3. };
  4. //--------------------------------
  5. struct point_2
  6. {
  7. double x_,y_;
  8. };
  9. struct st_coord :point_2
  10. {
  11. uint64_t gen_time_; //精确到毫秒的测距时间
  12. uint16_t m_ct;//ct
  13. };
  14. template<typename T>
  15. struct fixed_deque:std::deque<T>
  16. {
  17. const size_t max_size=100;
  18. };
  19. struct card
  20. {
  21. const static int MAX_HIS_LOCATION=100;
  22. std::shared_ptr<Card> m_card;
  23. //
  24. std::shared_ptr<area> m_area;
  25. //
  26. fixed_deque<st_coord> ori_list_;
  27. //
  28. fixed_deque<point_2> view_list_;
  29. st_coord pin_point()
  30. {
  31. ori_list_.push_back(...);
  32. if(ori_list_.size()>MAX_HIS_LOCATION)
  33. {
  34. ori_list_.pop_front();
  35. }
  36. return ori_list_.back();
  37. }
  38. void add_view_point(const point_2&pt)
  39. {
  40. view_list_.push_back(pt);
  41. if(view_list_.size()>MAX_HIS_LOCATION)
  42. {
  43. view_list_.pop_front();
  44. }
  45. }
  46. };
  47. //
  48. struct db_area
  49. {
  50. int m_areaid;
  51. std::vector<point_2> m_point;
  52. double m_default_speed;
  53. };
  54. struct area
  55. {
  56. area(std::shared_ptr<db_area> ptr)
  57. :db_area_(ptr)
  58. {
  59. }
  60. virtual ~area()
  61. {
  62. }
  63. std::shared_ptr<db_area> db_area_;
  64. std::shared_ptr<context> context_;
  65. virtual void on_card_enter(std::shared_ptr<Card> card, int prev_area){}
  66. virtual void on_card_move(const std::string& cardid){}
  67. virtual void on_card_leave(const std::string& cardid, int next_area){}
  68. virtual int get_card_count()const{}
  69. virtual void updata_position(){}
  70. };
  71. struct person
  72. {
  73. std::shared_ptr<card> card_;
  74. time_t enter_time_;
  75. };
  76. struct comp_linear_fit
  77. {
  78. std::map<int,linear_fit*> m_fit;
  79. comp_linear_fit(const int* begin,const int* end)
  80. {
  81. for(;begin!=end;++begin)
  82. {
  83. m_fit.insert(std::make_pair(*begin,new linear_fit(*begin)));
  84. }
  85. }
  86. ~comp_linear_fit()
  87. {
  88. for_each(m_fit.begin(),m_fit.end(),[](auto it){delete it->second;});
  89. }
  90. double getY(int num_point,uint64_t time)const
  91. {
  92. auto it=m_fit.find(num_point);
  93. if(it==m_fit.end())
  94. {
  95. //throw
  96. return 0.0;
  97. }
  98. return it->second->getY(time);
  99. }
  100. double getK(int num_point,uint64_t time)const
  101. {
  102. auto it=m_fit.find(num_point);
  103. if(it==m_fit.end())
  104. {
  105. //throw
  106. return 0.0;
  107. }
  108. return it->second->getK(time);
  109. }
  110. void push(uint64_t time, double x)
  111. {
  112. for_each(m_fit.begin(),m_fit.end(),[&](auto it){it->second->push(time,x);});
  113. }
  114. };
  115. struct monkey_person
  116. {
  117. std::shared_ptr<monkey_bus> m_bus;
  118. std::shared_ptr<person> m_person;
  119. fixed_deque<st_coord> m_d_ori;
  120. st_coord m_go_up_point;
  121. comp_linear_fit* m_linear_fit;
  122. monkey_person()
  123. {
  124. static const int num_fit_point[]={5,10,20,30};
  125. m_linear_fit=new comp_linear_fit(&num_fit_point[0],&num_fit_point[0]+sizeof(num_fit_point)/sizeof(num_fit_point[0]));
  126. }
  127. ~monkey_person()
  128. {
  129. delete m_linear_fit;
  130. }
  131. bool is_on_bus() const
  132. {
  133. return !!m_bus;
  134. }
  135. void on_step_map(const fp_path&path)
  136. {
  137. st_coord pt=m_person->pin_point();
  138. pt.x_=path.map(pt);
  139. pt.y=0;
  140. m_d_ori.push_back(pt);
  141. m_linear_fit.push(pt.gen_time_,pt.x);
  142. }
  143. bool fit_speed(double*ret_speed)
  144. {
  145. int num_point=0;
  146. if()
  147. return false;
  148. *ret_speed=m_linear_fit.getK(num_point);
  149. return true;
  150. }
  151. void updata_position()
  152. {
  153. }
  154. }
  155. //-----------------------------------------
  156. struct monkey_bus
  157. {
  158. monkey_bus(double speed, int direct)
  159. :m_speed(speed)
  160. ,m_direct(direct)
  161. {
  162. }
  163. std::list<std::shared_ptr<monkey_person>> person_list_;
  164. std::vector<double> m_AverageSpeed;
  165. double m_speed;
  166. int m_direct;
  167. bool test_get_on(double speed)
  168. {
  169. }
  170. bool test_get_off(double speed)
  171. {
  172. }
  173. void Set_AverageSpeed()
  174. {
  175. if(m_AverageSpeed.size()%2000 == 0)
  176. {
  177. //get Speed k-means
  178. }
  179. }
  180. void updata_position()
  181. {
  182. for(auto person:person_list_)
  183. person.updata_position();
  184. }
  185. };
  186. struct monkey_area :area
  187. {
  188. std::map<std::string,std::shared_ptr<monkey_person>> card_map_;
  189. std::vector<monkey_bus *> bus_;
  190. fp_path m_path;
  191. monkey_area(std::shared_ptr<db_area> db_areaptr)
  192. :area(db_areaptr)
  193. ,m_path(db_areaptr->m_point.begin(),db_areaptr->m_point.end())
  194. {
  195. for(int i=0;i<2;i++)
  196. {
  197. //反向
  198. monkey_bus * bus = new monkey_bus(db_area_->m_default_speed,1);
  199. bus_.push_back(bus);
  200. //正向
  201. //这里需要吧vector里面的字段逆序,然后放入到fp_path里
  202. monkey_bus * bus = new monkey_bus(db_area_->m_default_speed,-1);
  203. bus_.push_back(bus);
  204. }
  205. }
  206. ~monkey_area()
  207. {
  208. for_each(bus_.begin(),bus_.end(),[](auto bus){delete bus;});
  209. }
  210. std::shared_ptr<monkey_person> find(std::string cardid)
  211. {
  212. auto it = card_map_.find(cardid)
  213. if(it == card_map_.end())
  214. return nullptr;
  215. return it->second;
  216. }
  217. virtual void updata_position()
  218. {
  219. for(auto bus:bus_)
  220. bus.updata_position();
  221. }
  222. virtual void on_card_leave(const std::string& cardid, int next_area)
  223. {
  224. }
  225. virtual void on_card_enter(std::shared_ptr<Card> card, int prev_area)
  226. {
  227. new monkey_person;
  228. insert(cardid,monkey_person);
  229. }
  230. virtual void on_card_move(const std::string& cardid)
  231. {
  232. auto mp=find(cardid);
  233. mp.on_step_map(m_path);
  234. double speed=0;
  235. bool b=mp.fit_speed(&speed);
  236. //on bus
  237. if(mp->is_on_bus())
  238. {
  239. if(mp->m_bus->test_get_off(mp,speed))
  240. {
  241. mp->m_bus->get_off();
  242. }
  243. }
  244. else
  245. {
  246. monkey_bus*bus=m_bus[speed>0?0:1];
  247. if(bus->test_get_on(mp,speed))
  248. {
  249. bus->get_on(mp);
  250. }
  251. }
  252. }
  253. };