impl/voxel_volume: Voxel data compression

This commit is contained in:
Perttu Ahola 2014-10-12 23:14:25 +03:00
parent 7ccebbff05
commit b17fe8a7fc
2 changed files with 50 additions and 9 deletions

View File

@ -1,8 +1,10 @@
// http://www.apache.org/licenses/LICENSE-2.0
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
#include "interface/voxel_volume.h"
#include "interface/compress.h"
#include "core/log.h"
#include <cereal/archives/portable_binary.hpp>
#include <cereal/types/string.hpp>
#define MODULE "voxel_volume"
namespace interface {
@ -19,8 +21,7 @@ ss_ serialize_volume_simple(const pv::RawVolume<VoxelInstance> &volume)
auto region = volume.getEnclosingRegion();
auto lc = region.getLowerCorner();
auto uc = region.getUpperCorner();
size_t size = volume.getWidth() * volume.getHeight() * volume.getDepth();
for(size_t i=0; i<size; i++){
for(size_t i=0; i<volume.m_dataSize; i++){
const VoxelInstance &v = volume.m_pData[i];
ar((uint32_t)v.data);
}
@ -31,9 +32,27 @@ ss_ serialize_volume_simple(const pv::RawVolume<VoxelInstance> &volume)
ss_ serialize_volume_compressed(const pv::RawVolume<VoxelInstance> &volume)
{
std::ostringstream os(std::ios::binary);
os<<(uint8_t)3; // Format
// TODO
log_w(MODULE, "serialize_volume_compressed: Not implemented");
{
cereal::PortableBinaryOutputArchive ar(os);
ar((uint8_t)3); // Format
ar((int32_t)volume.getWidth());
ar((int32_t)volume.getHeight());
ar((int32_t)volume.getDepth());
std::ostringstream raw_os(std::ios::binary);
{
cereal::PortableBinaryOutputArchive ar(raw_os);
auto region = volume.getEnclosingRegion();
auto lc = region.getLowerCorner();
auto uc = region.getUpperCorner();
for(size_t i=0; i<volume.m_dataSize; i++){
const VoxelInstance &v = volume.m_pData[i];
ar((uint32_t)v.data);
}
}
std::ostringstream compressed_os(std::ios::binary);
interface::compress_zlib(raw_os.str(), compressed_os);
ar(compressed_os.str());
}
return os.str();
};
@ -51,15 +70,37 @@ up_<pv::RawVolume<VoxelInstance>> deserialize_volume(const ss_ &data)
pv::Region region(0, 0, 0, w-1, h-1, d-1);
up_<pv::RawVolume<VoxelInstance>> volume(
new pv::RawVolume<VoxelInstance>(region));
size_t size = volume->getWidth() * volume->getHeight() * volume->getDepth();
for(size_t i=0; i<size; i++){
for(size_t i=0; i<volume->m_dataSize; i++){
uint32_t v;
ar(v);
volume->m_pData[i].data = v;
}
return volume;
}
log_w(MODULE, "deserialize_volume(): Unknown format %i", format);
if(format == 3){
int32_t w = 0;
int32_t h = 0;
int32_t d = 0;
ar(w, h, d);
pv::Region region(0, 0, 0, w-1, h-1, d-1);
up_<pv::RawVolume<VoxelInstance>> volume(
new pv::RawVolume<VoxelInstance>(region));
ss_ compressed_data;
ar(compressed_data);
std::istringstream compressed_is(compressed_data, std::ios::binary);
std::ostringstream raw_os(std::ios::binary);
decompress_zlib(compressed_is, raw_os);
{
std::istringstream raw_is(raw_os.str(), std::ios::binary);
cereal::PortableBinaryInputArchive ar(raw_is);
for(size_t i=0; i<volume->m_dataSize; i++){
uint32_t v;
ar(v);
volume->m_pData[i].data = v;
}
}
return volume;
}
return up_<pv::RawVolume<VoxelInstance>>();;
}

View File

@ -5,7 +5,7 @@
namespace interface
{
void compress_zlib(const ss_ &data_in, std::ostream &os, int level);
void compress_zlib(const ss_ &data_in, std::ostream &os, int level=6);
void decompress_zlib(std::istream &is, std::ostream &os);
}
// vim: set noet ts=4 sw=4: