32 #include "../point.hpp"
38 D2Histogram(
const std::vector<Eigen::Vector3f>& points,
int bins = 10)
39 : MAX_SAMPLES(100000), MAX_PAIRS(100000)
42 m_d2Histogram = makeHistogram(bins, calculateDistances(points));
45 D2Histogram(
const std::pair<std::string, std::vector<Eigen::Vector3f>>& points,
int bins = 10)
46 : MAX_SAMPLES(100000), MAX_PAIRS(100000)
49 m_d2Histogram = makeHistogram(bins, calculateDistances(points.second));
72 return compareHistograms(m_d2Histogram, casted->
d2Histogram());
80 virtual void serialize(
const ObjectSerializerBasePtr& serializer,
const Ice::Current&)
const
90 featureObj->setDoubleArray(
m_name, m_d2Histogram);
94 featureObj = obj->createElement();
95 featureObj->setDoubleArray(
m_name, m_d2Histogram);
100 virtual void deserialize(
const ObjectSerializerBasePtr& serializer,
const Ice::Current&)
105 featureObj->getDoubleArray(
m_name, m_d2Histogram);
108 virtual std::ostream&
output(std::ostream& out)
const
112 if (!m_d2Histogram.empty())
114 out << m_d2Histogram[0];
116 for (
unsigned int i = 1; i < m_d2Histogram.size(); i++)
118 out <<
", " << m_d2Histogram[i];
126 const int MAX_SAMPLES;
129 std::vector<double> normalize(
const std::vector<double>& x)
const
131 std::vector<double> n(x.size());
132 double max = *std::max_element(x.begin(), x.end());
134 for (
unsigned int i = 0; i < n.size(); i++)
142 double compareHistograms(
const std::vector<double>&
a,
const std::vector<double>& b)
const
145 std::vector<double> an = normalize(
a);
146 std::vector<double> bn = normalize(b);
148 for (
unsigned int i = 0; i < an.size(); i++)
150 diff += std::pow(an[i] - bn[i], 2);
156 std::vector<double> calculateDistances(
const std::vector<Eigen::Vector3f>& points)
const
158 int numPoints = points.size();
159 std::vector<int>
indices = std::vector<int>(
std::min(numPoints, MAX_SAMPLES));
161 if (numPoints < MAX_SAMPLES)
164 for (
unsigned int i = 0; i <
indices.size(); i++)
172 for (
unsigned int i = 0; i <
indices.size(); i++)
174 indices[i] = rand() % numPoints;
178 std::vector<double> distances = std::vector<double>(MAX_PAIRS);
181 int numIndices =
indices.size();
184 for (
int i = 0; i < MAX_PAIRS; i++)
186 first = rand() % numIndices;
191 second = rand() % numIndices;
193 while (first == second);
202 std::vector<double> makeHistogram(
const int bins,
const std::vector<double>&
values)
const
204 std::vector<double> histogram(bins);
205 double width = *std::max_element(
values.begin(),
values.end()) / (double) bins;
207 for (
unsigned int i = 0; i <
values.size(); i++)
214 std::vector<double> m_d2Histogram;