Draco使用笔记(1)——图形解压缩

1. 概述

Draco是Google开发的图形压缩库,用于压缩和解压缩3D几何网格(geometric mesh)和点云(point cloud)。Draco还可以直接对obj或者ply格式的三维数据进行压缩和解压缩,甚至编译成wasm在浏览器端对glTF压缩和解压缩。

2. 详论

2.1. 工具

Draco编译完成后直接提供了压缩和解压缩的工具draco_decoder.exe和draco_encoder.exe。通过命令行,我们对某个已经压缩好的文件进行解压缩:

draco_decoder -i "D:/1.bin" -o "D:/1.ply"

2.2. 代码

如果需要用代码的方式实现,可以参考draco_decoder.exe中的源码,具体实现如下:

#include <core/decoder_buffer.h>
#include <io/mesh_io.h>

#include <fstream>
#include <iostream>

using namespace draco;
using namespace std;

int main() {
  string filePath = "D:/1.bin";

  ifstream infile(filePath, ios::binary);

  infile.seekg(0, std::ios::end);
  size_t data_size = infile.tellg();

  infile.seekg(0, std::ios::beg);

  vector<char> data(data_size, 0);
  infile.read(data.data(), data_size);

  DecoderBuffer buffer;
  buffer.Init(data.data(), data_size);

  //解压缩
  std::unique_ptr<draco::PointCloud> pc;
  auto type_statusor = draco::Decoder::GetEncodedGeometryType(&buffer);
  if (!type_statusor.ok()) {
    return 1;
  }

  //解析数据
  const draco::EncodedGeometryType geom_type = type_statusor.value();
  if (geom_type == draco::TRIANGULAR_MESH) {
    draco::Decoder decoder;
    auto statusor = decoder.DecodeMeshFromBuffer(&buffer);
    if (!statusor.ok()) {
      return 1;
    }
    std::unique_ptr<draco::Mesh> mesh = std::move(statusor).value();
    if (mesh) {
      const int pos_att_id =
          mesh->GetNamedAttributeId(GeometryAttribute::POSITION);

      //解析顶点属性
      for (PointIndex v(0); v < mesh->num_points(); ++v) {
        const auto *const pos_att = mesh->attribute(pos_att_id);

        const uint8_t *pos = pos_att->GetAddress(pos_att->mapped_index(v));
        int64_t length = pos_att->byte_stride();
        float temp[3];
        memcpy(temp, pos, length);
        printf("%f,%f,%f\t", temp[0], temp[1], temp[2]);
      }

      //解析顶点索引
      for (FaceIndex f(0); f < mesh->num_faces(); ++f) {
        printf("%d,%d,%d\t", mesh->face(f)[0].value(), mesh->face(f)[1].value(),
               mesh->face(f)[2].value());
      }
    }
  }
}

需要注意的就是两点:

  1. 传入draco::Decoder进行解压缩的需要二进制流,这个在从文件读取时一定要注意,很容易读成了文本流导致不能正常解压缩。
  2. 对draco::Mesh的解析。draco::Mesh的顶点属性中的buffer并不是顶点索引中存储的数据。这个时压缩算法决定的,解析Mesh时一定要按照实例中解析。直接解析顶点属性中的buffer会得不到正确的顶点顺序。
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

charlee44

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值