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  typedef boost::shared_ptr<OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> > Ptr;
82  typedef boost::shared_ptr<const OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> > ConstPtr;
83 
84  typedef typename OctreeT::LeafNode LeafNode;
85  typedef typename OctreeT::BranchNode 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  binary_tree_data_vector_ (),
112  binary_color_tree_vector_ (),
113  point_count_data_vector_ (),
114  point_count_data_vector_iterator_ (),
115  color_coder_ (),
116  point_coder_ (),
117  entropy_coder_ (),
118  do_voxel_grid_enDecoding_ (doVoxelGridDownDownSampling_arg), i_frame_rate_ (iFrameRate_arg),
119  i_frame_counter_ (0), frame_ID_ (0), point_count_ (0), i_frame_ (true),
120  do_color_encoding_ (doColorEncoding_arg), cloud_with_color_ (false), data_with_color_ (false),
121  point_color_offset_ (0), b_show_statistics_ (showStatistics_arg),
122  compressed_point_data_len_ (), compressed_color_data_len_ (), selected_profile_(compressionProfile_arg),
123  point_resolution_(pointResolution_arg), octree_resolution_(octreeResolution_arg),
124  color_bit_resolution_(colorBitResolution_arg),
125  object_count_(0)
126  {
127  initialization();
128  }
129 
130  /** \brief Empty deconstructor. */
131 
133  {
134  }
135 
136  /** \brief Initialize globals */
137  void initialization () {
138  if (selected_profile_ != MANUAL_CONFIGURATION)
139  {
140  // apply selected compression profile
141 
142  // retrieve profile settings
143  const configurationProfile_t selectedProfile = compressionProfiles_[selected_profile_];
144 
145  // apply profile settings
146  i_frame_rate_ = selectedProfile.iFrameRate;
147  do_voxel_grid_enDecoding_ = selectedProfile.doVoxelGridDownSampling;
148  this->setResolution (selectedProfile.octreeResolution);
149  point_coder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
150  do_color_encoding_ = selectedProfile.doColorEncoding;
151  color_coder_.setBitDepth (selectedProfile.colorBitResolution);
152 
153  }
154  else
155  {
156  // configure point & color coder
157  point_coder_.setPrecision (static_cast<float> (point_resolution_));
158  color_coder_.setBitDepth (color_bit_resolution_);
159  }
160 
161  if (point_coder_.getPrecision () == this->getResolution ())
162  //disable differential point colding
163  do_voxel_grid_enDecoding_ = true;
164 
165  }
166 
167  /** \brief Add point at index from input pointcloud dataset to octree
168  * \param[in] pointIdx_arg the index representing the point in the dataset given by \a setInputCloud to be added
169  */
170  void
171  addPointIdx (const int pointIdx_arg) override
172  {
173  ++object_count_;
175  }
176 
177  /** \brief Provide a pointer to the output data set.
178  * \param cloud_arg: the boost shared pointer to a PointCloud message
179  */
180  inline void
181  setOutputCloud (const PointCloudPtr &cloud_arg)
182  {
183  if (output_ != cloud_arg)
184  {
185  output_ = cloud_arg;
186  }
187  }
188 
189  /** \brief Get a pointer to the output point cloud dataset.
190  * \return pointer to pointcloud output class.
191  */
192  inline PointCloudPtr
193  getOutputCloud () const
194  {
195  return (output_);
196  }
197 
198  /** \brief Encode point cloud to output stream
199  * \param cloud_arg: point cloud to be compressed
200  * \param compressed_tree_data_out_arg: binary output stream containing compressed data
201  */
202  void
203  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressed_tree_data_out_arg);
204 
205  /** \brief Decode point cloud from input stream
206  * \param compressed_tree_data_in_arg: binary input stream containing compressed data
207  * \param cloud_arg: reference to decoded point cloud
208  */
209  void
210  decodePointCloud (std::istream& compressed_tree_data_in_arg, PointCloudPtr &cloud_arg);
211 
212  protected:
213 
214  /** \brief Write frame information to output stream
215  * \param compressed_tree_data_out_arg: binary output stream
216  */
217  void
218  writeFrameHeader (std::ostream& compressed_tree_data_out_arg);
219 
220  /** \brief Read frame information to output stream
221  * \param compressed_tree_data_in_arg: binary input stream
222  */
223  void
224  readFrameHeader (std::istream& compressed_tree_data_in_arg);
225 
226  /** \brief Synchronize to frame header
227  * \param compressed_tree_data_in_arg: binary input stream
228  */
229  void
230  syncToHeader (std::istream& compressed_tree_data_in_arg);
231 
232  /** \brief Apply entropy encoding to encoded information and output to binary stream
233  * \param compressed_tree_data_out_arg: binary output stream
234  */
235  void
236  entropyEncoding (std::ostream& compressed_tree_data_out_arg);
237 
238  /** \brief Entropy decoding of input binary stream and output to information vectors
239  * \param compressed_tree_data_in_arg: binary input stream
240  */
241  void
242  entropyDecoding (std::istream& compressed_tree_data_in_arg);
243 
244  /** \brief Encode leaf node information during serialization
245  * \param leaf_arg: reference to new leaf node
246  * \param key_arg: octree key of new leaf node
247  */
248  void
249  serializeTreeCallback (LeafT &leaf_arg, const OctreeKey& key_arg) override;
250 
251  /** \brief Decode leaf nodes information during deserialization
252  * \param key_arg octree key of new leaf node
253  */
254  // param leaf_arg reference to new leaf node
255  void
256  deserializeTreeCallback (LeafT&, const OctreeKey& key_arg) override;
257 
258 
259  /** \brief Pointer to output point cloud dataset. */
260  PointCloudPtr output_;
261 
262  /** \brief Vector for storing binary tree structure */
263  std::vector<char> binary_tree_data_vector_;
264 
265  /** \brief Iterator on binary tree structure vector */
266  std::vector<char> binary_color_tree_vector_;
267 
268  /** \brief Vector for storing points per voxel information */
269  std::vector<unsigned int> point_count_data_vector_;
270 
271  /** \brief Iterator on points per voxel vector */
272  std::vector<unsigned int>::const_iterator point_count_data_vector_iterator_;
273 
274  /** \brief Color coding instance */
276 
277  /** \brief Point coding instance */
279 
280  /** \brief Static range coder instance */
282 
284  uint32_t i_frame_rate_;
286  uint32_t frame_ID_;
287  uint64_t point_count_;
288  bool i_frame_;
289 
293  unsigned char point_color_offset_;
294 
295  //bool activating statistics
299 
300  // frame header identifier
301  static const char* frame_header_identifier_;
302 
304  const double point_resolution_;
305  const double octree_resolution_;
306  const unsigned char color_bit_resolution_;
307 
308  std::size_t object_count_;
309 
310  };
311 
312  // define frame identifier
313  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
315  }
316 
317 }
Octree pointcloud class
OctreePointCloudCompression< PointT, LeafT, BranchT, Octree2BufBase< LeafT, BranchT > > RealTimeStreamCompression
OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudPtr PointCloudPtr
ColorCoding< PointT > color_coder_
Color coding instance.
OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudConstPtr PointCloudConstPtr
Octree pointcloud compression class
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
virtual void addPointIdx(const int point_idx_arg)
Add point at index from input pointcloud dataset to octree.
PointCloudPtr getOutputCloud() const
Get a pointer to the output point cloud dataset.
PointCoding class
Definition: point_coding.h:56
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
boost::shared_ptr< OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > Ptr
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]
OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloud PointCloud
boost::shared_ptr< const OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > ConstPtr
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.
OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeBase< LeafT, BranchT > > SinglePointCloudCompressionLowMemory
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.
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.