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