mcpdf_vector.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 
39 #include <assert.h>
40 #include <vector>
41 #include <std_msgs/Float64.h>
43 #include <algorithm>
44 
45 namespace BFL
46 {
47 static const unsigned int NUM_CONDARG = 1;
48 
49 MCPdfVector::MCPdfVector(unsigned int num_samples)
50  : MCPdf<tf::Vector3> (num_samples, NUM_CONDARG)
51 {}
52 
54 
56 MCPdfVector::SampleGet(unsigned int particle) const
57 {
58  assert(static_cast<int>(particle) >= 0 && particle < _listOfSamples.size());
59  return _listOfSamples[particle];
60 }
61 
63 {
64  tf::Vector3 pos(0, 0, 0);
65  double current_weight;
66  std::vector<WeightedSample<tf::Vector3> >::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() * current_weight);
71  }
72 
73  return tf::Vector3(pos);
74 }
75 
77 void MCPdfVector::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();
82  tf::Vector3 M = _listOfSamples[0].ValueGet();
83 
84  // calculate min and max
85  for (unsigned int s = 0; s < num_samples; s++)
86  {
87  tf::Vector3 v = _listOfSamples[s].ValueGet();
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 = getHistogram(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  std::cout << "size total " << total << std::endl;
105 
106  std::vector<geometry_msgs::Point32> points(total);
107  std::vector<float> weights(total);
108  sensor_msgs::ChannelFloat32 channel;
109  for (unsigned int r = 1; r <= row; r++)
110  for (unsigned int c = 1; c <= col; c++)
111  if (hist(r, c) > threshold)
112  {
113  for (unsigned int i = 0; i < 3; i++)
114  points[t].x = m[0] + (step[0] * r);
115  points[t].y = m[1] + (step[1] * c);
116  points[t].z = m[2];
117  weights[t] = rgb[999 - static_cast<int>(trunc(std::max(0.0, std::min(999.0, hist(r, c) * 2 * total * total))))];
118  t++;
119  }
120  std::cout << "points size " << points.size() << std::endl;
121  cloud.header.frame_id = "base_link";
122  cloud.points = points;
123  channel.name = "rgb";
124  channel.values = weights;
125  cloud.channels.push_back(channel);
126 }
127 
129 MatrixWrapper::Matrix MCPdfVector::getHistogram(const tf::Vector3& m,
130  const tf::Vector3& M,
131  const tf::Vector3& step) const
132 {
133  unsigned int num_samples = _listOfSamples.size();
134  unsigned int rows = round((M[0] - m[0]) / step[0]);
135  unsigned int cols = round((M[1] - m[1]) / step[1]);
136  Matrix hist(rows, cols);
137  hist = 0;
138 
139  // calculate histogram
140  for (unsigned int i = 0; i < num_samples; i++)
141  {
142  tf::Vector3 rel(_listOfSamples[i].ValueGet() - m);
143  unsigned int r = round(rel[0] / step[0]);
144  unsigned int c = round(rel[1] / step[1]);
145  if (r >= 1 && c >= 1 && r <= rows && c <= cols)
146  hist(r, c) += _listOfSamples[i].WeightGet();
147  }
148 
149  return hist;
150 }
151 
152 unsigned int
154 {
155  return _listOfSamples.size();
156 }
157 } // namespace BFL
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE Vector3()
TFSIMD_FORCE_INLINE const tfScalar & y() const
virtual ~MCPdfVector()
Destructor.
virtual tf::Vector3 ExpectedValueGet() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
MatrixWrapper::Matrix getHistogram(const tf::Vector3 &min, const tf::Vector3 &max, const tf::Vector3 &step) const
Get pos histogram from certain area.
static const int rgb[]
Definition: rgb.h:37
vector< WeightedSample< tf::Vector3 > > _listOfSamples
MCPdfVector(unsigned int num_samples)
Constructor.
void getParticleCloud(const tf::Vector3 &step, double threshold, sensor_msgs::PointCloud &cloud) const
Get evenly distributed particle cloud.
virtual WeightedSample< tf::Vector3 > SampleGet(unsigned int particle) const
static const unsigned int NUM_CONDARG
virtual unsigned int numParticlesGet() const


people_tracking_filter
Author(s): Caroline Pantofaru
autogenerated on Sun Feb 21 2021 03:56:47