Point Cloud Library (PCL)  1.7.1
point_coding.h
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  {
53  /** \brief @b PointCoding class
54  * \note This class encodes 8-bit differential point information for octree-based point cloud compression.
55  * \note typename: PointT: type of point used in pointcloud
56  * \author Julius Kammerl (julius@kammerl.de)
57  */
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:
67  /** \brief Constructor. */
70  pointCompressionResolution_ (0.001f) // 1mm
71  {
72  }
73 
74  /** \brief Empty class constructor. */
75  virtual
77  {
78  }
79 
80  /** \brief Define precision of point information
81  * \param precision_arg: precision
82  */
83  inline void
84  setPrecision (float precision_arg)
85  {
86  pointCompressionResolution_ = precision_arg;
87  }
88 
89  /** \brief Retrieve precision of point information
90  * \return precision
91  */
92  inline float
94  {
96  }
97 
98  /** \brief Set amount of points within point cloud to be encoded and reserve memory
99  * \param pointCount_arg: amounts of points within point cloud
100  */
101  inline void
102  setPointCount (unsigned int pointCount_arg)
103  {
104  pointDiffDataVector_.reserve (pointCount_arg * 3);
105  }
106 
107  /** \brief Initialize encoding of differential point */
108  void
110  {
111  pointDiffDataVector_.clear ();
112  }
113 
114  /** \brief Initialize decoding of differential point */
115  void
117  {
119  }
120 
121  /** \brief Get reference to vector containing differential color data */
122  std::vector<char>&
124  {
125  return (pointDiffDataVector_);
126  }
127 
128  /** \brief Encode differential point information for a subset of points from point cloud
129  * \param indexVector_arg indices defining a subset of points from points cloud
130  * \param referencePoint_arg coordinates of reference point
131  * \param inputCloud_arg input point cloud
132  */
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 
162  /** \brief Decode differential point information
163  * \param outputCloud_arg output point cloud
164  * \param referencePoint_arg coordinates of reference point
165  * \param beginIdx_arg index indicating first point to be assiged with color information
166  * \param endIdx_arg index indicating last point to be assiged with color information
167  */
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:
198  /** \brief Pointer to output point cloud dataset. */
199  PointCloudPtr output_;
200 
201  /** \brief Vector for storing differential point information */
202  std::vector<char> pointDiffDataVector_;
203 
204  /** \brief Iterator on differential point information vector */
205  std::vector<char>::const_iterator pointDiffDataVectorIterator_;
206 
207  /** \brief Precision of point coding*/
209  };
210  }
211 }
212 
213 #define PCL_INSTANTIATE_ColorCoding(T) template class PCL_EXPORTS pcl::octree::ColorCoding<T>;
214 
215 #endif
std::vector< char >::const_iterator pointDiffDataVectorIterator_
Iterator on differential point information vector.
Definition: point_coding.h:205
PointCoding class
Definition: point_coding.h:59
void decodePoints(PointCloudPtr outputCloud_arg, const double *referencePoint_arg, std::size_t beginIdx_arg, std::size_t endIdx_arg)
Decode differential point information.
Definition: point_coding.h:169
PointCloudPtr output_
Pointer to output point cloud dataset.
Definition: point_coding.h:199
void setPointCount(unsigned int pointCount_arg)
Set amount of points within point cloud to be encoded and reserve memory.
Definition: point_coding.h:102
virtual ~PointCoding()
Empty class constructor.
Definition: point_coding.h:76
float pointCompressionResolution_
Precision of point coding.
Definition: point_coding.h:208
void initializeEncoding()
Initialize encoding of differential point.
Definition: point_coding.h:109
void encodePoints(const typename std::vector< int > &indexVector_arg, const double *referencePoint_arg, PointCloudConstPtr inputCloud_arg)
Encode differential point information for a subset of points from point cloud.
Definition: point_coding.h:134
PointCoding()
Constructor.
Definition: point_coding.h:68
void setPrecision(float precision_arg)
Define precision of point information.
Definition: point_coding.h:84
void initializeDecoding()
Initialize decoding of differential point.
Definition: point_coding.h:116
std::vector< char > pointDiffDataVector_
Vector for storing differential point information.
Definition: point_coding.h:202
A point structure representing Euclidean xyz coordinates, and the RGB color.
float getPrecision()
Retrieve precision of point information.
Definition: point_coding.h:93
std::vector< char > & getDifferentialDataVector()
Get reference to vector containing differential color data.
Definition: point_coding.h:123
PointCloud represents the base class in PCL for storing collections of 3D points. ...