examples/channels/Main.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "lvr2/types/Channel.hpp"
7 
9 
11 
12 
13 using namespace lvr2;
14 
15 template<typename T>
16 void fillChannel(Channel<T>& channel, T value)
17 {
18  for(int i=0; i<channel.numElements(); i++)
19  {
20  for(int j=0; j<channel.width(); j++)
21  {
22  channel[i][j] = value;
23  }
24  }
25 }
26 
31 {
32  std::cout << "1) Basic Channel Test" << std::endl;
33  size_t num_points = 10000;
34 
38 
39  // construct channel objects
40  Channel<float> points(num_points, 3);
41  Channel<float> normals(num_points, 3);
42  Channel<unsigned char> colors(num_points, 3);
43 
44  // fill channels
45  fillChannel(points, 5.0f);
46  fillChannel(normals, 1.0f);
47  fillChannel(colors, static_cast<unsigned char>(255));
48 
49  // print channels
50  std::cout << " points: " << points << std::endl;
51  std::cout << " number of normals: " << normals.numElements() << std::endl;
52  std::cout << " number of color channels: " << colors.width() << std::endl;
53 
54  // copy channels
55  std::cout << " deep copy" << std::endl;
56  Channel<float> points2(points);
57  std::cout << " shallow copy" << std::endl;
58  Channel<float> points3(0,0);
59  points3 = points2;
60 
64 
65  // build a variant channel of two possible types float or unsigned char
66  std::cout << " generate a variant channel..." << std::endl;
67  VariantChannel<float, unsigned char> vchannel(points);
68  std::cout << " a variant channel: " << vchannel << std::endl;
69 
70  // you can fetch the data with dataPtr<Type>()
71  auto data = vchannel.dataPtr<float>();
72  std::cout << " first element of points: " << data[0] << std::endl;
73 
74  // you can get the index of the variant with this function
75  std::cout << " float is the index "
77  << " in [float, unsigned char]"
78  << std::endl;
79 }
80 
81 
83 {
84  std::cout << "2) Multi Channel Map Usage" << std::endl;
85  size_t num_points = 10000;
86  Channel<float> points(num_points, 3);
87  Channel<float> normals(num_points, 3);
88  Channel<unsigned char> colors(num_points, 3);
89 
90  // fill channels
91  fillChannel(points, 5.0f);
92  fillChannel(normals, 1.0f);
93  fillChannel(colors, static_cast<unsigned char>(255));
94 
97 
98 
99 
100 
101  // 1) Insert Channels of different types
102  MultiChannelMap cm = {
103  {"points", points}
104  };
105  cm["colors"] = colors;
106  cm.insert({"normals", normals});
107 
108  // 2) Information
109  std::cout << " number of channels in channelmap: " << cm.size() << std::endl;
110  std::cout << " number of points: " << cm["points"].numElements() << std::endl;
111 
112 
113 
114  // 3) Get Channels
115  // use only if you now the channel exists
116  Channel<float> pts = cm.get<float>("points");
117 
118  // use this if you dont know
119  Channel<float>::Optional not_existing_channel = cm.getOptional<float>("hello world");
120  if(not_existing_channel) {
121  Channel<float> existing_channel = *not_existing_channel;
122  } else {
123  std::cout << " the channel 'hello world' doesnt exist" << std::endl;
124  }
125 
126  // or use find
127  auto it = cm.find("points");
128  if(it != cm.end())
129  {
130  std::cout << " " << it->first << " found!" << std::endl;
131  if(it->second.is_type<float>())
132  {
133  Channel<float> pts2 = it->second.extract<float>();
134  }
135  }
136 
137  // 4) iterator
138 
139  std::cout << " lets iterate" << std::endl;
140  for(auto it: cm)
141  {
142  std::cout << " --"<< it.first << ": " << it.second.numElements() << " x " << it.second.width() << std::endl;
143  }
144 
145  MultiChannelMap fcm;
146 
147  // typed iteration
148  std::cout << " lets iterate over the type float" << std::endl;
149  for(auto it = cm.typedBegin<float>(); it != cm.end(); ++it)
150  {
151  Channel<float> ch = it->second;
152  std::cout << " --" << it->first << ": " << ch << std::endl;
153 
154  fcm.insert(*it);
155  }
156 
157  // 5) Remove
158 
159  // remove all floats
160  std::cout << " removing floats..." << std::endl,
161  std::cout << " size before remove: " << cm.size() << std::endl;
162  auto it2 = cm.typedBegin<float>();
163  while(it2 != cm.end())
164  {
165  it2 = cm.erase(it2);
166  }
167  std::cout << " size after remove: " << cm.size() << std::endl;
168 
169  // remove with string
170  fcm.erase("points");
171 
172  // 6) other functions
173  std::vector<std::string> keys = fcm.keys<float>();
174  size_t num_float_channels = fcm.numChannels<float>();
175 
176  // 7) print
177  std::cout << " The channel map: " << std::endl;
178  std::cout << cm << std::endl;
179 
180  // 8) copy
181  std::cout << " Channel Map Copy:" << std::endl;
182  // deep copy
183  std::cout << " deep copy" << std::endl;
184  MultiChannelMap cm2(fcm);
185  std::cout << " deep copy" << std::endl;
186  MultiChannelMap cm3 = fcm;
187  // shallow copy
188  std::cout << " shallow copy" << std::endl;
189  MultiChannelMap cm4;
190  cm4 = cm3;
191 
192 }
193 
195 {
196  size_t num_points = 10000;
197 
198  Channel<float> points(num_points, 3);
199  Channel<float> normals(num_points, 3);
200  Channel<unsigned char> colors(num_points, 3);
201  Channel<unsigned char> hyper(600, 600);
202 
203  fillChannel(points, 0.0f);
204  fillChannel(normals, 1.0f);
205  fillChannel(colors, static_cast<unsigned char>(255));
206  fillChannel(hyper, static_cast<unsigned char>(100));
207 
208  std::cout << "3) Channel Manager Usage" << std::endl;
209 
210  // MultiChannelMap with extended functions -> ChannelManager
211 
212  BaseBuffer cm = {
213  {"points2" , points},
214  {"hyper", hyper}
215  };
216 
217  cm["points"] = points;
218  cm["colors"] = colors;
219  cm["normals"] = normals;
220 
221 
222  // Get Channels
223  FloatChannelOptional points_test;
224 
225  points_test = cm.getFloatChannel("points");
226  points_test = cm.getFloatChannel("colors");
227  points_test = cm.getFloatChannel("asdf");
228 
229  cm.getChannel("points", points_test);
230  cm.getChannel("colors", points_test);
231  cm.getChannel("asdf", points_test);
232 
233  // Atomics
234  cm.addFloatAtomic(5.5, "myatomic");
235  auto myatomic = cm.getFloatAtomic("myatomic");
236  myatomic = cm.getFloatAtomic("bla");
237 
238  std::cout << " total number of elements: " << cm.size() << std::endl;
239  std::cout << " num float channels: " << cm.numChannels<float>() << std::endl;
240  std::cout << " float channels are:" << std::endl;
241 
242  for(auto key : cm.keys<float>())
243  {
244  std::cout << " -- " << key << std::endl;
245  }
246 
247  BaseBuffer cm2;
248 
249  std::cout << " float channels again:" << std::endl;
250  for(auto it = cm.typedBegin<float>(); it != cm.end(); ++it)
251  {
252  std::cout << " -- " << it->first << " " << it->second.numElements() << std::endl;
253  cm2.insert({it->first, it->second});
254  cm2[it->first] = it->second;
255  cm2.insert(*it);
256  }
257  std::cout << " inserted elements: " << cm2.size() << std::endl;
258 
259  std::cout << " float iteration with remove:" << std::endl;
260  auto it = cm.typedBegin<float>();
261  while(it != cm.end())
262  {
263  std::cout << " remove " << it->first << std::endl;
264  it = cm.erase(it);
265  }
266 
267 
268  std::cout << " unsigned char iteration:" << std::endl;
269  for(auto it = cm.typedBegin<unsigned char>(); it != cm.end(); ++it)
270  {
271  std::cout << " -- " << it->first << std::endl;
272  }
273 
274  UCharChannelOptional colors3 = cm.getOptional<unsigned char>("yei");
275 
276  if(colors3)
277  {
278  std::cout << " colors found: " << colors3->numElements() << std::endl;
279  }
280 
281 }
282 
284 {
285  size_t num_points = 10000;
286 
287  Channel<float> points(num_points, 3);
288  Channel<float> normals(num_points, 3);
289  Channel<unsigned char> colors(num_points, 3);
290 
291  fillChannel(points, 0.0f);
292  fillChannel(normals, 1.0f);
293  fillChannel(colors, static_cast<unsigned char>(255));
294 
295  std::cout << "3) Channel Manager Usage" << std::endl;
296 
297  // MultiChannelMap with extended functions -> ChannelManager
298 
299  BaseBuffer cm = {
300  {"points2" , points}
301  };
302 
303  cm["points"] = points;
304  cm["colors"] = colors;
305  cm["normals"] = normals;
306 
307 
308  BaseBuffer cm_sliced = cm.manipulate(manipulators::Slice(10, 100));
309  std::cout << "Sliced:" << std::endl;
310  std::cout << cm_sliced << std::endl;
311 
312  BaseBuffer cm_sampled = cm.manipulate(manipulators::RandomSample(1000));
313  std::cout << "Random Sampled:" << std::endl;
314  std::cout << cm_sampled << std::endl;
315 
316  BaseBuffer cm_sliced_shallow = cm.manipulate(manipulators::SliceShallow(10, 100));
317 
318  // test shallow
319  Channel<float> pts3 = cm_sliced_shallow.get<float>("points");
320  pts3[0][0] = 42.0;
321  Channel<float> pts4 = cm.get<float>("points");
322  std::cout << " should be 42: " << pts4[10][0] << std::endl;
323 
324 
325 
326  // clone
327  BaseBuffer cm2 = cm.clone();
328 
329  Channel<float> pts = cm.get<float>("points");
330  Channel<float> pts2 = cm2.get<float>("points");
331 
332  for(int i=0; i<pts.numElements(); i++)
333  {
334  for(int j=0; j<pts.width(); j++)
335  {
336  if(pts[i][j] != pts2[i][j])
337  {
338  std::cout << "ERROR" << std::endl;
339  }
340  }
341  }
342 }
343 
345 {
346  size_t num_points = 10000;
347 
348  Channel<float> points(num_points, 3);
349  Channel<float> normals(num_points, 3);
350  Channel<unsigned char> colors(num_points, 3);
351 
352  BaseBuffer bb = {
353  {"points", points},
354  {"normals", normals},
355  {"color", colors}
356  };
357 
358  std::cout << " index of float: " << BaseBuffer::index_of_type<float>::value << std::endl;
359 
360  auto it = bb.find("points");
361  if(it != bb.end())
362  {
363  BaseBuffer::val_type vchannel;
364  vchannel = it->second;
365  switch(vchannel.type())
366  {
368  std::cout << " a char!" << std::endl;
369  break;
371  std::cout << " a float! " << vchannel.numElements() << std::endl;
372  break;
373  }
374  }
375 
376 }
377 
378 int main(int argc, const char** argv)
379 {
385 
386  return 0;
387 }
BaseBuffer manipulate(V visitor)
Definition: BaseBuffer.hpp:668
void compileTimeFunctionsUsage()
size_t width() const
void fillChannel(Channel< T > &channel, T value)
Access type index with type.
int main(int argc, const char **argv)
floatOptional getFloatAtomic(const std::string &name)
Gets an atomic float value.
Definition: BaseBuffer.hpp:636
Channel< U >::Optional getOptional(const std::string &name)
UCharChannel::Optional UCharChannelOptional
Definition: Channel.hpp:96
VariantChannel< T... > val_type
void basicChannelUsage()
Basic Channel Usage.
void manipulatorUsage()
iterator< U > erase(iterator< U > it)
FloatChannel::Optional FloatChannelOptional
Definition: Channel.hpp:88
void channelManagerUsage()
void multiChannelMapUsage()
Channel< U > & get(const std::string &name)
Gets AttributeChannel with type U from map as reference.
BaseBuffer clone() const
Definition: BaseBuffer.hpp:678
std::vector< std::string > keys()
Gets the available keys by a specific type.
boost::shared_array< U > dataPtr() const
void addFloatAtomic(float data, const std::string &name)
Adds an atomic float value. Exists only for compatibility reasons. Dont use atomics, they are implemented as Channels. -> memory overhead.
Definition: BaseBuffer.hpp:582
Channel< float >::Optional getFloatChannel(const std::string &name)
Gets a float channel and returns it as optional.
Definition: BaseBuffer.hpp:393
size_t numElements() const
ChannelManager class Store and access AttributeChannels. It expands the MultiChannelMap with downwoar...
Definition: BaseBuffer.hpp:54
size_t numChannels()
Counts the number of channels by a specific type. For total number of channels use "size()"...
static constexpr std::size_t value
Channel< T >::Optional getChannel(const std::string &name)
Gets a channel and returns it as optional.
char ** argv


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:08