Point Cloud Library (PCL)  1.12.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/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 
122  ~OctreePointCloudCompression () override = default;
123 
124  /** \brief Initialize globals */
125  void initialization () {
126  if (selected_profile_ != MANUAL_CONFIGURATION)
127  {
128  // apply selected compression profile
129 
130  // retrieve profile settings
131  const configurationProfile_t selectedProfile = compressionProfiles_[selected_profile_];
132 
133  // apply profile settings
134  i_frame_rate_ = selectedProfile.iFrameRate;
135  do_voxel_grid_enDecoding_ = selectedProfile.doVoxelGridDownSampling;
136  this->setResolution (selectedProfile.octreeResolution);
137  point_coder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
138  do_color_encoding_ = selectedProfile.doColorEncoding;
139  color_coder_.setBitDepth (selectedProfile.colorBitResolution);
140 
141  }
142  else
143  {
144  // configure point & color coder
145  point_coder_.setPrecision (static_cast<float> (point_resolution_));
146  color_coder_.setBitDepth (color_bit_resolution_);
147  }
148 
149  if (point_coder_.getPrecision () == this->getResolution ())
150  //disable differential point colding
151  do_voxel_grid_enDecoding_ = true;
152 
153  }
154 
155  /** \brief Add point at index from input pointcloud dataset to octree
156  * \param[in] pointIdx_arg the index representing the point in the dataset given by \a setInputCloud to be added
157  */
158  void
159  addPointIdx (const uindex_t pointIdx_arg) override
160  {
161  ++object_count_;
163  }
164 
165  /** \brief Provide a pointer to the output data set.
166  * \param cloud_arg: the boost shared pointer to a PointCloud message
167  */
168  inline void
169  setOutputCloud (const PointCloudPtr &cloud_arg)
170  {
171  if (output_ != cloud_arg)
172  {
173  output_ = cloud_arg;
174  }
175  }
176 
177  /** \brief Get a pointer to the output point cloud dataset.
178  * \return pointer to pointcloud output class.
179  */
180  inline PointCloudPtr
181  getOutputCloud () const
182  {
183  return (output_);
184  }
185 
186  /** \brief Encode point cloud to output stream
187  * \param cloud_arg: point cloud to be compressed
188  * \param compressed_tree_data_out_arg: binary output stream containing compressed data
189  */
190  void
191  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressed_tree_data_out_arg);
192 
193  /** \brief Decode point cloud from input stream
194  * \param compressed_tree_data_in_arg: binary input stream containing compressed data
195  * \param cloud_arg: reference to decoded point cloud
196  */
197  void
198  decodePointCloud (std::istream& compressed_tree_data_in_arg, PointCloudPtr &cloud_arg);
199 
200  protected:
201 
202  /** \brief Write frame information to output stream
203  * \param compressed_tree_data_out_arg: binary output stream
204  */
205  void
206  writeFrameHeader (std::ostream& compressed_tree_data_out_arg);
207 
208  /** \brief Read frame information to output stream
209  * \param compressed_tree_data_in_arg: binary input stream
210  */
211  void
212  readFrameHeader (std::istream& compressed_tree_data_in_arg);
213 
214  /** \brief Synchronize to frame header
215  * \param compressed_tree_data_in_arg: binary input stream
216  */
217  void
218  syncToHeader (std::istream& compressed_tree_data_in_arg);
219 
220  /** \brief Apply entropy encoding to encoded information and output to binary stream
221  * \param compressed_tree_data_out_arg: binary output stream
222  */
223  void
224  entropyEncoding (std::ostream& compressed_tree_data_out_arg);
225 
226  /** \brief Entropy decoding of input binary stream and output to information vectors
227  * \param compressed_tree_data_in_arg: binary input stream
228  */
229  void
230  entropyDecoding (std::istream& compressed_tree_data_in_arg);
231 
232  /** \brief Encode leaf node information during serialization
233  * \param leaf_arg: reference to new leaf node
234  * \param key_arg: octree key of new leaf node
235  */
236  void
237  serializeTreeCallback (LeafT &leaf_arg, const OctreeKey& key_arg) override;
238 
239  /** \brief Decode leaf nodes information during deserialization
240  * \param key_arg octree key of new leaf node
241  */
242  // param leaf_arg reference to new leaf node
243  void
244  deserializeTreeCallback (LeafT&, const OctreeKey& key_arg) override;
245 
246 
247  /** \brief Pointer to output point cloud dataset. */
249 
250  /** \brief Vector for storing binary tree structure */
251  std::vector<char> binary_tree_data_vector_;
252 
253  /** \brief Vector for storing points per voxel information */
254  std::vector<unsigned int> point_count_data_vector_;
255 
256  /** \brief Iterator on points per voxel vector */
257  std::vector<unsigned int>::const_iterator point_count_data_vector_iterator_;
258 
259  /** \brief Color coding instance */
261 
262  /** \brief Point coding instance */
264 
265  /** \brief Static range coder instance */
267 
269  std::uint32_t i_frame_rate_;
270  std::uint32_t i_frame_counter_;
271  std::uint32_t frame_ID_;
272  std::uint64_t point_count_;
273  bool i_frame_;
274 
278  unsigned char point_color_offset_;
279 
280  //bool activating statistics
284 
285  // frame header identifier
286  static const char* frame_header_identifier_;
287 
289  const double point_resolution_;
290  const double octree_resolution_;
291  const unsigned char color_bit_resolution_;
292 
293  std::size_t object_count_;
294 
295  };
296 
297  // define frame identifier
298  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
300  }
301 
302 }
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:80
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.