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