123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- #include <unistd.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <line.h>
- #include "tool_time.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);
- }
- void test()
- {
- {
- 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});
- }
- }
- int STATUS_HELP=0x80;
- int m_id=1;
- int help_bit=0;
- time_t help_last_time=0;
- void help(int st)
- {
- time_t now=time(0);
- if((help_bit & 1) && (st & STATUS_HELP))
- {
- // 1111111111
- // ^
- help_last_time=now;
- }
- else if((help_bit & 1) && (st & STATUS_HELP)==0)
- {
- // 11111111100000
- // ^
- help_bit<<=1;
- }
- else if((help_bit & 1)==0 && (st & STATUS_HELP))
- {
- // 00000000011111
- // ^
- if((help_bit&0x3)==2)
- {
- printf("handle_help,card_id:%d\n",m_id);
- }
-
- help_last_time=now;
- help_bit<<=1;
- help_bit|=1;
- }
- else
- {
- // 11111111100000
- // ^
- if(now-help_last_time>60)
- {
- help_bit=0;
- }
- }
- }
- void gen_help(int cnt,int bit=1)
- {
- for(int i=0;i<cnt;i++)
- {
- help(bit?STATUS_HELP:0);
- sleep(1);
- printf("%d",bit);
- fflush(stdout);
- }
- }
- static uint64_t morning_of_today_ms2()
- {
- time_t now = time(0);
- return (now+8*3600)/86400*86400-8*3600;
- }
- static uint64_t morning_of_today_ms()
- {
- time_t now = time(0);
- struct tm * loc_t = localtime(&now);
- loc_t->tm_hour=0;loc_t->tm_min=0;loc_t->tm_sec=0;
- time_t x = mktime(loc_t);
- return x*1000;
- }
- int main()
- {
- unsigned now=time(0);
- std::cout<<morning_of_today_ms2()<<","<<morning_of_today_ms()<<std::endl;
- // std::cout<<"sizeof(time_t)="<<sizeof(now)<<std::endl;
- uint64_t x=now*1000LL;
- std::cout<<now<<"|||"<<x<<std::endl;
- line_v l1(point(0,0),point(10,0));
- std::cout<<l1.widen(1).to_string()<<x<<std::endl;
- line_v l2(point(10,0),point(0,0));
- std::cout<<l2.widen(1).to_string()<<x<<std::endl;
- line_v l3(point(0,0),point(10,10));
- std::cout<<l3.widen(1).to_string()<<x<<std::endl;
- line_v l4(point(0,0),point(-10,10));
- std::cout<<l4.widen(1).to_string()<<x<<std::endl;
- help_bit=0;
- gen_help(10,0);
- gen_help(60,1);
- gen_help(10,0);
- gen_help(10,1);
- printf("\n");
- help_bit=0;
- gen_help(120,1);
- printf("\n");
- help_bit=0;
- gen_help(10,0);
- gen_help(60,1);
- gen_help(70,0);
- gen_help(10,1);
- gen_help(10,0);
- gen_help(60,1);
- printf("\n");
- help_bit=0;
- gen_help(120,0);
- printf("\n");
- return 0;
- }
|