Point Cloud Library (PCL)  1.3.1
octree_pointcloud_compression.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * Author: Julius Kammerl (julius@kammerl.de)
00035  */
00036 
00037 #ifndef OCTREE_COMPRESSION_H
00038 #define OCTREE_COMPRESSION_H
00039 
00040 #include "pcl/common/common.h"
00041 #include "pcl/common/io.h"
00042 #include "pcl/octree/octree_pointcloud.h"
00043 #include "entropy_range_coder.h"
00044 #include "color_coding.h"
00045 #include "point_coding.h"
00046 
00047 #include "compression_profiles.h"
00048 
00049 #include <iterator>
00050 #include <iostream>
00051 #include <vector>
00052 #include <string.h>
00053 #include <iostream>
00054 #include <stdio.h>
00055 #include <string.h>
00056 
00057 namespace pcl
00058 {
00059   namespace octree
00060   {
00061     using namespace std;
00062 
00064 
00070 
00071     template<typename PointT, typename LeafT = OctreeLeafDataTVector<int> , typename OctreeT = Octree2BufBase<int,
00072         OctreeLeafDataTVector<int> > >
00073       class PointCloudCompression : public OctreePointCloud<PointT, LeafT, OctreeT>
00074       {
00075       public:
00076 
00077         // public typedefs
00078 
00079         typedef typename OctreePointCloud<PointT, LeafT, OctreeT>::PointCloud PointCloud;
00080         typedef typename OctreePointCloud<PointT, LeafT, OctreeT>::PointCloudPtr PointCloudPtr;
00081         typedef typename OctreePointCloud<PointT, LeafT, OctreeT>::PointCloudConstPtr PointCloudConstPtr;
00082 
00083         typedef typename OctreePointCloud<PointT, LeafT, OctreeT>::OctreeKey OctreeKey;
00084         typedef typename OctreeT::OctreeLeaf OctreeLeaf;
00085 
00086         typedef PointCloudCompression<PointT, LeafT, Octree2BufBase<int, LeafT> > RealTimeStreamCompression;
00087         typedef PointCloudCompression<PointT, LeafT, OctreeLowMemBase<int, LeafT> > SinglePointCloudCompressionLowMemory;
00088 
00099         PointCloudCompression (compression_Profiles_e compressionProfile_arg = MED_RES_ONLINE_COMPRESSION_WITH_COLOR,
00100                                bool showStatistics_arg = false,
00101                                const double pointResolution_arg = 0.001,
00102                                const double octreeResolution_arg = 0.01,
00103                                bool doVoxelGridDownDownSampling_arg = false,
00104                                const unsigned int iFrameRate_arg = 30,
00105                                bool doColorEncoding_arg = true,
00106                                const unsigned char colorBitResolution_arg = 6
00107                                ) :
00108 
00109           OctreePointCloud<PointT, LeafT, OctreeT> (octreeResolution_arg),
00110               doVoxelGridEnDecoding_ (doVoxelGridDownDownSampling_arg), iFrameRate_ (iFrameRate_arg),
00111               iFrameCounter_ (0), frameID_ (0), pointCount_ (0), iFrame_ (true),
00112               doColorEncoding_ (doColorEncoding_arg), cloudWithColor_ (false), dataWithColor_ (false),
00113               pointColorOffset_ (0), bShowStatistics (showStatistics_arg)
00114 
00115         {
00116           output_ = PointCloudPtr ();
00117 
00118           if (compressionProfile_arg != MANUAL_CONFIGURATION)
00119           {
00120             // apply selected compression profile
00121 
00122             // retrieve profile settings
00123             const configurationProfile_t selectedProfile = compressionProfiles_[compressionProfile_arg];
00124 
00125             // apply profile settings
00126             iFrameRate_ = selectedProfile.iFrameRate;
00127             doVoxelGridEnDecoding_ = selectedProfile.doVoxelGridDownSampling;
00128             this->setResolution (selectedProfile.octreeResolution);
00129             pointCoder_.setPrecision (selectedProfile.pointResolution);
00130             doColorEncoding_ = selectedProfile.doColorEncoding;
00131             colorCoder_.setBitDepth (selectedProfile.colorBitResolution);
00132 
00133           } else {
00134             // configure point & color coder
00135             pointCoder_.setPrecision (pointResolution_arg);
00136             colorCoder_.setBitDepth (colorBitResolution_arg);
00137           }
00138 
00139           if ( pointCoder_.getPrecision() == this->getResolution() )
00140           {
00141             //disable differential point colding
00142             doVoxelGridEnDecoding_ = true;
00143           }
00144 
00145         }
00146 
00148         virtual
00149         ~PointCloudCompression ()
00150         {
00151         }
00152 
00156         inline void
00157         setOutputCloud (const PointCloudPtr &cloud_arg)
00158         {
00159           if (output_ != cloud_arg)
00160           {
00161             output_ = cloud_arg;
00162           }
00163         }
00164 
00168         inline PointCloudPtr
00169         getOutputCloud ()
00170         {
00171           return (output_);
00172         }
00173 
00178         void
00179         encodePointCloud (const PointCloudConstPtr &cloud_arg, std::ostream& compressedTreeDataOut_arg);
00180 
00185         void
00186         decodePointCloud (std::istream& compressedTreeDataIn_arg, PointCloudPtr &cloud_arg);
00187 
00188       protected:
00189 
00193         void
00194         writeFrameHeader (std::ostream& compressedTreeDataOut_arg);
00195 
00199         void
00200         readFrameHeader (std::istream& compressedTreeDataIn_arg);
00201 
00205         void
00206         entropyEncoding (std::ostream& compressedTreeDataOut_arg);
00207 
00211         void
00212         entropyDecoding (std::istream& compressedTreeDataIn_arg);
00213 
00218         virtual void
00219         serializeLeafCallback (OctreeLeaf& leaf_arg, const OctreeKey& key_arg);
00220 
00225         virtual void
00226         deserializeLeafCallback (OctreeLeaf& leaf_arg, const OctreeKey& key_arg);
00227 
00229         PointCloudPtr output_;
00230 
00232         std::vector<char> binaryTreeDataVector_;
00233 
00235         std::vector<char> binaryColorTreeVector_;
00236 
00238         std::vector<unsigned int> pointCountDataVector_;
00239 
00241         std::vector<unsigned int>::const_iterator pointCountDataVectorIterator_;
00242 
00244         ColorCoding<PointT> colorCoder_;
00245 
00247         PointCoding<PointT> pointCoder_;
00248 
00250         StaticRangeCoder entropyCoder_;
00251 
00252         bool doVoxelGridEnDecoding_;
00253         unsigned int iFrameRate_;
00254         unsigned int iFrameCounter_;
00255         unsigned int frameID_;
00256         unsigned long pointCount_;
00257         bool iFrame_;
00258 
00259         bool doColorEncoding_;
00260         bool cloudWithColor_;
00261         bool dataWithColor_;
00262         unsigned char pointColorOffset_;
00263 
00264         //bool activating statistics
00265         bool bShowStatistics;
00266         unsigned long compressedPointDataLen_;
00267         unsigned long compressedColorDataLen_;
00268 
00269         // frame header identifier
00270         static const char* frameHeaderIdentifier_;
00271 
00272       };
00273 
00274     // define frame header initialization
00275     template<typename PointT, typename LeafT, typename OctreeT>
00276       const char* PointCloudCompression<PointT, LeafT, OctreeT>::frameHeaderIdentifier_ = "<PCL-COMPRESSED>";
00277   }
00278 
00279 }
00280 
00281 
00282 #endif
00283 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines