BaseBufferManipulators.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #include <random>
31 #include <chrono>
32 #include <algorithm>
33 #include <unordered_set>
34 #include <memory>
35 #include <boost/core/typeinfo.hpp>
36 
38 
39 namespace lvr2 {
40 
41 namespace manipulators {
42 
43 class Slice : public boost::static_visitor< MultiChannelMap::val_type >
44 {
45 public:
46  Slice(size_t left, size_t right)
47  :m_left(left)
48  ,m_right(right)
49  {}
50 
51  template<typename T>
53  {
55 
56  const size_t range = m_right - m_left;
57  if(range < channel.numElements())
58  {
59  Channel<T> ret(range, channel.width());
60  for(size_t i = m_left; i<m_right; i++)
61  {
62  for(int j=0; j<channel.width(); j++)
63  {
64  ret[i-m_left][j] = channel[i][j];
65  }
66  }
67  vres = ret;
68  } else {
69  vres = channel;
70  }
71 
72  return vres;
73  }
74 private:
75  const size_t m_left;
76  const size_t m_right;
77 };
78 
79 template<typename T>
80 inline void no_delete(T* x)
81 {
82 
83 }
84 
85 class SliceShallow : public boost::static_visitor< MultiChannelMap::val_type >
86 {
87 public:
88  SliceShallow(size_t left, size_t right)
89  :m_left(left)
90  ,m_right(right)
91  {}
92 
93  template<typename T>
95  {
97 
98  const size_t range = m_right - m_left;
99 
100  size_t offset = m_left * channel.width();
101 
102  boost::core::typeinfo const & ti = BOOST_CORE_TYPEID(T);
103 
104  boost::shared_array<T> shallow_ptr(
105  channel.dataPtr().get() + offset,
106  no_delete<T>
107  );
108 
109  Channel<T> ret(
110  range,
111  channel.width(),
112  shallow_ptr
113  );
114 
115  vres = ret;
116  return vres;
117  }
118 private:
119  const size_t m_left;
120  const size_t m_right;
121 };
122 
123 class RandomSample : public boost::static_visitor< MultiChannelMap::val_type >
124 {
125 public:
126  RandomSample(size_t num_samples)
127  :m_num_samples(num_samples)
128  ,m_seed(std::chrono::steady_clock::now().time_since_epoch().count())
129  {
130  }
131 
132  template<typename T>
134  {
136 
137  Channel<T> res(m_num_samples, channel.width());
138 
139  if(m_num_samples > channel.numElements() / 2)
140  {
141  std::vector<size_t> indices(channel.numElements());
142  std::iota(indices.begin(), indices.end(), 0);
143  std::shuffle(indices.begin(), indices.end(), std::default_random_engine(m_seed));
144 
145  for(size_t i=0; i<m_num_samples; i++)
146  {
147  for(size_t j=0; j<channel.width(); j++)
148  {
149  res[i][j] = channel[indices[i]][j];
150  }
151  }
152  } else {
153  std::unordered_set<size_t> indices;
154  std::srand(m_seed);
155  auto eng = std::default_random_engine(m_seed);
156  std::uniform_int_distribution<> distr(0, channel.numElements() - 1);
157 
158  while(indices.size() < m_num_samples )
159  {
160  indices.insert(distr(eng));
161  }
162 
163  size_t i = 0;
164  for(const size_t& index : indices)
165  {
166  for(size_t j=0; j<channel.width(); j++)
167  {
168  res[i][j] = channel[index][j];
169  }
170  i++;
171  }
172  }
173 
174  vres = res;
175  return vres;
176  }
177 
178 private:
179  const size_t m_num_samples;
180  const size_t m_seed;
181 };
182 
183 } // namespace manipulators
184 
185 } // namespace lvr2
size_t width() const
const DataPtr dataPtr() const
SliceShallow(size_t left, size_t right)
MultiChannelMap::val_type operator()(Channel< T > &channel) const
MultiChannelMap::val_type operator()(Channel< T > &channel) const
MultiChannelMap::val_type operator()(Channel< T > &channel) const
size_t numElements() const
Slice(size_t left, size_t right)


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06