1
0

area.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef _AREA_HPP_
  2. #define _AREA_HPP_
  3. #include <atomic>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <point.h>
  7. #include "common.h"
  8. #include <write-copy.h>
  9. struct area_hover;
  10. struct point;
  11. struct area
  12. {
  13. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  14. :m_id(id)
  15. ,m_area_type(type)
  16. ,m_limit_person_second(limit_time_person)
  17. ,m_limit_person_count(limit_count_person)
  18. ,m_scale(scale)
  19. ,m_mapid(mapid)
  20. ,m_person_count(0)
  21. ,m_vehicle_count(0)
  22. {
  23. }
  24. virtual void on_hover(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  25. virtual void on_enter(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  26. virtual void on_leave(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  27. bool in_area(const point & p);
  28. int id()const
  29. {
  30. return m_id;
  31. }
  32. int mapid()const
  33. {
  34. return m_mapid;
  35. }
  36. double scale()const
  37. {
  38. return m_scale;
  39. }
  40. bool special()const
  41. {
  42. return m_area_type == AREA_TYPE_NO_COVER;
  43. }
  44. virtual ~area()
  45. {}
  46. ///区域卡数
  47. int card_count()const
  48. {
  49. return m_person_count + m_vehicle_count;
  50. }
  51. std::vector<point> m_bound;
  52. public:
  53. //std::atomic<int> m_card_count;
  54. int m_id;
  55. ///区域类型 AREA_TYPE
  56. int m_area_type;
  57. int m_limit_person_second;
  58. int m_limit_person_count;
  59. int m_limit_vehicle_second;
  60. int m_limit_vehicle_count;
  61. double m_scale;
  62. int32_t m_mapid;
  63. ///区域人卡数
  64. std::atomic<int> m_person_count;
  65. ///区域车卡数
  66. std::atomic<int> m_vehicle_count;
  67. };
  68. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  69. {
  70. area_list();
  71. std::shared_ptr<area> get_area(const point&pt);
  72. std::vector<point> init_path(std::string &str);
  73. void init_from_db();
  74. void init_monkeycar_area();
  75. };
  76. struct area_hover
  77. {
  78. std::shared_ptr<area> m_area;
  79. time_t m_enter_time,m_last_time;
  80. point m_enter_point,m_last_point;
  81. int landmark_id;
  82. int landmark_dir;
  83. double landmark_dis;
  84. area_hover()=default;
  85. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  86. :m_area(area)
  87. {
  88. m_enter_time=m_last_time=time(0);
  89. m_enter_point=m_last_point=pt;
  90. landmark_id=0;
  91. landmark_dir=0;
  92. landmark_dis=0;
  93. }
  94. int id()const
  95. {
  96. return m_area->id();
  97. }
  98. int mapid()const
  99. {
  100. return m_area->mapid();
  101. }
  102. double scale()const
  103. {
  104. return m_area->scale();
  105. }
  106. bool operator == (const area_hover&o)const
  107. {
  108. return m_area->id()==o.m_area->id();
  109. }
  110. bool operator < (const area_hover&o)const
  111. {
  112. return m_area->id()<o.m_area->id();
  113. }
  114. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  115. {
  116. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  117. }
  118. void setLandmark(const point &pt);
  119. void set(const point&pt)
  120. {
  121. m_last_time=time(0);
  122. m_last_point = pt;
  123. }
  124. };
  125. //每张卡包含一个对象
  126. //在解析出数据点时,调用on_point
  127. struct area_tool
  128. {
  129. std::shared_ptr<area_hover> m_area_hover=nullptr;
  130. void on_point(uint32_t card_id,const point&pt,double speed,int16_t type);
  131. void setLandmark(const point &pt)
  132. {
  133. if(m_area_hover)
  134. {
  135. m_area_hover->setLandmark(pt);
  136. }
  137. }
  138. //special area or no area att.,return true;else return false.
  139. bool special_area()
  140. {
  141. if(m_area_hover==nullptr)
  142. return true;
  143. return m_area_hover->m_area->special();
  144. }
  145. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  146. {
  147. if(m_area_hover)
  148. return m_area_hover->getLandmark();
  149. else
  150. return std::make_tuple(0,0,0,0,0,0,0);
  151. }
  152. //检测是否超时
  153. void on_timer(int64_t card_id)
  154. {
  155. }
  156. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  157. {
  158. m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  159. }
  160. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  161. {
  162. m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  163. }
  164. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  165. {
  166. m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  167. }
  168. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  169. {
  170. do_leave_biz(card_id,speed,type);
  171. auto area = area_list::instance()->get(new_areaid);
  172. point pt;
  173. m_area_hover.reset(new area_hover(area,pt,speed));
  174. do_enter_biz(card_id,speed,type);
  175. }
  176. };
  177. #endif