Point Cloud Library (PCL)  1.14.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. */
63  PointCoding () = default;
64 
65  /** \brief Empty class constructor. */
66  virtual
67  ~PointCoding () = default;
68 
69  /** \brief Define precision of point information
70  * \param precision_arg: precision
71  */
72  inline void
73  setPrecision (float precision_arg)
74  {
75  pointCompressionResolution_ = precision_arg;
76  }
77 
78  /** \brief Retrieve precision of point information
79  * \return precision
80  */
81  inline float
83  {
85  }
86 
87  /** \brief Set amount of points within point cloud to be encoded and reserve memory
88  * \param pointCount_arg: amounts of points within point cloud
89  */
90  inline void
91  setPointCount (unsigned int pointCount_arg)
92  {
93  pointDiffDataVector_.reserve (pointCount_arg * 3);
94  }
95 
96  /** \brief Initialize encoding of differential point */
97  void
99  {
100  pointDiffDataVector_.clear ();
101  }
102 
103  /** \brief Initialize decoding of differential point */
104  void
106  {
108  }
109 
110  /** \brief Get reference to vector containing differential color data */
111  std::vector<char>&
113  {
114  return (pointDiffDataVector_);
115  }
116 
117  /** \brief Encode differential point information for a subset of points from point cloud
118  * \param indexVector_arg indices defining a subset of points from points cloud
119  * \param referencePoint_arg coordinates of reference point
120  * \param inputCloud_arg input point cloud
121  */
122  void
123  encodePoints (const Indices& indexVector_arg, const double* referencePoint_arg,
124  PointCloudConstPtr inputCloud_arg)
125  {
126  // iterate over points within current voxel
127  for (const auto& idx: indexVector_arg)
128  {
129  unsigned char diffX, diffY, diffZ;
130 
131  // retrieve point from cloud
132  const PointT& idxPoint = (*inputCloud_arg)[idx];
133 
134  // differentially encode point coordinates and truncate overflow
135  diffX = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.x - referencePoint_arg[0]) / pointCompressionResolution_))));
136  diffY = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.y - referencePoint_arg[1]) / pointCompressionResolution_))));
137  diffZ = static_cast<unsigned char> (std::max (-127, std::min<int>(127, static_cast<int> ((idxPoint.z - referencePoint_arg[2]) / pointCompressionResolution_))));
138 
139  // store information in differential point vector
140  pointDiffDataVector_.push_back (diffX);
141  pointDiffDataVector_.push_back (diffY);
142  pointDiffDataVector_.push_back (diffZ);
143  }
144  }
145 
146  /** \brief Decode differential point information
147  * \param outputCloud_arg output point cloud
148  * \param referencePoint_arg coordinates of reference point
149  * \param beginIdx_arg index indicating first point to be assigned with color information
150  * \param endIdx_arg index indicating last point to be assigned with color information
151  */
152  void
153  decodePoints (PointCloudPtr outputCloud_arg, const double* referencePoint_arg, uindex_t beginIdx_arg,
154  uindex_t endIdx_arg)
155  {
156  assert (beginIdx_arg <= endIdx_arg);
157 
158  const uindex_t pointCount = endIdx_arg - beginIdx_arg;
159 
160  // iterate over points within current voxel
161  for (uindex_t i = 0; i < pointCount; i++)
162  {
163  // retrieve differential point information
164  const unsigned char& diffX = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
165  const unsigned char& diffY = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
166  const unsigned char& diffZ = static_cast<unsigned char> (*(pointDiffDataVectorIterator_++));
167 
168  // retrieve point from point cloud
169  PointT& point = (*outputCloud_arg)[beginIdx_arg + i];
170 
171  // decode point position
172  point.x = static_cast<float> (referencePoint_arg[0] + diffX * pointCompressionResolution_);
173  point.y = static_cast<float> (referencePoint_arg[1] + diffY * pointCompressionResolution_);
174  point.z = static_cast<float> (referencePoint_arg[2] + diffZ * pointCompressionResolution_);
175  }
176  }
177 
178  protected:
179  /** \brief Pointer to output point cloud dataset. */
180  PointCloudPtr output_{nullptr};
181 
182  /** \brief Vector for storing differential point information */
183  std::vector<char> pointDiffDataVector_;
184 
185  /** \brief Iterator on differential point information vector */
186  std::vector<char>::const_iterator pointDiffDataVectorIterator_;
187 
188  /** \brief Precision of point coding*/
190 };
191 
192 } // namespace octree
193 } // namespace pcl
194 
195 #define PCL_INSTANTIATE_ColorCoding(T) template class PCL_EXPORTS pcl::octree::ColorCoding<T>;
196 
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
PointCoding class
Definition: point_coding.h:55
void encodePoints(const Indices &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:123
void initializeEncoding()
Initialize encoding of differential point.
Definition: point_coding.h:98
std::vector< char > pointDiffDataVector_
Vector for storing differential point information
Definition: point_coding.h:183
float pointCompressionResolution_
Precision of point coding.
Definition: point_coding.h:189
std::vector< char > & getDifferentialDataVector()
Get reference to vector containing differential color data.
Definition: point_coding.h:112
void initializeDecoding()
Initialize decoding of differential point.
Definition: point_coding.h:105
void decodePoints(PointCloudPtr outputCloud_arg, const double *referencePoint_arg, uindex_t beginIdx_arg, uindex_t endIdx_arg)
Decode differential point information.
Definition: point_coding.h:153
float getPrecision()
Retrieve precision of point information.
Definition: point_coding.h:82
PointCoding()=default
Constructor.
virtual ~PointCoding()=default
Empty class constructor.
void setPointCount(unsigned int pointCount_arg)
Set amount of points within point cloud to be encoded and reserve memory.
Definition: point_coding.h:91
std::vector< char >::const_iterator pointDiffDataVectorIterator_
Iterator on differential point information vector.
Definition: point_coding.h:186
PointCloudPtr output_
Pointer to output point cloud dataset.
Definition: point_coding.h:180
void setPrecision(float precision_arg)
Define precision of point information.
Definition: point_coding.h:73
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.
Definition: types.h:120
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
A point structure representing Euclidean xyz coordinates, and the RGB color.