target.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <mtt/target.h>
33 
34 ostream& operator<<(ostream& o,Target& t)
35 {
36  o<<"target: "<<t._id;
37  o<<" (#"<<t._uid<<")";
38  o<<" cluster: "<<t._cluster;
39  o<<" hypothesis: "<<t._hypothesis;
40  o<<" miss associations: "<<t._missed_associations;
41  o<<" measurements size: "<<t._assigned_measurements.size();
42  return o;
43 }
44 
46 {
47  stt=10;
48 
50 }
51 
53 {
56 }
57 
59 {
61 }
62 
63 double Target::getDistance(MeasurementPtr& mes,int algorithm)
64 {
65  //calculate the Mahalanobis between the node and the cluster
66  Vector2d measurement;
67  Vector2d mean;
68  Matrix2d covariance;
69 // measurement(0)=mes->centroid.x;
70 // measurement(1)=mes->centroid.y;
71 
72  measurement(0)=mes->state[0];
73  measurement(1)=mes->state[1];
74 
75  mean(0)=_xp(0);
76  mean(1)=_xp(1);
77 
78  covariance(0,0)=_P(0,0);
79  covariance(0,1)=_P(0,1);
80 
81  covariance(1,0)=_P(1,0);
82  covariance(1,1)=_P(1,1);
83 
84  if(algorithm==0)//Mahalanobis
85  {
86  double val=mahalanobis(measurement,mean,covariance);
87 // cout<<"Dist calc, t: "<<_id<<" m: "<<mes->id<<" mah: "<<val<<endl;
88  return val;
89  }
90  else if(algorithm==1)//Bivariate pdf
91  {
92  double val=biVariatePDF(measurement,mean,covariance);
93 // cout<<"Dist calc, t: "<<_id<<" m: "<<mes->id<<" biv: "<<val<<endl;
94  return val;
95  }
96  else if(algorithm==2)//Euclidean
97  {
98  double val=sqrt(pow(measurement(0)-mean(0),2)+pow(measurement(1)-mean(1),2));
99  return val;
100  }
101 
102  return -1;
103 }
104 
106 {
107  _hypothesis = -1;
108  _cluster = -1;
109 // _id = -1;
110  _uid = _euid++;
112 }
113 
115 {
116 // cout<<"deleted: "<<*this<<endl;
117 // _assigned_measurements.clear();
118 // _past_states.clear();
119 }
120 
122 {
123  //No association
124  *this=*prev_target;
125 
126  //Copy estimator state
127  initEstimator(prev_target->_x,prev_target->_P);
128 
129 // kVector xx = _estimator.getX();
130 // kMatrix PP = _estimator.calculateP();
131 // cout<<"XX: "<<xx<<endl;
132 // cout<<"PP: "<<PP<<endl;
133 
134  assert(is_finite(prev_target->_x));
135  assert(is_finite(prev_target->_P));
136 
137  _id=prev_target->_id;
138  _uid = _euid++;
139 
140  Vector3d z;
141 
142  z(0)=prev_target->_xp(0);
143  z(1)=prev_target->_xp(1);
144  z(2)=prev_target->_xp(3);
145 
147 
150 
151  _assigned_measurements.clear();
152 
153 // cout<<"step in no association"<<endl;
154 //
155  stepEstimator(z);
156 // cout<<"step done"<<endl;
157 
158  if(_past_states.size()>500)
159  _past_states.erase(_past_states.begin());
160 
161  _past_states.push_back(_x);//Make a log of all past positions
162 
163 // if(!is_finite(_xp))
164 // {
165 // cout<<"_x: "<<_x<<endl;
166 // cout<<"p _x: "<<prev_target->_x<<endl;
167 // cout<<"z: "<<z<<endl;
168 // cout<<"miss association"<<endl;
169 // }
170 
171  if(!is_finite(_xp))
172  {
173  const char COL_RESET[] = "\x1b[0m";
174  const char RED[] = "\x1b[31m";
175  cout << RED << "Error!!, Major" << COL_RESET << endl;
176  cout<<"Bad failed target: "<<*this<<endl;
177  }
178 
179 // assert(is_finite(_xp));
180 // assert(is_finite(_P));
181 }
182 
183 Target::Target(TargetPtr prev_target,MeasurementPtr measurement)
184 {
185 // cout<<"NORMAL ASSOCIATION"<<endl;
186  //Good association
187 
188  *this=*prev_target;
189 
190  initEstimator(prev_target->_x,prev_target->_P);
191 
194 
195  _id=prev_target->_id;
196  _uid = _euid++;
197 
198 
199  _assigned_measurements.clear();
200  _assigned_measurements.push_back(measurement);
201 
203 
204  assert(is_finite(measurement->state));
205 
206 // cout<<"step in normal"<<endl;
207  stepEstimator(measurement->state);
208 
209  if(_past_states.size()>500)
210  _past_states.erase(_past_states.begin());
211 
212  _past_states.push_back(_x);//Make a log of all past positions
213 
214  if(!is_finite(_xp))
215  cout<<"Bad normal target: "<<*this<<endl;
216 }
217 
219 {
220  //New target
221  Vector3d z = measurement->state;
222 // Vector3d
223 
224  assert(is_finite(measurement->state));
225 
226 // cout<<"here"<<endl;
227  initEstimator(z);
228 // cout<<"t"<<endl;
229 
230  _past_states.clear();//New target
231  _past_states.push_back(_x);//Make a log of all past positions
232 
233  _hypothesis = 0;
234  _cluster = 0;
236 
239 
240  if(!is_finite(_xp))
241  cout<<"Bad new target: "<<*this<<endl;
242 
243  _assigned_measurements.clear();
244  _assigned_measurements.push_back(measurement);
245 
246 // cout<<"new target"<<endl;
247 // assert(is_finite(_xp));
248 // assert(is_finite(_P));
249 
250 // _id=_uid++;
251 
252  _ntotal++;
253  _id = _ntotal;//To regular use
254 // _id = measurement->id;//Just with matlab ground truth data, the measurement id IS the ground truth.
255  //We initialize each target with the correct id and check at each iteration if the association was good
256  _uid = _euid++;
257 
258 
259  _exuid = measurement->id;
260 
261 // cout<<"tc: "<<_id<<" uid: "<<_uid<<endl;
262  cout<<"||||||||!!!!!!!!"<<endl;
263  cout<<"_ntotal: "<<_ntotal<<endl;
264 }
265 
267 {
268  //calculate the Mahalanobis between the current target and the new target
269  Vector2d predicted_position;
270  Vector2d mean;
271  Matrix2d covariance;
272 
273  predicted_position(0)=target->_xp(0);
274  predicted_position(1)=target->_xp(1);
275 
276  mean(0)=_xp(0);
277  mean(1)=_xp(1);
278 
279  covariance(0,0)=_P(0,0);
280  covariance(0,1)=_P(0,1);
281 
282  covariance(1,0)=_P(1,0);
283  covariance(1,1)=_P(1,1);
284 
285  if(algorithm==0)
286  {
287  double val=mahalanobis(predicted_position,mean,covariance);
288 // cout<<"Dist calc, t: "<<_id<<" m: "<<mes->id<<" mah: "<<val<<endl;
289  return val;
290  }
291  else
292  {
293  double val=biVariatePDF(predicted_position,mean,covariance);
294 // cout<<"Dist calc, t: "<<_id<<" m: "<<mes->id<<" biv: "<<val<<endl;
295  return val;
296  }
297 }
298 
300 {
301  _assigned_measurements.clear();
302 }
303 
305 {
306  return t1->_id<t2->_id;
307 }
308 
310 {
311  return t1->_aux1<t2->_aux1;
312 }
313 
315 {
316  return t1->_aux1>t2->_aux1;
317 }
double stt
Definition: target.h:56
static long _euid
Definition: target.h:67
bool compareTargetsByAux1Descending(TargetPtr t1, TargetPtr t2)
Definition: target.cpp:314
boost::shared_ptr< Measurement > MeasurementPtr
Definition: cluster.h:47
Point measurement
Definition: gnn.cpp:518
Definition: target.h:52
double mahalanobis(Vector2d &y, Vector2d &mean, Matrix2d &cov)
Definition: metrics.cpp:34
vector< Vector5d > _past_states
Definition: target.h:74
void incrementMissedAssociations(int inc=1)
Definition: target.cpp:45
ostream & operator<<(ostream &o, Target &t)
Definition: target.cpp:34
bool compareTargetsByAux1(TargetPtr t1, TargetPtr t2)
Definition: target.cpp:309
double getDistanceToPredictedPosition(TargetPtr &target, int algorithm=0)
Definition: target.cpp:266
~Target()
Definition: target.cpp:114
Eigen::Matrix< double, 3, 1 > Vector3d
void cleanTargetAssociations(void)
Definition: target.cpp:299
bool compareTargetsById(TargetPtr t1, TargetPtr t2)
Definition: target.cpp:304
Point predicted_position
Definition: gnn.cpp:517
void initEstimator(Vector5d &x, Matrix5d &P)
Definition: motionModel.cpp:42
long _id
Definition: target.h:60
long _cluster
Definition: target.h:59
static long _ntotal
Definition: target.h:68
vector< MeasurementPtr > _assigned_measurements
Definition: target.h:72
void decrementMissedAssociations(int dec=1)
Definition: target.cpp:52
Vector5d _x
Estimated state.
Definition: motionModel.h:50
long _exuid
Definition: target.h:64
Timer t
Definition: k_best.cpp:34
long _missed_associations
Definition: target.h:61
double getDistance(MeasurementPtr &mes, int algorithm=0)
Definition: target.cpp:63
void zeroMissedAssociations(void)
Definition: target.cpp:58
double biVariatePDF(Vector2d &x, Vector2d &m, Matrix2d &cov)
Definition: metrics.cpp:42
Vector5d _xp
Predicted x.
Definition: motionModel.h:52
boost::shared_ptr< Target > TargetPtr
Shared pointer to the Target class.
Definition: hypothesis.h:37
Target class declaration.
nonholonomicEKFilter _estimator
Local estimator for process, Kalman filter with a constant velocity.
Definition: motionModel.h:59
Matrix5d _P
Posteriori error covariance.
Definition: motionModel.h:56
Target()
Definition: target.cpp:105
long _hypothesis
Definition: target.h:58
bool is_finite(const Eigen::MatrixBase< Derived > &x)
Definition: metrics.h:43
long _uid
Definition: target.h:63
void stepEstimator(Vector3d z)
Definition: motionModel.cpp:99


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18