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