ISDDistance.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package MemoryX::GaussianMixtureHelpers
17 * @author Alexey Kozlov <kozlov@kit.edu>
18 * @copyright 2013 Alexey Kozlov
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
26 
28 #include "GMMDistance.h"
29 
30 #include <Eigen/LU>
31 
32 #include <memory>
33 #include <string>
34 
35 namespace memoryx
36 {
37 
38  /**
39  * @class ISDDistance
40  * @ingroup CommonPlacesLearner
41  */
43  {
44  public:
45  /**
46  * Creates a new ISDDistance
47  */
49  {
50  }
51 
52  /**
53  *
54  *
55  */
56  float getDistance(const GaussianMixtureDistributionBasePtr& gmm1, const GaussianMixtureDistributionBasePtr& gmm2,
57  bool normalize)
58  {
59  // preprocessing
60  std::vector<NormalDistributionPtr> g1, g2;
61  std::vector<float> w1, w2;
62  preprocessGMM(gmm1, g1, w1);
63  preprocessGMM(gmm2, g2, w2);
64 
65  const float d11 = calcCrossSum(gmm1, g1, w1, gmm1, g1, w1);
66  const float d22 = calcCrossSum(gmm2, g2, w2, gmm2, g2, w2);
67  const float d12 = calcCrossSum(gmm1, g1, w1, gmm2, g2, w2);
68 
69  const float isd = d11 + d22 - 2 * d12;
70  return normalize ? (isd / (d11 + d22)) : isd;
71  }
72 
73  float getDistance(const GaussianMixtureDistributionBasePtr& gmm1, const GaussianMixtureDistributionBasePtr& gmm2) override
74  {
75  return getDistance(gmm1, gmm2, false);
76  }
77 
78  float getDistance(const GaussianMixtureDistributionBasePtr& gmm1, const GaussianMixtureDistributionBasePtr& gmm2,
79  float d11)
80  {
81  // preprocessing
82  std::vector<NormalDistributionPtr> g1, g2;
83  std::vector<float> w1, w2;
84  preprocessGMM(gmm1, g1, w1);
85  preprocessGMM(gmm2, g2, w2);
86 
87  const float d22 = calcCrossSum(gmm2, g2, w2, gmm2, g2, w2);
88  const float d12 = calcCrossSum(gmm1, g1, w1, gmm2, g2, w2);
89 
90  return d11 + d22 - 2 * d12;
91  }
92 
93  float getDistance(const GaussianMixtureComponent& comp1, const GaussianMixtureComponent& comp2) override
94  {
95  return 0.; // NYI
96  }
97 
98  float calcSelfLikeness(const GaussianMixtureDistributionBasePtr& gmm)
99  {
100  std::vector<NormalDistributionPtr> g;
101  std::vector<float> w;
102  preprocessGMM(gmm, g, w);
103 
104  return calcCrossSum(gmm, g, w, gmm, g, w);
105  }
106 
107 
108  private:
109  void preprocessGMM(const GaussianMixtureDistributionBasePtr& gmm, std::vector<NormalDistributionPtr>& g,
110  std::vector<float>& w)
111  {
112 
113  for (int i = 0; i < gmm->size(); ++i)
114  {
115  GaussianMixtureComponent c = gmm->getComponent(i);
116  g.push_back(NormalDistributionPtr::dynamicCast(c.gaussian));
117  w.push_back(c.weight);
118  }
119  }
120 
121  float calcCrossSum(const GaussianMixtureDistributionBasePtr& gmm1, std::vector<NormalDistributionPtr>& g1,
122  std::vector<float>& w1,
123  const GaussianMixtureDistributionBasePtr& gmm2, std::vector<NormalDistributionPtr>& g2,
124  std::vector<float>& w2)
125  {
126  float s = 0;
127 
128  const int GMM2_SIZE = gmm2->size();
129 
130  for (int i = 0; i < gmm1->size(); ++i)
131  {
132  const float wi = w1[i];
133  const Eigen::Vector3f mi = g1[i]->toEigenMean();
134  const Eigen::Matrix3f pi = g1[i]->toEigenCovariance();
135 
136  for (int j = 0; j < GMM2_SIZE; ++j)
137  {
138  const float f = evaluate3DGaussian(mi, g2[j]->toEigenMean(), pi + g2[j]->toEigenCovariance());
139  s += wi * w2[j] * f;
140  }
141  }
142 
143  return s;
144  }
145 
146  float evaluate3DGaussian(const Eigen::Vector3f& point, const Eigen::Vector3f& mean, const Eigen::Matrix3f& cov)
147  {
148  return evaluate3DGaussian(point, mean, cov.inverse(), cov.determinant());
149  }
150 
151  float evaluate3DGaussian(const Eigen::Vector3f& point, const Eigen::Vector3f& mean, const Eigen::Matrix3f& covInv, float covDet)
152  {
153  const float inner = -0.5 * (point - mean).transpose() * covInv * (point - mean);
154  const float e = expf(inner);
155 
156  static const float pm = powf(2 * M_PI, -1.5f);
157  return pm * powf(covDet, -0.5) * e;
158  }
159 
160  };
161 
162  using ISDDistancePtr = std::shared_ptr<ISDDistance>;
163 }
164 
memoryx::ISDDistance::getDistance
float getDistance(const GaussianMixtureDistributionBasePtr &gmm1, const GaussianMixtureDistributionBasePtr &gmm2, float d11)
Definition: ISDDistance.h:78
memoryx::GMMDistance
Definition: GMMDistance.h:39
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::ISDDistancePtr
std::shared_ptr< ISDDistance > ISDDistancePtr
Definition: ISDDistance.h:162
memoryx::ISDDistance::getDistance
float getDistance(const GaussianMixtureDistributionBasePtr &gmm1, const GaussianMixtureDistributionBasePtr &gmm2) override
Definition: ISDDistance.h:73
GMMDistance.h
pi
#define pi
Definition: Transition.cpp:37
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
memoryx::ISDDistance::calcSelfLikeness
float calcSelfLikeness(const GaussianMixtureDistributionBasePtr &gmm)
Definition: ISDDistance.h:98
M_PI
#define M_PI
Definition: MathTools.h:17
memoryx::ISDDistance::ISDDistance
ISDDistance()
Creates a new ISDDistance.
Definition: ISDDistance.h:48
memoryx::ISDDistance::getDistance
float getDistance(const GaussianMixtureDistributionBasePtr &gmm1, const GaussianMixtureDistributionBasePtr &gmm2, bool normalize)
Definition: ISDDistance.h:56
memoryx::ISDDistance::getDistance
float getDistance(const GaussianMixtureComponent &comp1, const GaussianMixtureComponent &comp2) override
Definition: ISDDistance.h:93
ProbabilityMeasures.h
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
armarx::transpose
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T >> &src, Thrower thrower)
Definition: SimoxCSpace.cpp:706
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
memoryx::ISDDistance
Definition: ISDDistance.h:42