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