123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include <unistd.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <sstream>
- #include <vector>
- #include <line.h>
- #define log_error printf
- #define log_info printf
- struct path
- {
- std::array<line_v,2> m_line;
- path()
- {
- }
- std::string to_str() const
- {
- std::stringstream ss;
- for(int i=0;i<2;i++)
- {
- ss<<"line:" <<m_line[i].to_string()<<"slope:"<<m_line[i][0].z<< " cos:"<<m_line[i].cos()<<" sin:"<<m_line[i].sin()<<" | ";
- }
- return ss.str();
- }
- bool vaild() const
- {
- return !m_line[0].empty();
- }
- line_v & operator[](int i)
- {
- return m_line[i];
- }
- const line_v & operator[](int i) const
- {
- return m_line[i];
- }
- };
- struct tant:point
- {
- int m_id;
- void set_path(const std::vector<line_v>&v_line,std::vector<line_v>::const_iterator itm)
- {
- std::array<path,2> m_path;
- auto
- it=itm;
- for(int i=0;i<2 && it!=v_line.end();++it,++i)
- m_path[0][i]=*it;
- it=itm-1;
- for(int i=0;i<2 && it>=v_line.begin();--it,++i)
- {
- m_path[1][i]=*it;
- m_path[1][i].swap_point();
- }
- for(auto&p:m_path)
- {
- log_info("%s\n",p.to_str().c_str());
- }
- }
- void set_path2(const std::vector<line_v>&v_line_)
- {
- std::vector<line_v> vl(v_line_);
- vl.reserve(vl.size()+2);
- //找到距离天线最近的端点
- auto min_it=vl.begin();
- double dist=10;
- for(auto it=vl.begin();it!=vl.end();it++)
- {
- double d=it->as_line().dist(*this);
- if(d<dist)
- {
- dist=d;
- min_it=it;
- }
- }
- if(min_it==vl.end())
- {
- log_error("分站路径距离分站太远site_id=%d",m_id);
- return;
- }
- if(abs(min_it->v[0].dist(*this)-dist)<1)
- {
- set_path(vl,min_it);
- }
- else if(abs(min_it->v[1].dist(*this)-dist)<1)
- {
- set_path(vl,min_it+1);
- }
- else
- {
- point proj=min_it->projection(*this);
- vl.insert(min_it+1,line_v(proj,min_it->v[1]));
- min_it->set_point(1,proj);
- if(min_it->v[0].z)//slope ..555
- {
- double slope=min_it->v[0].z;
- double len_a=min_it->v[0].dist((min_it+1)->v[1]);
- double len_0=slope*min_it->length()/len_a;
- double len_1=slope-len_0;
-
- min_it->v[0].z=min_it->v[1].z=len_0;
- (min_it+1)->v[0].z=(min_it+1)->v[1].z=len_1;
- }
- set_path(vl,min_it+1);
- }
- }
- };
- void print_vl(const std::vector<line_v>&vl)
- {
- printf("-----------------------------------\n");
- for(auto&v:vl)
- {
- printf("%s\n",v.to_string().c_str());
- }
- }
- void set_path(const std::vector<line_v>&v_line,const std::vector<double>&slope)
- {
- if(v_line.empty())
- return;
- const auto&find_line=[](const point&pt,int first,std::vector<line_v>&vl){
- for(auto it=vl.begin();it!=vl.end();it++)
- {
- if(it->v[first].dist(pt)<1)
- return it;
- if(it->v[first?0:1].dist(pt)<1)
- {
- point p=it->v[0];
- it->v[0]=it->v[1];
- it->v[1]=p;
- return it;
- }
- }
- return vl.end();
- };
-
- //构造一个首尾相连的结构
- print_vl(v_line);
- std::vector<line_v> vl(v_line.begin()+1,v_line.end());
- std::vector<line_v> target(v_line.begin(),v_line.begin()+1);
- target[0][0].z=target[0][1].z=slope[0];
- for(int i=0,c=vl.size();i<c;i++)
- vl[i][0].z=vl[i][1].z=slope[i+1];
- for(;;)
- {
- auto it=find_line(target.back().v[1],0,vl);
- if(it==vl.end())
- break;
- target.insert(target.end(),it,it+1);
- vl.erase(it);
- }
- for(;;)
- {
- auto it=find_line(target.front().v[0],1,vl);
- if(it==vl.end())
- break;
- target.insert(target.begin(),it,it+1);
- vl.erase(it);
- }
- print_vl(target);
- tant ta;
- ta.set(100,100);
- ta.set_path2(target);
- }
- int main()
- {
- {
- std::vector<line_v> vl;
- vl.push_back(line_v(point(-10,0),point(0,0)));
- vl.push_back(line_v(point(0,0),point(100,100.1)));
- print_vl(vl);
- set_path(vl,{10,20});
- }
- {
- std::vector<line_v> vl;
- vl.push_back(line_v(point(200,200),point(100,100.1)));
- vl.push_back(line_v(point(200,200),point(200,220)));
- set_path(vl,{10,20});
- }
- {
- std::vector<line_v> vl;
- vl.push_back(line_v(point(-10,0),point(0,0)));
- vl.push_back(line_v(point(0,0),point(200,200)));
- set_path(vl,{10,20});
- }
- {
- std::vector<line_v> vl;
- vl.push_back(line_v(point(0,0),point(200,200)));
- set_path(vl,{10});
- }
- return 0;
- }
|