-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTarget.cpp
More file actions
164 lines (114 loc) · 3.37 KB
/
Copy pathTarget.cpp
File metadata and controls
164 lines (114 loc) · 3.37 KB
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
/*
* Target.cpp
* wrapper to manage data going in and out of Kalman filter
* Brian J Gravelle
* ix.cs.uoregon.edu/~gravelle
* gravelle@cs.uoregon.edu
* See LICENSE file for licensing information and boring legal stuff
* If by some miricale you find this software useful, thanks are accepted in
* the form of chocolate, coffee, or introductions to potential employers.
*/
#include "Target.hpp"
int Target::next_id = 0;
double Target::max_dist_sqd = 6000000;
Target::Target(int _n, int _m, KALMAN_TYPE* A_init, KALMAN_TYPE* C_init, KALMAN_TYPE* Q_init, KALMAN_TYPE* R_init, KALMAN_TYPE* P_init, KALMAN_TYPE* x_hat_init) {
n = _n;
m = _m;
num_steps = 0;
num_empty_steps = 0;
radius = 10000;
id_num = next_id++;
is_counted = false;
bool success;
success = allocate_matrices(&A, &C, &Q, &R, &P, &K, n, m);
success = success && allocate_vectors(&x, &y, &x_hat, n, m);
success = success && allocate_temp_matrices(&x_hat_new, &A_T, &C_T, &id,
&temp_1, &temp_2, &temp_3, &temp_4, n, m);
if( !success ) {
printf("\n\nERROR allocating matrices in Target class\n\n");
exit(1);
}
copy_mat(A_init, A, n * n);
copy_mat(C_init, C, n * m);
copy_mat(Q_init, Q, n * n);
copy_mat(R_init, R, m * m);
copy_mat(P_init, P, n * n);
copy_mat(x_hat_init, x_hat, n);
}
Target::~Target() {
destroy_matrices(A, C, Q, R, P, K);
destroy_vectors(x, y, x_hat);
destroy_temp_matrices(x_hat_new, A_T, C_T, id, temp_1, temp_2, temp_3, temp_4);
}
void Target::update(vector<Object> &objects, double dt) {
bool found;
predict();
found = choose_next_point(objects);
correct(found);
num_steps++;
}
void Target::predict() {
::predict(x_hat, n, m, A, Q, P, x_hat_new, A_T, temp_1, temp_2);
}
void Target::correct(bool found) {
if(found) {
::correct(y, x_hat, n, m, C, R, P, K, x_hat_new, C_T, id, temp_1, temp_2, temp_3, temp_4);
} else {
num_empty_steps++;
}
}
bool Target::choose_next_point(vector<Object> &objects) {
bool found = false;
double dist, min_dist = -1;
double dx, dy;
Object* min_obj;
Point2d min_center;
Point2d prev_center(x_hat[0], x_hat[3]); //TODO this is very specific to how the kalman filter is made. please fix
for(vector<Object>::iterator it_obj = objects.begin(); it_obj != objects.end(); it_obj++) {
dist = it_obj->find_distance_sqd(prev_center);
if( (dist <= max_dist_sqd) && ((min_dist < 0) || (dist < min_dist)) ) {
min_dist = dist;
min_obj = &(*it_obj);
found = true;
}
}
if (found){
min_obj->set_is_found();
prev_obj = Object(*min_obj);
min_obj->get_center(min_center);
y[0] = min_center.x;
y[1] = min_center.y;
}
return found;
}
/************************ Getters and Setters *************************/
int Target::get_num_steps() {
return num_steps;
}
void Target::set_num_steps(int steps) {
num_steps = steps;
}
int Target::get_num_empty_steps() {
return num_empty_steps;
}
void Target::set_num_empty_steps(int steps) {
num_empty_steps = steps;
}
double Target::get_radius() {
return radius;
}
void Target::set_radius(double r) {
radius = r;
}
int Target::get_id_num(){
return id_num;
}
void Target::set_id_num(int _id_num){
id_num = _id_num;
}
bool Target::get_is_counted(){
return is_counted;
}
void Target::set_is_counted(bool new_ic /* = true */){
is_counted = new_ic;
}