Skip to content

Commit

Permalink
Merge pull request #5 from camnewnham/feature/encode-point-clouds
Browse files Browse the repository at this point in the history
support point cloud encoding
  • Loading branch information
atteneder authored Jan 24, 2023
2 parents d8f24e3 + 4c1f5e7 commit 9f7cc97
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 35 deletions.
120 changes: 92 additions & 28 deletions src/draco/unity/draco_unity_enc_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@

namespace draco {

DracoEncoder * dracoEncoderCreate(uint32_t vertexCount)
DracoEncoder *dracoEncoderCreate(uint32_t vertexCount)
{
DracoEncoder *encoder = new DracoEncoder;
encoder->mesh.set_num_points(vertexCount);
return encoder;
DracoMeshEncoder *encoder = new DracoMeshEncoder;
encoder->is_point_cloud = false;
encoder->mesh.set_num_points(vertexCount);
return encoder;
}

DracoEncoder *dracoEncoderCreatePointCloud(uint32_t vertexCount) {
DracoPointsEncoder *encoder = new DracoPointsEncoder;
encoder->is_point_cloud = true;
encoder->mesh.set_num_points(vertexCount);
return encoder;
}

void dracoEncoderRelease(DracoEncoder *encoder)
Expand All @@ -47,31 +55,64 @@ namespace draco {
encoder->quantization.generic = generic;
}

bool dracoEncoderEncode(DracoEncoder *encoder, uint8_t preserveTriangleOrder)
bool dracoEncoderEncode(DracoEncoder *encoder, uint8_t sequentialEncode)
{
draco::Encoder dracoEncoder;
dracoEncoder.SetSpeedOptions(encoder->encodingSpeed, encoder->decodingSpeed);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::POSITION, encoder->quantization.position);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::NORMAL, encoder->quantization.normal);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::TEX_COORD, encoder->quantization.uv);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::COLOR, encoder->quantization.color);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::GENERIC, encoder->quantization.generic);
dracoEncoder.SetTrackEncodedProperties(true);

if (preserveTriangleOrder) {
dracoEncoder.SetEncodingMethod(draco::MESH_SEQUENTIAL_ENCODING);
if (encoder->is_point_cloud) {
return dracoEncode((DracoPointsEncoder *)encoder, sequentialEncode);
} else {
return dracoEncode((DracoMeshEncoder *)encoder, sequentialEncode);
}
}

template<class T>
bool dracoEncode(T *encoderT, uint8_t sequentialEncode) {
auto encoder = (DracoEncoder *)encoderT;
draco::Encoder dracoEncoder;
dracoEncoder.SetSpeedOptions(encoder->encodingSpeed,
encoder->decodingSpeed);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::POSITION,
encoder->quantization.position);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::NORMAL,
encoder->quantization.normal);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::TEX_COORD,
encoder->quantization.uv);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::COLOR,
encoder->quantization.color);
dracoEncoder.SetAttributeQuantization(draco::GeometryAttribute::GENERIC,
encoder->quantization.generic);
dracoEncoder.SetTrackEncodedProperties(true);

draco::Status encodeStatus;

if (encoder->is_point_cloud) {
if (sequentialEncode) {
dracoEncoder.SetEncodingMethod(draco::POINT_CLOUD_SEQUENTIAL_ENCODING);
} else {
dracoEncoder.SetEncodingMethod(draco::MESH_EDGEBREAKER_ENCODING);
dracoEncoder.SetEncodingMethod(draco::POINT_CLOUD_KD_TREE_ENCODING);
}
auto encoderStatus = dracoEncoder.EncodeMeshToBuffer(encoder->mesh, &encoder->encoderBuffer);
if (encoderStatus.ok()) {
encoder->encodedVertices = static_cast<uint32_t>(dracoEncoder.num_encoded_points());
encoder->encodedIndices = static_cast<uint32_t>(dracoEncoder.num_encoded_faces() * 3);
return true;

encodeStatus = dracoEncoder.EncodePointCloudToBuffer(
((DracoPointsEncoder *)encoder)->mesh, &encoder->encoderBuffer);
} else {
if (sequentialEncode) {
dracoEncoder.SetEncodingMethod(draco::MESH_SEQUENTIAL_ENCODING);
} else {
return false;
dracoEncoder.SetEncodingMethod(draco::MESH_EDGEBREAKER_ENCODING);
}

encodeStatus = dracoEncoder.EncodeMeshToBuffer(
((DracoMeshEncoder *)encoder)->mesh, &encoder->encoderBuffer);
}

if (encodeStatus.ok()) {
encoder->encodedVertices =
static_cast<uint32_t>(dracoEncoder.num_encoded_points());
encoder->encodedIndices =
static_cast<uint32_t>(dracoEncoder.num_encoded_faces() * 3);
return true;
} else {
return false;
}
}

uint32_t dracoEncoderGetEncodedVertexCount(DracoEncoder *encoder)
Expand All @@ -94,7 +135,7 @@ namespace draco {
memcpy(data, encoder->encoderBuffer.data(), encoder->encoderBuffer.size());
}

