Point Cloud Library (PCL)  1.11.1-dev
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 #pragma once
39 
40 #include <algorithm>
41 #include <iostream>
42 #include <vector>
43 
44 namespace pcl
45 {
46 namespace octree
47 {
48 /** \brief @b PointCoding class
49  * \note This class encodes 8-bit differential point information for octree-based point cloud compression.
50  * \note typename: PointT: type of point used in pointcloud
51  * \author Julius Kammerl (julius@kammerl.de)
52  */
53 template<typename PointT>
55 {
56  // public typedefs
58  using PointCloudPtr = typename PointCloud::Ptr;
59  using PointCloudConstPtr = typename PointCloud::ConstPtr;
60 
61  public:
62  /** \brief Constructor. */
64  output_ (),
65  pointCompressionResolution_ (0.001f) // 1mm
66  {
67  }
68 
69  /** \brief Empty class constructor. */
70  virtual
72  {
73  }
74 
75  /** \brief Define precision of point information
76  * \param precision_arg: precision
77  */
78  inline void
79  setPrecision (float precision_arg)
80  {
81  pointCompressionResolution_ = precision_arg;
82  }
83 
84  /** \brief Retrieve precision of point information
85  * \return precision
86  */
87  inline float
89  {
91  }
92 
93  /** \brief Set amount of points within point cloud to be encoded and reserve memory
94  * \param pointCount_arg: amounts of points within point cloud
95  */
96  inline void
97  setPointCount (unsigned int pointCount_arg)
98  {
99  pointDiffDataVector_.reserve (pointCount_arg * 3);
100  }
101 
102  /** \brief Initialize encoding of differential point */
103  void
105  {
106  pointDiffDataVector_.clear ();
107  }
108 
109  /** \brief Initialize decoding of differential point */
110  void
112  {
114  }
115 
116  /** \brief Get reference to vector containing differential color data */
117  std::vector<char>&
119  {
120  return (pointDiffDataVector_);
121  }
122 
123  /** \brief Encode differential point information for a subset of points from point cloud
124  * \param indexVector_arg indices defining a subset of points from points cloud
125  * \param referencePoint_arg coordinates of reference point
126  * \param inputCloud_arg input point cloud
127  */
128  void
129  encodePoints (const typename std::vector<int>& indexVector_arg, const double* referencePoint_arg,
130  PointCloudConstPtr inputCloud_arg)
131  {
132  std::size_t len = indexVector_arg.size ();
133 
134  // iterate over points within current voxel
135  for (std::size_t i = 0; i < len; i++)
136  {
137  unsigned char diffX, diffY, diffZ;
138 
139  // retrieve point from cloud
140  const int& idx = indexVector_arg[i];
141  const PointT& idxPoint = (*inputCloud_arg)[idx];
142 
143  // differentially encode point coordinates and truncate overflow
144  diffX = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.x - referencePoint_arg[0]) / pointCompressionResolution_))));
145  diffY = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.y - referencePoint_arg[1]) / pointCompressionResolution_))));
146  diffZ = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.z - referencePoint_arg[2]) / pointCompressionResolution_))));
147 
148  // store information in differential point vector
149  pointDiffDataVector_.push_back (diffX);
150  pointDiffDataVector_.push_back (diffY);
151  pointDiffDataVector_.push_back (diffZ);
152  }
153  }
154 
155  /** \brief Decode differential point information
156  * \param outputCloud_arg output point cloud
157  * \param referencePoint_arg coordinates of reference point
158  * \param beginIdx_arg index indicating first point to be assigned with color information
159  * \param endIdx_arg index indicating last point to be assigned with color information
160  */
161  void
162  decodePoints (PointCloudPtr outputCloud_arg, const double* referencePoint_arg, std::size_t beginIdx_arg,
163  std::size_t endIdx_arg)
164  {
165  assert (beginIdx_arg <= endIdx_arg);
166 
167  unsigned int pointCount = static_cast<unsigned int> (endIdx_arg - beginIdx_arg);
168 
169  // iterate over points within current voxel
170  for (std::size_t i = 0; i < pointCount; i++)
171  {
172  // retrieve differential point information
173  const unsigned char& diffX = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
174  const unsigned char& diffY = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
175  const unsigned char& diffZ = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
176 
177  // retrieve point from point cloud
178  PointT& point = (*outputCloud_arg)[beginIdx_arg + i];
179 
180  // decode point position
181  point.x = static_cast<float> (referencePoint_arg[0] + diffX * pointCompressionResolution_);
182  point.y = static_cast<float> (referencePoint_arg[1] + diffY * pointCompressionResolution_);
183  point.z = static_cast<float> (referencePoint_arg[2] + diffZ * pointCompressionResolution_);
184  }
185  }
186 
187  protected:
188  /** \brief Pointer to output point cloud dataset. */
189  PointCloudPtr output_;
190 
191  /** \brief Vector for storing differential point information */
192  std::vector<char> pointDiffDataVector_;
193 
194  /** \brief Iterator on differential point information vector */
195  std::vector<char>::const_iterator pointDiffDataVectorIterator_;
196 
197  /** \brief Precision of point coding*/
199 };
200 
201 } // namespace octree
202 } // namespace pcl
203 
204 #define PCL_INSTANTIATE_ColorCoding(T) template class PCL_EXPORTS pcl::octree::ColorCoding<T>;
205 
pcl
Definition: convolution.h:46
pcl::octree::PointCoding::setPointCount
void setPointCount(unsigned int pointCount_arg)
Set amount of points within point cloud to be encoded and reserve memory.
Definition: point_coding.h:97
pcl::octree::PointCoding::decodePoints
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:162
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::octree::PointCoding::encodePoints
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:129
pcl::octree::PointCoding::initializeEncoding
void initializeEncoding()
Initialize encoding of differential point.
Definition: point_coding.h:104
pcl::octree::PointCoding::pointDiffDataVector_
std::vector< char > pointDiffDataVector_
Vector for storing differential point information
Definition: point_coding.h:192
pcl::octree::PointCoding
PointCoding class
Definition: point_coding.h:54
pcl::octree::PointCoding::pointDiffDataVectorIterator_
std::vector< char >::const_iterator pointDiffDataVectorIterator_
Iterator on differential point information vector.
Definition: point_coding.h:195
pcl::octree::PointCoding::output_
PointCloudPtr output_
Pointer to output point cloud dataset.
Definition: point_coding.h:189
pcl::octree::PointCoding::initializeDecoding
void initializeDecoding()
Initialize decoding of differential point.
Definition: point_coding.h:111
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:406
pcl::octree::PointCoding::getPrecision
float getPrecision()
Retrieve precision of point information.
Definition: point_coding.h:88
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:407
pcl::octree::PointCoding::getDifferentialDataVector
std::vector< char > & getDifferentialDataVector()
Get reference to vector containing differential color data.
Definition: point_coding.h:118
pcl::octree::PointCoding::setPrecision
void setPrecision(float precision_arg)
Define precision of point information.
Definition: point_coding.h:79
pcl::octree::PointCoding::PointCoding
PointCoding()
Constructor.
Definition: point_coding.h:63
pcl::octree::PointCoding::~PointCoding
virtual ~PointCoding()
Empty class constructor.
Definition: point_coding.h:71
pcl::octree::PointCoding::pointCompressionResolution_
float pointCompressionResolution_
Precision of point coding.
Definition: point_coding.h:198