AcademySoftwareFoundation / openvdb

OpenVDB - Sparse volume data structure and tools
http://www.openvdb.org/
Mozilla Public License 2.0
2.62k stars 647 forks source link

[BUG] bad_alloc when reading a PointIndexGrid from file #1660

Closed leelegend94 closed 9 months ago

leelegend94 commented 1 year ago

Environment

Operating System: Ubuntu 18.04.6 Version / Commit SHA: 10.0.2 **Other: c++17 gcc/g++ 9.4.0

Describe the bug

bad_alloc occurs when reading PointIndexGrid from a file with delay-loading turned OFF. gdb backtraces the error to openvdb/io/Compression.h:260

To Reproduce

There is a simple example to reproduce the error:

#include <pcl/point_cloud.h>
#include <pcl/io/ply_io.h>

#include <openvdb/openvdb.h>
#include <openvdb/points/PointConversion.h>

using namespace std;

int main(int argc, char** argv)
{  
    string pc_path = argv[1];

    pcl::PointCloud<pcl::PointXYZ>::Ptr pc(new pcl::PointCloud<pcl::PointXYZ>);
    if(pcl::io::loadPLYFile(pc_path, *pc) != 0){
        cout << "load " << pc_path << " failed!" << endl;
        return -1;
    }

    openvdb::initialize();
    float voxelSize = 0.06;
    openvdb::math::Transform::Ptr transform = 
        openvdb::math::Transform::createLinearTransform(voxelSize);

    std::vector<openvdb::Vec3f> points(pc->size());
    for(int i=0;i<pc->size();++i) {
        points[i].x() = pc->at(i).x;
        points[i].y() = pc->at(i).y;
        points[i].z() = pc->at(i).z;
    }

    openvdb::points::PointAttributeVector<openvdb::Vec3f> pc_wrapper(points);

    openvdb::tools::PointIndexGrid::Ptr grid = 
        openvdb::tools::createPointIndexGrid<openvdb::tools::PointIndexGrid>(pc_wrapper, *transform);

    grid->setName("Points");
    cout << "export ./test.vdb" << endl;
    openvdb::io::File("./test.vdb").write({grid});

    openvdb::io::File newFile("./test.vdb");
    newFile.open(false);
    openvdb::GridBase::Ptr baseGrid = newFile.readGrid("Points");
    auto testGrid = openvdb::gridPtrCast<openvdb::tools::PointIndexGrid>(baseGrid);
    testGrid->print();
}

Expected behavior

Throw a bad_alloc error when executing newFile.open(false);

Idclip commented 1 year ago

Hi @leelegend94, I don't seem to be able to reproduce this. My setup is:

I've tested with a minimal point set as I don't have access to the point data you're reading in from your pc_path so it could be something in your dataset. Could you confirm what system you're testing on, compiler and how VDB was configured (if you're building it yourself then the cmake options/output you're using)

Idclip commented 9 months ago

Closing due to no reply - please reopen if this is still an issue!