wangzhengquan
2020-09-17 f6d7dbd2ac6938526b1023d1bc571d562b4625cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <usg_common.h>        /* cos */
#include <graph.h>
#include <IndirectAlg.h>
#include <fstream>
using namespace std;
char *ltrim(char *str, const char *seps)
{
    size_t totrim;
    if (seps == NULL) {
        seps = "\t\n\v\f\r ";
    }
    totrim = strspn(str, seps);
    if (totrim > 0) {
        size_t len = strlen(str);
        if (totrim == len) {
            str[0] = '\0';
        }
        else {
            memmove(str, str + totrim, len + 1 - totrim);
        }
    }
    return str;
}
 
char *rtrim(char *str, const char *seps)
{
    int i;
    if (seps == NULL) {
        seps = "\t\n\v\f\r ";
    }
    i = strlen(str) - 1;
    while (i >= 0 && strchr(seps, str[i]) != NULL) {
        str[i] = '\0';
        i--;
    }
    return str;
}
 
char *trim(char *str, const char *seps)
{
    return ltrim(rtrim(str, seps), seps);
}
 
 
int test2() {
    // IndirectAlg indirectAlg({  {{0.0, 0.0}, {1000.0, 0.0}}, {{1000.0, 0.0}, {1000.0, 1000.0}}, {{1000.0, 1000.0}, {0.0, 1000.0}}, {{0.0, 1000.0}, {0.0, 0.0}} }, 5, {1, 1});
    /**
     * @points  组成观测区域的彼此相邻的点
     * @_direction 正确行进的方向方向
     * @_keepTime 保持时间
     * @ minDistance 最小移动距离
     */
    IndirectAlg indirectAlg((std::initializer_list<Point>){{0.0, 0.0}, {1000.0, 0.0},  {1000.0, 1000.0}, {0.0, 1000.0} }, 
        {1, 1}, 5, 1);
    
    int i = 0;
    // time_t start_time;
    srand((unsigned) time(0));
 
    double rx, ry;
    bool isRetrograde;
    //起点
    Point start = {0, 0};
    //传入的流数据记录信息Record 
    Record record;
    record.id = 1; //目标ID 
    record.timestamp = time(0); //时间戳
    record.coordinate = start; // 坐标
    // time(&start_time);
    //判断是否逆行
    while(!(isRetrograde = indirectAlg.isRetrograde(record)) ) {
        rx = ((double)(rand()%10))/100000;
        ry = ((double)(rand()%10))/100000;
 
        //std::cout << timestamp << ":" <<  << "isRetrograde" << isRetrograde;
       // err_msg(0, "%ld : {%f, %f}  %d\n", record.timestamp, record.coordinate.x, record.coordinate.y, isRetrograde);
        record.timestamp = time(0);
        
        //if (difftime(time(0), start_time) > 1)
        if (i > 10)
        {
            record.coordinate = {record.coordinate.x - rx , record.coordinate.y - ry};
        } else {
            record.coordinate = {record.coordinate.x + rx , record.coordinate.y + ry};
        }
 
        if (i > 100)
            break;
 
        sleep(1);
        i++;
    }
    printf("return %ld : {%f, %f}  %d\n", record.timestamp, record.coordinate.x, record.coordinate.y, isRetrograde);
    return 0;
}
 
void test3() {
 
    std::ifstream fin("test.txt");
    char line[1024];
    //std::string line;
    char *targetIdStr;
    char *timestampStr;
    char *targetStr;
    char *targetStrBegin;
    char *targetStrEnd;
    char *targetPointXstr;
    char *targetPointYstr;
//{"x":148,"y":45},{"x":148,"y":539},{"x":769,"y":539},{"x":769,"y":45}
    //{148, 45}, {148, 539},  {769, 539}, {769, 45}
    Record record;
    IndirectAlg indirectAlg((std::initializer_list<Point>){}, {621, 0}, 3, 0);
    
    // const char *delim = " ";
    while(fin.getline(line, 1024)) {
        // printf("line=%s\n", line);
        if(strlen(trim(line, NULL))== 0)
            continue;
        if(*line == '#') {
            continue;
        }
 
        
 
        targetStrBegin = strchr(line, '{'); 
        targetStrEnd = strchr(line, '}'); 
 
 
       
        *targetStrBegin = 0;
        *targetStrEnd = 0;
 
        targetIdStr  = strtok(line, " ");
        timestampStr = strtok(NULL, " ");
 
        targetStr = targetStrBegin + 1;
 
        targetPointXstr =  strtok(targetStr, " ");
        targetPointYstr = strtok(NULL, " ");
        //std::cout << targetIdStr << "," << timestampStr << ","  << targetPointXstr << ","  << targetPointYstr << std::endl;
        record.id = targetIdStr;
        record.timestamp = atol(timestampStr);
        record.coordinate.x =  strtod(targetPointXstr, 0);
        record.coordinate.y =  strtod(targetPointYstr, 0);
       
        if ( indirectAlg.isRetrograde(record)) {
             std::cout << record.id  << "," <<  record.timestamp << ","  << record.coordinate.x << ","  << record.coordinate.y << std::endl;
        }
        // while(token) {
        //     puts(token);
        //     token = strtok(NULL, " ");
        // }
        
        // printf("key = %s, value=%s\n", key, value);
    }
    fin.close();
}
 
 
int main() {
    test3();
 
 
}