wangzhengquan
2020-08-24 fcf50e1eafee305e3f0bd34064f988a2ac8f5e5d
algorithm/IndirectAlg.c
@@ -19,6 +19,10 @@
    time_t firstInvalTime;
    //上次逆行的时间
    time_t lastInvalTime;
     //第一次正确行的时间
    time_t firstValTime;
    //上次逆=正确行的时间
    time_t lastValTime;
    //上一次出现的坐标
    Point lastCoordinate;
    // Status(std::string _id, time_t ftime, time_t ltime, Point coordinate): 
@@ -37,13 +41,16 @@
IndirectAlg::IndirectAlg(const initializer_list<Point> & points, const time_t & _keepTime, const Vector2 & _direction): 
    keepTime(_keepTime), direction(_direction), mterminate(false) {
    std::vector<Edge> edges;
    initializer_list<Point>::iterator iter;
    for(iter = points.begin(); iter < points.end(); iter++ ) {
        edges.push_back({*iter, *(iter +1)});
    if (points.size() > 0) {
        std::vector<Edge> edges;
        initializer_list<Point>::iterator iter;
        for(iter = points.begin(); iter < points.end(); iter++ ) {
            edges.push_back({*iter, *(iter +1)});
        }
        edges.push_back({*(points.end()), *(points.begin())});
        frame = Figure(edges);
    }
    edges.push_back({*(points.end()), *(points.begin())});
    frame = Figure(edges);
    mthread = std::thread(std::bind(&IndirectAlg::threadRoutine, this));
@@ -72,25 +79,34 @@
    map<string, Status *>::iterator iter; 
    Status *status = nullptr;
    while (!mterminate.load()) {
       // err_msg(0, "===========thread==========run" );
        std::this_thread::sleep_for(std::chrono::seconds(this->keepTime));
        std::this_thread::sleep_for(std::chrono::seconds(this->keepTime * 2));
        //sleep(this->keepTime);
        for(iter = statusMap.begin(); iter != statusMap.end(); iter++) {
            //cout<<iter->first<<' '<<iter->second<<endl;
        for(iter = statusMap.begin(); iter != statusMap.end();) {
            status = iter->second;
            if (status != nullptr) {
                if(time_t(0) - status->lastTime > this->keepTime) {
                // cout<< "iter " << iter->first <<' '<< iter->second <<  status->lastTime << endl;
               // printf("%ld, %ld, %ld\n", time(NULL), status->lastTime, (time(0) - status->lastTime));
                if(difftime(time(NULL), status->lastTime) > this->keepTime * 2) {
                    std::cout << status->id << "脱离监控区, remove from map\n";
                    delete status;
                    statusMap.erase(iter);
                }
                    iter = statusMap.erase(iter);
                    continue;
                }
            }
            iter++;
        }
    }
     
}
/**
 * for debug
 */
void IndirectAlg::printRecord(int tag, Record &record) {
     printf("%d  %ld : {%f, %f}\n", tag, record.timestamp, record.coordinate.x, record.coordinate.y);
   // printf("%d  %ld : {%f, %f}\n", tag, record.timestamp, record.coordinate.x, record.coordinate.y);
}
/**
@@ -117,6 +133,8 @@
                .lastTime = record.timestamp,
                .firstInvalTime = 0,
                .lastInvalTime = 0,
                .firstValTime = 0,
                .lastValTime = 0,
                .lastCoordinate = record.coordinate});
            statusMap.insert({record.id, status});
@@ -126,28 +144,35 @@
        return false;
    }
    status->lastTime = record.timestamp;
    Vector2 currentDirection = getVecor2(status -> lastCoordinate, record.coordinate);
    if(difftime(record.timestamp, status->lastTime) >= this->keepTime) {
//printf("update lastCoordinate curtime=%ld, lastTime=%ld, diff=%f\n", record.timestamp, status->lastTime, difftime(record.timestamp, status->lastTime));
        status->lastTime = record.timestamp;
        status->lastCoordinate = record.coordinate;
    }
    if (currentDirection.x == 0 && currentDirection.y == 0) {
        //没动
        printRecord(3, record);
        return false;
    }
    status -> lastCoordinate = record.coordinate;
    //status -> lastCoordinate = record.coordinate;
    //逆行判断 cos小于0即两个向量的夹角大于90度,表示逆行了
    if(frame.contains(record.coordinate) && getVector2Angle(direction, currentDirection) <= 0) {
        if(status->firstInvalTime ==0) {
            //第一次发生逆行
            status->firstInvalTime = status->lastInvalTime = record.timestamp;
//printf("%第一次发生逆行的时间=%ld\n", status->firstInvalTime);
            printRecord(4, record);
            return false;
            
        } else {
            status->lastInvalTime = record.timestamp;
//printf("%后续发生逆行的时间=%ld\n", status->lastInvalTime);
            //逆行时间超过keeptime
            if (status->lastInvalTime - status-> firstInvalTime > keepTime) {
            if (difftime(status->lastInvalTime, status->firstInvalTime) >= keepTime) {
                status->firstValTime = status->lastValTime = 0;
                printRecord(5, record);
                return true;
            } 
@@ -155,8 +180,20 @@
            return false;
        }
    } else {
        printRecord(7, record);
        status->firstInvalTime = status->lastInvalTime = 0;
        if(status->firstValTime ==0) {
            //记录第一次正确行走时间
            status->firstValTime = status->lastValTime = record.timestamp;
            printRecord(7, record);
        } else {
            status->lastValTime = record.timestamp;
             //正确行走时间超过keeptime
            if (difftime(status->lastValTime, status->firstValTime) >= keepTime) {
                status->firstInvalTime = status->lastInvalTime = 0;
                printRecord(8, record);
            }
        }
        return false;
    }