types_implementation.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 "types_declaration.h"
33 
34 double get_m_phi(t_pose&i)
35 {
36  return i.m_phi;
37 }
38 
39 double get_m_vl(t_pose&i)
40 {
41  return i.m_vl;
42 }
43 
44 double get_phi(t_pose&i)
45 {
46  return i.phi;
47 }
48 
50 {
51  return i.orientation;
52 }
53 
54 double get_vl(t_pose&i)
55 {
56  return i.vl;
57 }
58 
59 double max_orientation(const std::vector<t_posePtr>& vec)
60 {
61  if(vec.empty())return 0;
62 
63  double max= vec[0]->orientation;
64  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
65  if((*i)->orientation > max)
66  max = (*i)->orientation;
67 
68  return max;
69 }
70 
71 double min_orientation(const std::vector<t_posePtr>& vec)
72 {
73  if(vec.empty())return 0;
74 
75  double min= vec[0]->orientation;
76  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
77  if((*i)->orientation < min)
78  min = (*i)->orientation;
79 
80  return min;
81 }
82 
83 double max_x(const std::vector<t_posePtr>& vec)
84 {
85  if(vec.empty())return 0;
86 
87  double max = vec[0]->x;
88  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
89  if((*i)->x > max)
90  max = (*i)->x;
91 
92  return max;
93 }
94 
95 double min_x(const std::vector<t_posePtr>& vec)
96 {
97  if(vec.empty())return 0;
98 
99  double min = vec[0]->x;
100  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
101  if((*i)->x < min)
102  min = (*i)->x;
103 
104  return min;
105 }
106 
107 double max_y(const std::vector<t_posePtr>& vec)
108 {
109  if(vec.empty())return 0;
110 
111  double max = vec[0]->y;
112  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
113  if((*i)->y > max)
114  max = (*i)->y;
115 
116  return max;
117 }
118 
119 double min_y(const std::vector<t_posePtr>& vec)
120 {
121  if(vec.empty())return 0;
122 
123  double min = vec[0]->y;
124  for(std::vector<t_posePtr>::const_iterator i = vec.begin(); i != vec.end(); ++i)
125  if((*i)->y < min)
126  min = (*i)->y;
127 
128  return min;
129 }
130 
131 
133 {
134  double *range_n;
135  double *range_l;
136  double diff;
137 
138  for(int i=0;i<config->num_rays;i++)
139  {
140  if(rays[i].range->total<2)//single measurment
141  continue;
142 
143  for(int e=1;e<rays[i].range->total;e++)
144  {
145  range_l=(double*)cvGetSeqElem(rays[i].range,e-1);
146  range_n=(double*)cvGetSeqElem(rays[i].range,e);
147 
148  diff=fabs(*range_l-*range_n);
149 
150  if(diff<config->minimum_delta)
151  {
152  *range_n=(*range_l+*range_n)/2;//i don't know if i should use the average or the closest measurment
153  cvSeqRemove(rays[i].range,e);
154  e--;
155  }
156  }
157  }
158 }
159 
161 {
162  if(!src)
163  {
164  printf("Watch out for memory allocation, null pointer in %s\n",__FUNCTION__);
165  return;
166  }
167 
168  RemoveOverlappingPoints(&src->cfg,src->dt);
169 }
170 
171 int Theta2Index(double theta,ray_config_t*config)
172 {
173  int index=cvRound(theta/config->angular_resolution);
174 
175  if(index==config->num_rays)
176  index=0;
177 
178  return index;
179 }
180 
181 void Pointcloud2Rays(pcl::PointCloud<pcl::PointXYZ>& point_cloud,ray_config_t*config,ray_measurment_t*rays)
182 {
183  double theta,range,*rg;
184  int ix;
185  int e=0;
186 
187 // config->timestamp=point_cloud.header.stamp.toSec();
188  printf("PCL timestamp not correct, error during migration lar3 to lar4\n");
189 
190 
191  //this function does not trim the resulting rays so handle with care
192  for(uint i=0;i<point_cloud.points.size();i++)
193  {
194  theta=atan2(point_cloud.points[i].y,point_cloud.points[i].x);
195  if(theta<0)
196  theta+=2*M_PI;
197 
198  range=sqrt(pow(point_cloud.points[i].x,2)+pow(point_cloud.points[i].y,2));
199 
200  if(range<1.5)
201  continue;
202 
203  ix=Theta2Index(theta,config);
204 
205  if(ix<0 || ix>config->num_rays)
206  {
207  printf("Error!, index outside grid, maybe you didn't initialized the ray config\n");
208  continue;
209  }
210 
211  for(e=0;e<rays[ix].range->total;e++)
212  {
213  rg=(double*)cvGetSeqElem(rays[ix].range,e);
214 
215  if(range<*rg)
216  break;
217  }
218 
219  cvSeqInsert(rays[ix].range,e,&range);
220  }
221 }
222 
223 void PointCloud2Ray(pcl::PointCloud<pcl::PointXYZ>& point_cloud,ray_definition*dst)
224 {
225  if(!dst)
226  {
227  printf("Wrong pointer, be carefull with memory allocation!\n");
228  return;
229  }
230 
231 // dst->cfg.timestamp=ptc->timestamp;
232  dst->cfg.timestamp=ros::Time::now().toSec();
233 // printf("TS %f\n",carmen_get_time()-dst->cfg.timestamp);
234  Pointcloud2Rays(point_cloud,&dst->cfg,dst->dt);
235 }
236 
238 {
239  for(int i=0;i<ray_config->num_rays;i++)
240  cvClearSeq(rays[i].range);
241 }
242 
244 {
245  if(!src)
246  {
247  printf("Watch out for memory allocation, null pointer in %s\n",__FUNCTION__);
248  return;
249  }
250 
251  ClearRays(src->dt,&src->cfg);
252 }
253 
255 {
256  src->cfg.angular_resolution=deg2rad(0.5);
257  src->cfg.minimum_delta=0.30;
258  src->cfg.max_points_per_ray=-1;
259  src->cfg.num_rays=2*M_PI/src->cfg.angular_resolution;
260  src->cfg.timestamp=0;
261  src->cfg.x=0;
262  src->cfg.y=0;
263  src->cfg.t=0;
264  src->dt=(ray_measurment_t*)malloc(src->cfg.num_rays*sizeof(ray_measurment_t));
265 
266  //start cvseqs for all rays
267  for(int i=0;i<src->cfg.num_rays;i++)
268  {
269  src->dt[i].theta=i*src->cfg.angular_resolution;
270  src->dt[i].range_storage=cvCreateMemStorage(0);
271  src->dt[i].range=cvCreateSeq(0,sizeof(CvSeq),sizeof(double),src->dt[i].range_storage);
272  }
273 }
274 
276 {
277  if(!src || !dst)
278  {
279  printf("Watch out for memory allocation, null pointer in %s\n",__FUNCTION__);
280  return;
281  }
282 
283  memcpy(&dst->cfg,&src->cfg,sizeof(ray_config_t));
284 
285  CopyRays(&src->cfg,src->dt,dst->dt);
286 }
287 
289 {
290  //this function does not copy cfg
291  ClearRays(dst,cfg);
292  double*range;
293 
294  for(int i=0;i<cfg->num_rays;i++)
295  {
296  dst[i].theta=src[i].theta;
297 
298  for(int e=0;e<src[i].range->total;e++)
299  {
300  range=(double*)cvGetSeqElem(src[i].range,e);
301  cvSeqInsert(dst[i].range,e,range);
302  }
303  }
304 }
305 
307 {
308  h_rays->data_storage=cvCreateMemStorage(0);;
309  h_rays->data=cvCreateSeq(0,sizeof(CvSeq),sizeof(ray_definition),h_rays->data_storage);
310 }
311 
313 {
314  ray_definition rays;
315 
316  InitRayDefinition(&rays);
317 
318  CopyRays(src,&rays);
319 
320  cvSeqPushFront(h_rays->data,&rays);
321 
322  if(h_rays->data->total>50)
323  {
324  ray_definition*rmv=(ray_definition*)cvGetSeqElem(h_rays->data,h_rays->data->total-1);
325 
326  for(int i=0;i<rmv->cfg.num_rays;i++)
327  cvReleaseMemStorage(&(rmv->dt[i].range_storage));
328 
329  cvSeqPop(h_rays->data);
330  }
331 }
332 
333 double get_fps(double dt,t_fps*acc)
334 {
335  static bool initialise=TRUE;
336  static unsigned int max_size=30;
337 
338  if(initialise)
339  {
340  memset(acc->fps,0,max_size*sizeof(double));
341  acc->position=0;
342  acc->current_size=0;
343 
344  initialise=FALSE;
345  }
346 
347  acc->fps[acc->position]=1./(double)dt;
348  acc->position++;
349 
350  if(acc->current_size<max_size)
351  acc->current_size++;
352 
353  if(acc->position==max_size)
354  acc->position=0;
355 
356  double mean_fps=0;
357 
358  for(unsigned int i=0;i<acc->current_size;i++)
359  mean_fps+=acc->fps[i];
360 
361  mean_fps/=acc->current_size;
362 
363  return mean_fps;
364 }
double fps[100]
double get_orientation(t_pose &i)
double orientation
double min_y(const std::vector< t_posePtr > &vec)
double max_orientation(const std::vector< t_posePtr > &vec)
unsigned int position
unsigned int current_size
int Theta2Index(double theta, ray_config_t *config)
double get_phi(t_pose &i)
void ClearRays(ray_measurment_t *rays, ray_config_t *ray_config)
#define deg2rad(a)
ray_measurment_t * dt
double get_m_phi(t_pose &i)
Generic types declaration for use in the lidar_egomotion algorithm.
double angular_resolution
double get_vl(t_pose &i)
void RemoveOverlappingPoints(ray_config_t *config, ray_measurment_t *rays)
#define M_PI
Definition: TData.h:61
double max_y(const std::vector< t_posePtr > &vec)
void CopyRays(ray_definition *src, ray_definition *dst)
double get_fps(double dt, t_fps *acc)
double get_m_vl(t_pose &i)
void InitRayDefinition(ray_definition *src)
void PointCloud2Ray(pcl::PointCloud< pcl::PointXYZ > &point_cloud, ray_definition *dst)
ray_config_t cfg
void AddToHistory(ray_definition *src, ray_history *h_rays)
ray_history h_rays
void InitRayHistory(ray_history *h_rays)
CvMemStorage * data_storage
double min_orientation(const std::vector< t_posePtr > &vec)
double m_phi
void Pointcloud2Rays(pcl::PointCloud< pcl::PointXYZ > &point_cloud, ray_config_t *config, ray_measurment_t *rays)
double min_x(const std::vector< t_posePtr > &vec)
CvMemStorage * range_storage
double max_x(const std::vector< t_posePtr > &vec)


lidar_egomotion
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:10