123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- #include "landmark.h"
- #include <cfloat>
- #include "db_api/CDBSingletonDefine.h"
- #include "log.h"
- //Landmark_list * Landmark_list::instance()
- //{
- // static Landmark_list lm_list;
- // return &lm_list;
- //}
- void Landmark_list::init_mapDirection()
- {
- const char * sql = "select map_direction_id, map_id, north_angle from dat_map_direction;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_map_direction. The record count=%d\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- int north_angle = 0;
- DBRes.GetField( "north_angle",north_angle, Error );
- std::shared_ptr<map_direction> md = std::make_shared<map_direction>(map_id,north_angle);
- m_map_direction.insert({map_id,md});
- log_info("map_direction.....mapid:%d,north:%d",map_id,north_angle);
- }
- }
- }
- void Landmark_list::init_from_db()
- {
- init_landmarkInfo();
- init_mapDirection();
- }
- void Landmark_list::init_landmarkInfo(int id/*=-1*/)
- {
- std::string sql = "select landmark_id, name, map_id, area_id, x, y, z from dat_landmark";
- if (-1 == id)
- {
- sql += ";";
- }
- else
- {
- sql += " WHERE landmark_id=" + std::to_string(id) +";";
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
-
- if (nCount > 0)
- {
- log_info( "init_landmark_info. The record count=%d\n", nCount );
- std::unordered_map<int, std::shared_ptr<LandmarkInfo>> map;
- while ( DBRes.GetNextRecod(Error) )
- {
- int landmark_id = 0;
- DBRes.GetField( "landmark_id",landmark_id, Error );
- std::string name;
- DBRes.GetField( "name",name, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- double x = 0;
- DBRes.GetField( "x",x, Error );
- double y = 0;
- DBRes.GetField( "y",y, Error );
- double z = 0;
- DBRes.GetField( "z",z, Error );
- point p(x,y,z);
- std::shared_ptr<LandmarkInfo> landmark = std::make_shared<LandmarkInfo>(p, landmark_id, name, map_id, area_id);
- if (-1 == id)
- {
- map.insert(std::make_pair(landmark_id, landmark));
- //m_v.push_back(landmark);
- 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);
- }
- else
- {
- auto markptr = Landmark_list::instance()->get(landmark_id);
- if (markptr != nullptr)
- {
- markptr->set(p);
- markptr->landmark_id = landmark_id;
- markptr->landmark_name = name;
- markptr->map_id = map_id;
- markptr->area_id = area_id;
- 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);
- }
- else
- {
- 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);
- Landmark_list::instance()->add(landmark_id, landmark);
- }
- break;
- }
- }
- if (-1== id)
- {
- Landmark_list::instance()->add(map);
- }
- }
- }
- std::tuple<int,int,double> Landmark_list::get_landmark(int mapid ,int areaid,const point &pt)
- {
- int32_t id = -1;
- double dis = DBL_MAX;
- point p;
- auto m_v = Landmark_list::instance()->m_map;
- auto it_s = m_v.begin();
- while(it_s!=m_v.end())
- {
- auto lm = it_s->second;
- if(lm->area_id == areaid)
- {
- double tmp = pt.dist(*lm);
- if(tmp < dis)
- {
- id = lm->landmark_id;
- dis=tmp;
- p=*lm;
- }
- }
- it_s++;
- }
- if(id==-1)
- return std::make_tuple(0,0,0);
- int dir = GetDirectionType(pt,p,mapid);
- return std::make_tuple(id,dir,dis);
- }
- DIRECTION_TYPE Landmark_list::GetDirectionType(point card_point, point landmark_point, int map_id)
- {
- DIRECTION_TYPE direction = NODIRECTORY;
- int x_offset = 0;
- int y_offset = 0;
- int angle = 0;
- x_offset = card_point.x - landmark_point.x;
- y_offset = card_point.y - landmark_point.y;
- if(abs(x_offset) < 1)
- x_offset = 0;
- if(abs(y_offset) < 1)
- y_offset = 0;
- std::shared_ptr<map_direction> pMapDirectoryInfo = nullptr;
- auto it = m_map_direction.find(map_id);
- if(it == m_map_direction.end())
- {
- return NODIRECTORY;
- }
- else
- {
- pMapDirectoryInfo = it->second;
- }
- if(0 == y_offset)
- {
- if(x_offset > 0)
- angle = pMapDirectoryInfo->north_angle + 0;
- if(x_offset < 0)
- angle = pMapDirectoryInfo->north_angle - 180;
- }
- else if(0 == x_offset)
- {
- if(y_offset < 0)
- angle = pMapDirectoryInfo->north_angle - 90;
- if(y_offset > 0)
- angle = pMapDirectoryInfo->north_angle + 90;
- }
- else if(abs(x_offset) >= abs(y_offset))
- {
- if(x_offset > 0)
- angle = pMapDirectoryInfo->north_angle + 0;
- if(x_offset < 0)
- angle = pMapDirectoryInfo->north_angle - 180;
- }
- else
- {
- if(y_offset < 0)
- angle = pMapDirectoryInfo->north_angle - 90;
- if(y_offset > 0)
- angle = pMapDirectoryInfo->north_angle + 90;
- }
- direction = (DIRECTION_TYPE)((angle % 360) / 90);
- if(angle % 360 == 0)
- direction = NORTH;
- if((0 == y_offset) && (0 == x_offset))
- direction = NODIRECTORY;
- return direction;
- }
|