54 obj->setFloat(
"prob", prob);
61 prob = obj->getFloat(
"prob");
71 this->dimensions = dimensions;
72 this->
mean.resize(dimensions);
76 NormalDistributionBase(
mean.size(),
mean)
82 this->dimensions =
mean.rows();
83 this->mean.resize(dimensions);
89 NormalDistributionBase()
92 this->dimensions = other.dimensions;
93 this->
mean = other.mean;
98 Eigen::VectorXf result = Eigen::Map<const Eigen::VectorXf>(this->
mean.data(), this->dimensions);
104 if (this->dimensions ==
mean.rows())
106 Eigen::Map<Eigen::VectorXf>(this->mean.data(), this->dimensions) =
mean;
120 s <<
"Standard deviation(mm): " << pow(cov.determinant(), 0.5 / dimensions);
125 for (
size_t i = 0; i <
mean.size(); ++i)
127 s <<
mean[i] << ((i !=
mean.size() - 1) ?
", " :
") ");
132 for (
int i = 0; i < dimensions; ++i)
133 for (
int j = 0; j < dimensions; ++j)
135 s <<
getCovariance(i, j) << ((i != dimensions - 1 || j != dimensions - 1) ?
", " :
")");
145 obj->setFloatArray(
"mean",
mean);
152 obj->getFloatArray(
"mean",
mean);
153 dimensions =
mean.size();
162 UnivariateNormalDistributionBase::UnivariateNormalDistributionBase(),
169 UnivariateNormalDistributionBase::UnivariateNormalDistributionBase(),
173 this->mean.push_back(
mean);
178 NormalDistributionBase(),
179 UnivariateNormalDistributionBase(),
182 variance = other.variance;
197 if ((row == 0) && (col == 0))
207 Eigen::MatrixXf result(1, 1);
208 result << this->variance;
214 if (cov.rows() == 1 && cov.cols() == 1)
216 this->variance = cov(1, 1);
229 obj->setFloat(
"var", variance);
237 variance = obj->getFloat(
"var");
247 this->varVector.resize(dimensions);
253 this->varVector = vars;
259 this->varVector.resize(dimensions);
265 NormalDistributionBase(other),
266 IsotropicNormalDistributionBase(other),
269 this->varVector.assign(other.varVector.begin(), other.varVector.end());
275 return (
size_t) dim < varVector.size() ? varVector[dim] : -1.;
280 if ((
size_t) dim < varVector.size())
282 varVector[dim] = var;
293 return varVector[row];
298 Eigen::MatrixXf result(dimensions, dimensions);
300 for (
int i = 0; i < dimensions; ++i)
302 result(i, i) = varVector[i];
310 if (cov.rows() == 1 && cov.cols() == dimensions)
312 for (
int i = 0; i < dimensions; ++i)
314 varVector[i] = cov(0, i);
317 else if (cov.rows() == dimensions && cov.cols() == dimensions)
319 for (
int i = 0; i < dimensions; ++i)
321 varVector[i] = cov(i, i);
336 obj->setFloatArray(
"var", varVector);
344 obj->getFloatArray(
"var", varVector);
355 this->covMatrix.resize(dimensions * dimensions);
361 this->covMatrix = vars;
367 this->covMatrix.resize(dimensions * dimensions);
373 NormalDistributionBase(),
374 MultivariateNormalDistributionBase(),
377 this->covMatrix = other.covMatrix;
383 const int index = row * dimensions + col;
385 if (
index >= 0 && (
size_t)
index < covMatrix.size())
387 return covMatrix[
index];
389 throw armarx::IndexOutOfBoundsException();
394 const int index = row * dimensions + col;
396 if (
index >= 0 && (
size_t)
index < covMatrix.size())
398 covMatrix[
index] = cov;
402 throw armarx::IndexOutOfBoundsException();
411 return pow(cov.determinant(), 0.5 / dimensions);
421 Eigen::MatrixXf result = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(this->covMatrix.data(), this->dimensions, this->dimensions);
427 if (cov.rows() == this->dimensions && cov.cols() == this->dimensions)
429 Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(this->covMatrix.data(), this->dimensions, this->dimensions) = cov;
439 if (point.rows() != dimensions)
447 float e = expf(inner(0, 0));
449 return pow(2 *
M_PI, -dimensions / 2.0f) * pow(cov.determinant(), -0.5) * e;
457 obj->setFloatArray(
"cov", covMatrix);
465 obj->getFloatArray(
"cov", covMatrix);
473 this->dimensions = 0;
478 GaussianMixtureDistributionBase(other)
482 for (GaussianMixtureComponentList::const_iterator it = other.components.begin(); it != other.components.end(); ++it)
484 GaussianMixtureComponent comp;
485 comp.gaussian = NormalDistributionBasePtr::dynamicCast(it->gaussian->clone());
486 comp.weight = it->weight;
498 if (
index >= 0 && (
size_t)
index < components.size())
500 return components[
index];
502 throw armarx::IndexOutOfBoundsException();
507 if (components.empty())
509 return GaussianMixtureComponent();
512 GaussianMixtureComponentList::const_iterator res = components.end();
514 for (GaussianMixtureComponentList::const_iterator it = components.begin(); it != components.end(); ++it)
515 if (res == components.end() || it->weight > res->weight)
525 if (components.empty())
527 dimensions = component.gaussian->getDimensions();
529 else if (component.gaussian->getDimensions() != dimensions)
534 components.push_back(component);
539 GaussianMixtureComponent comp;
540 comp.gaussian = gaussian;
541 comp.weight = weight;
552 if (
index < 0 && (
size_t)
index >= components.size())
554 throw armarx::IndexOutOfBoundsException();
556 else if (component.gaussian->getDimensions() != dimensions)
562 components[
index] = component;
568 if (
index >= 0 && (
size_t)
index < components.size())
570 components.erase(components.begin() +
index);
574 throw armarx::IndexOutOfBoundsException();
585 return (::
Ice::Int) components.size();
588 float GaussianMixtureDistribution::getTotalWeight()
const
592 for (GaussianMixtureComponentList::const_iterator it = components.begin(); it != components.end(); ++it)
594 result += it->weight;
602 for (
int i = 0; i < other->size(); ++i)
610 for (GaussianMixtureComponentList::iterator it = components.begin(); it != components.end(); ++it)
612 it->weight *= factor;
618 const float totalWeight = getTotalWeight();
619 GaussianMixtureComponentList::iterator it = components.begin();
621 while (it != components.end())
623 if (it->weight / totalWeight < threshold)
625 it = components.erase(it);
636 const float totalWeight = getTotalWeight();
638 for (GaussianMixtureComponentList::iterator it = components.begin(); it != components.end(); ++it)
640 it->weight /= totalWeight;
646 const float totalWeight = getTotalWeight();
649 for (GaussianMixtureComponentList::iterator it = components.begin(); it != components.end(); ++it)
652 result += gaussian->getDensity(point) * it->weight / totalWeight;
669 NormalDistributionBasePtr gaussian = NormalDistributionBasePtr::dynamicCast(probMeasure);
675 result->addComponent(gaussian, 1.);
687 for (GaussianMixtureComponentList::const_iterator it = components.begin(); it != components.end(); ++it)
689 s <<
"{ gaussian: " << it->gaussian->output() <<
"weight: " << it->weight <<
"} \n";
702 for (GaussianMixtureComponentList::const_iterator it = components.begin(); it != components.end(); ++it)
706 compValue->setVariant(
"gaussian", gaussianVar);
707 compValue->setFloat(
"weight", it->weight);
708 obj->append(compValue);
718 for (
unsigned int i = 0; i < obj->size(); ++i)
722 GaussianMixtureComponent comp;
724 comp.gaussian = gaussianVar->getClass<NormalDistributionBase>();
725 comp.weight = compValue->getFloat(
"weight");