From e7e07f42b336bb7b5c488eb3bcc6397c0a2a03f4 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期一, 24 八月 2020 16:36:09 +0800
Subject: [PATCH] fix conflict

---
 algorithm/IndirectAlg.c |  200 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 200 insertions(+), 0 deletions(-)

diff --git a/algorithm/IndirectAlg.c b/algorithm/IndirectAlg.c
new file mode 100644
index 0000000..f9a3d51
--- /dev/null
+++ b/algorithm/IndirectAlg.c
@@ -0,0 +1,200 @@
+#include "usg_common.h"
+#include "IndirectAlg.h"
+#include "graph.h"
+ 
+#define PI 3.14159265
+ 
+using namespace std;
+
+
+struct Status {
+ 
+    // target id
+    std::string id;
+    //绗竴娆″嚭鐜扮殑鏃堕棿
+    time_t firstTime;
+    //涓婃鍑虹幇鐨勬椂闂�
+    time_t lastTime;
+    //绗竴娆¢�嗚鐨勬椂闂�
+    time_t firstInvalTime;
+    //涓婃閫嗚鐨勬椂闂�
+    time_t lastInvalTime;
+     //绗竴娆℃纭鐨勬椂闂�
+    time_t firstValTime;
+    //涓婃閫�=姝g‘琛岀殑鏃堕棿
+    time_t lastValTime;
+    //涓婁竴娆″嚭鐜扮殑鍧愭爣
+    Point lastCoordinate;
+    // Status(std::string _id, time_t ftime, time_t ltime, Point coordinate): 
+    //     id(_id), firstInvalTime(ftime), lastInvalTime(ltime), lastCoordinate(coordinate){}
+};
+
+
+IndirectAlg::IndirectAlg() {}
+
+IndirectAlg::IndirectAlg(const initializer_list<Edge> & edges, const time_t & _keepTime, const Vector2 & _direction): 
+    frame(edges), keepTime(_keepTime), direction(_direction), mterminate(false) {
+
+    mthread = std::thread(std::bind(&IndirectAlg::threadRoutine, this));
+
+}
+
+IndirectAlg::IndirectAlg(const initializer_list<Point> & points, const time_t & _keepTime, const Vector2 & _direction): 
+    keepTime(_keepTime), direction(_direction), mterminate(false) {
+    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);
+    }
+    
+
+    mthread = std::thread(std::bind(&IndirectAlg::threadRoutine, this));
+
+}
+
+IndirectAlg::~IndirectAlg() {
+    //閿�姣佸畾鏃剁嚎绋�
+    mterminate.store(true);
+    mthread.join();
+    //閿�姣乵ap
+    map<string, Status *>::iterator iter; 
+    Status *status = nullptr;
+    for(iter = statusMap.begin(); iter != statusMap.end(); iter++) {
+        //cout<<iter->first<<' '<<iter->second<<endl; 
+        status = iter->second;
+        if (status != nullptr) {
+            delete status;
+            statusMap.erase(iter);
+        }
+    }
+}
+
+void IndirectAlg::threadRoutine() {
+    //瀹氭椂娓呯悊涓嶅湪鐩戞帶鍖虹殑瀵硅薄
+    //sleep(this->keepTime);
+    map<string, Status *>::iterator iter; 
+    Status *status = nullptr;
+    while (!mterminate.load()) {
+        std::this_thread::sleep_for(std::chrono::seconds(this->keepTime * 2));
+        //sleep(this->keepTime);
+        for(iter = statusMap.begin(); iter != statusMap.end();) {
+          
+            status = iter->second;
+            if (status != nullptr) {
+                // 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;
+
+                    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);
+}
+
+/**
+ * 鏄惁閫嗚
+*/
+bool IndirectAlg::isRetrograde(Record &record) {
+    if(record.id.length() == 0) {
+        return false;
+    }
+
+    Status *status = nullptr;
+
+    map<string, Status *>::iterator statusIter = statusMap.find(record.id);
+    if( statusIter != statusMap.end() ) {
+     status = statusIter->second;
+    }
+
+    if (status == nullptr) {
+        // 绗竴娆″嚭鐜�
+        if(frame.contains(record.coordinate)) {
+            status = new Status({ 
+                .id = record.id, 
+                .firstTime = record.timestamp, 
+                .lastTime = record.timestamp,
+                .firstInvalTime = 0,
+                .lastInvalTime = 0,
+                .firstValTime = 0,
+                .lastValTime = 0,
+                .lastCoordinate = record.coordinate});
+
+            statusMap.insert({record.id, status});
+            printRecord(1, record);
+        }
+        printRecord(2, record);
+        return false;
+    }
+
+    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;
+    //閫嗚鍒ゆ柇 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 (difftime(status->lastInvalTime, status->firstInvalTime) >= keepTime) {
+                status->firstValTime = status->lastValTime = 0;
+                printRecord(5, record);
+                return true;
+            } 
+            printRecord(6, record);
+            return false;
+        }
+    } else {
+       
+        if(status->firstValTime ==0) {
+            //璁板綍绗竴娆℃纭璧版椂闂�
+            status->firstValTime = status->lastValTime = record.timestamp;
+            printRecord(7, record);
+        } else {
+            status->lastValTime = record.timestamp;
+             //姝g‘琛岃蛋鏃堕棿瓒呰繃keeptime
+            if (difftime(status->lastValTime, status->firstValTime) >= keepTime) {
+                status->firstInvalTime = status->lastInvalTime = 0;
+                printRecord(8, record);
+            } 
+        }
+
+        return false;
+    }
+     
+}
\ No newline at end of file

--
Gitblit v1.8.0