OpenVDB seems really amazing, and the addressing of the nodes is really smart. There are some operations that I don't understand, in particular CSG operations. This is a example code. It takes as input two arguments:
The algorithm should take the input,
I'm trying to use VDB grids as data container in place of classicaloctree algorithm, for physical simulations that needs an high level of detail in collisions.
I understand the concept of transformation between world coordinates and grid coordinates, what I cannot understand is how to perform a transformation of data inside the tree, like translate or rotate the level-sets, like a rigid object. In the example, I think I'm only changing the transformation between world and lattice.
This is the result (the same for level-set and volume):Initial GridTrasformed grid, it seems that the rotation is performed...no final result?
Do you have suggestions?
Attached: one example and a link to the LINK REMOVED that I'm using (sorry, it is 133MB...)
#include <cmath>
#include "openvdb/openvdb.h"
#include "openvdb/util/Util.h"
#include "openvdb/io/Stream.h"
#include "openvdb/tools/Composite.h"
using namespace openvdb;
int main(int argc, char** argv) {
openvdb::initialize();
openvdb::io::File file(argv[1]);
file.open();
GridBase::Ptr baseGrid;
for (openvdb::io::File::NameIterator nameIter = file.beginName();
nameIter != file.endName(); ++nameIter)
{ baseGrid = file.readGrid(nameIter.gridName()); }
file.close();
FloatGrid::Ptr gridA = gridPtrCast<FloatGrid>(baseGrid);
FloatGrid::Ptr gridB = gridA->deepCopy();
FloatGrid::Ptr result = gridA ->deepCopy();
gridB->transform().postRotate(M_PI/4.0f, math::Y_AXIS);
tools::csgUnion(*result, *gridB);
openvdb::io::File file_out(argv[2]);
GridPtrVec grids;
grids.push_back(gridA);
grids.push_back(gridB);
grids.push_back(result);
file_out.write(grids);
file_out.close();
return 0;
}
The solution of my answer, thanks to VDB support in OpenVDB forum, is:
tools::resampleToMatch
, choosing one of the interpolator (in my case tools::BoxSample
) available.FYI, there is an extreme difference in execution time
using optimization flag -O3
(400% time reduction).
#include "openvdb/io/Stream.h"
#include "openvdb/openvdb.h"
#include "openvdb/tools/Composite.h"
#include "openvdb/tools/GridTransformer.h"
#include "openvdb/tools/Interpolation.h"
#include "openvdb/util/Util.h"
#include <cmath>
using namespace openvdb;
int main(int argc, char **argv) {
openvdb::initialize();
openvdb::io::File file(argv[1]);
file.open();
GridBase::Ptr baseGrid;
for (openvdb::io::File::NameIterator nameIter = file.beginName();
nameIter != file.endName(); ++nameIter) {
baseGrid = file.readGrid(nameIter.gridName());
}
file.close();
FloatGrid::Ptr gridA = gridPtrCast<FloatGrid>(baseGrid);
FloatGrid::Ptr gridB = gridA->copy(CP_NEW);
gridB->setTransform(gridA->transform().copy());
gridB->transform().postRotate(M_PI / 4.0f, math::Y_AXIS);
tools::resampleToMatch<tools::BoxSampler>(*gridA, *gridB);
FloatGrid::Ptr result = gridA->deepCopy();
FloatGrid::Ptr gridB2 = gridB->deepCopy();
tools::csgUnion(*result, *gridB);
openvdb::io::File file_out(argv[2]);
GridPtrVec grids;
grids.push_back(gridA);
grids.push_back(gridB2);
grids.push_back(result);
file_out.write(grids);
file_out.close();
return 0;
}
Reference: OpenVDB Forum