D2Histogram.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <vector>
27 #include <limits>
28 #include <algorithm>
29 #include <math.h>
30 
31 #include "Feature.hpp"
32 #include "../point.hpp"
33 
34 class D2Histogram : public Feature
35 {
36 public:
37  D2Histogram() : MAX_SAMPLES(100000), MAX_PAIRS(100000) {}
38  D2Histogram(const std::vector<Eigen::Vector3f>& points, int bins = 10)
39  : MAX_SAMPLES(100000), MAX_PAIRS(100000)
40  {
41  m_name = "D2Histogram";
42  m_d2Histogram = makeHistogram(bins, calculateDistances(points));
43  }
44 
45  D2Histogram(const std::pair<std::string, std::vector<Eigen::Vector3f>>& points, int bins = 10)
46  : MAX_SAMPLES(100000), MAX_PAIRS(100000)
47  {
48  m_name = "D2Histogram";
49  m_d2Histogram = makeHistogram(bins, calculateDistances(points.second));
50  }
51 
52  std::vector<double> d2Histogram() const
53  {
54  return m_d2Histogram;
55  }
56 
57  FeaturePtr calculate(const Points& points)
58  {
59  return FeaturePtr(new D2Histogram(points));
60  }
62  {
63  return FeaturePtr(new D2Histogram(points));
64  }
65 
66  double compare(const Feature& other) const
67  {
68  const D2Histogram* casted = dynamic_cast<const D2Histogram*>(&other);
69 
70  if (casted)
71  {
72  return compareHistograms(m_d2Histogram, casted->d2Histogram());
73  }
74  else
75  {
77  }
78  }
79 
80  virtual void serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
81  {
82  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
83 
84  AbstractObjectSerializerPtr featureObj;
85 
86  //Check if object already has a "features" field, else create a new one
87  if (obj->hasElement(FEATURE_FIELD_NAME))
88  {
89  featureObj = obj->getElement(FEATURE_FIELD_NAME);
90  featureObj->setDoubleArray(m_name, m_d2Histogram);
91  }
92  else
93  {
94  featureObj = obj->createElement();
95  featureObj->setDoubleArray(m_name, m_d2Histogram);
96  obj->setElement(FEATURE_FIELD_NAME, featureObj);
97  }
98  }
99 
100  virtual void deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
101  {
102  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
103  AbstractObjectSerializerPtr featureObj = obj->getElement(FEATURE_FIELD_NAME);
104  //Now copy the result array from the DB
105  featureObj->getDoubleArray(m_name, m_d2Histogram);
106  }
107 
108  virtual std::ostream& output(std::ostream& out) const
109  {
110  out << "[";
111 
112  if (!m_d2Histogram.empty())
113  {
114  out << m_d2Histogram[0];
115 
116  for (unsigned int i = 1; i < m_d2Histogram.size(); i++)
117  {
118  out << ", " << m_d2Histogram[i];
119  }
120  }
121 
122  out << "]";
123  return out;
124  }
125 private:
126  const int MAX_SAMPLES;
127  const int MAX_PAIRS;
128 
129  std::vector<double> normalize(const std::vector<double>& x) const
130  {
131  std::vector<double> n(x.size());
132  double max = *std::max_element(x.begin(), x.end());
133 
134  for (unsigned int i = 0; i < n.size(); i++)
135  {
136  n[i] = x[i] / max;
137  }
138 
139  return n;
140  }
141 
142  double compareHistograms(const std::vector<double>& a, const std::vector<double>& b) const
143  {
144  double diff = 0.0;
145  std::vector<double> an = normalize(a);
146  std::vector<double> bn = normalize(b);
147 
148  for (unsigned int i = 0; i < an.size(); i++)
149  {
150  diff += std::pow(an[i] - bn[i], 2);
151  }
152 
153  return diff;
154  }
155 
156  std::vector<double> calculateDistances(const std::vector<Eigen::Vector3f>& points) const
157  {
158  int numPoints = points.size();
159  std::vector<int> indices = std::vector<int>(std::min(numPoints, MAX_SAMPLES));
160 
161  if (numPoints < MAX_SAMPLES)
162  {
163  //Take all the points
164  for (unsigned int i = 0; i < indices.size(); i++)
165  {
166  indices[i] = i;
167  }
168  }
169  else
170  {
171  //Sample enough points
172  for (unsigned int i = 0; i < indices.size(); i++)
173  {
174  indices[i] = rand() % numPoints;
175  }
176  }
177 
178  std::vector<double> distances = std::vector<double>(MAX_PAIRS);
179  int first;
180  int second;
181  int numIndices = indices.size();
182 
183  //Now make the pairs and calculate the distances
184  for (int i = 0; i < MAX_PAIRS; i++)
185  {
186  first = rand() % numIndices;
187 
188  //Sample a `different` point
189  do
190  {
191  second = rand() % numIndices;
192  }
193  while (first == second);
194 
195  //Calculate the distance
196  distances[i] = distance(points[indices[first]], points[indices[second]]);
197  }
198 
199  return distances;
200  }
201 
202  std::vector<double> makeHistogram(const int bins, const std::vector<double>& values) const
203  {
204  std::vector<double> histogram(bins);
205  double width = *std::max_element(values.begin(), values.end()) / (double) bins;
206 
207  for (unsigned int i = 0; i < values.size(); i++)
208  {
209  histogram[std::min((int)(values.at(i) / width), bins - 1)]++;
210  }
211 
212  return histogram;
213  }
214  std::vector<double> m_d2Histogram;
215 };
216 
Feature
Definition: Feature.hpp:44
D2Histogram::calculate
FeaturePtr calculate(const Points &points)
Definition: D2Histogram.hpp:57
Feature.hpp
Feature::m_name
std::string m_name
Definition: Feature.hpp:87
D2Histogram::d2Histogram
std::vector< double > d2Histogram() const
Definition: D2Histogram.hpp:52
D2Histogram::calculate
FeaturePtr calculate(const TaggedPoints &points)
Definition: D2Histogram.hpp:61
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
Feature::TaggedPoints
std::pair< std::string, Points > TaggedPoints
Definition: Feature.hpp:48
D2Histogram::compare
double compare(const Feature &other) const
Definition: D2Histogram.hpp:66
IceInternal::Handle
Definition: forward_declarations.h:8
FEATURE_FIELD_NAME
const std::string FEATURE_FIELD_NAME
Definition: Feature.hpp:42
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:737
Feature::Points
std::vector< Eigen::Vector3f > Points
Definition: Feature.hpp:47
D2Histogram::serialize
virtual void serialize(const ObjectSerializerBasePtr &serializer, const Ice::Current &) const
Definition: D2Histogram.hpp:80
max
T max(T t1, T t2)
Definition: gdiam.h:48
D2Histogram
Definition: D2Histogram.hpp:34
D2Histogram::deserialize
virtual void deserialize(const ObjectSerializerBasePtr &serializer, const Ice::Current &)
Definition: D2Histogram.hpp:100
D2Histogram::output
virtual std::ostream & output(std::ostream &out) const
Definition: D2Histogram.hpp:108
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
Feature::FeaturePtr
std::shared_ptr< Feature > FeaturePtr
Definition: Feature.hpp:49
D2Histogram::D2Histogram
D2Histogram(const std::pair< std::string, std::vector< Eigen::Vector3f >> &points, int bins=10)
Definition: D2Histogram.hpp:45
D2Histogram::D2Histogram
D2Histogram()
Definition: D2Histogram.hpp:37
min
T min(T t1, T t2)
Definition: gdiam.h:42
D2Histogram::D2Histogram
D2Histogram(const std::vector< Eigen::Vector3f > &points, int bins=10)
Definition: D2Histogram.hpp:38