Samplers.h
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 RobotComponents
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <random>
27 #include <type_traits>
28 #include <vector>
29 #include <tuple>
30 
31 #include <Eigen/Core>
32 #include <Eigen/SVD>
33 #include <Eigen/LU>
34 
35 #include "Volume.h"
36 #include "Metrics.h"
37 
39 
40 namespace armarx
41 {
42  /**
43  * @brief Stores a distribution and a generator.
44  * The call operator passes the generator to the distribution
45  */
46  template<class Distribution, class Generator>
47  class Sampler
48  {
49  public:
50  /**
51  * @brief Type of the stored distribution
52  */
53  using DistributionType = Distribution;
54  /**
55  * @brief Type of the stored generator
56  */
57  using GeneratorType = Generator ;
58  protected:
59  /**
60  * @brief The stored distribution.
61  */
62  Distribution distribution;
63  /**
64  * @brief The stored generator
65  */
66  Generator generator;
67  public:
68  /**
69  * @brief Ctor
70  * @param dist The distribution to store.
71  * @param gen The generator to store.
72  */
73  Sampler(Distribution&& dist, Generator&& gen):
74  distribution {dist},
75  generator {gen}
76  {
77  }
78 
79  /**
80  * @brief Passes all arguments followed by the generator to the distribution.
81  * @param args Additional parameters to pass to the distribution.
82  * @return The return value of the distribution.
83  */
84  template<class...Args>
85  auto operator()(Args&& ...args)
86  ->decltype(distribution(std::forward<Args>(args)..., generator))
87  {
88  return distribution(std::forward<Args>(args)..., generator);
89  }
90 
91  /**
92  * @brief Returns the stored distribution.
93  * @return The stored distribution.
94  */
95  Distribution& getDistribution()
96  {
97  return distribution;
98  }
99 
100  /**
101  * @brief Returns the stored generator.
102  * @return The stored generator.
103  */
104  Generator& getGenerator()
105  {
106  return generator;
107  }
108  };
109 
110  /**
111  * @brief Uniform distribution over the surface of an n dimensional unit sphere.
112  *
113  * (using the method described in doi>10.1145/377939.377946 (https://dl.acm.org/citation.cfm?doid=377939.377946))
114  */
115  template<class RealType>
117  {
118  public:
119  /**
120  * @brief Ctor
121  */
123  normDist {0.0, 0.1}
124  {
125  }
126 
127  /**
128  * @brief Fills [first, last) with a uniform sample of n dimensional unit sphere's surface (n = distance(first, last)).
129  * @param first The first value to fill.
130  * @param last One past the last value to fill.
131  * @param gen The generator to use.
132  */
133  template<class Iterator, class Generator>
134  void operator()(const Iterator& first, const Iterator& last, Generator& gen)
135  {
136  RealType length = 0;
137 
138  for (auto it = first; it != last; ++it)
139  {
140  *it = normDist(gen);
141  length += std::pow(*it, 2.0);
142  }
143 
144  length = std::sqrt(length);
145 
146  for (auto it = first; it != last; ++it)
147  {
148  *it /= length;
149  }
150  }
151  protected:
152  /**
153  * @brief The internally used normal distribution.
154  */
155  std::normal_distribution<RealType> normDist;
156  };
157 
158  /**
159  * @brief Uniform distribution over the volume (closed set) of an n dimensional unit sphere.
160  *
161  * https://math.stackexchange.com/questions/87230/picking-random-points-in-the-volume-of-sphere-with-uniform-probability/87238#87238
162  */
163  template<class RealType>
165  {
166  public:
167  /**
168  * @brief Ctor
169  */
171  normDist {0.0, 0.1},
172  radDist {0.0, 1.0}
173  {
174  }
175 
176  /**
177  * @brief Fills [first, last) with a uniform sample of n dimensional unit sphere's volume (n = distance(first, last)).
178  * @param first
179  * @param last
180  * @param gen
181  * @param first The first value to fill.
182  * @param last One past the last value to fill.
183  * @param gen The generator to use.
184  */
185  template<class Iterator, class Generator>
186  void operator()(const Iterator& first, const Iterator& last, Generator& gen)
187  {
188  const RealType dim = std::distance(first, last);
189  RealType length = 0;
190 
191  for (auto it = first; it != last; ++it)
192  {
193  *it = normDist(gen);
194  length += std::pow(*it, 2.0);
195  }
196 
197  length = std::sqrt(length);
198  const auto scalingFactor = std::pow(radDist(gen), 1.0 / dim);
199 
200  for (auto it = first; it != last; ++it)
201  {
202  *it = ((*it) / length) * scalingFactor;
203  }
204  }
205  protected:
206  /**
207  * @brief The internal used normal distribution for samples of the sphere surfaces.
208  */
209  std::normal_distribution<RealType> normDist;
210  /**
211  * @brief The uniform distribution used to transform sphere surface samples to samples in the sphere.
212  */
213  std::uniform_real_distribution<RealType> radDist;
214  };
215 
216  /**
217  * @brief Uniform distribution of an cuboid space.
218  */
219  template<class RealType>
221  {
222  public:
223  /**
224  * @brief Type to pass the bounds of a dimension.
225  */
226  using BoundsType = std::pair<RealType, RealType>;
227 
228  /**
229  * @brief Constructs a cuboid distribution with the bounds [boundsFirst, boundsLast).
230  * bounds.first is used as lower and bounds.second as upper bound.
231  * @param boundsFirst The bounds for the first dimension.
232  * @param boundsLast One past the bounds for the last dimension.
233  */
234  template<class Iterator>
235  UniformCuboidDistribution(Iterator boundsFirst, Iterator boundsLast)
236  {
237  volume = 1;
238 
239  for (; boundsFirst != boundsLast; ++boundsFirst)
240  {
241  ARMARX_CHECK_EXPRESSION(boundsFirst->first < boundsFirst->second);
242  volume *= (boundsFirst->second - boundsFirst->first);
243 
244  //set distribution
245  spaceDist.emplace_back(
246  boundsFirst->first,
247  std::nextafter(boundsFirst->second, std::numeric_limits<RealType>::max())
248  );
249  }
250  }
251 
252  /**
253  * @brief Fills the range [first, first + dimensionality) with an uniform sample of the cuboid.
254  * [first, first + dimensionality) has to be a valid range. If not the behavior is undefined.
255  * @param first The first value to fill.
256  * @param gen The used generator.
257  */
258  template<class Iterator, class Generator>
259  void operator()(Iterator first, Generator& gen)
260  {
261  for (std::size_t i = 0; i < spaceDist.size(); ++i, ++first)
262  {
263  *first = spaceDist.at(i)(gen);
264  }
265  }
266 
267  /**
268  * @brief Returns whether the range [first, first + dimensionality) is a point contained by the cuboid.
269  * [first, first + dimensionality) has to be a valid range. If not the behavior is undefined.
270  * @param first The first value of the point.
271  * @return Whether the point is contained by the cuboid.
272  */
273  template<class Iterator>
274  bool isInBounds(Iterator first)
275  {
276  for (std::size_t i = 0; i < spaceDist.size(); ++i, ++first)
277  {
278  if ((*first < spaceDist.at(i).a()) || (*first > spaceDist.at(i).b()))
279  {
280  return false;
281  }
282  }
283 
284  return true;
285  }
286 
287  /**
288  * @brief Returns the cuboid's volume.
289  * @return The cuboid's volume.
290  */
291  RealType getVolume() const
292  {
293  return volume;
294  }
295 
296  /**
297  * @brief Returns the cuboid's dimensionality.
298  * @return The cuboid's dimensionality.
299  */
300  template<class T = std::size_t>
302  {
303  return static_cast<T>(spaceDist.size());
304  }
305  protected:
306  /**
307  * @brief The cuboid's volume.
308  */
309  RealType volume;
310  /**
311  * @brief Distributions for the dimensions of the cuboid.
312  */
313  std::vector<std::uniform_real_distribution<RealType>> spaceDist;
314  };
315 
317  /**
318  * @brief Uniform distribution of a prolate hyper spheroid.
319  */
320  template<class RealType = float>
322  {
323  protected:
324  //internally used. are not required for the external interface => protected
325  /**
326  * @brief Internal used Eigen vector type.
327  */
329  /**
330  * @brief Internal used Eigen matrix type.
331  */
333  public:
334  /**
335  * @brief Constructs a uiform distribution of a prolate hyper spheroid with the focal points [focalAFirst, focalALast)
336  * and [focalBFirst, focalBFirst + distance(focalAFirst, focalALast)).
337  * @param focalAFirst The first value of the first focal point.
338  * @param focalALast One past the last value of the first focal point.
339  * @param focalBFirst The first value of the second focal point.
340  */
341  template<class ValIter>
343  ValIter focalAFirst, ValIter focalALast,
344  ValIter focalBFirst
345  ):
347  tmpVal(std::distance(focalAFirst, focalALast)),
348  //distanceFocalPoints,
349  polarDiameter {std::numeric_limits<RealType>::infinity()},
350  volumeProlateSpheroid {std::numeric_limits<RealType>::infinity()},
356  {
357  setFocalPoints(focalAFirst, focalBFirst);
358  }
359 
360  /**
361  * @brief Constructs a uiform distribution of a prolate hyper spheroid with the focal points [focalAFirst, focalALast)
362  * and [focalBFirst, focalBFirst + distance(focalAFirst, focalALast)) and the given rotation matrix.
363  * @param focalAFirst The first value of the first focal point.
364  * @param focalALast One past the last value of the first focal point.
365  * @param focalBFirst The first value of the second focal point.
366  * @param rotationMatrix The used rotation matrix (the values should be ordered to be conform with eigens mapping of matrices to vectors.
367  * (as defined by Eigen::Map<Eigen::Matrix<RealType, dimensionality, dimensionality>>)
368  */
369  template<class ValIter>
371  ValIter focalAFirst, ValIter focalALast,
372  ValIter focalBFirst,
373  const std::vector<RealType>& rotationMatrix
374  ):
376  tmpVal(std::distance(focalAFirst, focalALast)),
377  //distanceFocalPoints,
378  polarDiameter {std::numeric_limits<RealType>::infinity()},
379  volumeProlateSpheroid {std::numeric_limits<RealType>::infinity()},
385  {
386 
387  setFocalPoints(focalAFirst, focalBFirst, false);
388  ARMARX_CHECK_EXPRESSION(rotationMatrix.size() == getDimensionality() * getDimensionality());
389 
390  rotation = Eigen::Map<const MatType>(rotationMatrix.data(), getDimensionality(), getDimensionality());
391  }
392 
393  /**
394  * @brief Fills [first, first + dimensionality) with a uniform sample of the prolate hyper spheroid.
395  * @param first The first value to fill.
396  * @param gen The used generator.
397  */
398  template<class Generator>
399  void operator()(RealType* first, Generator& gen)
400  {
401  sphereDistribution(tmpVal.begin(), tmpVal.end(), gen);
402  Eigen::Map<VecType> tmpVec {tmpVal.data(), getDimensionality<long int>()};
403  Eigen::Map<VecType>(first, getDimensionality()) = scaleThenRotate * tmpVec + centre;
404 
406  }
407 
408  /**
409  * @brief Returns the dimensionality of the distribution.
410  * @return The dimensionality of the distribution.
411  */
412  template<class T = std::size_t>
414  {
415  return static_cast<T>(tmpVal.size());
416  }
417 
418  /**
419  * @brief Returns whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid.
420  * @param first The first value of the point to check.
421  * @return Whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid.
422  */
423  bool isInBounds(RealType* first) const
424  {
425  const auto srcVec = Eigen::Map<VecType>(first, getDimensionality());
426  //dont use std::numeric_limits<RealType>::epsilon() since its about 1e-7 and all the floating errors add up
427  return ((focalPointA - srcVec).norm() + (focalPointB - srcVec).norm()) <=
428  getPolarDiameter() + 0.001;
429  }
430 
431  /**
432  * @brief Returns the prolate hyper spheroid's volume.
433  * @return The prolate hyper spheroid's volume.
434  */
435  RealType getVolume() const
436  {
437  return volumeProlateSpheroid;
438  }
439 
440  /**
441  * @brief Returns the distance between the prolate hyper spheroid's focal points.
442  * @return The distance between the prolate hyper spheroid's focal points.
443  */
444  RealType getDistanceFocalPoints() const
445  {
446  return distanceFocalPoints;
447  }
448 
449  /**
450  * @brief Returns the prolate hyper spheroid's polar diameter.
451  * @return The prolate hyper spheroid's polar diameter.
452  */
453  RealType getPolarDiameter() const
454  {
455  return polarDiameter;
456  }
457 
458  /**
459  * @brief Sets the prolate hyper spheroid's polar diameter.
460  * @param The new prolate hyper spheroid's polar diameter.
461  */
462  void setPolarDiameter(RealType newPolarDiameter)
463  {
464  ARMARX_CHECK_EXPRESSION(newPolarDiameter < std::numeric_limits<RealType>::infinity());
465  Eigen::Map<VecType> tmpVec {tmpVal.data(), getDimensionality<long int>()};
466  tmpVec(0) = newPolarDiameter / 2.0;
467 
468  const auto polarDiameterQuad = std::pow(newPolarDiameter, 2.0);
469  const auto distanceFocalPointsQuad = std::pow(distanceFocalPoints, 2.0);
470  const auto equatorialRadius = std::sqrt(polarDiameterQuad - distanceFocalPointsQuad) / 2.0;
471 
472  for (std::size_t i = 1; i < getDimensionality(); ++i) //requires this-> (2 phase lookup)
473  {
474  tmpVec(i) = equatorialRadius;
475  }
476 
477  volumeProlateSpheroid = volumeOfHyperspheroid<RealType>(getDimensionality(), newPolarDiameter / 2.0, equatorialRadius);
478  //calculate compound transformation
479  scaleThenRotate = rotation * tmpVec.asDiagonal();
480  polarDiameter = newPolarDiameter;
481  }
482 
483  /**
484  * @brief Returns the rotation matrix stored in a vector.
485  * @return The rotation matrix stored in a vector.
486  */
487  std::vector<RealType> getRotationMatrix() const
488  {
489  std::vector<RealType> copy(getDimensionality()*getDimensionality());
490  Eigen::Map<MatType>(copy.data(), getDimensionality(), getDimensionality()) = rotation;
491  return copy;
492  }
493 
494  protected:
495  /**
496  * @brief Sets the focal points.
497  * @param focalAFirst The first value of the first focal point.
498  * @param focalBFirst The first value of the second focal point.
499  * @param updateRotationMatrix Whether the rotation matrix should be calculated.
500  */
501  template<class IteratorType>
502  void setFocalPoints(IteratorType focalAFirst, IteratorType focalBFirst, bool updateRotationMatrix = true)
503  {
504  Eigen::Map<VecType> tmpVec {tmpVal.data(), getDimensionality<long int>()};
505  //reset
506  distanceFocalPoints = 0.0;
507 
508  for (std::size_t i = 0; i < getDimensionality(); ++i, ++focalAFirst, ++focalBFirst)
509  {
510  focalPointA(i) = *focalAFirst;
511  focalPointB(i) = *focalBFirst;
512  centre(i) = (focalPointA(i) + focalPointB(i)) / 2.0;
513 
514  const auto focalPointsDelta = focalPointB(i) - focalPointA(i);
515 
516  distanceFocalPoints += std::pow(focalPointsDelta, 2.0);
517  tmpVec(i) = focalPointsDelta;
518  }
519 
521  tmpVec /= distanceFocalPoints;
522 
523  if (updateRotationMatrix)
524  {
525  refreshRotationMatrix(tmpVec);
526  }
527  }
528 
529  /**
530  * @brief Calculates the rotation matrix as described in the paper
531  *
532  * Variable names (mostly) represent the names from the paper
533  * @param normalizedFocalPointAToB corresponds to a_1 from the paper
534  */
535  void refreshRotationMatrix(const VecType& normalizedFocalPointAToB)
536  {
537  const auto n = getDimensionality();
538  ARMARX_CHECK_EXPRESSION(normalizedFocalPointAToB.size() >= 0);
539  ARMARX_CHECK_EXPRESSION(static_cast<std::size_t>(normalizedFocalPointAToB.size()) == n);
540 
541  if (distanceFocalPoints < std::numeric_limits<RealType>::epsilon())
542  {
543  //it is a circle => no rotation
544  rotation.setIdentity(n, n);
545  }
546  else
547  {
548  //from eigen doc (http://eigen.tuxfamily.org/dox/classEigen_1_1JacobiSVD.html)
549  //
550  //NoQRPreconditioner allows not to use a QR preconditioner at all.
551  //This is useful if you know that you will only be computing JacobiSVD decompositions of square matrices.
552  //Non-square matrices require a QR preconditioner. Using this option will result in faster compilation
553  //and smaller executable code.
554  //It won't significantly speed up computation, since JacobiSVD is always checking if QR
555  //preconditioning is needed before applying it anyway.
556  //
557  //our matrix is square, so we will take this small performance boost
558  Eigen::JacobiSVD<MatType, Eigen::NoQRPreconditioner> jacoby(
559  //M = a_1 * <first column of the identity matrix>^T
560  (normalizedFocalPointAToB * (MatType::Identity(n, n).col(0).transpose())),
561  Eigen::ComputeFullV | Eigen::ComputeFullU // we need U and V and we dont want thin matrices
562  );
563 
564  //C = U * diag{1,...,1, det(U)* det(V)} * V^T
565  VecType diag = VecType::Ones(n); // fill with 1
566  //set last value
567  diag(n - 1) = jacoby.matrixU().determinant() * jacoby.matrixV().determinant();
568  //calc rotation = C = U * diag{1,...,1, det(U)* det(V)} * V^T
569 
570  rotation = jacoby.matrixU() * diag.asDiagonal() * jacoby.matrixV().transpose();
571  }
572  }
573 
574  //data
575  /**
576  * @brief The internal used sphere volume distribution. (It's results are transformed to the prolate hyper spheroid)
577  */
579 
580  /**
581  * @brief Buffer for a temporary sample.
582  */
583  mutable std::vector<RealType> tmpVal;
584  /**
585  * @brief The distance of the focalpoints.
586  */
588  /**
589  * @brief The prolate hyper spheroid's diameter.
590  */
591  RealType polarDiameter;
592  /**
593  * @brief The prolate hyper spheroid's volume.
594  */
596 
597  /**
598  * @brief The prolate hyper spheroid's first focal point.
599  */
601  /**
602  * @brief The prolate hyper spheroid's second focal point.
603  */
605  /**
606  * @brief The prolate hyper spheroid's center. (0.5 * (focalPointA + focalPointB))
607  */
609  /**
610  * @brief The used rotation matrix.
611  */
613  /**
614  * @brief The used transformation matrix to transform uniform points from the volume of a sphere
615  * to a uniform sample of the prolate hyper spheroid's volume.
616  */
618  };
619 
620  /**
621  * @brief Implements a distribution as required by Informed RRT*
622  *
623  * (as described in DOI>10.1109/IROS.2014.6942976)
624  */
625  template<class RealType = float>
627  {
628  public:
629  /**
630  * @brief Constructs a uniform informed distribution containing a cuboid distribution (with the bounds [boundsFirst, boundsLast))
631  * and a prolate hyper spheroid distribution (with the focal points [focalAFirst, focalALast)
632  * and [focalBFirst, focalBFirst + distance(focalAFirst, focalALast)) and the given rotation matrix).
633  * @param boundsFirst The bounds for the first dimension.
634  * @param boundsLast One past the bounds for the last dimension.
635  * @param focalAFirst The first value of the first focal point.
636  * @param focalALast One past the last value of the first focal point.
637  * @param focalBFirst The first value of the second focal point.
638  * @param rotationMatrix The used rotation matrix (the values should be ordered to be conform with eigens mapping of matrices to vectors.
639  * (as defined by Eigen::Map<Eigen::Matrix<RealType, dimensionality, dimensionality>>)
640  */
641  template<class ValIter, class BoundsIter>
643  BoundsIter boundsFirst, BoundsIter boundsLast,
644  ValIter focalAFirst, ValIter focalALast,
645  ValIter focalBFirst,
646  const std::vector<RealType>& rotationMatrix
647  ):
648  spheroidDistribution {focalAFirst, focalALast, focalBFirst, rotationMatrix},
649  cuboidDistribution {boundsFirst, boundsLast},
650  cMax {std::numeric_limits<RealType>::infinity()}
651  {
652  if (spheroidDistribution.getDimensionality() != cuboidDistribution.getDimensionality())
653  {
654  throw std::logic_error {"differend bounds for spheroid and cuboid"};
655  }
656  }
657 
658  /**
659  * @brief Constructs a uniform informed distribution containing a cuboid distribution (with the bounds [boundsFirst, boundsLast))
660  * and a prolate hyper spheroid distribution (with the focal points [focalAFirst, focalALast)
661  * and [focalBFirst, focalBFirst + distance(focalAFirst, focalALast))).
662  * @param boundsFirst The bounds for the first dimension.
663  * @param boundsLast One past the bounds for the last dimension.
664  * @param focalAFirst The first value of the first focal point.
665  * @param focalALast One past the last value of the first focal point.
666  * @param focalBFirst The first value of the second focal point.
667  */
668  template<class ValIter, class BoundsIter>
670  BoundsIter boundsFirst, BoundsIter boundsLast,
671  ValIter focalAFirst, ValIter focalALast,
672  ValIter focalBFirst
673  ):
674  spheroidDistribution {focalAFirst, focalALast, focalBFirst},
675  cuboidDistribution {boundsFirst, boundsLast},
676  cMax {std::numeric_limits<RealType>::infinity()}
677  {
678  if (spheroidDistribution.getDimensionality() != cuboidDistribution.getDimensionality())
679  {
680  throw std::logic_error {"differend bounds for spheroid and cuboid"};
681  }
682  }
683 
684  /**
685  * @brief Fills [first, first + dimensionality) with a uniform sample of the intersection of the prolate hyper spheroid and cuboid space.
686  * @param first The first value to fill.
687  * @param gen The used generator.
688  */
689  template<class Generator>
690  void operator()(RealType* first, Generator& gen)
691  {
692  if (cMax < std::numeric_limits<RealType>::infinity())
693  {
694  if (cuboidDistribution.getVolume() <= spheroidDistribution.getVolume())
695  {
696  //the space has a smaller volume => sample the space
697  do
698  {
699  cuboidDistribution(first, gen);
700  }
701  while (!spheroidDistribution.isInBounds(first));
702  }
703  else
704  {
705  //rejection sample the ellipsoid until the result is valid
706  do
707  {
708  spheroidDistribution(first, gen);
709  }
710  while (!cuboidDistribution.isInBounds(first));
711  }
712  }
713  else
714  {
715  //sample space
716  cuboidDistribution(first, gen);
717  }
718  }
719 
720  /**
721  * @brief Sets the maximal cost to max if the current maximal cost is higher.
722  * @param max The new maximal cost to set.
723  * @return Whether the new cost was lower than the old cost.
724  */
725  bool setMaximalCost(RealType max)
726  {
727  max = std::max(max, spheroidDistribution.getDistanceFocalPoints()); //in case of floating error
728 
729  if (!(max < cMax))
730  {
731  //don't set to a worse value + if there is no change we don't need to refresh
732  return false;
733  }
734 
735  //cMax < max <= std::numeric_limits<RealType>::infinity() => cMax < inf
736  cMax = max;
737  //if(cMax < std::numeric_limits<RealType>::infinity())
738  //{
739  //since cMax is init with std::numeric_limits<RealType>::infinity()
740  //it should never be std::numeric_limits<RealType>::infinity() here.
741  //the assert might catch other nans
742  ARMARX_CHECK_EXPRESSION(cMax < std::numeric_limits<RealType>::infinity());
743  spheroidDistribution.setPolarDiameter(cMax);
744  //}
745  return true;
746  }
747 
748  /**
749  * @brief Returns the distributions volume. It is the minimum of the prolate hyper spheroid's and the cuboid's volumes.
750  * @return The distributions volume.
751  */
752  RealType getVolume() const
753  {
754  return std::min(cuboidDistribution.getVolume(), spheroidDistribution.getVolume());
755  }
756 
757 
758  /**
759  * @brief Returns the distributions dimensionality.
760  * @return The distributions dimensionality.
761  */
762  template<class T = std::size_t>
764  {
765  return cuboidDistribution.template getDimensionality<T>();
766  }
767 
768  /**
769  * @brief Returns whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid and the cuboid.
770  * @param first The first value of the point to check.
771  * @return Whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid and the cuboid.
772  */
773  bool isInBounds(const RealType* first) const
774  {
775  return cuboidDistribution.isInBounds(first) && spheroidDistribution.isInBounds(first);
776  }
777 
778  protected:
779  /**
780  * @brief The prolate hyper spheroid's distribution.
781  */
783  /**
784  * @brief The cuboid's distribution.
785  */
787  /**
788  * @brief The current maximal cost.
789  */
790  RealType cMax;
791  };
792 
793  /**
794  * @brief Typedef for a sampler as required by informed rrt*.
795  */
797 }
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::Sampler::getDistribution
Distribution & getDistribution()
Returns the stored distribution.
Definition: Samplers.h:95
armarx::UniformProlateSpheroidDistribution::rotation
MatType rotation
The used rotation matrix.
Definition: Samplers.h:612
armarx::Sampler::operator()
auto operator()(Args &&...args) -> decltype(distribution(std::forward< Args >(args)..., generator))
Passes all arguments followed by the generator to the distribution.
Definition: Samplers.h:85
armarx::UniformProlateSpheroidDistribution::tmpVal
std::vector< RealType > tmpVal
Buffer for a temporary sample.
Definition: Samplers.h:583
armarx::UniformProlateSpheroidDistribution::focalPointB
VecType focalPointB
The prolate hyper spheroid's second focal point.
Definition: Samplers.h:604
armarx::UniformInformedProlateSpheroidDistribution::spheroidDistribution
UniformProlateSpheroidDistribution< RealType > spheroidDistribution
The prolate hyper spheroid's distribution.
Definition: Samplers.h:782
armarx::Sampler::Sampler
Sampler(Distribution &&dist, Generator &&gen)
Ctor.
Definition: Samplers.h:73
armarx::UniformUnitSphereDistribution::operator()
void operator()(const Iterator &first, const Iterator &last, Generator &gen)
Fills [first, last) with a uniform sample of n dimensional unit sphere's volume (n = distance(first,...
Definition: Samplers.h:186
armarx::UniformCuboidDistribution::spaceDist
std::vector< std::uniform_real_distribution< RealType > > spaceDist
Distributions for the dimensions of the cuboid.
Definition: Samplers.h:313
armarx::UniformProlateSpheroidDistribution::isInBounds
bool isInBounds(RealType *first) const
Returns whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid.
Definition: Samplers.h:423
armarx::UniformProlateSpheroidDistribution::getDimensionality
T getDimensionality() const
Returns the dimensionality of the distribution.
Definition: Samplers.h:413
armarx::UniformInformedProlateSpheroidDistribution
Implements a distribution as required by Informed RRT*.
Definition: Samplers.h:626
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::Sampler
Stores a distribution and a generator.
Definition: Samplers.h:47
armarx::UniformProlateSpheroidDistribution::focalPointA
VecType focalPointA
The prolate hyper spheroid's first focal point.
Definition: Samplers.h:600
armarx::UniformInformedProlateSpheroidDistribution::getDimensionality
T getDimensionality() const
Returns the distributions dimensionality.
Definition: Samplers.h:763
armarx::UniformInformedProlateSpheroidDistribution::setMaximalCost
bool setMaximalCost(RealType max)
Sets the maximal cost to max if the current maximal cost is higher.
Definition: Samplers.h:725
armarx::Sampler::generator
Generator generator
The stored generator.
Definition: Samplers.h:66
armarx::UniformProlateSpheroidDistribution::refreshRotationMatrix
void refreshRotationMatrix(const VecType &normalizedFocalPointAToB)
Calculates the rotation matrix as described in the paper.
Definition: Samplers.h:535
armarx::UniformUnitSphereSurfaceDistribution::operator()
void operator()(const Iterator &first, const Iterator &last, Generator &gen)
Fills [first, last) with a uniform sample of n dimensional unit sphere's surface (n = distance(first,...
Definition: Samplers.h:134
armarx::UniformProlateSpheroidDistribution::polarDiameter
RealType polarDiameter
The prolate hyper spheroid's diameter.
Definition: Samplers.h:591
armarx::UniformCuboidDistribution< float >::BoundsType
std::pair< float, float > BoundsType
Type to pass the bounds of a dimension.
Definition: Samplers.h:226
armarx::UniformInformedProlateSpheroidDistribution::isInBounds
bool isInBounds(const RealType *first) const
Returns whether [first, first + dimensionality) is a point contained by the prolate hyper spheroid an...
Definition: Samplers.h:773
armarx::UniformProlateSpheroidDistribution::getVolume
RealType getVolume() const
Returns the prolate hyper spheroid's volume.
Definition: Samplers.h:435
armarx::UniformProlateSpheroidDistribution::sphereDistribution
UniformUnitSphereDistribution< RealType > sphereDistribution
The internal used sphere volume distribution.
Definition: Samplers.h:578
armarx::UniformInformedProlateSpheroidDistribution::getVolume
RealType getVolume() const
Returns the distributions volume.
Definition: Samplers.h:752
armarx::UniformProlateSpheroidDistribution::getRotationMatrix
std::vector< RealType > getRotationMatrix() const
Returns the rotation matrix stored in a vector.
Definition: Samplers.h:487
Volume.h
armarx::UniformProlateSpheroidDistribution::UniformProlateSpheroidDistribution
UniformProlateSpheroidDistribution(ValIter focalAFirst, ValIter focalALast, ValIter focalBFirst, const std::vector< RealType > &rotationMatrix)
Constructs a uiform distribution of a prolate hyper spheroid with the focal points [focalAFirst,...
Definition: Samplers.h:370
armarx::UniformProlateSpheroidDistribution::getDistanceFocalPoints
RealType getDistanceFocalPoints() const
Returns the distance between the prolate hyper spheroid's focal points.
Definition: Samplers.h:444
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::Sampler::getGenerator
Generator & getGenerator()
Returns the stored generator.
Definition: Samplers.h:104
Metrics.h
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::UniformProlateSpheroidDistribution::getPolarDiameter
RealType getPolarDiameter() const
Returns the prolate hyper spheroid's polar diameter.
Definition: Samplers.h:453
armarx::UniformUnitSphereDistribution::UniformUnitSphereDistribution
UniformUnitSphereDistribution()
Ctor.
Definition: Samplers.h:170
armarx::UniformProlateSpheroidDistribution::scaleThenRotate
MatType scaleThenRotate
The used transformation matrix to transform uniform points from the volume of a sphere to a uniform s...
Definition: Samplers.h:617
armarx::UniformCuboidDistribution::volume
RealType volume
The cuboid's volume.
Definition: Samplers.h:309
armarx::UniformUnitSphereDistribution::radDist
std::uniform_real_distribution< RealType > radDist
The uniform distribution used to transform sphere surface samples to samples in the sphere.
Definition: Samplers.h:213
armarx::Sampler::DistributionType
Distribution DistributionType
Type of the stored distribution.
Definition: Samplers.h:53
armarx::UniformInformedProlateSpheroidDistribution::cuboidDistribution
UniformCuboidDistribution< RealType > cuboidDistribution
The cuboid's distribution.
Definition: Samplers.h:786
armarx::UniformUnitSphereSurfaceDistribution::normDist
std::normal_distribution< RealType > normDist
The internally used normal distribution.
Definition: Samplers.h:155
armarx::UniformInformedProlateSpheroidDistribution::UniformInformedProlateSpheroidDistribution
UniformInformedProlateSpheroidDistribution(BoundsIter boundsFirst, BoundsIter boundsLast, ValIter focalAFirst, ValIter focalALast, ValIter focalBFirst, const std::vector< RealType > &rotationMatrix)
Constructs a uniform informed distribution containing a cuboid distribution (with the bounds [boundsF...
Definition: Samplers.h:642
armarx::UniformCuboidDistribution
Uniform distribution of an cuboid space.
Definition: Samplers.h:220
armarx::UniformUnitSphereDistribution
Uniform distribution over the volume (closed set) of an n dimensional unit sphere.
Definition: Samplers.h:164
armarx::UniformCuboidDistribution::operator()
void operator()(Iterator first, Generator &gen)
Fills the range [first, first + dimensionality) with an uniform sample of the cuboid.
Definition: Samplers.h:259
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::UniformCuboidDistribution::getVolume
RealType getVolume() const
Returns the cuboid's volume.
Definition: Samplers.h:291
ExpressionException.h
armarx::UniformInformedProlateSpheroidDistribution::operator()
void operator()(RealType *first, Generator &gen)
Fills [first, first + dimensionality) with a uniform sample of the intersection of the prolate hyper ...
Definition: Samplers.h:690
armarx::UniformCuboidDistribution::isInBounds
bool isInBounds(Iterator first)
Returns whether the range [first, first + dimensionality) is a point contained by the cuboid.
Definition: Samplers.h:274
armarx::UniformCuboidDistribution::UniformCuboidDistribution
UniformCuboidDistribution(Iterator boundsFirst, Iterator boundsLast)
Constructs a cuboid distribution with the bounds [boundsFirst, boundsLast).
Definition: Samplers.h:235
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::Sampler::distribution
Distribution distribution
The stored distribution.
Definition: Samplers.h:62
armarx::UniformInformedProlateSpheroidDistribution::UniformInformedProlateSpheroidDistribution
UniformInformedProlateSpheroidDistribution(BoundsIter boundsFirst, BoundsIter boundsLast, ValIter focalAFirst, ValIter focalALast, ValIter focalBFirst)
Constructs a uniform informed distribution containing a cuboid distribution (with the bounds [boundsF...
Definition: Samplers.h:669
armarx::UniformProlateSpheroidDistribution::UniformProlateSpheroidDistribution
UniformProlateSpheroidDistribution(ValIter focalAFirst, ValIter focalALast, ValIter focalBFirst)
Constructs a uiform distribution of a prolate hyper spheroid with the focal points [focalAFirst,...
Definition: Samplers.h:342
Eigen::Matrix< float, Eigen::Dynamic, 1 >
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
armarx::Sampler::GeneratorType
Generator GeneratorType
Type of the stored generator.
Definition: Samplers.h:57
armarx::UniformCuboidDistribution::getDimensionality
T getDimensionality() const
Returns the cuboid's dimensionality.
Definition: Samplers.h:301
armarx::UniformProlateSpheroidDistribution::operator()
void operator()(RealType *first, Generator &gen)
Fills [first, first + dimensionality) with a uniform sample of the prolate hyper spheroid.
Definition: Samplers.h:399
armarx::UniformInformedProlateSpheroidDistribution::cMax
RealType cMax
The current maximal cost.
Definition: Samplers.h:790
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::UniformProlateSpheroidDistribution::volumeProlateSpheroid
RealType volumeProlateSpheroid
The prolate hyper spheroid's volume.
Definition: Samplers.h:595
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::transpose
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T >> &src, Thrower thrower)
Definition: SimoxCSpace.cpp:706
armarx::UniformProlateSpheroidDistribution
Uniform distribution of a prolate hyper spheroid.
Definition: Samplers.h:321
armarx::UniformUnitSphereDistribution::normDist
std::normal_distribution< RealType > normDist
The internal used normal distribution for samples of the sphere surfaces.
Definition: Samplers.h:209
armarx::UniformProlateSpheroidDistribution::setPolarDiameter
void setPolarDiameter(RealType newPolarDiameter)
Sets the prolate hyper spheroid's polar diameter.
Definition: Samplers.h:462
armarx::UniformUnitSphereSurfaceDistribution
Uniform distribution over the surface of an n dimensional unit sphere.
Definition: Samplers.h:116
armarx::UniformProlateSpheroidDistribution::distanceFocalPoints
RealType distanceFocalPoints
The distance of the focalpoints.
Definition: Samplers.h:587
armarx::UniformProlateSpheroidDistribution::setFocalPoints
void setFocalPoints(IteratorType focalAFirst, IteratorType focalBFirst, bool updateRotationMatrix=true)
Sets the focal points.
Definition: Samplers.h:502
armarx::UniformUnitSphereSurfaceDistribution::UniformUnitSphereSurfaceDistribution
UniformUnitSphereSurfaceDistribution()
Ctor.
Definition: Samplers.h:122
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::UniformProlateSpheroidDistribution::centre
VecType centre
The prolate hyper spheroid's center.
Definition: Samplers.h:608
norm
double norm(const Point &a)
Definition: point.hpp:94