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