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