I searched this example on the internet
too but I failed.
OpenVDB is too hardcore for newbies like me. May I suggest
openvdb::tools::ParticlesToLevelSet ?
All you need is just points positions. My simple code was like this:
struct MyParticle
{
openvdb::Vec3R p, v;
openvdb::Real r;
};
class MyParticleList {
public:
openvdb::Real mRadiusScale;
openvdb::Real mVelocityScale;
std::vector<MyParticle> mParticleList;
typedef openvdb::Vec3R PosType;
MyParticleList(openvdb::Real rScale=1, openvdb::Real vScale=1)
: mRadiusScale(rScale), mVelocityScale(vScale) {}
void add(const openvdb::Vec3R &p, const openvdb::Real &r,
const openvdb::Vec3R &v=openvdb::Vec3R(0,0,0))
{
MyParticle pa;
pa.p = p;
pa.r = r;
pa.v = v;
mParticleList.push_back(pa);
}
// Return the total number of particles in list.
// Always required!
size_t size() const { return mParticleList.size(); };
// Get the world space position of the nth particle.
// Required by ParticledToLevelSet::rasterizeSphere(*this,radius).
void getPos(size_t n, openvdb::Vec3R& pos) const { pos = mParticleList[n].p; };
// Get the world space position and radius of the nth particle.
// Required by ParticledToLevelSet::rasterizeSphere(*this).
void getPosRad(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad) const {
pos = mParticleList[n].p;
rad = mRadiusScale*mParticleList[n].r;
};
// Get the world space position, radius and velocity of the nth particle.
// Required by ParticledToLevelSet::rasterizeSphere(*this,radius).
void getPosRadVel(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad, openvdb::Vec3R& vel) const {
pos = mParticleList[n].p;
rad = mRadiusScale*mParticleList[n].r;
vel = mVelocityScale*mParticleList[n].v;
};
// Get the attribute of the nth particle. AttributeType is user-defined!
// Only required if attribute transfer is enabled in ParticlesToLevelSet.
void getAtt(size_t n, openvdb::Index32& att) const { att = openvdb::Index32(n); };
};
....... SOME CODE ......
// VDB transform
openvdb::math::Transform::Ptr xform = openvdb::math::Transform::createLinearTransform(0.01);
MyParticleList pa(1,1);
for (openvdb::Vec3s pos : vertices) {
pa.add(openvdb::Vec3R( pos[0], pos[1], pos[2]), 0.1, openvdb::Vec3R( 0, 0, 0));
}
openvdb::FloatGrid::Ptr densityGrid = openvdb::createLevelSet<openvdb::FloatGrid>(vdb_voxel_size, 3);
std::cout << "set density grid";
densityGrid->setGridClass(openvdb::GRID_LEVEL_SET);
densityGrid->setTransform(xform);
densityGrid->setName("density");
openvdb::tools::ParticlesToLevelSet<openvdb::FloatGrid> raster(*densityGrid);
raster.rasterizeSpheres(pa);
raster.finalize(true);
//openvdb::FloatGrid::Ptr grid = densityGrid; // TEST!!!!!!