1
0

landmark.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "landmark.h"
  2. #include <cfloat>
  3. #include "db_api/CDBSingletonDefine.h"
  4. #include "log.h"
  5. //Landmark_list * Landmark_list::instance()
  6. //{
  7. // static Landmark_list lm_list;
  8. // return &lm_list;
  9. //}
  10. void Landmark_list::init_mapDirection()
  11. {
  12. const char * sql = "select map_direction_id, map_id, north_angle from dat_map_direction;";
  13. std::string Error;
  14. YADB::CDBResultSet DBRes;
  15. sDBConnPool.Query(sql,DBRes,Error);
  16. int nCount = DBRes.GetRecordCount( Error );
  17. if (nCount > 0)
  18. {
  19. log_info( "init_map_direction. The record count=%d\n", nCount );
  20. while ( DBRes.GetNextRecod(Error) )
  21. {
  22. int map_id = 0;
  23. DBRes.GetField( "map_id",map_id, Error );
  24. int north_angle = 0;
  25. DBRes.GetField( "north_angle",north_angle, Error );
  26. std::shared_ptr<map_direction> md = std::make_shared<map_direction>(map_id,north_angle);
  27. m_map_direction.insert({map_id,md});
  28. log_info("map_direction.....mapid:%d,north:%d",map_id,north_angle);
  29. }
  30. }
  31. }
  32. void Landmark_list::init_from_db()
  33. {
  34. init_landmarkInfo();
  35. init_mapDirection();
  36. }
  37. void Landmark_list::init_landmarkInfo(int id/*=-1*/)
  38. {
  39. std::string sql = "select landmark_id, name, map_id, area_id, x, y, z from dat_landmark";
  40. if (-1 == id)
  41. {
  42. sql += ";";
  43. }
  44. else
  45. {
  46. sql += " WHERE landmark_id=" + std::to_string(id) +";";
  47. }
  48. std::string Error;
  49. YADB::CDBResultSet DBRes;
  50. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  51. int nCount = DBRes.GetRecordCount( Error );
  52. if (nCount > 0)
  53. {
  54. log_info( "init_landmark_info. The record count=%d\n", nCount );
  55. std::unordered_map<int, std::shared_ptr<LandmarkInfo>> map;
  56. while ( DBRes.GetNextRecod(Error) )
  57. {
  58. int landmark_id = 0;
  59. DBRes.GetField( "landmark_id",landmark_id, Error );
  60. std::string name;
  61. DBRes.GetField( "name",name, Error );
  62. int map_id = 0;
  63. DBRes.GetField( "map_id",map_id, Error );
  64. int area_id = 0;
  65. DBRes.GetField( "area_id",area_id, Error );
  66. double x = 0;
  67. DBRes.GetField( "x",x, Error );
  68. double y = 0;
  69. DBRes.GetField( "y",y, Error );
  70. double z = 0;
  71. DBRes.GetField( "z",z, Error );
  72. point p(x,y,z);
  73. std::shared_ptr<LandmarkInfo> landmark = std::make_shared<LandmarkInfo>(p, landmark_id, name, map_id, area_id);
  74. if (-1 == id)
  75. {
  76. map.insert(std::make_pair(landmark_id, landmark));
  77. //m_v.push_back(landmark);
  78. log_info("landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z);
  79. }
  80. else
  81. {
  82. auto markptr = Landmark_list::instance()->get(landmark_id);
  83. if (markptr != nullptr)
  84. {
  85. markptr->set(p);
  86. markptr->landmark_id = landmark_id;
  87. markptr->landmark_name = name;
  88. markptr->map_id = map_id;
  89. markptr->area_id = area_id;
  90. log_info("update landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z);
  91. }
  92. else
  93. {
  94. log_info("add landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z);
  95. Landmark_list::instance()->add(landmark_id, landmark);
  96. }
  97. break;
  98. }
  99. }
  100. if (-1== id)
  101. {
  102. Landmark_list::instance()->add(map);
  103. }
  104. }
  105. }
  106. std::tuple<int,int,double> Landmark_list::get_landmark(int mapid ,int areaid,const point &pt)
  107. {
  108. int32_t id = -1;
  109. double dis = DBL_MAX;
  110. point p;
  111. auto m_v = Landmark_list::instance()->m_map;
  112. auto it_s = m_v.begin();
  113. while(it_s!=m_v.end())
  114. {
  115. auto lm = it_s->second;
  116. if(lm->area_id == areaid)
  117. {
  118. double tmp = pt.dist(*lm);
  119. if(tmp < dis)
  120. {
  121. id = lm->landmark_id;
  122. dis=tmp;
  123. p=*lm;
  124. }
  125. }
  126. it_s++;
  127. }
  128. if(id==-1)
  129. return std::make_tuple(0,0,0);
  130. int dir = GetDirectionType(pt,p,mapid);
  131. return std::make_tuple(id,dir,dis);
  132. }
  133. DIRECTION_TYPE Landmark_list::GetDirectionType(point card_point, point landmark_point, int map_id)
  134. {
  135. DIRECTION_TYPE direction = NODIRECTORY;
  136. int x_offset = 0;
  137. int y_offset = 0;
  138. int angle = 0;
  139. x_offset = card_point.x - landmark_point.x;
  140. y_offset = card_point.y - landmark_point.y;
  141. if(abs(x_offset) < 1)
  142. x_offset = 0;
  143. if(abs(y_offset) < 1)
  144. y_offset = 0;
  145. std::shared_ptr<map_direction> pMapDirectoryInfo = nullptr;
  146. auto it = m_map_direction.find(map_id);
  147. if(it == m_map_direction.end())
  148. {
  149. return NODIRECTORY;
  150. }
  151. else
  152. {
  153. pMapDirectoryInfo = it->second;
  154. }
  155. if(0 == y_offset)
  156. {
  157. if(x_offset > 0)
  158. angle = pMapDirectoryInfo->north_angle + 0;
  159. if(x_offset < 0)
  160. angle = pMapDirectoryInfo->north_angle - 180;
  161. }
  162. else if(0 == x_offset)
  163. {
  164. if(y_offset < 0)
  165. angle = pMapDirectoryInfo->north_angle - 90;
  166. if(y_offset > 0)
  167. angle = pMapDirectoryInfo->north_angle + 90;
  168. }
  169. else if(abs(x_offset) >= abs(y_offset))
  170. {
  171. if(x_offset > 0)
  172. angle = pMapDirectoryInfo->north_angle + 0;
  173. if(x_offset < 0)
  174. angle = pMapDirectoryInfo->north_angle - 180;
  175. }
  176. else
  177. {
  178. if(y_offset < 0)
  179. angle = pMapDirectoryInfo->north_angle - 90;
  180. if(y_offset > 0)
  181. angle = pMapDirectoryInfo->north_angle + 90;
  182. }
  183. direction = (DIRECTION_TYPE)((angle % 360) / 90);
  184. if(angle % 360 == 0)
  185. direction = NORTH;
  186. if((0 == y_offset) && (0 == x_offset))
  187. direction = NODIRECTORY;
  188. return direction;
  189. }