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 #include <algorithm>
43 
44 namespace BFL
45 {
46 static const unsigned int NUM_CONDARG = 1;
47 
48 MCPdfPosVel::MCPdfPosVel(unsigned int num_samples)
49  : MCPdf<StatePosVel> (num_samples, NUM_CONDARG)
50 {}
51 
53 
55 MCPdfPosVel::SampleGet(unsigned int particle) const
56 {
57  assert(static_cast<int>(particle) >= 0 && particle < _listOfSamples.size());
58  return _listOfSamples[particle];
59 }
60 
62 {
63  tf::Vector3 pos(0, 0, 0);
64  tf::Vector3 vel(0, 0, 0);
65  double current_weight;
66  std::vector<WeightedSample<StatePosVel> >::const_iterator it_los;
67  for (it_los = _listOfSamples.begin() ; it_los != _listOfSamples.end() ; it_los++)
68  {
69  current_weight = it_los->WeightGet();
70  pos += (it_los->ValueGet().pos_ * current_weight);
71  vel += (it_los->ValueGet().vel_ * current_weight);
72  }
73  return StatePosVel(pos, vel);
74 }
75 
77 void MCPdfPosVel::getParticleCloud(const tf::Vector3& step, double threshold, sensor_msgs::PointCloud& cloud) const
78 {
79  unsigned int num_samples = _listOfSamples.size();
80  assert(num_samples > 0);
81  tf::Vector3 m = _listOfSamples[0].ValueGet().pos_;
82  tf::Vector3 M = _listOfSamples[0].ValueGet().pos_;
83 
84  // calculate min and max
85  for (unsigned int s = 0; s < num_samples; s++)
86  {
87  tf::Vector3 v = _listOfSamples[s].ValueGet().pos_;
88  for (unsigned int i = 0; i < 3; i++)
89  {
90  if (v[i] < m[i]) m[i] = v[i];
91  if (v[i] > M[i]) M[i] = v[i];
92  }
93  }
94 
95  // get point cloud from histogram
96  Matrix hist = getHistogramPos(m, M, step);
97  unsigned int row = hist.rows();
98  unsigned int col = hist.columns();
99  unsigned int total = 0;
100  unsigned int t = 0;
101  for (unsigned int r = 1; r <= row; r++)
102  for (unsigned int c = 1; c <= col; c++)
103  if (hist(r, c) > threshold) total++;
104 
105  std::vector<geometry_msgs::Point32> points(total);
106  std::vector<float> weights(total);
107  sensor_msgs::ChannelFloat32 channel;
108  for (unsigned int r = 1; r <= row; r++)
109  for (unsigned int c = 1; c <= col; c++)
110  if (hist(r, c) > threshold)
111  {
112  for (unsigned int i = 0; i < 3; i++)
113  points[t].x = m[0] + (step[0] * r);
114  points[t].y = m[1] + (step[1] * c);
115  points[t].z = m[2];
116  weights[t] = rgb[999 - static_cast<int>(trunc(std::max(0.0, std::min(999.0, hist(r, c) * 2 * total * total))))];
117  t++;
118  }
119  cloud.header.frame_id = "odom_combined";
120  cloud.points = points;
121  channel.name = "rgb";
122  channel.values = weights;
123  cloud.channels.push_back(channel);
124 }
125 
127 MatrixWrapper::Matrix MCPdfPosVel::getHistogramPos(const tf::Vector3& m,
128  const tf::Vector3& M,
129  const tf::Vector3& step) const
130 {
131  return getHistogram(m, M, step, true);
132 }
133 
135 MatrixWrapper::Matrix MCPdfPosVel::getHistogramVel(const tf::Vector3& m,
136  const tf::Vector3& M,
137  const tf::Vector3& step) const
138 {
139  return getHistogram(m, M, step, false);
140 }
141 
143 MatrixWrapper::Matrix MCPdfPosVel::getHistogram(const tf::Vector3& m, const tf::Vector3& M, const tf::Vector3& step,
144  bool pos_hist) const
145 {
146  unsigned int num_samples = _listOfSamples.size();
147  unsigned int rows = round((M[0] - m[0]) / step[0]);
148  unsigned int cols = round((M[1] - m[1]) / step[1]);
149  Matrix hist(rows, cols);
150  hist = 0;
151 
152  // calculate histogram
153  for (unsigned int i = 0; i < num_samples; i++)
154  {
155  tf::Vector3 rel;
156  if (pos_hist)
157  rel = _listOfSamples[i].ValueGet().pos_ - m;
158  else
159  rel = _listOfSamples[i].ValueGet().vel_ - m;
160 
161  unsigned int r = round(rel[0] / step[0]);
162  unsigned int c = round(rel[1] / step[1]);
163  if (r >= 1 && c >= 1 && r <= rows && c <= cols)
164  hist(r, c) += _listOfSamples[i].WeightGet();
165  }
166 
167  return hist;
168 }
169 
170 unsigned int
172 {
173  return _listOfSamples.size();
174 }
175 } // namespace BFL
void getParticleCloud(const tf::Vector3 &step, double threshold, sensor_msgs::PointCloud &cloud) const
Get evenly distributed particle cloud.
virtual StatePosVel ExpectedValueGet() const
XmlRpcServer s
MatrixWrapper::Matrix getHistogram(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step, bool pos_hist) const
Get histogram from certain area.
Class representing state with pos and vel.
Definition: state_pos_vel.h:46
virtual WeightedSample< StatePosVel > SampleGet(unsigned int particle) const
virtual unsigned int numParticlesGet() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
MatrixWrapper::Matrix getHistogramPos(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step) const
Get pos histogram from certain area.
virtual ~MCPdfPosVel()
Destructor.
MCPdfPosVel(unsigned int num_samples)
Constructor.
static const int rgb[]
Definition: rgb.h:37
vector< WeightedSample< StatePosVel > > _listOfSamples
static const unsigned int NUM_CONDARG
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 Sun Feb 21 2021 03:56:47