Point Cloud Library (PCL)  1.9.1-dev
octree_pointcloud_compression.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-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 <pcl/common/common.h>
41 #include <pcl/common/io.h>
42 #include <pcl/octree/octree2buf_base.h>
43 #include <pcl/octree/octree_pointcloud.h>
44 #include "entropy_range_coder.h"
45 #include "color_coding.h"
46 #include "point_coding.h"
47 
48 #include "compression_profiles.h"
49 
50 #include <cstdio>
51 #include <cstring>
52 #include <iostream>
53 #include <iterator>
54 #include <vector>
55 
56 using namespace pcl::octree;
57 
58 namespace pcl
59 {
60  namespace io
61  {
62  /** \brief @b Octree pointcloud compression class
63  * \note This class enables compression and decompression of point cloud data based on octree data structures.
64  * \note
65  * \note typename: PointT: type of point used in pointcloud
66  * \author Julius Kammerl (julius@kammerl.de)
67  */
68  template<typename PointT, typename LeafT = OctreeContainerPointIndices,
69  typename BranchT = OctreeContainerEmpty,
70  typename OctreeT = Octree2BufBase<LeafT, BranchT> >
71  class OctreePointCloudCompression : public OctreePointCloud<PointT, LeafT,
72  BranchT, OctreeT>
73  {
74  public:
75  // public typedefs
79 
80  // Boost shared pointers
81  using Ptr = boost::shared_ptr<OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> >;
82  using ConstPtr = boost::shared_ptr<const OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> >;
83 
84  using LeafNode = typename OctreeT::LeafNode;
85  using BranchNode = typename OctreeT::BranchNode;
86 
89 
90 
91  /** \brief Constructor
92  * \param compressionProfile_arg: define compression profile
93  * \param octreeResolution_arg: octree resolution at lowest octree level
94  * \param pointResolution_arg: precision of point coordinates
95  * \param doVoxelGridDownDownSampling_arg: voxel grid filtering
96  * \param iFrameRate_arg: i-frame encoding rate
97  * \param doColorEncoding_arg: enable/disable color coding
98  * \param colorBitResolution_arg: color bit depth
99  * \param showStatistics_arg: output compression statistics
100  */
101  OctreePointCloudCompression (compression_Profiles_e compressionProfile_arg = MED_RES_ONLINE_COMPRESSION_WITH_COLOR,
102  bool showStatistics_arg = false,
103  const double pointResolution_arg = 0.001,
104  const double octreeResolution_arg = 0.01,
105  bool doVoxelGridDownDownSampling_arg = false,
106  const unsigned int iFrameRate_arg = 30,
107  bool doColorEncoding_arg = true,
108  const unsigned char colorBitResolution_arg = 6) :
109  OctreePointCloud<PointT, LeafT, BranchT, OctreeT> (octreeResolution_arg),
110  output_ (PointCloudPtr ()),
111  color_coder_ (),
112  point_coder_ (),
113  do_voxel_grid_enDecoding_ (doVoxelGridDownDownSampling_arg), i_frame_rate_ (iFrameRate_arg),
114  i_frame_counter_ (0), frame_ID_ (0), point_count_ (0), i_frame_ (true),
115  do_color_encoding_ (doColorEncoding_arg), cloud_with_color_ (false), data_with_color_ (false),
116  point_color_offset_ (0), b_show_statistics_ (showStatistics_arg),
117  compressed_point_data_len_ (), compressed_color_data_len_ (), selected_profile_(compressionProfile_arg),
118  point_resolution_(pointResolution_arg), octree_resolution_(octreeResolution_arg),
119  color_bit_resolution_(colorBitResolution_arg),
120  object_count_(0)
121  {
122  initialization();
123  }
124 
125  /** \brief Empty deconstructor. */
126 
128  {
129  }
130 
131  /** \brief Initialize globals */
132  void initialization () {
133  if (selected_profile_ != MANUAL_CONFIGURATION)
134  {
135  // apply selected compression profile
136 
137  // retrieve profile settings
138  const configurationProfile_t selectedProfile = compressionProfiles_[selected_profile_];
139 
140  // apply profile settings
141  i_frame_rate_ = selectedProfile.iFrameRate;
142  do_voxel_grid_enDecoding_ = selectedProfile.doVoxelGridDownSampling;
143  this->setResolution (selectedProfile.octreeResolution);
144  point_coder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
145  do_color_encoding_ = selectedProfile.doColorEncoding;
146  color_coder_.setBitDepth (selectedProfile.colorBitResolution);
147 
148  }
149  else
150  {
151  // configure point & color coder
152  point_coder_.setPrecision (static_cast<float> (point_resolution_));
153  color_coder_.setBitDepth (color_bit_resolution_);
154  }
155 
156  if (point_coder_.getPrecision () == this->getResolution ())
157  //disable differential point colding
158  do_voxel_grid_enDecoding_ = true;
159 
160  }
161 
162  /** \brief Add point at index from input pointcloud dataset to octree
163  * \param[in] pointIdx_arg the index representing the point in the dataset given by \a setInputCloud to be added
164  */
165  void
166  addPointIdx (const int pointIdx_arg) override
167  {
168  ++object_count_;
170  }
171 
172  /** \brief Provide a pointer to the output data set.
173  * \param cloud_arg: the boost shared pointer to a PointCloud message
174  */
175  inline void
176  setOutputCloud (const PointCloudPtr &cloud_arg)
177  {
178  if (output_ != cloud_arg)
179  {
180  output_ = cloud_arg;
181  }
182  }
183 
184  /** \brief Get a pointer to the output point cloud dataset.
185  * \return pointer to pointcloud output class.
186  */
187  inline PointCloudPtr
188  getOutputCloud () const
189  {
190  return (output_);
191  }
192 
193  /** \brief Encode point cloud to output stream
194  * \param cloud_arg: point cloud to be compressed
195  * \param compressed_tree_data_out_arg: binary output stream containing compressed data
196  */
197  void
198  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressed_tree_data_out_arg);
199 
200  /** \brief Decode point cloud from input stream
201  * \param compressed_tree_data_in_arg: binary input stream containing compressed data
202  * \param cloud_arg: reference to decoded point cloud
203  */
204  void
205  decodePointCloud (std::istream& compressed_tree_data_in_arg, PointCloudPtr &cloud_arg);
206 
207  protected:
208 
209  /** \brief Write frame information to output stream
210  * \param compressed_tree_data_out_arg: binary output stream
211  */
212  void
213  writeFrameHeader (std::ostream& compressed_tree_data_out_arg);
214 
215  /** \brief Read frame information to output stream
216  * \param compressed_tree_data_in_arg: binary input stream
217  */
218  void
219  readFrameHeader (std::istream& compressed_tree_data_in_arg);
220 
221  /** \brief Synchronize to frame header
222  * \param compressed_tree_data_in_arg: binary input stream
223  */
224  void
225  syncToHeader (std::istream& compressed_tree_data_in_arg);
226 
227  /** \brief Apply entropy encoding to encoded information and output to binary stream
228  * \param compressed_tree_data_out_arg: binary output stream
229  */
230  void
231  entropyEncoding (std::ostream& compressed_tree_data_out_arg);
232 
233  /** \brief Entropy decoding of input binary stream and output to information vectors
234  * \param compressed_tree_data_in_arg: binary input stream
235  */
236  void
237  entropyDecoding (std::istream& compressed_tree_data_in_arg);
238 
239  /** \brief Encode leaf node information during serialization
240  * \param leaf_arg: reference to new leaf node
241  * \param key_arg: octree key of new leaf node
242  */
243  void
244  serializeTreeCallback (LeafT &leaf_arg, const OctreeKey& key_arg) override;
245 
246  /** \brief Decode leaf nodes information during deserialization
247  * \param key_arg octree key of new leaf node
248  */
249  // param leaf_arg reference to new leaf node
250  void
251  deserializeTreeCallback (LeafT&, const OctreeKey& key_arg) override;
252 
253 
254  /** \brief Pointer to output point cloud dataset. */
256 
257  /** \brief Vector for storing binary tree structure */
258  std::vector<char> binary_tree_data_vector_;
259 
260  /** \brief Iterator on binary tree structure vector */
261  std::vector<char> binary_color_tree_vector_;
262 
263  /** \brief Vector for storing points per voxel information */
264  std::vector<unsigned int> point_count_data_vector_;
265 
266  /** \brief Iterator on points per voxel vector */
267  std::vector<unsigned int>::const_iterator point_count_data_vector_iterator_;
268 
269  /** \brief Color coding instance */
271 
272  /** \brief Point coding instance */
274 
275  /** \brief Static range coder instance */
277 
279  std::uint32_t i_frame_rate_;
280  std::uint32_t i_frame_counter_;
281  std::uint32_t frame_ID_;
282  std::uint64_t point_count_;
283  bool i_frame_;
284 
288  unsigned char point_color_offset_;
289 
290  //bool activating statistics
294 
295  // frame header identifier
296  static const char* frame_header_identifier_;
297 
299  const double point_resolution_;
300  const double octree_resolution_;
301  const unsigned char color_bit_resolution_;
302 
303  std::size_t object_count_;
304 
305  };
306 
307  // define frame identifier
308  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
310  }
311 
312 }
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloud PointCloud
Octree pointcloud class
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudPtr PointCloudPtr
ColorCoding< PointT > color_coder_
Color coding instance.
Octree pointcloud compression class
PointCloudPtr getOutputCloud() const
Get a pointer to the output point cloud dataset.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
virtual void addPointIdx(const int point_idx_arg)
Add point at index from input pointcloud dataset to octree.
PointCoding class
Definition: point_coding.h:56
boost::shared_ptr< OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > Ptr
Define standard C methods and C++ classes that are common to all methods.
ColorCoding class
Definition: color_coding.h:61
PointCoding< PointT > point_coder_
Point coding instance.
Octree container class that does store a vector of point indices.
std::vector< char > binary_color_tree_vector_
Iterator on binary tree structure vector.
PointCloudPtr output_
Pointer to output point cloud dataset.
Octree key class
Definition: octree_key.h:50
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudConstPtr PointCloudConstPtr
StaticRangeCoder compression class
std::vector< unsigned int >::const_iterator point_count_data_vector_iterator_
Iterator on points per voxel vector.
Octree double buffer class
const struct configurationProfile_t compressionProfiles_[COMPRESSION_PROFILE_COUNT]
A point structure representing Euclidean xyz coordinates, and the RGB color.
void addPointIdx(const int pointIdx_arg) override
Add point at index from input pointcloud dataset to octree.
std::vector< unsigned int > point_count_data_vector_
Vector for storing points per voxel information.
Octree container class that does not store any information.
OctreePointCloudCompression(compression_Profiles_e compressionProfile_arg=MED_RES_ONLINE_COMPRESSION_WITH_COLOR, bool showStatistics_arg=false, const double pointResolution_arg=0.001, const double octreeResolution_arg=0.01, bool doVoxelGridDownDownSampling_arg=false, const unsigned int iFrameRate_arg=30, bool doColorEncoding_arg=true, const unsigned char colorBitResolution_arg=6)
Constructor.
StaticRangeCoder entropy_coder_
Static range coder instance.
boost::shared_ptr< const OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > ConstPtr
std::vector< char > binary_tree_data_vector_
Vector for storing binary tree structure.
void setOutputCloud(const PointCloudPtr &cloud_arg)
Provide a pointer to the output data set.