-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment.cpp
executable file
·166 lines (149 loc) · 2.84 KB
/
Segment.cpp
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
165
166
#include "StdAfx.h"
#include "Segment.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
CSegment::CSegment(void)
{
HCR=0;
SR=0;
VCR=0;
// In_Out=0;
// W_N=0;
}
CSegment::CSegment(int num,vector<GPoint> vec,double h, double s, double v) //reload constructor
{
nCount=num;
Seg=vec;
HCR=h;
SR=s;
VCR=v;
}
string CSegment::MytoString()
{
ostringstream convert;
convert<<nCount<<','<<stat<<','<<HCR<<','<<Lable<<','<<start_time<<','<<end_time<<','<<Duration<<','<<x_std<<','<<y_std<<','<<x_mean<<','<<y_mean;
string para=convert.str();
return para;
}
string CSegment::MyPointoString(GPoint p)
{
GPoint GP=p;
ostringstream convert;
convert.precision(10);
convert<<GP.ID<<','<<GP.Lon<<','<<GP.Lat<<','<<GP.Speed<<','<<GP.dist<<','<<Lable; //setprecision
string para=convert.str();
return para;
}
double CSegment::SetTime()
{
start_time=Seg[0].T_V;
end_time=Seg[nCount-1].T_V;
Duration=end_time-start_time;
return Duration;
}
void CSegment::Average()
{
x_mean=0;
y_mean=0;
for (int i=0;i<nCount;i++)
{
x_mean+=Seg[i].Lon;
y_mean+=Seg[i].Lat;
}
x_mean=x_mean/nCount;
y_mean=y_mean/nCount;
}
void CSegment::stdeviation()
{
x_std=0;
y_std=0;
for (int i=0;i<nCount;i++)
{
x_std+=(Seg[i].Lon-x_mean)*(Seg[i].Lon-x_mean);
y_std+=(Seg[i].Lat-y_mean)*(Seg[i].Lat-y_mean);
}
x_std=sqrt(x_std/nCount);
y_std=sqrt(y_std/nCount);
}
double CSegment::GetHCR(float Threshold) //Calculate HCR of each segment
{
float T=Threshold;
vector<double> Angle_Point;
Angle_Point.push_back(0);
int num_above_T=0;
for (int i=1;i<Seg.size();i++)
{
double a=GetAngle(Seg[i-1],Seg[i]);
Angle_Point.push_back(a);
}
vector<double> Angle_Change;
Angle_Change.push_back(0);
Angle_Change.push_back(0);
for (int j=2;j<Angle_Point.size();j++)
{
double b=abs(Angle_Point[j]-Angle_Point[j-1]);
if (b>PI)
{
b=2*PI-b;
}
Angle_Change.push_back(b);
}
for (int j=0;j<Angle_Change.size();j++)
{
if (Angle_Change[j]>T)
{
num_above_T++;
}
}
HCR=double(num_above_T)/double(Angle_Change.size()-2);
return HCR;
}
double CSegment::GetAngle(GPoint p1,GPoint p2) //Calculate the clockwise direction between two points based on positive X axis
{
double x=p2.Lon-p1.Lon;
double y=p2.Lat-p1.Lat;
double angle;
if (x==0) //when x is equal to 0, arctan() can't be calculated
{
if (y==0)
{
angle=0;
}
else if (y>0)
{
angle=PI/2;
}
else
{
angle=3*PI/2;
}
}
else
{
angle=atan2(y,x);
if (angle>0)
{
if (x<0)
{
angle=angle+PI;
}
}
else if (angle<0)
{
if (x>0)
{
angle=angle+2*PI;
}
else
{
angle=angle+PI;
}
}
}
return angle;
}
CSegment::~CSegment(void)
{
}