Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
point_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) 2011-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 POINT_COMPRESSION_H
39 #define POINT_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  {
58  template<typename PointT>
60  {
61  // public typedefs
63  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
64  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
65 
66  public:
69  output_ (), pointDiffDataVector_ (), pointDiffDataVectorIterator_ (),
70  pointCompressionResolution_ (0.001f) // 1mm
71  {
72  }
73 
75  virtual
77  {
78  }
79 
83  inline void
84  setPrecision (float precision_arg)
85  {
86  pointCompressionResolution_ = precision_arg;
87  }
88 
92  inline float
94  {
95  return (pointCompressionResolution_);
96  }
97 
101  inline void
102  setPointCount (unsigned int pointCount_arg)
103  {
104  pointDiffDataVector_.reserve (pointCount_arg * 3);
105  }
106 
108  void
110  {
111  pointDiffDataVector_.clear ();
112  }
113 
115  void
117  {
118  pointDiffDataVectorIterator_ = pointDiffDataVector_.begin ();
119  }
120 
122  std::vector<char>&
124  {
125  return (pointDiffDataVector_);
126  }
127 
133  void
134  encodePoints (const typename std::vector<int>& indexVector_arg, const double* referencePoint_arg,
135  PointCloudConstPtr inputCloud_arg)
136  {
137  std::size_t i, len;
138 
139  len = indexVector_arg.size ();
140 
141  // iterate over points within current voxel
142  for (i = 0; i < len; i++)
143  {
144  unsigned char diffX, diffY, diffZ;
145 
146  // retrieve point from cloud
147  const int& idx = indexVector_arg[i];
148  const PointT& idxPoint = inputCloud_arg->points[idx];
149 
150  // differentially encode point coordinates and truncate overflow
151  diffX = static_cast<unsigned char> (max (-127, min<int>(127, static_cast<int> ((idxPoint.x - referencePoint_arg[0]) / pointCompressionResolution_))));
152  diffY = static_cast<unsigned char> (max (-127, min<int>(127, static_cast<int> ((idxPoint.y - referencePoint_arg[1]) / pointCompressionResolution_))));
153  diffZ = static_cast<unsigned char> (max (-127, min<int>(127, static_cast<int> ((idxPoint.z - referencePoint_arg[2]) / pointCompressionResolution_))));
154 
155  // store information in differential point vector
156  pointDiffDataVector_.push_back (diffX);
157  pointDiffDataVector_.push_back (diffY);
158  pointDiffDataVector_.push_back (diffZ);
159  }
160  }
161 
168  void
169  decodePoints (PointCloudPtr outputCloud_arg, const double* referencePoint_arg, std::size_t beginIdx_arg,
170  std::size_t endIdx_arg)
171  {
172  std::size_t i;
173  unsigned int pointCount;
174 
175  assert (beginIdx_arg <= endIdx_arg);
176 
177  pointCount = static_cast<unsigned int> (endIdx_arg - beginIdx_arg);
178 
179  // iterate over points within current voxel
180  for (i = 0; i < pointCount; i++)
181  {
182  // retrieve differential point information
183  const unsigned char& diffX = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
184  const unsigned char& diffY = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
185  const unsigned char& diffZ = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
186 
187  // retrieve point from point cloud
188  PointT& point = outputCloud_arg->points[beginIdx_arg + i];
189 
190  // decode point position
191  point.x = static_cast<float> (referencePoint_arg[0] + diffX * pointCompressionResolution_);
192  point.y = static_cast<float> (referencePoint_arg[1] + diffY * pointCompressionResolution_);
193  point.z = static_cast<float> (referencePoint_arg[2] + diffZ * pointCompressionResolution_);
194  }
195  }
196 
197  protected:
199  PointCloudPtr output_;
200 
202  std::vector<char> pointDiffDataVector_;
203 
205  std::vector<char>::const_iterator pointDiffDataVectorIterator_;
206 
208  float pointCompressionResolution_;
209  };
210  }
211 }
212 
213 #define PCL_INSTANTIATE_ColorCoding(T) template class PCL_EXPORTS pcl::octree::ColorCoding<T>;
214 
215 #endif