A3Histogram.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 A3Histogram : public Feature
35 {
36 public:
37  A3Histogram(): MAX_SAMPLES(100000), MAX_TRIOS(100000) {}
38  A3Histogram(const std::vector<Eigen::Vector3f>& points, int bins = 10)
39  : MAX_SAMPLES(100000), MAX_TRIOS(100000)
40  {
41  m_name = "A3Histogram";
42  m_a3Histogram = makeHistogram(bins, calculateAngles(points));
43  }
44 
45  A3Histogram(const std::pair<std::string, std::vector<Eigen::Vector3f>>& points, int bins = 10)
46  : MAX_SAMPLES(100000), MAX_TRIOS(100000)
47  {
48  m_name = "A3Histogram";
49  m_a3Histogram = makeHistogram(bins, calculateAngles(points.second));
50  }
51 
52 
53  std::vector<double> a3Histogram() const
54  {
55  return m_a3Histogram;
56  }
57 
58  FeaturePtr calculate(const Points& points)
59  {
60  return FeaturePtr(new A3Histogram(points));
61  }
63  {
64  return FeaturePtr(new A3Histogram(points));
65  }
66 
67  double compare(const Feature& other) const
68  {
69  const A3Histogram* casted = dynamic_cast<const A3Histogram*>(&other);
70 
71  if (casted)
72  {
73  return compareHistograms(m_a3Histogram, casted->a3Histogram());
74  }
75  else
76  {
78  }
79  }
80 
81  virtual void serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
82  {
83  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
84 
85  AbstractObjectSerializerPtr featureObj;
86 
87  //Check if object already has a "features" field, else create a new one
88  if (obj->hasElement(FEATURE_FIELD_NAME))
89  {
90  featureObj = obj->getElement(FEATURE_FIELD_NAME);
91  featureObj->setDoubleArray(m_name, m_a3Histogram);
92  }
93  else
94  {
95  featureObj = obj->createElement();
96  featureObj->setDoubleArray(m_name, m_a3Histogram);
97  obj->setElement(FEATURE_FIELD_NAME, featureObj);
98  }
99  }
100 
101  virtual void deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
102  {
103  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
104  AbstractObjectSerializerPtr featureObj = obj->getElement(FEATURE_FIELD_NAME);
105  //Now copy the result array from the DB
106  featureObj->getDoubleArray(m_name, m_a3Histogram);
107  }
108 
109  virtual std::ostream& output(std::ostream& out) const
110  {
111  out << "[";
112 
113  if (!m_a3Histogram.empty())
114  {
115  out << m_a3Histogram[0];
116 
117  for (unsigned int i = 1; i < m_a3Histogram.size(); i++)
118  {
119  out << ", " << m_a3Histogram[i];
120  }
121  }
122 
123  out << "]";
124  return out;
125  }
126 
127 private:
128  const int MAX_SAMPLES;
129  const int MAX_TRIOS;
130 
131  std::vector<double> normalize(const std::vector<double>& x) const
132  {
133  std::vector<double> n(x.size());
134  double max = *std::max_element(x.begin(), x.end());
135 
136  for (unsigned int i = 0; i < n.size(); i++)
137  {
138  n[i] = x[i] / max;
139  }
140 
141  return n;
142  }
143 
144  double compareHistograms(const std::vector<double>& a, const std::vector<double>& b) const
145  {
146  double diff = 0.0;
147  std::vector<double> an = normalize(a);
148  std::vector<double> bn = normalize(b);
149 
150  for (unsigned int i = 0; i < an.size(); i++)
151  {
152  diff += std::pow(an[i] - bn[i], 2);
153  }
154 
155  return diff;
156  }
157 
158  std::vector<double> calculateAngles(const std::vector<Eigen::Vector3f>& points) const
159  {
160  int numPoints = points.size();
161  std::vector<int> indices = std::vector<int>(std::min(numPoints, MAX_SAMPLES));
162 
163  if (numPoints < MAX_SAMPLES)
164  {
165  //Take all the points
166  for (unsigned int i = 0; i < indices.size(); i++)
167  {
168  indices[i] = i;
169  }
170  }
171  else
172  {
173  //Sample enough points
174  for (unsigned int i = 0; i < indices.size(); i++)
175  {
176  indices[i] = rand() % numPoints;
177  }
178  }
179 
180  std::vector<double> angles(MAX_TRIOS);
181  int first;
182  int second;
183  int third;
184  int numIndices = indices.size();
185 
186  //Now make the trios and calculate the angles
187  for (int i = 0; i < MAX_TRIOS; i++)
188  {
189  first = rand() % numIndices;
190 
191  //Sample a `different` point
192  do
193  {
194  second = rand() % numIndices;
195  }
196  while (first == second);
197 
198  do
199  {
200  third = rand() % numIndices;
201  }
202  while (first == third || second == third);
203 
204  //Calculate the angle
205  angles[i] = angle(points[indices[first]], points[indices[second]], points[indices[third]]);
206  }
207 
208  return angles;
209  }
210 
211  std::vector<double> makeHistogram(const int bins, const std::vector<double>& values) const
212  {
213  std::vector<double> histogram(bins);
214  double width = *std::max_element(values.begin(), values.end()) / (double) bins;
215 
216  for (unsigned int i = 0; i < values.size(); i++)
217  {
218  histogram[std::min((int)(values.at(i) / width), bins - 1)]++;
219  }
220 
221  return histogram;
222  }
223  std::vector<double> m_a3Histogram;
224 };
225 
Feature
Definition: Feature.hpp:44
A3Histogram::a3Histogram
std::vector< double > a3Histogram() const
Definition: A3Histogram.hpp:53
Feature.hpp
Feature::m_name
std::string m_name
Definition: Feature.hpp:87
A3Histogram::deserialize
virtual void deserialize(const ObjectSerializerBasePtr &serializer, const Ice::Current &)
Definition: A3Histogram.hpp:101
A3Histogram::A3Histogram
A3Histogram()
Definition: A3Histogram.hpp:37
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
A3Histogram
Definition: A3Histogram.hpp:34
Feature::TaggedPoints
std::pair< std::string, Points > TaggedPoints
Definition: Feature.hpp:48
A3Histogram::A3Histogram
A3Histogram(const std::pair< std::string, std::vector< Eigen::Vector3f >> &points, int bins=10)
Definition: A3Histogram.hpp:45
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
A3Histogram::output
virtual std::ostream & output(std::ostream &out) const
Definition: A3Histogram.hpp:109
A3Histogram::calculate
FeaturePtr calculate(const TaggedPoints &points)
Definition: A3Histogram.hpp:62
A3Histogram::calculate
FeaturePtr calculate(const Points &points)
Definition: A3Histogram.hpp:58
max
T max(T t1, T t2)
Definition: gdiam.h:48
A3Histogram::compare
double compare(const Feature &other) const
Definition: A3Histogram.hpp:67
A3Histogram::A3Histogram
A3Histogram(const std::vector< Eigen::Vector3f > &points, int bins=10)
Definition: A3Histogram.hpp:38
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
Feature::FeaturePtr
std::shared_ptr< Feature > FeaturePtr
Definition: Feature.hpp:49
min
T min(T t1, T t2)
Definition: gdiam.h:42
A3Histogram::serialize
virtual void serialize(const ObjectSerializerBasePtr &serializer, const Ice::Current &) const
Definition: A3Histogram.hpp:81