Point Cloud Library (PCL)  1.14.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 
110  do_color_encoding_ (doColorEncoding_arg), b_show_statistics_ (showStatistics_arg),
111  selected_profile_(compressionProfile_arg),
112  point_resolution_(pointResolution_arg), octree_resolution_(octreeResolution_arg),
113  color_bit_resolution_(colorBitResolution_arg)
114  {
115  initialization();
116  }
117 
118  /** \brief Empty deconstructor. */
119 
120  ~OctreePointCloudCompression () override = default;
121 
122  /** \brief Initialize globals */
123  void initialization () {
124  if (selected_profile_ != MANUAL_CONFIGURATION)
125  {
126  // apply selected compression profile
127 
128  // retrieve profile settings
129  const configurationProfile_t selectedProfile = compressionProfiles_[selected_profile_];
130 
131  // apply profile settings
132  i_frame_rate_ = selectedProfile.iFrameRate;
133  do_voxel_grid_enDecoding_ = selectedProfile.doVoxelGridDownSampling;
134  this->setResolution (selectedProfile.octreeResolution);
135  point_coder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
136  do_color_encoding_ = selectedProfile.doColorEncoding;
137  color_coder_.setBitDepth (selectedProfile.colorBitResolution);
138 
139  }
140  else
141  {
142  // configure point & color coder
143  point_coder_.setPrecision (static_cast<float> (point_resolution_));
144  color_coder_.setBitDepth (color_bit_resolution_);
145  }
146 
147  if (point_coder_.getPrecision () == this->getResolution ())
148  //disable differential point colding
149  do_voxel_grid_enDecoding_ = true;
150 
151  }
152 
153  /** \brief Add point at index from input pointcloud dataset to octree
154  * \param[in] pointIdx_arg the index representing the point in the dataset given by \a setInputCloud to be added
155  */
156  void
157  addPointIdx (const uindex_t pointIdx_arg) override
158  {
159  ++object_count_;
161  }
162 
163  /** \brief Provide a pointer to the output data set.
164  * \param cloud_arg: the boost shared pointer to a PointCloud message
165  */
166  inline void
167  setOutputCloud (const PointCloudPtr &cloud_arg)
168  {
169  if (output_ != cloud_arg)
170  {
171  output_ = cloud_arg;
172  }
173  }
174 
175  /** \brief Get a pointer to the output point cloud dataset.
176  * \return pointer to pointcloud output class.
177  */
178  inline PointCloudPtr
179  getOutputCloud () const
180  {
181  return (output_);
182  }
183 
184  /** \brief Encode point cloud to output stream
185  * \param cloud_arg: point cloud to be compressed
186  * \param compressed_tree_data_out_arg: binary output stream containing compressed data
187  */
188  void
189  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressed_tree_data_out_arg);
190 
191  /** \brief Decode point cloud from input stream
192  * \param compressed_tree_data_in_arg: binary input stream containing compressed data
193  * \param cloud_arg: reference to decoded point cloud
194  * \warning This function is blocking until there is data available from the input stream. If the stream never contains any data, this will hang forever!
195  */
196  void
197  decodePointCloud (std::istream& compressed_tree_data_in_arg, PointCloudPtr &cloud_arg);
198 
199  protected:
200 
201  /** \brief Write frame information to output stream
202  * \param compressed_tree_data_out_arg: binary output stream
203  */
204  void
205  writeFrameHeader (std::ostream& compressed_tree_data_out_arg);
206 
207  /** \brief Read frame information to output stream
208  * \param compressed_tree_data_in_arg: binary input stream
209  */
210  void
211  readFrameHeader (std::istream& compressed_tree_data_in_arg);
212 
213  /** \brief Synchronize to frame header
214  * \param compressed_tree_data_in_arg: binary input stream
215  */
216  void
217  syncToHeader (std::istream& compressed_tree_data_in_arg);
218 
219  /** \brief Apply entropy encoding to encoded information and output to binary stream
220  * \param compressed_tree_data_out_arg: binary output stream
221  */
222  void
223  entropyEncoding (std::ostream& compressed_tree_data_out_arg);
224 
225  /** \brief Entropy decoding of input binary stream and output to information vectors
226  * \param compressed_tree_data_in_arg: binary input stream
227  */
228  void
229  entropyDecoding (std::istream& compressed_tree_data_in_arg);
230 
231  /** \brief Encode leaf node information during serialization
232  * \param leaf_arg: reference to new leaf node
233  * \param key_arg: octree key of new leaf node
234  */
235  void
236  serializeTreeCallback (LeafT &leaf_arg, const OctreeKey& key_arg) override;
237 
238  /** \brief Decode leaf nodes information during deserialization
239  * \param key_arg octree key of new leaf node
240  */
241  // param leaf_arg reference to new leaf node
242  void
243  deserializeTreeCallback (LeafT&, const OctreeKey& key_arg) override;
244 
245 
246  /** \brief Pointer to output point cloud dataset. */
248 
249  /** \brief Vector for storing binary tree structure */
250  std::vector<char> binary_tree_data_vector_;
251 
252  /** \brief Vector for storing points per voxel information */
253  std::vector<unsigned int> point_count_data_vector_;
254 
255  /** \brief Iterator on points per voxel vector */
256  std::vector<unsigned int>::const_iterator point_count_data_vector_iterator_;
257 
258  /** \brief Color coding instance */
260 
261  /** \brief Point coding instance */
263 
264  /** \brief Static range coder instance */
266 
267  bool do_voxel_grid_enDecoding_{false};
268  std::uint32_t i_frame_rate_{0};
269  std::uint32_t i_frame_counter_{0};
270  std::uint32_t frame_ID_{0};
271  std::uint64_t point_count_{0};
272  bool i_frame_{true};
273 
274  bool do_color_encoding_{false};
275  bool cloud_with_color_{false};
276  bool data_with_color_{false};
277  unsigned char point_color_offset_{0};
278 
279  //bool activating statistics
280  bool b_show_statistics_{false};
281  std::uint64_t compressed_point_data_len_{0};
282  std::uint64_t compressed_color_data_len_{0};
283 
284  // frame header identifier
285  static const char* frame_header_identifier_;
286 
288  const double point_resolution_;
289  const double octree_resolution_;
290  const unsigned char color_bit_resolution_;
291 
292  std::size_t object_count_{0};
293 
294  };
295 
296  // define frame identifier
297  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
298  const char* OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT>::frame_header_identifier_ = "<PCL-OCT-COMPRESSED>";
299  }
300 
301 }
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
StaticRangeCoder compression class
Octree pointcloud compression class
shared_ptr< OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > Ptr
ColorCoding< PointT > color_coder_
Color coding instance.
~OctreePointCloudCompression() override=default
Empty deconstructor.
std::vector< unsigned int >::const_iterator point_count_data_vector_iterator_
Iterator on points per voxel vector.
PointCoding< PointT > point_coder_
Point coding instance.
void addPointIdx(const uindex_t pointIdx_arg) override
Add point at index from input pointcloud dataset to octree.
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloud PointCloud
StaticRangeCoder entropy_coder_
Static range coder instance.
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudConstPtr PointCloudConstPtr
void setOutputCloud(const PointCloudPtr &cloud_arg)
Provide a pointer to the output data set.
PointCloudPtr getOutputCloud() const
Get a pointer to the output point cloud dataset.
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.
std::vector< char > binary_tree_data_vector_
Vector for storing binary tree structure.
shared_ptr< const OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > ConstPtr
PointCloudPtr output_
Pointer to output point cloud dataset.
std::vector< unsigned int > point_count_data_vector_
Vector for storing points per voxel information
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudPtr PointCloudPtr
ColorCoding class
Definition: color_coding.h:57
Octree double buffer class
Octree container class that does not store any information.
Octree container class that does store a vector of point indices.
Octree key class
Definition: octree_key.h:54
Abstract octree leaf class
Definition: octree_nodes.h:81
Octree pointcloud class
typename PointCloud::Ptr PointCloudPtr
typename PointCloud::ConstPtr PointCloudConstPtr
virtual void addPointIdx(uindex_t point_idx_arg)
Add point at index from input pointcloud dataset to octree.
PointCoding class
Definition: point_coding.h:55
const struct configurationProfile_t compressionProfiles_[COMPRESSION_PROFILE_COUNT]
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.
Definition: types.h:120
A point structure representing Euclidean xyz coordinates, and the RGB color.