Point Cloud Library (PCL)  1.8.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 #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  // Boost shared pointers
84  typedef boost::shared_ptr<OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> > Ptr;
85  typedef boost::shared_ptr<const OctreePointCloudCompression<PointT, LeafT, BranchT, OctreeT> > ConstPtr;
86 
87  typedef typename OctreeT::LeafNode LeafNode;
88  typedef typename OctreeT::BranchNode BranchNode;
89 
92 
93 
94  /** \brief Constructor
95  * \param compressionProfile_arg: define compression profile
96  * \param octreeResolution_arg: octree resolution at lowest octree level
97  * \param pointResolution_arg: precision of point coordinates
98  * \param doVoxelGridDownDownSampling_arg: voxel grid filtering
99  * \param iFrameRate_arg: i-frame encoding rate
100  * \param doColorEncoding_arg: enable/disable color coding
101  * \param colorBitResolution_arg: color bit depth
102  * \param showStatistics_arg: output compression statistics
103  */
104  OctreePointCloudCompression (compression_Profiles_e compressionProfile_arg = MED_RES_ONLINE_COMPRESSION_WITH_COLOR,
105  bool showStatistics_arg = false,
106  const double pointResolution_arg = 0.001,
107  const double octreeResolution_arg = 0.01,
108  bool doVoxelGridDownDownSampling_arg = false,
109  const unsigned int iFrameRate_arg = 30,
110  bool doColorEncoding_arg = true,
111  const unsigned char colorBitResolution_arg = 6) :
112  OctreePointCloud<PointT, LeafT, BranchT, OctreeT> (octreeResolution_arg),
113  output_ (PointCloudPtr ()),
114  binary_tree_data_vector_ (),
115  binary_color_tree_vector_ (),
116  point_count_data_vector_ (),
117  point_count_data_vector_iterator_ (),
118  color_coder_ (),
119  point_coder_ (),
120  entropy_coder_ (),
121  do_voxel_grid_enDecoding_ (doVoxelGridDownDownSampling_arg), i_frame_rate_ (iFrameRate_arg),
122  i_frame_counter_ (0), frame_ID_ (0), point_count_ (0), i_frame_ (true),
123  do_color_encoding_ (doColorEncoding_arg), cloud_with_color_ (false), data_with_color_ (false),
124  point_color_offset_ (0), b_show_statistics_ (showStatistics_arg),
125  compressed_point_data_len_ (), compressed_color_data_len_ (), selected_profile_(compressionProfile_arg),
126  point_resolution_(pointResolution_arg), octree_resolution_(octreeResolution_arg),
127  color_bit_resolution_(colorBitResolution_arg),
128  object_count_(0)
129  {
130  initialization();
131  }
132 
133  /** \brief Empty deconstructor. */
134  virtual
136  {
137  }
138 
139  /** \brief Initialize globals */
140  void initialization () {
141  if (selected_profile_ != MANUAL_CONFIGURATION)
142  {
143  // apply selected compression profile
144 
145  // retrieve profile settings
146  const configurationProfile_t selectedProfile = compressionProfiles_[selected_profile_];
147 
148  // apply profile settings
149  i_frame_rate_ = selectedProfile.iFrameRate;
150  do_voxel_grid_enDecoding_ = selectedProfile.doVoxelGridDownSampling;
151  this->setResolution (selectedProfile.octreeResolution);
152  point_coder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
153  do_color_encoding_ = selectedProfile.doColorEncoding;
154  color_coder_.setBitDepth (selectedProfile.colorBitResolution);
155 
156  }
157  else
158  {
159  // configure point & color coder
160  point_coder_.setPrecision (static_cast<float> (point_resolution_));
161  color_coder_.setBitDepth (color_bit_resolution_);
162  }
163 
164  if (point_coder_.getPrecision () == this->getResolution ())
165  //disable differential point colding
166  do_voxel_grid_enDecoding_ = true;
167 
168  }
169 
170  /** \brief Add point at index from input pointcloud dataset to octree
171  * \param[in] pointIdx_arg the index representing the point in the dataset given by \a setInputCloud to be added
172  */
173  virtual void
174  addPointIdx (const int pointIdx_arg)
175  {
176  ++object_count_;
178  }
179 
180  /** \brief Provide a pointer to the output data set.
181  * \param cloud_arg: the boost shared pointer to a PointCloud message
182  */
183  inline void
184  setOutputCloud (const PointCloudPtr &cloud_arg)
185  {
186  if (output_ != cloud_arg)
187  {
188  output_ = cloud_arg;
189  }
190  }
191 
192  /** \brief Get a pointer to the output point cloud dataset.
193  * \return pointer to pointcloud output class.
194  */
195  inline PointCloudPtr
196  getOutputCloud () const
197  {
198  return (output_);
199  }
200 
201  /** \brief Encode point cloud to output stream
202  * \param cloud_arg: point cloud to be compressed
203  * \param compressed_tree_data_out_arg: binary output stream containing compressed data
204  */
205  void
206  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressed_tree_data_out_arg);
207 
208  /** \brief Decode point cloud from input stream
209  * \param compressed_tree_data_in_arg: binary input stream containing compressed data
210  * \param cloud_arg: reference to decoded point cloud
211  */
212  void
213  decodePointCloud (std::istream& compressed_tree_data_in_arg, PointCloudPtr &cloud_arg);
214 
215  protected:
216 
217  /** \brief Write frame information to output stream
218  * \param compressed_tree_data_out_arg: binary output stream
219  */
220  void
221  writeFrameHeader (std::ostream& compressed_tree_data_out_arg);
222 
223  /** \brief Read frame information to output stream
224  * \param compressed_tree_data_in_arg: binary input stream
225  */
226  void
227  readFrameHeader (std::istream& compressed_tree_data_in_arg);
228 
229  /** \brief Synchronize to frame header
230  * \param compressed_tree_data_in_arg: binary input stream
231  */
232  void
233  syncToHeader (std::istream& compressed_tree_data_in_arg);
234 
235  /** \brief Apply entropy encoding to encoded information and output to binary stream
236  * \param compressed_tree_data_out_arg: binary output stream
237  */
238  void
239  entropyEncoding (std::ostream& compressed_tree_data_out_arg);
240 
241  /** \brief Entropy decoding of input binary stream and output to information vectors
242  * \param compressed_tree_data_in_arg: binary input stream
243  */
244  void
245  entropyDecoding (std::istream& compressed_tree_data_in_arg);
246 
247  /** \brief Encode leaf node information during serialization
248  * \param leaf_arg: reference to new leaf node
249  * \param key_arg: octree key of new leaf node
250  */
251  virtual void
252  serializeTreeCallback (LeafT &leaf_arg, const OctreeKey& key_arg);
253 
254  /** \brief Decode leaf nodes information during deserialization
255  * \param key_arg octree key of new leaf node
256  */
257  // param leaf_arg reference to new leaf node
258  virtual void
259  deserializeTreeCallback (LeafT&, const OctreeKey& key_arg);
260 
261 
262  /** \brief Pointer to output point cloud dataset. */
264 
265  /** \brief Vector for storing binary tree structure */
266  std::vector<char> binary_tree_data_vector_;
267 
268  /** \brief Interator on binary tree structure vector */
269  std::vector<char> binary_color_tree_vector_;
270 
271  /** \brief Vector for storing points per voxel information */
272  std::vector<unsigned int> point_count_data_vector_;
273 
274  /** \brief Interator on points per voxel vector */
275  std::vector<unsigned int>::const_iterator point_count_data_vector_iterator_;
276 
277  /** \brief Color coding instance */
279 
280  /** \brief Point coding instance */
282 
283  /** \brief Static range coder instance */
285 
287  uint32_t i_frame_rate_;
289  uint32_t frame_ID_;
290  uint64_t point_count_;
291  bool i_frame_;
292 
296  unsigned char point_color_offset_;
297 
298  //bool activating statistics
302 
303  // frame header identifier
304  static const char* frame_header_identifier_;
305 
307  const double point_resolution_;
308  const double octree_resolution_;
309  const unsigned char color_bit_resolution_;
310 
311  std::size_t object_count_;
312 
313  };
314 
315  // define frame identifier
316  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
318  }
319 
320 }
321 
322 
323 #endif
324 
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
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:59
virtual ~OctreePointCloudCompression()
Empty deconstructor.
ColorCoding class
Definition: color_coding.h:64
PointCoding< PointT > point_coder_
Point coding instance.
Octree container class that does store a vector of point indices.
virtual void addPointIdx(const int pointIdx_arg)
Add point at index from input pointcloud dataset to octree.
std::vector< char > binary_color_tree_vector_
Interator on binary tree structure vector.
PointCloudPtr output_
Pointer to output point cloud dataset.
Octree key class
Definition: octree_key.h:51
boost::shared_ptr< OctreePointCloudCompression< PointT, LeafT, BranchT, OctreeT > > Ptr
StaticRangeCoder compression class
std::vector< unsigned int >::const_iterator point_count_data_vector_iterator_
Interator 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.
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.