Parasol Planning Library (PPL)
PointConstruction.h
Go to the documentation of this file.
1 #ifndef PMPL_POINT_CONSTRUCTION_H
2 #define PMPL_POINT_CONSTRUCTION_H
3 
5 #include "Vector.h"
6 
8  public:
10  PointConstruction(XMLNode& _node);
11 
12  virtual void Initialize() override;
13 
14 
22  template <size_t D>
23  Vector<double, D> SampleSphereSurface(
24  Vector<double, D> center, Vector<double, D> bounds);
25 
26  template <size_t D>
27  std::vector<Vector<double, D>> SampleSphereSurface(
28  Vector<double, D> center, Vector<double, D> bounds, int nsamples);
29 
30  template <size_t D>
31  std::vector<Vector<double, D>> SampleSphereSurface(
32  Vector<double, D> center, double bounds, int nsamples);
33 
36  double NormalDistribution(double mean, double variance);
37 
38  private:
39  template<size_t D>
40  Vector<double, D> ElementwiseMultiply(
41  Vector<double, D> a, Vector<double, D> b);
42 };
43 
44 
45 template <size_t D>
46 std::vector<Vector<double, D>>
48 SampleSphereSurface(Vector<double, D> center, double bounds, int nsamples) {
49  double b[D];
50  for (size_t i = 0; i<D; i++) { b[i] = bounds; }
51  Vector<double, D> _bounds(b);
52  return SampleSphereSurface(center, _bounds, nsamples);
53 };
54 
55 
56 template <size_t D>
57 std::vector<Vector<double, D>>
59 SampleSphereSurface(Vector<double, D> center, Vector<double, D> bounds, int nsamples) {
60  std::vector<Vector<double, D>> samples;
61  for (int i = 0; i < nsamples; i++) {
62  samples.push_back(SampleSphereSurface(center, bounds));
63  }
64 
65  return samples;
66 };
67 
68 
69 template <size_t D>
70 Vector<double, D>
72 SampleSphereSurface(Vector<double, D> center, Vector<double, D> bounds) {
73  // This follows the Muller Method (#3 and #19):
74  // http://extremelearning.com.au/how-to-generate-uniformly-random-points-on-n-spheres-and-n-balls/
75 
76  double normal_samples[D];
77  for (size_t i = 0; i < D; i++) {
78  normal_samples[i] = GaussianDistribution(0, 1);
79  }
80  Vector<double, D> samples(normal_samples);
81  if (this->m_debug) {
82  std::cout << "Normal distribution sample: " << normal_samples << std::endl;
83  }
84 
85  // elementwiseMultiply both before and after normalization is intentional move.
86  // if one of our bounds is 0, this becomes a (n-1)-d object.
87  // And that affects our calcuations.
88  // samples = samples.elementwiseMultiply(bounds);
89  samples = samples.selfNormalize();
90  samples = ElementwiseMultiply(samples, bounds);
91  samples += center;
92  return samples;
93 }
94 
95 
96 template <size_t D>
97 Vector<double, D>
98 PointConstruction::
99 ElementwiseMultiply(Vector<double, D> a, Vector<double, D> b) {
100  Vector<double, D> multiplied;
101  for (size_t i = 0; i < D; i++) {
102  multiplied[i] = a[i] * b[i];
103  }
104  return multiplied;
105 }
106 
107 #endif
double GaussianDistribution(double _mean, double _stdev)
Same as GRand, but one can specify the mean and stdev of the distribution.
Definition: MPUtils.cpp:44
Definition: MPBaseObject.h:46
bool m_debug
Print debug info?
Definition: MPBaseObject.h:183
Definition: PointConstruction.h:7
Vector< double, D > SampleSphereSurface(Vector< double, D > center, Vector< double, D > bounds)
Definition: PointConstruction.h:72
virtual void Initialize() override
Definition: PointConstruction.cpp:21
PointConstruction()
Definition: PointConstruction.cpp:8
double NormalDistribution(double mean, double variance)
Definition: XMLNode.h:27