mcpdf_pos_vel.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Wim Meeussen */
36 
38 #include <assert.h>
39 #include <vector>
40 #include <std_msgs/Float64.h>
42 
43 
44 using namespace MatrixWrapper;
45 using namespace BFL;
46 using namespace tf;
47 
48 static const unsigned int NUM_CONDARG = 1;
49 
50 
51 MCPdfPosVel::MCPdfPosVel(unsigned int num_samples)
52  : MCPdf<StatePosVel> (num_samples, NUM_CONDARG)
53 {}
54 
56 
57 
58 WeightedSample<StatePosVel>
59 MCPdfPosVel::SampleGet(unsigned int particle) const
60 {
61  assert((int)particle >= 0 && particle < _listOfSamples.size());
62  return _listOfSamples[particle];
63 }
64 
65 
67 {
68  tf::Vector3 pos(0, 0, 0);
69  tf::Vector3 vel(0, 0, 0);
70  double current_weight;
71  std::vector<WeightedSample<StatePosVel> >::const_iterator it_los;
72  for (it_los = _listOfSamples.begin() ; it_los != _listOfSamples.end() ; it_los++)
73  {
74  current_weight = it_los->WeightGet();
75  pos += (it_los->ValueGet().pos_ * current_weight);
76  vel += (it_los->ValueGet().vel_ * current_weight);
77  }
78  return StatePosVel(pos, vel);
79 }
80 
81 
83 void MCPdfPosVel::getParticleCloud(const tf::Vector3& step, double threshold, sensor_msgs::PointCloud& cloud) const
84 {
85  unsigned int num_samples = _listOfSamples.size();
86  assert(num_samples > 0);
87  Vector3 m = _listOfSamples[0].ValueGet().pos_;
88  Vector3 M = _listOfSamples[0].ValueGet().pos_;
89 
90  // calculate min and max
91  for (unsigned int s = 0; s < num_samples; s++)
92  {
93  Vector3 v = _listOfSamples[s].ValueGet().pos_;
94  for (unsigned int i = 0; i < 3; i++)
95  {
96  if (v[i] < m[i]) m[i] = v[i];
97  if (v[i] > M[i]) M[i] = v[i];
98  }
99  }
100 
101  // get point cloud from histogram
102  Matrix hist = getHistogramPos(m, M, step);
103  unsigned int row = hist.rows();
104  unsigned int col = hist.columns();
105  unsigned int total = 0;
106  unsigned int t = 0;
107  for (unsigned int r = 1; r <= row; r++)
108  for (unsigned int c = 1; c <= col; c++)
109  if (hist(r, c) > threshold) total++;
110 
111  vector<geometry_msgs::Point32> points(total);
112  vector<float> weights(total);
113  sensor_msgs::ChannelFloat32 channel;
114  for (unsigned int r = 1; r <= row; r++)
115  for (unsigned int c = 1; c <= col; c++)
116  if (hist(r, c) > threshold)
117  {
118  for (unsigned int i = 0; i < 3; i++)
119  points[t].x = m[0] + (step[0] * r);
120  points[t].y = m[1] + (step[1] * c);
121  points[t].z = m[2];
122  weights[t] = rgb[999 - (int)trunc(max(0.0, min(999.0, hist(r, c) * 2 * total * total)))];
123  t++;
124  }
125  cloud.header.frame_id = "odom_combined";
126  cloud.points = points;
127  channel.name = "rgb";
128  channel.values = weights;
129  cloud.channels.push_back(channel);
130 }
131 
132 
134 MatrixWrapper::Matrix MCPdfPosVel::getHistogramPos(const Vector3& m, const Vector3& M, const Vector3& step) const
135 {
136  return getHistogram(m, M, step, true);
137 }
138 
139 
141 MatrixWrapper::Matrix MCPdfPosVel::getHistogramVel(const Vector3& m, const Vector3& M, const Vector3& step) const
142 {
143  return getHistogram(m, M, step, false);
144 }
145 
146 
148 MatrixWrapper::Matrix MCPdfPosVel::getHistogram(const Vector3& m, const Vector3& M, const Vector3& step, bool pos_hist) const
149 {
150  unsigned int num_samples = _listOfSamples.size();
151  unsigned int rows = round((M[0] - m[0]) / step[0]);
152  unsigned int cols = round((M[1] - m[1]) / step[1]);
153  Matrix hist(rows, cols);
154  hist = 0;
155 
156  // calculate histogram
157  for (unsigned int i = 0; i < num_samples; i++)
158  {
159  Vector3 rel;
160  if (pos_hist)
161  rel = _listOfSamples[i].ValueGet().pos_ - m;
162  else
163  rel = _listOfSamples[i].ValueGet().vel_ - m;
164 
165  unsigned int r = round(rel[0] / step[0]);
166  unsigned int c = round(rel[1] / step[1]);
167  if (r >= 1 && c >= 1 && r <= rows && c <= cols)
168  hist(r, c) += _listOfSamples[i].WeightGet();
169  }
170 
171  return hist;
172 }
173 
174 
175 
176 unsigned int
178 {
179  return _listOfSamples.size();
180 }
181 
182 
MatrixWrapper::Matrix getHistogramPos(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step) const
Get pos histogram from certain area.
XmlRpcServer s
Class representing state with pos and vel.
Definition: state_pos_vel.h:46
virtual ~MCPdfPosVel()
Destructor.
static const unsigned int NUM_CONDARG
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual StatePosVel ExpectedValueGet() const
void getParticleCloud(const tf::Vector3 &step, double threshold, sensor_msgs::PointCloud &cloud) const
Get evenly distributed particle cloud.
virtual unsigned int numParticlesGet() const
int min(int a, int b)
static const int rgb[]
Definition: rgb.h:4
MatrixWrapper::Matrix getHistogram(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step, bool pos_hist) const
Get histogram from certain area.
virtual WeightedSample< StatePosVel > SampleGet(unsigned int particle) const
MatrixWrapper::Matrix getHistogramVel(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step) const
Get vel histogram from certain area.


people_tracking_filter
Author(s): Caroline Pantofaru
autogenerated on Fri Jun 7 2019 22:07:49