38#ifndef OCTREE_COMPRESSION_HPP
39#define OCTREE_COMPRESSION_HPP
41#include <pcl/common/io.h>
42#include <pcl/compression/entropy_range_coder.h>
56 std::ostream& compressed_tree_data_out_arg)
58 auto recent_tree_depth =
73 std::vector<pcl::PCLPointField>
fields;
112 color_coder_.setPointCount (
static_cast<unsigned int> (cloud_arg->size ()));
117 point_coder_.setPointCount (
static_cast<unsigned int> (cloud_arg->size ()));
135 this->switchBuffers ();
145 PCL_INFO (
"*** POINTCLOUD ENCODING ***\n");
148 PCL_INFO (
"Encoding Frame: Intra frame\n");
150 PCL_INFO (
"Encoding Frame: Prediction frame\n");
151 PCL_INFO (
"Number of encoded points: %ld\n",
point_count_);
152 PCL_INFO (
"XYZ compression percentage: %f%%\n", bytes_per_XYZ / (3.0f *
sizeof (
float)) * 100.0f);
153 PCL_INFO (
"XYZ bytes per point: %f bytes\n", bytes_per_XYZ);
154 PCL_INFO (
"Color compression percentage: %f%%\n", bytes_per_color / (
sizeof (
int)) * 100.0f);
155 PCL_INFO (
"Color bytes per point: %f bytes\n", bytes_per_color);
156 PCL_INFO (
"Size of uncompressed point cloud: %f kBytes\n",
static_cast<float> (
point_count_) * (
sizeof (
int) + 3.0f *
sizeof (
float)) / 1024.0f);
158 PCL_INFO (
"Total bytes per point: %f bytes\n", bytes_per_XYZ + bytes_per_color);
159 PCL_INFO (
"Total compression percentage: %f%%\n", (bytes_per_XYZ + bytes_per_color) / (
sizeof (
int) + 3.0f *
sizeof (
float)) * 100.0f);
160 PCL_INFO (
"Compression ratio: %f\n\n",
static_cast<float> (
sizeof (
int) + 3.0f *
sizeof (
float)) /
static_cast<float> (bytes_per_XYZ + bytes_per_color));
166 PCL_INFO (
"Info: Dropping empty point cloud\n");
174 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
176 std::istream& compressed_tree_data_in_arg,
184 this->switchBuffers ();
189 std::vector<pcl::PCLPointField>
fields;
192 if (rgba_index == -1)
223 output_->width = cloud_arg->size ();
231 PCL_INFO (
"*** POINTCLOUD DECODING ***\n");
234 PCL_INFO (
"Decoding Frame: Intra frame\n");
236 PCL_INFO (
"Decoding Frame: Prediction frame\n");
237 PCL_INFO (
"Number of decoded points: %ld\n",
point_count_);
238 PCL_INFO (
"XYZ compression percentage: %f%%\n", bytes_per_XYZ / (3.0f *
sizeof (
float)) * 100.0f);
239 PCL_INFO (
"XYZ bytes per point: %f bytes\n", bytes_per_XYZ);
240 PCL_INFO (
"Color compression percentage: %f%%\n", bytes_per_color / (
sizeof (
int)) * 100.0f);
241 PCL_INFO (
"Color bytes per point: %f bytes\n", bytes_per_color);
242 PCL_INFO (
"Size of uncompressed point cloud: %f kBytes\n",
static_cast<float> (
point_count_) * (
sizeof (
int) + 3.0f *
sizeof (
float)) / 1024.0f);
244 PCL_INFO (
"Total bytes per point: %f bytes\n", bytes_per_XYZ + bytes_per_color);
245 PCL_INFO (
"Total compression percentage: %f%%\n", (bytes_per_XYZ + bytes_per_color) / (
sizeof (
int) + 3.0f *
sizeof (
float)) * 100.0f);
246 PCL_INFO (
"Compression ratio: %f\n\n",
static_cast<float> (
sizeof (
int) + 3.0f *
sizeof (
float)) /
static_cast<float> (bytes_per_XYZ + bytes_per_color));
251 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
254 std::uint64_t binary_tree_data_vector_size;
255 std::uint64_t point_avg_color_data_vector_size;
262 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&binary_tree_data_vector_size),
sizeof (binary_tree_data_vector_size));
264 compressed_tree_data_out_arg);
269 std::vector<char>& pointAvgColorDataVector =
color_coder_.getAverageDataVector ();
270 point_avg_color_data_vector_size = pointAvgColorDataVector.size ();
271 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&point_avg_color_data_vector_size),
272 sizeof (point_avg_color_data_vector_size));
274 compressed_tree_data_out_arg);
279 std::uint64_t pointCountDataVector_size;
280 std::uint64_t point_diff_data_vector_size;
281 std::uint64_t point_diff_color_data_vector_size;
285 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&pointCountDataVector_size),
sizeof (pointCountDataVector_size));
287 compressed_tree_data_out_arg);
290 std::vector<char>& point_diff_data_vector =
point_coder_.getDifferentialDataVector ();
291 point_diff_data_vector_size = point_diff_data_vector.size ();
292 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&point_diff_data_vector_size),
sizeof (point_diff_data_vector_size));
294 compressed_tree_data_out_arg);
298 std::vector<char>& point_diff_color_data_vector =
color_coder_.getDifferentialDataVector ();
299 point_diff_color_data_vector_size = point_diff_color_data_vector.size ();
300 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&point_diff_color_data_vector_size),
301 sizeof (point_diff_color_data_vector_size));
303 compressed_tree_data_out_arg);
307 compressed_tree_data_out_arg.flush ();
311 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
314 std::uint64_t binary_tree_data_vector_size;
315 std::uint64_t point_avg_color_data_vector_size;
321 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&binary_tree_data_vector_size),
sizeof (binary_tree_data_vector_size));
329 std::vector<char>& point_avg_color_data_vector =
color_coder_.getAverageDataVector ();
330 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&point_avg_color_data_vector_size),
sizeof (point_avg_color_data_vector_size));
331 point_avg_color_data_vector.resize (
static_cast<std::size_t
> (point_avg_color_data_vector_size));
333 point_avg_color_data_vector);
338 std::uint64_t point_count_data_vector_size;
339 std::uint64_t point_diff_data_vector_size;
340 std::uint64_t point_diff_color_data_vector_size;
343 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&point_count_data_vector_size),
sizeof (point_count_data_vector_size));
349 std::vector<char>& pointDiffDataVector =
point_coder_.getDifferentialDataVector ();
350 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&point_diff_data_vector_size),
sizeof (point_diff_data_vector_size));
351 pointDiffDataVector.resize (
static_cast<std::size_t
> (point_diff_data_vector_size));
353 pointDiffDataVector);
358 std::vector<char>& pointDiffColorDataVector =
color_coder_.getDifferentialDataVector ();
359 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&point_diff_color_data_vector_size),
sizeof (point_diff_color_data_vector_size));
360 pointDiffColorDataVector.resize (
static_cast<std::size_t
> (point_diff_color_data_vector_size));
362 pointDiffColorDataVector);
368 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
374 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&
frame_ID_),
sizeof (
frame_ID_));
376 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&
i_frame_),
sizeof (
i_frame_));
379 double min_x, min_y, min_z, max_x, max_y, max_z;
380 double octree_resolution;
381 unsigned char color_bit_depth;
382 double point_resolution;
400 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&octree_resolution),
sizeof (octree_resolution));
401 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&color_bit_depth),
sizeof (color_bit_depth));
402 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&point_resolution),
sizeof (point_resolution));
405 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&min_x),
sizeof (min_x));
406 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&min_y),
sizeof (min_y));
407 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&min_z),
sizeof (min_z));
408 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&max_x),
sizeof (max_x));
409 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&max_y),
sizeof (max_y));
410 compressed_tree_data_out_arg.write (
reinterpret_cast<const char*
> (&max_z),
sizeof (max_z));
415 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
419 unsigned int header_id_pos = 0;
423 compressed_tree_data_in_arg.read (
static_cast<char*
> (&readChar),
sizeof (readChar));
430 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
434 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&
frame_ID_),
sizeof (
frame_ID_));
435 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
>(&
i_frame_),
sizeof (
i_frame_));
438 double min_x, min_y, min_z, max_x, max_y, max_z;
439 double octree_resolution;
440 unsigned char color_bit_depth;
441 double point_resolution;
447 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&octree_resolution),
sizeof (octree_resolution));
448 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&color_bit_depth),
sizeof (color_bit_depth));
449 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&point_resolution),
sizeof (point_resolution));
452 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&min_x),
sizeof (min_x));
453 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&min_y),
sizeof (min_y));
454 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&min_z),
sizeof (min_z));
455 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&max_x),
sizeof (max_x));
456 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&max_y),
sizeof (max_y));
457 compressed_tree_data_in_arg.read (
reinterpret_cast<char*
> (&max_z),
sizeof (max_z));
466 point_coder_.setPrecision (
static_cast<float> (point_resolution));
471 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
473 LeafT &leaf_arg,
const OctreeKey & key_arg)
476 const auto& leafIdx = leaf_arg.getPointIndicesVector();
480 double lowerVoxelCorner[3];
486 lowerVoxelCorner[0] =
static_cast<double> (key_arg.x) * this->
resolution_ + this->
min_x_;
487 lowerVoxelCorner[1] =
static_cast<double> (key_arg.y) * this->
resolution_ + this->
min_y_;
488 lowerVoxelCorner[2] =
static_cast<double> (key_arg.z) * this->
resolution_ + this->
min_z_;
506 template<
typename Po
intT,
typename LeafT,
typename BranchT,
typename OctreeT>
void
512 std::size_t pointCount = 1;
517 const auto cloudSize =
output_->size ();
524 for (std::size_t i = 0; i < pointCount; i++)
525 output_->points.push_back (newPoint);
528 double lowerVoxelCorner[3];
529 lowerVoxelCorner[0] =
static_cast<double> (key_arg.x) * this->
resolution_ + this->
min_x_;
530 lowerVoxelCorner[1] =
static_cast<double> (key_arg.y) * this->
resolution_ + this->
min_y_;
531 lowerVoxelCorner[2] =
static_cast<double> (key_arg.z) * this->
resolution_ + this->
min_z_;
539 newPoint.x =
static_cast<float> ((
static_cast<double> (key_arg.x) + 0.5) * this->
resolution_ + this->
min_x_);
540 newPoint.y =
static_cast<float> ((
static_cast<double> (key_arg.y) + 0.5) * this->
resolution_ + this->
min_y_);
541 newPoint.z =
static_cast<float> ((
static_cast<double> (key_arg.z) + 0.5) * this->
resolution_ + this->
min_z_);
544 output_->points.push_back (newPoint);
Octree pointcloud compression class
void encodePointCloud(const PointCloudConstPtr &cloud_arg, std::ostream &compressed_tree_data_out_arg)
Encode point cloud to output stream.
std::uint64_t compressed_point_data_len_
std::size_t object_count_
void entropyEncoding(std::ostream &compressed_tree_data_out_arg)
Apply entropy encoding to encoded information and output to binary stream.
void syncToHeader(std::istream &compressed_tree_data_in_arg)
Synchronize to frame header.
std::uint64_t point_count_
ColorCoding< PointT > color_coder_
Color coding instance.
void readFrameHeader(std::istream &compressed_tree_data_in_arg)
Read frame information to output stream.
static const char * frame_header_identifier_
std::vector< unsignedint >::const_iterator point_count_data_vector_iterator_
Iterator on points per voxel vector.
void writeFrameHeader(std::ostream &compressed_tree_data_out_arg)
Write frame information to output stream.
PointCoding< PointT > point_coder_
Point coding instance.
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.
void entropyDecoding(std::istream &compressed_tree_data_in_arg)
Entropy decoding of input binary stream and output to information vectors.
void decodePointCloud(std::istream &compressed_tree_data_in_arg, PointCloudPtr &cloud_arg)
Decode point cloud from input stream.
void deserializeTreeCallback(LeafT &, const OctreeKey &key_arg) override
Decode leaf nodes information during deserialization.
std::vector< char > binary_tree_data_vector_
Vector for storing binary tree structure.
std::uint64_t compressed_color_data_len_
std::uint32_t i_frame_counter_
PointCloudPtr output_
Pointer to output point cloud dataset.
std::vector< unsigned int > point_count_data_vector_
Vector for storing points per voxel information.
std::uint32_t i_frame_rate_
bool do_voxel_grid_enDecoding_
unsigned char point_color_offset_
void serializeTreeCallback(LeafT &leaf_arg, const OctreeKey &key_arg) override
Encode leaf node information during serialization.
typename OctreePointCloud< PointT, LeafT, BranchT, OctreeT >::PointCloudPtr PointCloudPtr
std::size_t leaf_count_
Amount of leaf nodes.
void deserializeTree(std::vector< char > &binary_tree_input_arg)
Deserialize a binary octree description vector and create a corresponding octree structure.
OctreeBase< LeafContainerT, BranchContainerT > OctreeT
void serializeTree(std::vector< char > &binary_tree_out_arg) const
Serialize octree into a binary output vector describing its branch node structure.
void getBoundingBox(double &min_x_arg, double &min_y_arg, double &min_z_arg, double &max_x_arg, double &max_y_arg, double &max_z_arg) const
Get bounding box for octree.
void deleteTree()
Delete the octree structure and its leaf nodes.
void addPointsFromInputCloud()
Add points from input point cloud to octree.
PointCloudConstPtr input_
Pointer to input point cloud dataset.
uindex_t getTreeDepth() const
Get the maximum depth of the octree.
void setInputCloud(const PointCloudConstPtr &cloud_arg, const IndicesConstPtr &indices_arg=IndicesConstPtr())
Provide a pointer to the input data set.
void setResolution(double resolution_arg)
Set/change the octree voxel resolution.
void defineBoundingBox()
Investigate dimensions of pointcloud data set and define corresponding bounding box for octree.
double getResolution() const
Get octree voxel resolution.
double resolution_
Octree resolution.
int getFieldIndex(const pcl::PointCloud< PointT > &, const std::string &field_name, std::vector< pcl::PCLPointField > &fields)