Skip to content

Instantly share code, notes, and snippets.

@AllanHasegawa
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllanHasegawa/8ffe63d5e9c973718e22 to your computer and use it in GitHub Desktop.
Save AllanHasegawa/8ffe63d5e9c973718e22 to your computer and use it in GitHub Desktop.
openvdb "hello world" tests
#include <iostream>
#include <typeinfo>
#include <tuple>
#include <limits>
#include <openvdb/openvdb.h>
typedef openvdb::FloatGrid GridType;
typedef GridType::TreeType TreeType;
typedef TreeType::RootNodeType RootType; // level 3 RootNode
typedef RootType::ChildNodeType Int1Type; // level 2 InternalNode
typedef Int1Type::ChildNodeType Int2Type; // level 1 InternalNode
typedef TreeType::LeafNodeType LeafType; // level 0 LeafNode
using namespace std;
int main()
{
assert(RootType::LEVEL == 3);
// Initialize the OpenVDB library. This must be called at least
// once per program and may safely be called multiple times.
openvdb::initialize();
// Create an empty floating-point grid with background value 0.
openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create();
grid->setGridClass(openvdb::GRID_LEVEL_SET);
std::cout << "Testing random access:" << std::endl;
// Get an accessor for coordinate-based access to voxels.
openvdb::FloatGrid::Accessor accessor = grid->getAccessor();
// Define a coordinate with large signed indices.
openvdb::Coord xyz(1000, -200000000, 30000000);
for (int z{0}; z < 8; ++z) {
for (int y{0}; y < 8; ++y) {
for (int x{0}; x < 8; ++x) {
xyz.reset(x, y, z);
//accessor.setValue(xyz, x+y*4+z*4*4);
accessor.setValue(xyz, x);
}
}
}
for (int z{8}; z < 16; ++z) {
for (int y{0}; y < 8; ++y) {
for (int x{0}; x < 8; ++x) {
xyz.reset(x, y, z);
accessor.setValue(xyz, numeric_limits<float>::infinity());
}
}
}
//xyz.reset(8,0,0);
//accessor.setValue(xyz, 1);
/*
// Set the voxel value at (1000, -200000000, 30000000) to 1.
accessor.setValue(xyz, 1.0);
// Verify that the voxel value at (1000, -200000000, 30000000) is 1.
std::cout << "Grid" << xyz << " = " << accessor.getValue(xyz) << std::endl;
// Reset the coordinates to those of a different voxel.
xyz.reset(1000, 200000000, -30000000);
// Verify that the voxel value at (1000, 200000000, -30000000) is
// the background value, 0.
std::cout << "Grid" << xyz << " = " << accessor.getValue(xyz) << std::endl;
// Set the voxel value at (1000, 200000000, -30000000) to 2.
accessor.setValue(xyz, 2.0);
// Set the voxels at the two extremes of the available coordinate space.
// For 32-bit signed coordinates these are (-2147483648, -2147483648, -2147483648)
// and (2147483647, 2147483647, 2147483647).
accessor.setValue(openvdb::Coord::min(), 3.0f);
accessor.setValue(openvdb::Coord::max(), 4.0f);
*/
/*
if (false) {
std::cout << "ValueOnCIter: Testing sequential access:" << std::endl;
// Print all active ("on") voxels by means of an iterator.
for (openvdb::FloatGrid::ValueOnCIter iter = grid->cbeginValueOn(); iter; ++iter) {
std::cout << "Grid" << iter.getCoord() << " = " << *iter << std::endl;
}
}
if (false) {
std::cout << "ValueOffCIter: Testing sequential access:" << std::endl;
// Print all active ("on") voxels by means of an iterator.
for (openvdb::FloatGrid::ValueOffCIter iter = grid->cbeginValueOff(); iter; ++iter) {
std::cout << "Grid" << iter.getCoord() << " = " << *iter << std::endl;
}
}
*/
if (true) {
std::cout << "NodeIter: Testing sequential access:" << std::endl;
TreeType::NodeIter iter = grid->tree().beginNode();
iter.setMinDepth(3);
auto buffer_index = [](int index) -> std::tuple<int,int,int> {
int z = index % 8;
int y = (index / 8) % 8;
int x = (index / (8*8)) % 8;
return make_tuple(x,y,z);
};
auto pb = [](std::tuple<int,int,int> t) {
cout << get<0>(t) << "," << get<1>(t) << "," << get<2>(t);
};
for (;iter; ++iter) {
cout << iter.getDepth() << ": ";
switch (iter.getDepth()) {
case 0: { RootType* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 1: { Int1Type* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 2: { Int2Type* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 3: {
LeafType* node = NULL;
iter.getNode(node);
if (node) cout << iter.getBoundingBox() << endl;
//cout << typeid(*node).name() << endl;
//cout << node->onVoxelCount() << endl;
//cout << node->str() << endl;
LeafType::Buffer& b = node->buffer();
cout << b.size() << endl;
cout << b.memUsage() << endl;
for (int i{0}; i < b.size(); ++i) {
cout << b.getValue(i) << " ";
pb(buffer_index(i));
cout << endl;
}
const float& m = b.getValue(0);
memset(const_cast<float*>(&m), 10, b.memUsage());
break;
}
}
}
grid->tree().prune();
cout << string(32, '*') << endl << string(32, '*') << endl;
iter = grid->tree().beginNode();
//iter.setMinDepth(3);
for (;iter; ++iter) {
cout << iter.getDepth() << ": ";
switch (iter.getDepth()) {
case 0: { RootType* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 1: { Int1Type* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 2: { Int2Type* node = NULL; iter.getNode(node); if (node) cout << iter.getBoundingBox() << endl; break; }
case 3: {
LeafType* node = NULL;
iter.getNode(node);
if (node) cout << iter.getBoundingBox() << endl;
//cout << typeid(*node).name() << endl;
//cout << node->onVoxelCount() << endl;
LeafType::Buffer b = node->buffer();
cout << b.size() << endl;
cout << b.memUsage() << endl;
for (int i{0}; i < b.size(); ++i) {
cout << b.getValue(i) << " ";
pb(buffer_index(i));
cout << endl;
}
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment