Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
color_coding.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef COLOR_COMPRESSION_H
39 #define COLOR_COMPRESSION_H
40 
41 #include <iterator>
42 #include <iostream>
43 #include <vector>
44 #include <string.h>
45 #include <iostream>
46 #include <stdio.h>
47 #include <string.h>
48 
49 namespace pcl
50 {
51  namespace octree
52  {
53  using namespace std;
54 
56 
62 
63  template<typename PointT>
65  {
66 
67  // public typedefs
69  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
70  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
71 
72  public:
73 
78  output_ (), pointAvgColorDataVector_ (), pointAvgColorDataVector_Iterator_ (),
79  pointDiffColorDataVector_ (), pointDiffColorDataVector_Iterator_ (), colorBitReduction_ (0)
80  {
81  }
82 
84  virtual
86  {
87  }
88 
92  inline
93  void
94  setBitDepth (unsigned char bitDepth_arg)
95  {
96  assert (bitDepth_arg <= bitDepth_arg);
97  colorBitReduction_ = static_cast<unsigned char> (8 - bitDepth_arg);
98  }
99 
103  inline unsigned char
104  getBitDepth ()
105  {
106  return (static_cast<unsigned char> (8 - colorBitReduction_));
107  }
108 
112  inline void
113  setVoxelCount (unsigned int voxelCount_arg)
114  {
115  pointAvgColorDataVector_.reserve (voxelCount_arg * 3);
116  }
117 
121  inline
122  void
123  setPointCount (unsigned int pointCount_arg)
124  {
125  pointDiffColorDataVector_.reserve (pointCount_arg * 3);
126  }
127 
130  void
131  initializeEncoding ()
132  {
133  pointAvgColorDataVector_.clear ();
134 
135  pointDiffColorDataVector_.clear ();
136  }
137 
140  void
141  initializeDecoding ()
142  {
143  pointAvgColorDataVector_Iterator_ = pointAvgColorDataVector_.begin ();
144 
145  pointDiffColorDataVector_Iterator_ = pointDiffColorDataVector_.begin ();
146  }
147 
150  std::vector<char>&
151  getAverageDataVector ()
152  {
153  return pointAvgColorDataVector_;
154  }
155 
158  std::vector<char>&
159  getDifferentialDataVector ()
160  {
161  return pointDiffColorDataVector_;
162  }
163 
169  void
170  encodeAverageOfPoints (const typename std::vector<int>& indexVector_arg, unsigned char rgba_offset_arg, PointCloudConstPtr inputCloud_arg)
171  {
172  std::size_t i, len;
173 
174  unsigned int avgRed;
175  unsigned int avgGreen;
176  unsigned int avgBlue;
177 
178  // initialize
179  avgRed = avgGreen = avgBlue = 0;
180 
181  // iterate over points
182  len = indexVector_arg.size ();
183  for (i = 0; i < len; i++)
184  {
185  // get color information from points
186  const int& idx = indexVector_arg[i];
187  const char* idxPointPtr = reinterpret_cast<const char*> (&inputCloud_arg->points[idx]);
188  const int& colorInt = *reinterpret_cast<const int*> (idxPointPtr+rgba_offset_arg);
189 
190  // add color information
191  avgRed += (colorInt >> 0) & 0xFF;
192  avgGreen += (colorInt >> 8) & 0xFF;
193  avgBlue += (colorInt >> 16) & 0xFF;
194 
195  }
196 
197  // calculated average color information
198  if (len > 1)
199  {
200  avgRed /= static_cast<unsigned int> (len);
201  avgGreen /= static_cast<unsigned int> (len);
202  avgBlue /= static_cast<unsigned int> (len);
203  }
204 
205  // remove least significant bits
206  avgRed >>= colorBitReduction_;
207  avgGreen >>= colorBitReduction_;
208  avgBlue >>= colorBitReduction_;
209 
210  // add to average color vector
211  pointAvgColorDataVector_.push_back (static_cast<char> (avgRed));
212  pointAvgColorDataVector_.push_back (static_cast<char> (avgGreen));
213  pointAvgColorDataVector_.push_back (static_cast<char> (avgBlue));
214  }
215 
221  void
222  encodePoints (const typename std::vector<int>& indexVector_arg, unsigned char rgba_offset_arg, PointCloudConstPtr inputCloud_arg)
223  {
224  std::size_t i, len;
225 
226  unsigned int avgRed;
227  unsigned int avgGreen;
228  unsigned int avgBlue;
229 
230  // initialize
231  avgRed = avgGreen = avgBlue = 0;
232 
233  // iterate over points
234  len = indexVector_arg.size ();
235  for (i = 0; i < len; i++)
236  {
237  // get color information from point
238  const int& idx = indexVector_arg[i];
239  const char* idxPointPtr = reinterpret_cast<const char*> (&inputCloud_arg->points[idx]);
240  const int& colorInt = *reinterpret_cast<const int*> (idxPointPtr+rgba_offset_arg);
241 
242  // add color information
243  avgRed += (colorInt >> 0) & 0xFF;
244  avgGreen += (colorInt >> 8) & 0xFF;
245  avgBlue += (colorInt >> 16) & 0xFF;
246 
247  }
248 
249  if (len > 1)
250  {
251  unsigned char diffRed;
252  unsigned char diffGreen;
253  unsigned char diffBlue;
254 
255  // calculated average color information
256  avgRed /= static_cast<unsigned int> (len);
257  avgGreen /= static_cast<unsigned int> (len);
258  avgBlue /= static_cast<unsigned int> (len);
259 
260  // iterate over points for differential encoding
261  for (i = 0; i < len; i++)
262  {
263  const int& idx = indexVector_arg[i];
264  const char* idxPointPtr = reinterpret_cast<const char*> (&inputCloud_arg->points[idx]);
265  const int& colorInt = *reinterpret_cast<const int*> (idxPointPtr+rgba_offset_arg);
266 
267  // extract color components and do XOR encoding with predicted average color
268  diffRed = (static_cast<unsigned char> (avgRed)) ^ static_cast<unsigned char> (((colorInt >> 0) & 0xFF));
269  diffGreen = (static_cast<unsigned char> (avgGreen)) ^ static_cast<unsigned char> (((colorInt >> 8) & 0xFF));
270  diffBlue = (static_cast<unsigned char> (avgBlue)) ^ static_cast<unsigned char> (((colorInt >> 16) & 0xFF));
271 
272  // remove least significant bits
273  diffRed = static_cast<unsigned char> (diffRed >> colorBitReduction_);
274  diffGreen = static_cast<unsigned char> (diffGreen >> colorBitReduction_);
275  diffBlue = static_cast<unsigned char> (diffBlue >> colorBitReduction_);
276 
277  // add to differential color vector
278  pointDiffColorDataVector_.push_back (static_cast<char> (diffRed));
279  pointDiffColorDataVector_.push_back (static_cast<char> (diffGreen));
280  pointDiffColorDataVector_.push_back (static_cast<char> (diffBlue));
281  }
282  }
283 
284  // remove least significant bits from average color information
285  avgRed >>= colorBitReduction_;
286  avgGreen >>= colorBitReduction_;
287  avgBlue >>= colorBitReduction_;
288 
289  // add to differential color vector
290  pointAvgColorDataVector_.push_back (static_cast<char> (avgRed));
291  pointAvgColorDataVector_.push_back (static_cast<char> (avgGreen));
292  pointAvgColorDataVector_.push_back (static_cast<char> (avgBlue));
293 
294  }
295 
302  void
303  decodePoints (PointCloudPtr outputCloud_arg, std::size_t beginIdx_arg, std::size_t endIdx_arg, unsigned char rgba_offset_arg)
304  {
305  std::size_t i;
306  unsigned int pointCount;
307  unsigned int colorInt;
308 
309  assert (beginIdx_arg <= endIdx_arg);
310 
311  // amount of points to be decoded
312  pointCount = static_cast<unsigned int> (endIdx_arg - beginIdx_arg);
313 
314  // get averaged color information for current voxel
315  unsigned char avgRed = *(pointAvgColorDataVector_Iterator_++);
316  unsigned char avgGreen = *(pointAvgColorDataVector_Iterator_++);
317  unsigned char avgBlue = *(pointAvgColorDataVector_Iterator_++);
318 
319  // invert bit shifts during encoding
320  avgRed = static_cast<unsigned char> (avgRed << colorBitReduction_);
321  avgGreen = static_cast<unsigned char> (avgGreen << colorBitReduction_);
322  avgBlue = static_cast<unsigned char> (avgBlue << colorBitReduction_);
323 
324  // iterate over points
325  for (i = 0; i < pointCount; i++)
326  {
327  if (pointCount > 1)
328  {
329  // get differential color information from input vector
330  unsigned char diffRed = static_cast<unsigned char> (*(pointDiffColorDataVector_Iterator_++));
331  unsigned char diffGreen = static_cast<unsigned char> (*(pointDiffColorDataVector_Iterator_++));
332  unsigned char diffBlue = static_cast<unsigned char> (*(pointDiffColorDataVector_Iterator_++));
333 
334  // invert bit shifts during encoding
335  diffRed = static_cast<unsigned char> (diffRed << colorBitReduction_);
336  diffGreen = static_cast<unsigned char> (diffGreen << colorBitReduction_);
337  diffBlue = static_cast<unsigned char> (diffBlue << colorBitReduction_);
338 
339  // decode color information
340  colorInt = ((avgRed ^ diffRed) << 0) |
341  ((avgGreen ^ diffGreen) << 8) |
342  ((avgBlue ^ diffBlue) << 16);
343  }
344  else
345  {
346  // decode color information
347  colorInt = (avgRed << 0) | (avgGreen << 8) | (avgBlue << 16);
348  }
349 
350  char* idxPointPtr = reinterpret_cast<char*> (&outputCloud_arg->points[beginIdx_arg + i]);
351  int& pointColor = *reinterpret_cast<int*> (idxPointPtr+rgba_offset_arg);
352  // assign color to point from point cloud
353  pointColor=colorInt;
354  }
355  }
356 
363  void
364  setDefaultColor (PointCloudPtr outputCloud_arg, std::size_t beginIdx_arg, std::size_t endIdx_arg, unsigned char rgba_offset_arg)
365  {
366  std::size_t i;
367  unsigned int pointCount;
368 
369  assert (beginIdx_arg <= endIdx_arg);
370 
371  // amount of points to be decoded
372  pointCount = static_cast<unsigned int> (endIdx_arg - beginIdx_arg);
373 
374  // iterate over points
375  for (i = 0; i < pointCount; i++)
376  {
377  char* idxPointPtr = reinterpret_cast<char*> (&outputCloud_arg->points[beginIdx_arg + i]);
378  int& pointColor = *reinterpret_cast<int*> (idxPointPtr+rgba_offset_arg);
379  // assign color to point from point cloud
380  pointColor = defaultColor_;
381  }
382  }
383 
384 
385  protected:
386 
388  PointCloudPtr output_;
389 
391  std::vector<char> pointAvgColorDataVector_;
392 
394  std::vector<char>::const_iterator pointAvgColorDataVector_Iterator_;
395 
397  std::vector<char> pointDiffColorDataVector_;
398 
400  std::vector<char>::const_iterator pointDiffColorDataVector_Iterator_;
401 
403  unsigned char colorBitReduction_;
404 
405  // frame header identifier
406  static const int defaultColor_;
407 
408  };
409 
410  // define default color
411  template<typename PointT>
412  const int ColorCoding<PointT>::defaultColor_ = ((255) << 0) |
413  ((255) << 8) |
414  ((255) << 16);
415 
416  }
417 }
418 
419 #define PCL_INSTANTIATE_ColorCoding(T) template class PCL_EXPORTS pcl::octree::ColorCoding<T>;
420 
421 #endif