Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
octree_pointcloud_compression.h
Go to the documentation of this file.
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>
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 <iterator>
51 #include <iostream>
52 #include <vector>
53 #include <string.h>
54 #include <iostream>
55 #include <stdio.h>
56 #include <string.h>
57 
58 namespace pcl
59 {
60  namespace octree
61  {
68  template<typename PointT, typename LeafT = OctreeContainerDataTVector<int>,
69  typename BranchT = OctreeContainerEmpty<int>,
70  typename OctreeT = Octree2BufBase<int, LeafT, BranchT> >
71  class PointCloudCompression : public OctreePointCloud<PointT, LeafT,
72  BranchT, OctreeT>
73  {
74  public:
75  // public typedefs
79 
80  typedef typename OctreeT::LeafNode LeafNode;
81  typedef typename OctreeT::BranchNode BranchNode;
82 
85 
86 
98  bool showStatistics_arg = false,
99  const double pointResolution_arg = 0.001,
100  const double octreeResolution_arg = 0.01,
101  bool doVoxelGridDownDownSampling_arg = false,
102  const unsigned int iFrameRate_arg = 30,
103  bool doColorEncoding_arg = true,
104  const unsigned char colorBitResolution_arg = 6) :
105  OctreePointCloud<PointT, LeafT, BranchT, OctreeT> (octreeResolution_arg),
106  output_ (PointCloudPtr ()),
107  binaryTreeDataVector_ (),
108  binaryColorTreeVector_ (),
109  pointCountDataVector_ (),
110  pointCountDataVectorIterator_ (),
111  colorCoder_ (),
112  pointCoder_ (),
113  entropyCoder_ (),
114  doVoxelGridEnDecoding_ (doVoxelGridDownDownSampling_arg), iFrameRate_ (iFrameRate_arg),
115  iFrameCounter_ (0), frameID_ (0), pointCount_ (0), iFrame_ (true),
116  doColorEncoding_ (doColorEncoding_arg), cloudWithColor_ (false), dataWithColor_ (false),
117  pointColorOffset_ (0), bShowStatistics (showStatistics_arg),
118  compressedPointDataLen_ (), compressedColorDataLen_ (), selectedProfile_(compressionProfile_arg),
119  pointResolution_(pointResolution_arg), octreeResolution_(octreeResolution_arg), colorBitResolution_(colorBitResolution_arg)
120  {
121  initialization();
122  }
123 
125  virtual
127  {
128  }
129 
131  void initialization () {
132  if (selectedProfile_ != MANUAL_CONFIGURATION)
133  {
134  // apply selected compression profile
135 
136  // retrieve profile settings
137  const configurationProfile_t selectedProfile = compressionProfiles_[selectedProfile_];
138 
139  // apply profile settings
140  iFrameRate_ = selectedProfile.iFrameRate;
141  doVoxelGridEnDecoding_ = selectedProfile.doVoxelGridDownSampling;
142  this->setResolution (selectedProfile.octreeResolution);
143  pointCoder_.setPrecision (static_cast<float> (selectedProfile.pointResolution));
144  doColorEncoding_ = selectedProfile.doColorEncoding;
145  colorCoder_.setBitDepth (selectedProfile.colorBitResolution);
146 
147  }
148  else
149  {
150  // configure point & color coder
151  pointCoder_.setPrecision (static_cast<float> (pointResolution_));
152  colorCoder_.setBitDepth (colorBitResolution_);
153  }
154 
155  if (pointCoder_.getPrecision () == this->getResolution ())
156  //disable differential point colding
157  doVoxelGridEnDecoding_ = true;
158 
159  }
160 
164  inline void
165  setOutputCloud (const PointCloudPtr &cloud_arg)
166  {
167  if (output_ != cloud_arg)
168  {
169  output_ = cloud_arg;
170  }
171  }
172 
176  inline PointCloudPtr
177  getOutputCloud () const
178  {
179  return (output_);
180  }
181 
186  void
187  encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressedTreeDataOut_arg);
188 
193  void
194  decodePointCloud (std::istream& compressedTreeDataIn_arg, PointCloudPtr &cloud_arg);
195 
196  protected:
197 
201  void
202  writeFrameHeader (std::ostream& compressedTreeDataOut_arg);
203 
207  void
208  readFrameHeader (std::istream& compressedTreeDataIn_arg);
209 
213  void
214  syncToHeader (std::istream& compressedTreeDataIn_arg);
215 
219  void
220  entropyEncoding (std::ostream& compressedTreeDataOut_arg);
221 
225  void
226  entropyDecoding (std::istream& compressedTreeDataIn_arg);
227 
232  virtual void
233  serializeTreeCallback (LeafNode &leaf_arg, const OctreeKey& key_arg);
234 
239  virtual void
240  deserializeTreeCallback (LeafNode&, const OctreeKey& key_arg);
241 
242 
244  PointCloudPtr output_;
245 
247  std::vector<char> binaryTreeDataVector_;
248 
250  std::vector<char> binaryColorTreeVector_;
251 
253  std::vector<unsigned int> pointCountDataVector_;
254 
256  std::vector<unsigned int>::const_iterator pointCountDataVectorIterator_;
257 
259  ColorCoding<PointT> colorCoder_;
260 
262  PointCoding<PointT> pointCoder_;
263 
265  StaticRangeCoder entropyCoder_;
266 
267  bool doVoxelGridEnDecoding_;
268  uint32_t iFrameRate_;
269  uint32_t iFrameCounter_;
270  uint32_t frameID_;
271  uint64_t pointCount_;
272  bool iFrame_;
273 
274  bool doColorEncoding_;
275  bool cloudWithColor_;
276  bool dataWithColor_;
277  unsigned char pointColorOffset_;
278 
279  //bool activating statistics
280  bool bShowStatistics;
281  uint64_t compressedPointDataLen_;
282  uint64_t compressedColorDataLen_;
283 
284  // frame header identifier
285  static const char* frameHeaderIdentifier_;
286 
287  const compression_Profiles_e selectedProfile_;
288  const double pointResolution_;
289  const double octreeResolution_;
290  const unsigned char colorBitResolution_;
291 
292  };
293 
294  // define frame header initialization
295  template<typename PointT, typename LeafT, typename BranchT, typename OctreeT>
296  const char* PointCloudCompression<PointT, LeafT, BranchT, OctreeT>::frameHeaderIdentifier_ = "<PCL-COMPRESSED>";
297  }
298 
299 }
300 
301 
302 #endif
303