-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.h
249 lines (195 loc) · 5.88 KB
/
helper_functions.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* helper_functions.h
* Some helper functions for the 2D particle filter.
* Created on: Dec 13, 2016
* Author: Tiffany Huang
*/
#ifndef HELPER_FUNCTIONS_H_
#define HELPER_FUNCTIONS_H_
#include <sstream>
#include <fstream>
#include <math.h>
#include <vector>
#include "map.h"
/*
* Struct representing one position/control measurement.
*/
struct control_s {
double velocity; // Velocity [m/s]
double yawrate; // Yaw rate [rad/s]
};
/*
* Struct representing one ground truth position.
*/
struct ground_truth {
double x; // Global vehicle x position [m]
double y; // Global vehicle y position
double theta; // Global vehicle yaw [rad]
};
/*
* Struct representing one landmark observation measurement.
*/
struct LandmarkObs {
int id; // Id of matching landmark in the map.
double x; // Local (vehicle coordinates) x position of landmark observation [m]
double y; // Local (vehicle coordinates) y position of landmark observation [m]
};
/*
* Computes the Euclidean distance between two 2D points.
* @param (x1,y1) x and y coordinates of first point
* @param (x2,y2) x and y coordinates of second point
* @output Euclidean distance between two 2D points
*/
inline double dist(double x1, double y1, double x2, double y2) {
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
inline double * getError(double gt_x, double gt_y, double gt_theta, double pf_x, double pf_y, double pf_theta) {
static double error[3];
error[0] = fabs(pf_x - gt_x);
error[1] = fabs(pf_y - gt_y);
error[2] = fabs(pf_theta - gt_theta);
error[2] = fmod(error[2], 2.0 * M_PI);
if (error[2] > M_PI) {
error[2] = 2.0 * M_PI - error[2];
}
return error;
}
/* Reads map data from a file.
* @param filename Name of file containing map data.
* @output True if opening and reading file was successful
*/
inline bool read_map_data(std::string filename, Map& map) {
// Get file of map:
std::ifstream in_file_map(filename.c_str(),std::ifstream::in);
// Return if we can't open the file.
if (!in_file_map) {
return false;
}
// Declare single line of map file:
std::string line_map;
// Run over each single line:
while(getline(in_file_map, line_map)){
std::istringstream iss_map(line_map);
// Declare landmark values and ID:
float landmark_x_f, landmark_y_f;
int id_i;
// Read data from current line to values::
iss_map >> landmark_x_f;
iss_map >> landmark_y_f;
iss_map >> id_i;
// Declare single_landmark:
Map::single_landmark_s single_landmark_temp;
// Set values
single_landmark_temp.id_i = id_i;
single_landmark_temp.x_f = landmark_x_f;
single_landmark_temp.y_f = landmark_y_f;
// Add to landmark list of map:
map.landmark_list.push_back(single_landmark_temp);
}
return true;
}
/* Reads control data from a file.
* @param filename Name of file containing control measurements.
* @output True if opening and reading file was successful
*/
inline bool read_control_data(std::string filename, std::vector<control_s>& position_meas) {
// Get file of position measurements:
std::ifstream in_file_pos(filename.c_str(),std::ifstream::in);
// Return if we can't open the file.
if (!in_file_pos) {
return false;
}
// Declare single line of position measurement file:
std::string line_pos;
// Run over each single line:
while(getline(in_file_pos, line_pos)){
std::istringstream iss_pos(line_pos);
// Declare position values:
double velocity, yawrate;
// Declare single control measurement:
control_s meas;
//read data from line to values:
iss_pos >> velocity;
iss_pos >> yawrate;
// Set values
meas.velocity = velocity;
meas.yawrate = yawrate;
// Add to list of control measurements:
position_meas.push_back(meas);
}
return true;
}
/* Reads ground truth data from a file.
* @param filename Name of file containing ground truth.
* @output True if opening and reading file was successful
*/
inline bool read_gt_data(std::string filename, std::vector<ground_truth>& gt) {
// Get file of position measurements:
std::ifstream in_file_pos(filename.c_str(),std::ifstream::in);
// Return if we can't open the file.
if (!in_file_pos) {
return false;
}
// Declare single line of position measurement file:
std::string line_pos;
// Run over each single line:
while(getline(in_file_pos, line_pos)){
std::istringstream iss_pos(line_pos);
// Declare position values:
double x, y, azimuth;
// Declare single ground truth:
ground_truth single_gt;
//read data from line to values:
iss_pos >> x;
iss_pos >> y;
iss_pos >> azimuth;
// Set values
single_gt.x = x;
single_gt.y = y;
single_gt.theta = azimuth;
// Add to list of control measurements and ground truth:
gt.push_back(single_gt);
}
return true;
}
/* Reads landmark observation data from a file.
* @param filename Name of file containing landmark observation measurements.
* @output True if opening and reading file was successful
*/
inline bool read_landmark_data(std::string filename, std::vector<LandmarkObs>& observations) {
// Get file of landmark measurements:
std::ifstream in_file_obs(filename.c_str(),std::ifstream::in);
// Return if we can't open the file.
if (!in_file_obs) {
return false;
}
// Declare single line of landmark measurement file:
std::string line_obs;
// Run over each single line:
while(getline(in_file_obs, line_obs)){
std::istringstream iss_obs(line_obs);
// Declare position values:
double local_x, local_y;
//read data from line to values:
iss_obs >> local_x;
iss_obs >> local_y;
// Declare single landmark measurement:
LandmarkObs meas;
// Set values
meas.x = local_x;
meas.y = local_y;
// Add to list of control measurements:
observations.push_back(meas);
}
return true;
}
/* Wraps theta between [0,2pi)
* @param theta unwrapped angle
*/
inline void wrap_theta(double& theta) {
while ( theta >= 2*M_PI)
theta -= 2*M_PI;
while ( theta < 0. )
theta += 2*M_PI;
}
#endif /* HELPER_FUNCTIONS_H_ */