bool dracoEncodeIndices(DracoEncoder *encoder, uint32_t indexCount, DataType indexType, void *indices)
bool dracoEncodeIndices(DracoMeshEncoder *encoder, uint32_t indexCount, DataType indexType, void *indices)
{
switch (indexType)
{
Expand All @@ -112,7 +153,7 @@ namespace draco {
}

template<class T>
void dracoEncodeIndices(DracoEncoder *encoder, uint32_t indexCount, T *indices)
void dracoEncodeIndices(DracoMeshEncoder *encoder, uint32_t indexCount, T *indices)
{
int face_count = indexCount / 3;
encoder->mesh.SetNumFaces(static_cast<size_t>(face_count));
Expand All @@ -129,7 +170,7 @@ namespace draco {
}
}

bool dracoEncoderSetIndices(DracoEncoder *encoder, DataType indexComponentType, uint32_t indexCount, void *indices)
bool dracoEncoderSetIndices(DracoMeshEncoder *encoder, DataType indexComponentType, uint32_t indexCount, void *indices)
{
switch (indexComponentType)
{
Expand All @@ -154,7 +195,30 @@ namespace draco {
return true;
}

uint32_t dracoEncoderSetAttribute(DracoEncoder *encoder, GeometryAttribute::Type attributeType, draco::DataType dracoDataType, int32_t componentCount, int32_t stride, void *data)

uint32_t dracoEncoderSetAttribute(DracoEncoder *encoder,
GeometryAttribute::Type attributeType,
draco::DataType dracoDataType,
int32_t componentCount, int32_t stride,
void *data) {
if (encoder->is_point_cloud) {
return dracoSetAttribute<DracoPointsEncoder>(
(DracoPointsEncoder*)encoder, attributeType, dracoDataType,
componentCount, stride, data);
} else {
return dracoSetAttribute((DracoMeshEncoder *)encoder, attributeType,
dracoDataType,
componentCount, stride, data);
}

}

template <class T>
uint32_t dracoSetAttribute(T *encoder,
GeometryAttribute::Type attributeType,
draco::DataType dracoDataType,
int32_t componentCount, int32_t stride,
void *data)
{
auto buffer = std::unique_ptr<draco::DataBuffer>( new draco::DataBuffer());
uint32_t count = encoder->mesh.num_points();
Expand Down
34 changes: 27 additions & 7 deletions src/draco/unity/draco_unity_enc_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace draco {

struct DracoEncoder
{
draco::Mesh mesh;
uint32_t encodedVertices;
uint32_t encodedIndices;
std::vector<std::unique_ptr<draco::DataBuffer>> buffers;
Expand All @@ -52,14 +51,34 @@ namespace draco {
uint32_t color = 10;
uint32_t generic = 12;
} quantization;
bool is_point_cloud;
};

struct DracoMeshEncoder : DracoEncoder {
draco::Mesh mesh;
};

struct DracoPointsEncoder : DracoEncoder {
draco::PointCloud mesh;
};

template<class T>
void dracoEncodeIndices(DracoEncoder *encoder, uint32_t indexCount, T *indices);
void dracoEncodeIndices(DracoMeshEncoder *encoder, uint32_t indexCount,
T *indices);
template <class T>
uint32_t dracoSetAttribute(T *encoder, GeometryAttribute::Type attributeType,
draco::DataType dracoDataType,
int32_t componentCount, int32_t stride,
void *data);

template<class T>
bool dracoEncode(T *encoder, uint8_t sequentialEncode);


extern "C" {

EXPORT_API DracoEncoder * dracoEncoderCreate(uint32_t vertexCount);
EXPORT_API DracoEncoder *dracoEncoderCreate(uint32_t vertexCount);
EXPORT_API DracoEncoder *dracoEncoderCreatePointCloud(uint32_t pointCount);
void EXPORT_API dracoEncoderRelease(DracoEncoder *encoder);
void EXPORT_API dracoEncoderSetCompressionSpeed(DracoEncoder *encoder, uint32_t encodingSpeed, uint32_t decodingSpeed);
void EXPORT_API dracoEncoderSetQuantizationBits(DracoEncoder *encoder, uint32_t position, uint32_t normal, uint32_t uv, uint32_t color, uint32_t generic);
Expand All @@ -68,10 +87,11 @@ extern "C" {
uint32_t EXPORT_API dracoEncoderGetEncodedIndexCount(DracoEncoder *encoder);
uint64_t EXPORT_API dracoEncoderGetByteLength(DracoEncoder *encoder);
void EXPORT_API dracoEncoderCopy(DracoEncoder *encoder, uint8_t *data);
bool EXPORT_API dracoEncoderSetIndices(DracoEncoder *encoder, DataType indexComponentType, uint32_t indexCount, void *indices);
uint32_t EXPORT_API dracoEncoderSetAttribute(DracoEncoder *encoder, GeometryAttribute::Type attributeType, draco::DataType dracoDataType, int32_t componentCount, int32_t stride, void *data);

} // extern "C"
bool EXPORT_API dracoEncoderSetIndices(DracoMeshEncoder *encoder,
DataType indexComponentType,
uint32_t indexCount, void *indices);
uint32_t EXPORT_API dracoEncoderSetAttribute(DracoEncoder *encoder, GeometryAttribute::Type attributeType, draco::DataType dracoDataType, int32_t componentCount, int32_t stride, void *data);
} // extern "C"

} // namespace draco

Expand Down

0 comments on commit 9f7cc97

Please sign in to comment.