openvdb_example.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <openvdb/openvdb.h>
  2. #include <iostream>
  3. int main()
  4. {
  5. // Initialize the OpenVDB library. This must be called at least
  6. // once per program and may safely be called multiple times.
  7. openvdb::initialize();
  8. // Create an empty floating-point grid with background value 0.
  9. openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create();
  10. std::cout << "Testing random access:" << std::endl;
  11. // Get an accessor for coordinate-based access to voxels.
  12. openvdb::FloatGrid::Accessor accessor = grid->getAccessor();
  13. // Define a coordinate with large signed indices.
  14. openvdb::Coord xyz(1000, -200000000, 30000000);
  15. // Set the voxel value at (1000, -200000000, 30000000) to 1.
  16. accessor.setValue(xyz, 1.0);
  17. // Verify that the voxel value at (1000, -200000000, 30000000) is 1.
  18. std::cout << "Grid" << xyz << " = " << accessor.getValue(xyz) << std::endl;
  19. // Reset the coordinates to those of a different voxel.
  20. xyz.reset(1000, 200000000, -30000000);
  21. // Verify that the voxel value at (1000, 200000000, -30000000) is
  22. // the background value, 0.
  23. std::cout << "Grid" << xyz << " = " << accessor.getValue(xyz) << std::endl;
  24. // Set the voxel value at (1000, 200000000, -30000000) to 2.
  25. accessor.setValue(xyz, 2.0);
  26. // Set the voxels at the two extremes of the available coordinate space.
  27. // For 32-bit signed coordinates these are (-2147483648, -2147483648, -2147483648)
  28. // and (2147483647, 2147483647, 2147483647).
  29. accessor.setValue(openvdb::Coord::min(), 3.0f);
  30. accessor.setValue(openvdb::Coord::max(), 4.0f);
  31. std::cout << "Testing sequential access:" << std::endl;
  32. // Print all active ("on") voxels by means of an iterator.
  33. for (openvdb::FloatGrid::ValueOnCIter iter = grid->cbeginValueOn(); iter; ++iter) {
  34. std::cout << "Grid" << iter.getCoord() << " = " << *iter << std::endl;
  35. }
  36. }